Skip to content

Instantly share code, notes, and snippets.

@marcioj
Created April 3, 2018 16:54
Show Gist options
  • Save marcioj/81a17ba8c191e597cca0adf8ffcb7a70 to your computer and use it in GitHub Desktop.
Save marcioj/81a17ba8c191e597cca0adf8ffcb7a70 to your computer and use it in GitHub Desktop.
transforms wrapper.findTest("id") to findTest(wrapper, "id")
export default function transformer(file, api) {
const j = api.jscodeshift;
const root = j(file.source);
const run = root
.find(j.CallExpression, { callee: { property: { name: "findTest" } } })
.forEach(path => {
const object = path.value.callee.object;
const args = path.value.arguments;
const prop = path.value.callee.property;
const func = j.callExpression(prop, [object].concat(args));
path.parent.get("object").replace(func);
})
.size();
if (run) {
root.find(j.Program).forEach(path => {
const helperImport = j.importDeclaration([j.importSpecifier(j.identifier("findTest"))], j.literal("lib/test-helpers"));
path.value.body.unshift(helperImport);
});
}
return run ? root.toSource() : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment