Skip to content

Instantly share code, notes, and snippets.

@snowman
Forked from necccc/builder_example.js
Last active July 25, 2021 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save snowman/bc82ffc864c873a13be53f293a6d6fdb to your computer and use it in GitHub Desktop.
Save snowman/bc82ffc864c873a13be53f293a6d6fdb 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