Created
July 3, 2019 15:21
-
-
Save patocallaghan/f53023bef08385a1b57b808ea387aabd 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
function findSuitableRepacements(j, code) { | |
return j(code).find(j.ExpressionStatement, { | |
expression: { | |
type: 'CallExpression', | |
callee: { | |
type: 'MemberExpression', | |
property: { | |
type: 'Identifier', | |
name: 'mockjax', | |
}, | |
}, | |
}, | |
}); | |
/*.filter(path => { | |
if (!path.value.expression.arguments[0].properties) { | |
return false; | |
} | |
// don't filter mocks with response or data function expressions e.g. response(response) { assert.ok(response, {"hello"}) } | |
return !path.value.expression.arguments[0].properties.some(property => { | |
return (property.value.type == "FunctionExpression" && (property.key.name == "response" || property.key.name == "data")) | |
}); | |
})*/ | |
/*.filter(path => { | |
// don't filter mocks with non 200 status and responseText | |
let properties = path.value.expression.arguments[0].properties.map(property => property.key); | |
return !properties.some(property => property.name == 'status' && property.value != 200) && properties.some(property => property.name == 'responseText') | |
});*/ | |
} | |
function convertToMockFromMockjax(j, suitableReplacements) { | |
return suitableReplacements | |
.forEach(path => { | |
j(path).replaceWith( | |
j.expressionStatement( | |
j.callExpression(j.identifier('mock'), [ | |
j.objectExpression(path.value.expression.arguments[0].properties), | |
]), | |
), | |
); | |
}) | |
.toSource(); | |
} | |
function convertDataToParams(j, suitableReplacements) { | |
return suitableReplacements | |
.forEach(path => { | |
if (!path.value.expression.arguments[0].properties.some(p => p.key.name == 'data')) { | |
return; | |
} | |
let data = path.value.expression.arguments[0].properties.find(p => p.key.name == 'data') | |
.value; | |
let newData; | |
if (data.type == 'CallExpression') { | |
newData = data.arguments[0]; | |
} else { | |
newData = data; | |
} | |
let newProps = path.value.expression.arguments[0].properties.filter( | |
prop => prop.key.name != 'data', | |
); | |
j(path).replaceWith( | |
j.expressionStatement( | |
j.callExpression( | |
j.memberExpression( | |
j.callExpression(j.identifier('mock'), [j.objectExpression(newProps)]), | |
j.identifier('withParams'), | |
), | |
[newData], | |
), | |
), | |
); | |
}) | |
.toSource(); | |
} | |
module.exports = function transformer(file, api) { | |
const j = api.jscodeshift; | |
let code = file.source; | |
let suitableReplacements = findSuitableRepacements(j, code); | |
let result = convertToMockFromMockjax(j, suitableReplacements); | |
result = convertDataToParams(j, suitableReplacements); | |
return result; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment