UNPKG

@expressjs/codemod

Version:

Codemods for updating express servers.

39 lines (38 loc) 1.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = transformer; const jscodeshift_1 = require("jscodeshift"); const parse_1 = require("../utils/parse"); const recastOptions_1 = require("../utils/recastOptions"); const recursiveParent_1 = require("../utils/recursiveParent"); const unifiedMagicString = (path, projectRequestName) => { const pathArguments = path.value.arguments; if (pathArguments.length === 1 && pathArguments[0]?.type === 'StringLiteral' && pathArguments[0].value === 'back') { path.value.arguments = [ (0, jscodeshift_1.logicalExpression)('||', (0, jscodeshift_1.callExpression)((0, jscodeshift_1.memberExpression)((0, jscodeshift_1.identifier)(projectRequestName), (0, jscodeshift_1.identifier)('get')), [(0, jscodeshift_1.identifier)('"Referrer"')]), (0, jscodeshift_1.literal)('/')), ]; return path; } }; function transformer(file, _api) { const parsedFile = (0, parse_1.getParsedFile)(file); parsedFile .find(jscodeshift_1.CallExpression, { callee: { property: { name: 'redirect', }, }, }) .map((path) => unifiedMagicString(path, (0, recursiveParent_1.recursiveParent)(path.parentPath) || 'req')); parsedFile .find(jscodeshift_1.CallExpression, { callee: { property: { name: 'location', }, }, }) .map((path) => unifiedMagicString(path, (0, recursiveParent_1.recursiveParent)(path.parentPath) || 'req')); return parsedFile.toSource((0, recastOptions_1.getOptions)(file.source)); }