Skip to content

Instantly share code, notes, and snippets.

@boopathi
Created August 30, 2016 11:23
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 boopathi/d979e5266df861052783acf9d3e61d22 to your computer and use it in GitHub Desktop.
Save boopathi/d979e5266df861052783acf9d3e61d22 to your computer and use it in GitHub Desktop.
Transform babel-minify tests
#!/usr/bin/env node
const fs = require("fs");
const minimist = require("minimist");
const transform = require("./index");
module.exports = run;
if (require.main === module) {
run(process.argv.slice(2))
}
function run(rawArgv) {
const argv = minimist(rawArgv);
for (let i = 0; i < argv._.length; i++) {
const file = argv._[i];
console.log("transforming", file);
const contents = fs.readFileSync(file).toString();
const output = transform(contents).code;
fs.writeFileSync(file, output);
}
}
const recast = require("recast");
const t = recast.types.namedTypes;
const b = recast.types.builders;
module.exports = transform;
function transform(input) {
const ast = recast.parse(input);
const removeIds = [];
recast.visit(ast, {
visitVariableDeclaration(path) {
const node = path.value;
if (
node.declarations.length === 1
&& t.Identifier.check(node.declarations[0].id)
) {
const decl = node.declarations[0];
if (decl.id.name === "expected") {
return null;
}
if (decl.id.name === "source") {
if (
t.CallExpression.check(decl.init)
&& t.Identifier.check(decl.init.callee)
&& decl.init.callee.name === "unpad"
) {
decl.init = decl.init.arguments[0];
return false;
}
}
}
this.traverse(path);
},
visitCallExpression(path) {
const node = path.value;
if (isExpect(node)) {
node.callee.property.name = "toMatchSnapshot";
node.arguments = [];
return node;
}
this.traverse(path);
}
});
return recast.print(ast);
}
function isExpect(node) {
return t.MemberExpression.check(node.callee) // expect().toBe
&& t.CallExpression.check(node.callee.object) // expect()
&& t.Identifier.check(node.callee.object.callee) // expect
&& node.callee.object.callee.name === "expect" // expect
&& t.Identifier.check(node.callee.property) // toBe
&& node.callee.property.name.indexOf("to") === 0; // toBe
}
{
"dependencies": {
"minimist": "^1.2.0",
"recast": "^0.11.11"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment