Created
April 3, 2018 16:54
-
-
Save marcioj/81a17ba8c191e597cca0adf8ffcb7a70 to your computer and use it in GitHub Desktop.
transforms wrapper.findTest("id") to findTest(wrapper, "id")
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
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