Last active
January 13, 2018 13:25
-
-
Save brizental/a0dea8c79b0ee0bc18eaa7066a9e2422 to your computer and use it in GitHub Desktop.
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
module.exports = function(file, api) { | |
// Alias the jscodeshift API. | |
var j = api.jscodeshift; | |
// Parse JS code into AST. | |
var root = j(file.source); | |
root.find(j.ExpressionStatement).forEach(function(p) { | |
// Only change the registerSuite method. | |
if (p.value.expression.callee && | |
p.value.expression.callee.name === "registerSuite") { | |
let obj = p.value.expression.arguments[0]; | |
let props = p.value.expression.arguments[0].properties; | |
// Get the suite name. | |
const name = j.literal(props[0].value.value); | |
// Remove first element from props, because it's the name of the suite. | |
props.shift(); | |
let new_props = [], | |
tests = []; | |
// This are the reserved methods from Intern 3 | |
// and their equivalents in Intern 4. | |
const reservedMethods = { | |
"setup": "before", | |
"teardown": "after" | |
} | |
props.forEach(function(m) { | |
// Get the old name and turn it into a valid function name. | |
const m_name = m.key.type === "Identifier" ? m.key : | |
j.identifier('"' + m.key.value + '"'); | |
// Get the actual function expression. | |
const m_exp = m.value; | |
const method = j.property("init", m_name, m_exp); | |
method.method = true; | |
let found = false; | |
for(const reservedMethod in reservedMethods){ | |
if (m_name.name === reservedMethod) { | |
method.key.name = reservedMethods[reservedMethod]; | |
new_props.push(method); | |
found = true; | |
} | |
} | |
if(!found) tests.push(method) | |
}); | |
const testsId = j.identifier("tests") | |
const testsObj = j.objectExpression(tests) | |
const testsProperty = j.property("init", testsId, testsObj); | |
new_props.push(testsProperty); | |
// Generate Intern 4 methods object. | |
const methods = j.objectExpression(new_props); | |
// Do the actual replacing. | |
p.value.expression.arguments[0] = name; | |
p.value.expression.arguments.push(methods); | |
} | |
}); | |
return root.toSource(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment