Created
March 15, 2016 16:48
-
-
Save Sinewyk/98caad933f87bc86ea7f to your computer and use it in GitHub Desktop.
should to expect transform
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
const printOptions = { | |
quote: 'single', | |
trailingComma: true, | |
}; | |
const SHOULD = 'should'; | |
module.exports = function transform(file, api) { | |
const source = file.source; | |
const j = api.jscodeshift; | |
const root = j(source); | |
function processFile() { | |
// should(undefined).stuff | |
const firstFormCount = root.find(j.CallExpression, { | |
callee: { | |
name: SHOULD, | |
}, | |
}).size(); | |
const secondFormCount = root.find(j.MemberExpression, { | |
property: { | |
name: SHOULD, | |
}, | |
}).size(); | |
return firstFormCount + secondFormCount > 0; | |
} | |
if (!processFile()) { | |
return source; | |
} | |
const createExpectCall = arg => j.callExpression( | |
j.identifier('expect'), | |
[arg] | |
); | |
const createMemberAccessTo = object => j.memberExpression( | |
object, | |
j.identifier('to') | |
); | |
const createExpectImport = () => j.importDeclaration( | |
[j.importDefaultSpecifier(j.identifier('expect'))], | |
j.literal('test-unit/expect') | |
); | |
const isRequiringShould = p => | |
p.value.declarations.length === 1 && | |
p.value.declarations[0].id.name === SHOULD && | |
p.value.declarations[0].init.type === 'CallExpression'; | |
let foundImport = 0; | |
// replace requires of should for require of expect | |
// leading comment https://github.com/facebook/jscodeshift/issues/44 | |
const leadingComment = root.find(j.Program).get('body', 0).node.leadingComments; | |
root | |
.find(j.VariableDeclaration) | |
.filter(isRequiringShould) | |
.replaceWith(() => { | |
foundImport++; | |
return createExpectImport(); | |
}); | |
// also handle the require('should') form if we were counting on the global form | |
root | |
.find(j.ExpressionStatement, { | |
expression: { | |
type: 'CallExpression', | |
}, | |
}) | |
.filter(p => p.value.expression.callee.name === 'require' && | |
p.value.expression.arguments.length === 1 && | |
p.value.expression.arguments[0].type === 'Literal' && | |
p.value.expression.arguments[0].value === SHOULD | |
) | |
.replaceWith(() => { | |
foundImport++; | |
return createExpectImport(); | |
}); | |
root | |
.find(j.EmptyStatement) | |
.forEach(p => console.log(p)); | |
if (!root.find(j.Program).get('body', 0).node.leadingComments && leadingComment) { | |
root.get().node.comments = leadingComment; | |
} | |
// There was no import of should ... we need to insert one for expect | |
if (foundImport === 0) { | |
console.log(`No should is required in ${file.path}: | |
add '${j(createExpectImport()).toSource()}'`); | |
} | |
// replace A.should with expect(A).to | |
root | |
.find(j.MemberExpression, { | |
property: { | |
type: 'Identifier', | |
name: SHOULD, | |
}, | |
}) | |
.replaceWith(p => createMemberAccessTo(createExpectCall(p.value.object))); | |
return root.toSource(printOptions); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment