function transform(fileInfo, api) {
const j = api.jscodeshift;
const root = j(fileInfo.source);
root
.find(j.CallExpression, { callee: { object: { name: 'app' }, property: { name: 'del' } } })
.forEach(path => {
j(path).replaceWith(
j.callExpression(
j.memberExpression(j.identifier('app'), j.identifier('delete')),
path.node.arguments
)
);
});
root
.find(j.CallExpression, { callee: { object: { name: 'req' }, property: { name: 'param' } } })
.forEach(path => {
const paramName = path.node.arguments[0];
j(path).replaceWith(
j.logicalExpression('||',
j.memberExpression(j.identifier('req'), j.memberExpression(j.identifier('params'), paramName, true)),
j.logicalExpression('||',
j.memberExpression(j.identifier('req'), j.memberExpression(j.identifier('body'), paramName, true)),
j.memberExpression(j.identifier('req'), j.memberExpression(j.identifier('query'), paramName, true))
)
)
);
});
root
.find(j.CallExpression, { callee: { object: { name: 'res' }, property: { name: 'json' } } })
.filter(path => path.node.arguments.length === 2)
.forEach(path => {
const [obj, status] = path.node.arguments;
j(path).replaceWith(
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(j.identifier('res'), j.identifier('status')),
[status]
),
j.identifier('json')
),
[obj]
)
);
});
root
.find(j.CallExpression, { callee: { object: { name: 'res' }, property: { name: 'send' } } })
.filter(path => path.node.arguments.length === 2)
.forEach(path => {
const [body, status] = path.node.arguments;
j(path).replaceWith(
j.callExpression(
j.memberExpression(
j.callExpression(
j.memberExpression(j.identifier('res'), j.identifier('status')),
[status]
),
j.identifier('send')
),
[body]
)
);
});
root
.find(j.CallExpression, { callee: { object: { name: 'res' }, property: { name: 'send' } } })
.filter(path => path.node.arguments.length === 1 && j.Literal.check(path.node.arguments[0]) && typeof path.node.arguments[0].value === 'number')
.forEach(path => {
j(path).replaceWith(
j.callExpression(
j.memberExpression(j.identifier('res'), j.identifier('sendStatus')),
path.node.arguments
)
);
});
root
.find(j.CallExpression, { callee: { object: { name: 'res' }, property: { name: 'sendfile' } } })
.forEach(path => {
j(path).replaceWith(
j.callExpression(
j.memberExpression(j.identifier('res'), j.identifier('sendFile')),
path.node.arguments
)
);
});
const pluralizedMethods = [
{ old: 'acceptsCharset', new: 'acceptsCharsets' },
{ old: 'acceptsEncoding', new: 'acceptsEncodings' },
{ old: 'acceptsLanguage', new: 'acceptsLanguages' }
];
pluralizedMethods.forEach(({ old, new: newMethod }) => {
root
.find(j.CallExpression, { callee: { object: { name: 'req' }, property: { name: old } } })
.forEach(path => {
j(path).replaceWith(
j.callExpression(
j.memberExpression(j.identifier('req'), j.identifier(newMethod)),
path.node.arguments
)
);
});
});
return root.toSource();
};
export default transform;