function transform(fileInfo, api) { const j = api.jscodeshift; const root = j(fileInfo.source); // Transform app.del() to app.delete() 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 ) ); }); // Transform req.param(name) to appropriate req.params, req.body, or req.query 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)) ) ) ); }); // Transform res.json(obj, status) to res.status(status).json(obj) 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] ) ); }); // Transform res.send(body, status) to res.status(status).send(body) 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] ) ); }); // Transform res.send(status) to res.sendStatus(status) 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 ) ); }); // Transform res.sendfile() to res.sendFile() 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 ) ); }); // Transform deprecated pluralized method names 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;
Input
const express = require('express'); const app = express(); app.del('/delete-route', (req, res) => { res.send('This route will be deleted'); }); app.param((name, fn) => { // param handling }); app.param(':id', (req, res, next, id) => { req.userId = id; next(); }); app.get('/pluralized-methods', (req, res) => { const charset = req.acceptsCharset('utf-8'); const encoding = req.acceptsEncoding('gzip'); const language = req.acceptsLanguage('en'); res.json({ charset, encoding, language }, 200); }); app.get('/req-param', (req, res) => { const userId = req.param('userId'); res.send(`User ID is ${userId}`); }); app.get('/res-methods', (req, res) => { res.json({ message: 'Hello World' }, 200); res.jsonp({ message: 'Hello World' }, 200); res.send('Hello World', 200); res.send(200); }); app.get('/sendfile', (req, res) => { res.sendfile('/path/to/file'); }); app.use((req, res, next) => { // Simulate an async error return Promise.reject(new Error('Something went wrong')); }); app.listen(3000, () => { console.log('Server running on port 3000'); });
Output
loading
Read-only