Created
October 21, 2019 07:32
-
-
Save necccc/ed8d8854f42efc530b5c27c55a3eb081 to your computer and use it in GitHub Desktop.
Gist for Transform your codebase using codemods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// create a function call that looks like | |
// "myfunc(someVar, 'bar')" | |
const callExpr = j.callExpression( | |
j.identifier(‘myFunc’), | |
[ | |
j.indentifier('someVar') | |
j.literal('bar'), | |
] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def("CallExpression") | |
.bases("Expression") | |
.build("callee", "arguments") | |
.field("callee", def("Expression")) | |
// See comment for NewExpression above. | |
.field("arguments", [def("Expression")]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// find all member expressions of the `obj` object | |
ast.find( | |
j.MemberExpression, | |
{ | |
object: { | |
name: 'obj' | |
} | |
} | |
).closest(j.CallExpression) | |
// returns method calls like `obj.foo()` | |
// but not the property access like `obj.bar` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the jscodeshift api has been assigned to j previously | |
const ast = j(source) | |
// find all MemberExpressions on object `obj.` | |
ast.find( | |
j.MemberExpression, { | |
object: { | |
name: 'obj' | |
} | |
} | |
).filter((path) => { | |
const { name } = path.value.property | |
// filter all accessed properties that starts with `get` | |
return /^get[A-Z].*/.test(name) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// the jscodeshift api has been assigned to j previously | |
const ast = j(source) | |
ast.find(j.MemberExpression, { | |
object: { | |
name: 'library' | |
}, | |
property: { | |
name: 'smoosh' | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ast.findVariableDeclarators(‘foo’) | |
///same as running this | |
ast.find(jscodeshift.VariableDeclarator, { id: { name: ‘foo’ }}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ jscodeshift -t some-codemod.js --ignore-config ignore_config.txt /path/to/the/codebase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
vendor | |
node_modules | |
*config*.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ast | |
.find(j.CallExpression) | |
.closest(j.ExpressionStatement) | |
.insertAfter([ | |
j.expressionStatement( | |
j.callExpression( | |
j.identifier('asd'), | |
[] | |
) | |
) | |
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// insert a track() call after every function call | |
// passing in their numeric index in the code | |
const createTracker = (path, index) => { | |
return j.expressionStatement( | |
j.callExpression( | |
j.identifier('track'), | |
[ j.literal(index) ] | |
) | |
) | |
} | |
ast | |
.find(j.CallExpression) | |
.closest(j.ExpressionStatement) | |
.insertAfter(createTracker) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ast | |
.findVariableDeclarators(‘foo’) | |
.insertAfter( | |
j.indentifier('bar') | |
) | |
// this alters code from | |
// const foo = 'asd'; | |
// to | |
// const foo = 'ast, bar; | |
// which is invalid! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ npm i jscodeshift -g | |
$ jscodeshift -t some-codemod.js /path/to/the/codebase |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ast.findVariableDeclarators('foo').remove() | |
ast.find(j.CallExpression, { | |
callee: { | |
object: { name: 'console'}, | |
property: { name: 'log'}, | |
} | |
}).remove() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jscodeshift(source) | |
.findVariableDeclarators('foo') | |
.renameTo('bar') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// replace variable name ‘lorem’ to ‘ipsum’ at every code occurrence | |
ast.find(j.Identifier, {name: ‘lorem’ }) | |
.replaceWith( | |
j.identifier(‘ipsum’) | |
) | |
// replace console.log() to another logging target, | |
// while keeping the passed arguments | |
ast.find(j.CallExpression, { | |
callee: { | |
object: { name: 'console'}, | |
property: { name: 'log'}, | |
} | |
}).replaceWith((path) => ([ | |
j.callExpression( | |
j.identifier(‘mylogger’), | |
path.value.arguments | |
) | |
])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(file, api, options) { | |
const { source, path } = file | |
// `source` is the source code in file at `path` | |
const { jscodeshift, stats } = api | |
// use `jscodeshift` to access the API | |
// `stats` is a function to collect statistics | |
// during --dry runs | |
// do some magic here... | |
// return the changed source as string | |
return source; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment