Skip to content

Commit 2a97ca5

Browse files
authored
Update README.md
* Adding options parameter to all examples of the transform function to increase discoverability and consistency * Giving more concrete examples of parsers being specified in the transform files. People misunderstanding the documentation and putting the module.exports.parser line above the module.exports line was causing parsers to be undefined when that was not the original intent. This change to the doc should make it more copypasta friendly.
1 parent 561efa7 commit 2a97ca5

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

README.md

+32-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ a more detailed description can be found below.
150150
/**
151151
* This replaces every occurrence of variable "foo".
152152
*/
153-
module.exports = function(fileInfo, api) {
153+
module.exports = function(fileInfo, api, options) {
154154
return api.jscodeshift(fileInfo.source)
155155
.findVariableDeclarators('foo')
156156
.renameTo('bar')
@@ -202,18 +202,43 @@ You can collect even more stats via the `stats` function as explained above.
202202

203203
### Parser
204204

205-
The transform can let jscodeshift know with which parser to parse the source
205+
The transform file can let jscodeshift know with which parser to parse the source
206206
files (and features like templates).
207207

208208
To do that, the transform module can export `parser`, which can either be one
209209
of the strings `"babel"`, `"babylon"`, `"flow"`, `"ts"`, or `"tsx"`,
210-
or it can be a parser object that is compatible with recast.
210+
or it can be a parser object that is compatible with recast and follows the estree spec.
211+
212+
__Example: specifying parser type string in the transform file__
213+
214+
```js
215+
216+
module.exports = function transformer(file, api, options) {
217+
const j = api.jscodeshift;
218+
const rootSource = j(file.source);
219+
220+
// whatever other code...
221+
222+
return rootSource.toSource();
223+
}
224+
225+
// use the flow parser
226+
module.exports.parser = 'flow';
227+
```
211228

212-
For example:
229+
__Example: specifying a custom parser object in the transform file__
213230

214231
```js
215-
module.exports.parser = 'flow'; // use the flow parser
216-
// or
232+
233+
module.exports = function transformer(file, api, options) {
234+
const j = api.jscodeshift;
235+
const rootSource = j(file.source);
236+
237+
// whatever other code...
238+
239+
return rootSource.toSource();
240+
}
241+
217242
module.exports.parser = {
218243
parse: function(source) {
219244
// return estree compatible AST
@@ -486,7 +511,7 @@ If you're authoring your transforms and tests using ES modules, make sure to imp
486511
```js
487512
// MyTransform.js
488513
export const parser = 'flow'
489-
export default function MyTransform(fileInfo, api) {
514+
export default function MyTransform(fileInfo, api, options) {
490515
// ...
491516
}
492517
```

0 commit comments

Comments
 (0)