From 2a97ca52ed416d2fdf60e38afadbac9d83722f1b Mon Sep 17 00:00:00 2001 From: Elon Volo <63563814+ElonVolo@users.noreply.github.com> Date: Wed, 4 May 2022 02:57:52 -0400 Subject: [PATCH] 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. --- README.md | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index b58fb967..8a6f9479 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,7 @@ a more detailed description can be found below. /** * This replaces every occurrence of variable "foo". */ -module.exports = function(fileInfo, api) { +module.exports = function(fileInfo, api, options) { return api.jscodeshift(fileInfo.source) .findVariableDeclarators('foo') .renameTo('bar') @@ -202,18 +202,43 @@ You can collect even more stats via the `stats` function as explained above. ### Parser -The transform can let jscodeshift know with which parser to parse the source +The transform file can let jscodeshift know with which parser to parse the source files (and features like templates). To do that, the transform module can export `parser`, which can either be one of the strings `"babel"`, `"babylon"`, `"flow"`, `"ts"`, or `"tsx"`, -or it can be a parser object that is compatible with recast. +or it can be a parser object that is compatible with recast and follows the estree spec. + +__Example: specifying parser type string in the transform file__ + +```js + +module.exports = function transformer(file, api, options) { + const j = api.jscodeshift; + const rootSource = j(file.source); + + // whatever other code... + + return rootSource.toSource(); +} + +// use the flow parser +module.exports.parser = 'flow'; +``` -For example: +__Example: specifying a custom parser object in the transform file__ ```js -module.exports.parser = 'flow'; // use the flow parser -// or + +module.exports = function transformer(file, api, options) { + const j = api.jscodeshift; + const rootSource = j(file.source); + + // whatever other code... + + return rootSource.toSource(); +} + module.exports.parser = { parse: function(source) { // return estree compatible AST @@ -486,7 +511,7 @@ If you're authoring your transforms and tests using ES modules, make sure to imp ```js // MyTransform.js export const parser = 'flow' -export default function MyTransform(fileInfo, api) { +export default function MyTransform(fileInfo, api, options) { // ... } ```