You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Each codemod package should be coupled with a `codeshift.config.js` file.
54
+
The config file is the entry-point of your codemod and is responsible for holding all of the relevant
55
+
metadata about it, as well as references to the transformer functions themselves.
56
+
57
+
They typically look like this:
58
+
59
+
```js
60
+
exportdefault {
61
+
maintainers: ['danieldelcore'],
62
+
transforms: {
63
+
'18.0.0':require('./18.0.0/transform'),
64
+
'19.0.0':require('./19.0.0/transform'),
65
+
},
66
+
};
67
+
```
68
+
69
+
-`maintainers`: Github usernames of the people that maintain that codemod, they will be notified on PRs etc.
70
+
-`transforms`: A key value pair of transforms organized by semver-compatible versions
71
+
49
72
## Versioning
50
73
51
74
You might wonder why we require that codemods are named by a semver version like `react-cool-library/18.0.0`.
@@ -62,7 +85,7 @@ Patched codemods will then be automatically published when merged to the repo, e
62
85
63
86
## Transformers
64
87
65
-
Transformers are the main entrypoint to your codemod, they are responsible for accepting a raw file and applying the appropriate modifications to it.
88
+
Transformers are the main entrypoint to your codemod, they are responsible for accepting a raw file, applying the appropriate modifications to it and finally outputting the resulting AST to the original file.
66
89
67
90
**Example:**
68
91
@@ -73,53 +96,107 @@ import updateBorderWidth from './motions/update-border-width';
returnsource.toSource(options.printOptions); // Writes modified AST to file
84
107
}
85
108
```
86
109
87
110
## Motions
88
111
89
112
A motion (aka migration) is what we call specific actions performed within a codemod. For example, `updateBorderWidth` or `removeDeprecatedProps`.
90
-
They can be simply thought of a functions that are responsible for a single action within a codemod. It is not required but they are a helpful design pattern to isolate more complicated parts of your codemod into discrete pieces.
113
+
They can be simply thought of a functions that are responsible for a single action within a codemod. It is not required but they are highly recommended as
114
+
a helpful design pattern to isolate more complicated parts of your codemod into discrete pieces.
91
115
92
116
**Example:**
93
117
94
118
```ts
95
-
function removeDeprecatedProps(
96
-
j:core.JSCodeshift,
97
-
source:ReturnType<typeofj>,
98
-
) {
99
-
// TODO:
119
+
function removeDeprecatedProps(j, source) {
120
+
// Some logic here
100
121
}
101
122
```
102
123
103
-
## Testing
124
+
Motions can then be applied from the main transform, just like any other function.
104
125
105
-
It's very likely that consumers will run into all sorts of edge-cases when running your transform. That's why it's important to start by writing some tests to assert it's behavior. Luckily, [jscodeshift provides some testing utilities](https://github.com/facebook/jscodeshift#unit-testing).
When creating a codemod, it's best to always try to write your tests first (TDD style). Think about the start and end state and how you might be able to achieve that. Also, make sure to consider as many edge-cases as you possibly can.
134
+
// Execute individual motions
135
+
removeDeprecatedProps(j, source);
136
+
restructureImports(j, source);
108
137
109
-
**Example:**
138
+
returnsource.toSource(options.printOptions); // Writes modified AST to file
139
+
}
140
+
```
141
+
142
+
Each motion receives a reference to the AST (`source`) which it can then manipulate as required.
143
+
144
+
Alternatively, you can use the utility function [applyMotions](./utils#applymotionsj-source-motions) to run motions in sequence.
It's very likely that consumers will run into all sorts of edge-cases when running your transform.
164
+
That's why it's important to start by writing some tests to assert it's behavior. Luckily, both [CodeshiftCommunity](./test-utils) & [jscodeshift](https://github.com/facebook/jscodeshift#unit-testing) provides testing utilities to help.
165
+
166
+
When creating a codemod, it's best to always try to write your tests first (TDD style).
167
+
Think about the start and end state and how you might be able to achieve that. Also, make sure to consider as many edge-cases as you possibly can.
168
+
125
169
For more information, please see the [testing docs](testing).
0 commit comments