Skip to content

Instantly share code, notes, and snippets.

@patocallaghan
Created July 10, 2018 22:15
Show Gist options
  • Save patocallaghan/acb4409c1155a3a0e70efcb50a3b4508 to your computer and use it in GitHub Desktop.
Save patocallaghan/acb4409c1155a3a0e70efcb50a3b4508 to your computer and use it in GitHub Desktop.
Delete empty Qunit setups
/* eslint-env node */
/*
BEFORE
moduleForAcceptance('test', {
beforeEach() {},
afterEach() {},
});
AFTER
moduleForAcceptance('test');
*/
export default function(file, api, options) {
let j = api.jscodeshift;
let printOptions = options.printOptions || { quote: 'single' };
let root = j(file.source);
let moduleFor = {
type: 'CallExpression',
callee: {
type: 'Identifier',
name: 'moduleForAcceptance',
},
};
root.find(j.CallExpression, moduleFor)
.forEach(p => {
let args = p.value.arguments;
let objectExpression = args.find(p => p.type === 'ObjectExpression');
let updatedObjectExpression = objectExpression.properties.filter(o => {
return !/^(before|after)Each$/.test(o.key.name) ||
(o.value.type === 'FunctionExpression' &&
o.value.body.body.length);
});
let newArgs = updatedObjectExpression.length ? [args[0], j.objectExpression(updatedObjectExpression)] : [args[0]];
p.replace(
j.callExpression(
j.identifier(p.value.callee.name),
newArgs
)
);
});
return root.toSource(printOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment