@expressjs/codemod
Version:
Codemods for updating express servers.
39 lines (38 loc) • 1.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = transformer;
const jscodeshift_1 = require("jscodeshift");
const recastOptions_1 = require("../utils/recastOptions");
const recursiveParent_1 = require("../utils/recursiveParent");
function transformer(file, _api) {
const parser = (0, jscodeshift_1.withParser)('ts');
return parser(file.source)
.find(jscodeshift_1.CallExpression, {
callee: {
property: {
name: 'param',
},
},
})
.filter((path) => Boolean((0, recursiveParent_1.recursiveParent)(path.parentPath)))
.replaceWith((path) => {
const pathArguments = path.node.arguments;
if (pathArguments.length > 1) {
return path;
}
if (pathArguments[0].type === 'StringLiteral') {
if (pathArguments[0].value === 'query') {
// convert to req.query
return (0, jscodeshift_1.memberExpression)((0, jscodeshift_1.identifier)((0, recursiveParent_1.recursiveParent)(path.parentPath) || 'req'), (0, jscodeshift_1.identifier)('query'));
}
if (pathArguments[0].value === 'body') {
// convert to req.body
return (0, jscodeshift_1.memberExpression)((0, jscodeshift_1.identifier)((0, recursiveParent_1.recursiveParent)(path.parentPath) || 'req'), (0, jscodeshift_1.identifier)('body'));
}
// convert to req.params.[value]
return (0, jscodeshift_1.memberExpression)((0, jscodeshift_1.memberExpression)((0, jscodeshift_1.identifier)((0, recursiveParent_1.recursiveParent)(path.parentPath) || 'req'), (0, jscodeshift_1.identifier)('params')), (0, jscodeshift_1.identifier)(pathArguments[0].value));
}
return path;
})
.toSource((0, recastOptions_1.getOptions)(file.source));
}