-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jasmine/Jest v20+: this syntax codemod
Closes #57 Context: jestjs/jest#3553
- Loading branch information
Showing
4 changed files
with
578 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/** | ||
* Codemod for transforming Jasmine `this` context into Jest v20+ compatible syntax. | ||
*/ | ||
|
||
import finale from '../utils/finale'; | ||
|
||
const testFunctionNames = ['after', 'afterEach', 'before', 'beforeEach', 'it', 'test']; | ||
const allFunctionNames = ['describe'].concat(testFunctionNames); | ||
const ignoredIdentifiers = ['retries', 'skip', 'slow', 'timeout']; | ||
|
||
function isFunctionExpressionWithinSpecificFunctions(path, acceptedFunctionNames) { | ||
if (!path || !path.parentPath || !Array.isArray(path.parentPath.value)) { | ||
return false; | ||
} | ||
|
||
const callExpressionPath = path.parentPath.parentPath; | ||
|
||
return ( | ||
!!callExpressionPath && | ||
!!callExpressionPath.value && | ||
callExpressionPath.value.callee.type === 'Identifier' && | ||
acceptedFunctionNames.indexOf(callExpressionPath.value.callee.name) > -1 | ||
); | ||
} | ||
|
||
function isWithinObjectOrClass(path) { | ||
const invalidParentTypes = ['Property', 'MethodDefinition']; | ||
let currentPath = path; | ||
|
||
while ( | ||
currentPath && | ||
currentPath.value && | ||
invalidParentTypes.indexOf(currentPath.value.type) === -1 | ||
) { | ||
currentPath = currentPath.parentPath; | ||
} | ||
return currentPath ? invalidParentTypes.indexOf(currentPath.value.type) > -1 : false; | ||
} | ||
|
||
function isWithinSpecificFunctions(path, acceptedFunctionNames, matchAll) { | ||
if (!matchAll) { | ||
// Do not replace within functions declared as object properties or class methods | ||
// See `transforms plain functions within lifecycle methods` test | ||
if (isWithinObjectOrClass(path)) { | ||
return false; | ||
} | ||
} | ||
let currentPath = path; | ||
|
||
while ( | ||
currentPath && | ||
currentPath.value && | ||
currentPath.value.type !== 'FunctionExpression' | ||
) { | ||
currentPath = currentPath.parentPath; | ||
} | ||
|
||
return ( | ||
isFunctionExpressionWithinSpecificFunctions(currentPath, acceptedFunctionNames) || | ||
(currentPath | ||
? isWithinSpecificFunctions(currentPath.parentPath, testFunctionNames, false) | ||
: false) | ||
); | ||
} | ||
|
||
export default function jasmineThis(fileInfo, api, options) { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
const getValidThisExpressions = node => { | ||
return j(node) | ||
.find(j.MemberExpression, { | ||
object: { | ||
type: j.ThisExpression.name, | ||
}, | ||
property: { | ||
name: name => ignoredIdentifiers.indexOf(name) === -1, | ||
}, | ||
}) | ||
.filter(path => isWithinSpecificFunctions(path, allFunctionNames, true)); | ||
}; | ||
|
||
const mutateScope = (ast, body) => { | ||
const replacedIdentifiersMap = {}; | ||
|
||
const updateThisExpressions = () => { | ||
return ast | ||
.find(j.MemberExpression) | ||
.filter(path => path.value.object.type === 'ThisExpression') | ||
.filter(path => isWithinSpecificFunctions(path, allFunctionNames, true)) | ||
.replaceWith(replaceThisExpression) | ||
.size(); | ||
}; | ||
|
||
const replaceThisExpression = path => { | ||
const originalName = path.value.property.name; | ||
const newName = originalName + 'Context'; | ||
replacedIdentifiersMap[originalName] = newName; | ||
return j.identifier(newName); | ||
}; | ||
|
||
const addLetDeclarations = () => { | ||
Object.keys(replacedIdentifiersMap) | ||
// Reverse the keys because we're adding them one by one to the front of the body array | ||
.sort() | ||
.reverse() | ||
.forEach(originalName => { | ||
body.unshift( | ||
j.variableDeclaration('let', [ | ||
j.variableDeclarator( | ||
j.identifier(replacedIdentifiersMap[originalName]), | ||
null | ||
), | ||
]) | ||
); | ||
}); | ||
}; | ||
|
||
updateThisExpressions(); | ||
addLetDeclarations(); | ||
}; | ||
|
||
const mutateDescribe = path => { | ||
const functionExpression = path.value.arguments.find( | ||
node => | ||
node.type === 'FunctionExpression' || | ||
node.type === 'ArrowFunctionExpression' | ||
); | ||
const functionBody = functionExpression.body; | ||
const ast = j(functionBody); | ||
|
||
mutateScope(ast, functionBody.body); | ||
}; | ||
|
||
const updateRoot = () => { | ||
const topLevelLifecycleMethods = root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: j.Identifier.name, | ||
name: name => testFunctionNames.indexOf(name) > -1, | ||
}, | ||
}) | ||
// Find only lifecyle methods which are in the root scope | ||
.filter( | ||
path => | ||
path.parentPath.value.type === j.ExpressionStatement.name && | ||
Array.isArray(path.parentPath.parentPath.value) && | ||
path.parentPath.parentPath.parentPath.value.type === j.Program.name | ||
) | ||
.filter(path => getValidThisExpressions(path.value).size() > 0) | ||
.size(); | ||
|
||
if (topLevelLifecycleMethods > 0) { | ||
const path = root.get(); | ||
mutateScope(root, path.value.program.body); | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}; | ||
|
||
const updateDescribes = () => { | ||
return root | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: j.Identifier.name, | ||
name: 'describe', | ||
}, | ||
}) | ||
.filter(path => getValidThisExpressions(path.value).size() > 0) | ||
.forEach(mutateDescribe) | ||
.size(); | ||
}; | ||
|
||
const updateFunctionExpressions = () => { | ||
return root | ||
.find(j.FunctionExpression) | ||
.filter(path => | ||
isFunctionExpressionWithinSpecificFunctions(path, allFunctionNames) | ||
) | ||
.replaceWith(path => { | ||
const newFn = j.arrowFunctionExpression( | ||
path.value.params, | ||
path.value.body, | ||
path.value.expression | ||
); | ||
newFn.async = path.value.async; | ||
return newFn; | ||
}) | ||
.size(); | ||
}; | ||
|
||
const mutations = updateRoot() + updateDescribes() + updateFunctionExpressions(); | ||
|
||
if (!mutations) { | ||
return null; | ||
} | ||
|
||
return finale(fileInfo, j, root, options); | ||
} |
Oops, something went wrong.