facebook / facebook/jscodeshift

Discussion on how to integrate with Babel's ability to use recast

Open
#168 9 comments 0 reactions 0 assignees View on GitHub
discussion improvement
Dominant language
JavaScript
Stars
10k
Forks
498
PR merge metrics
No merged PRs in 30d

Description

So after https://github.com/babel/babel/pull/3561 allows babel to use recast as the parser/generator and the other prs fixed the babel 6 ast nodes for ast-types/recast we should be able to use babel as the transformer for jscodeshift.

Is there anything we want to do to the api to make it easier or should users just add it in themselves? babel-core/recast are already dependencies, but yeah it's not necessary (*Ah* I see babel-core is v5 for the babel 5 parser). Maybe it's just a docs change.

```js
module.exports = function(fileInfo, api) {
return api.jscodeshift(fileInfo.source)
.findVariableDeclarators('foo')
.renameTo('bar')
.toSource();
}
```

```js
const transform = require('babel-core').transform;
const recast = require('recast');
const plugin = require('./plugin');

module.exports = function(fileInfo, api, options) {
return transform(fileInfo.source, {
parserOpts: {
parser: recast.parse
},
generatorOpts: {
generator: recast.print
},
plugins: [plugin]
}).code;
};

// ./plugin.js
module.exports = {
visitor: {
VariableDeclarator({ node }) {
if (node.id.name === "foo") {
node.id.name = "bar";
}
}
}
};
```

Also interesting sidenote would be using the jscodeshift type api in babel

cc @fkling, @cpojer, @drewml

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.