Last active
March 16, 2016 19:03
-
-
Save iamdustan/63e916d50f7a14e9db6e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Automatically add `import expect from "must";` if it doesn’t exist in a | |
* given -test.js file | |
*/ | |
module.exoprts = function transformer(file, api) { | |
const j = api.jscodeshift; | |
const {expression, statement, statements} = j.template; | |
// ensure this is a test file | |
if (!/-test\.js$/.test(fileInfo.path)) { | |
return; | |
} | |
const ast = j(fileInfo.source); | |
const hasMustImport = ast | |
.find(j.ImportDeclaration) | |
.filter((path) => path.node.source.value.type === "must") | |
.size() > 0; | |
// if it already has the must import return | |
if (hasMustImport) { | |
return; | |
} | |
// add the must expect to the file | |
return ast | |
.find(j.Program) | |
.forEach(path => { | |
path.get("body").value.unshift( | |
j.importDeclaration( | |
[j.importDefaultSpecifier(j.identifier("expect"))], | |
j.literal("must") | |
) | |
); | |
}) | |
.toSource(); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment