Skip to content

Instantly share code, notes, and snippets.

@iamdustan
Last active March 16, 2016 19:03
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 iamdustan/63e916d50f7a14e9db6e to your computer and use it in GitHub Desktop.
Save iamdustan/63e916d50f7a14e9db6e to your computer and use it in GitHub Desktop.
/**
* 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