Created
July 10, 2018 22:15
-
-
Save patocallaghan/acb4409c1155a3a0e70efcb50a3b4508 to your computer and use it in GitHub Desktop.
Delete empty Qunit setups
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
/* 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