Skip to content

Instantly share code, notes, and snippets.

@necccc
Created October 21, 2019 07:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save necccc/ed8d8854f42efc530b5c27c55a3eb081 to your computer and use it in GitHub Desktop.
Save necccc/ed8d8854f42efc530b5c27c55a3eb081 to your computer and use it in GitHub Desktop.
Gist for Transform your codebase using codemods
// create a function call that looks like
// "myfunc(someVar, 'bar')"
const callExpr = j.callExpression(
j.identifier(‘myFunc’),
[
j.indentifier('someVar')
j.literal('bar'),
]
)
def("CallExpression")
.bases("Expression")
.build("callee", "arguments")
.field("callee", def("Expression"))
// See comment for NewExpression above.
.field("arguments", [def("Expression")]);
// 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`
// 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)
})
// the jscodeshift api has been assigned to j previously
const ast = j(source)
ast.find(j.MemberExpression, {
object: {
name: 'library'
},
property: {
name: 'smoosh'
}
})
ast.findVariableDeclarators(‘foo’)
///same as running this
ast.find(jscodeshift.VariableDeclarator, { id: { name: ‘foo’ }})
$ jscodeshift -t some-codemod.js --ignore-config ignore_config.txt /path/to/the/codebase
vendor
node_modules
*config*.js
ast
.find(j.CallExpression)
.closest(j.ExpressionStatement)
.insertAfter([
j.expressionStatement(
j.callExpression(
j.identifier('asd'),
[]
)
)
])
// 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)
ast
.findVariableDeclarators(‘foo’)
.insertAfter(
j.indentifier('bar')
)
// this alters code from
// const foo = 'asd';
// to
// const foo = 'ast, bar;
// which is invalid!
$ npm i jscodeshift -g
$ jscodeshift -t some-codemod.js /path/to/the/codebase
ast.findVariableDeclarators('foo').remove()
ast.find(j.CallExpression, {
callee: {
object: { name: 'console'},
property: { name: 'log'},
}
}).remove()
jscodeshift(source)
.findVariableDeclarators('foo')
.renameTo('bar')
// 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
)
]))
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