From 05a1fcd76de376d4d3ba82df8c2a86e6732e4450 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Thu, 25 Dec 2025 22:19:37 -0500 Subject: [PATCH 1/4] feat(redirect-arg-order): add migration codemod for res.redirect argument order --- codemods/redirect-arg-order/README.md | 22 ++++++++++ codemods/redirect-arg-order/codemod.yaml | 24 +++++++++++ codemods/redirect-arg-order/package.json | 22 ++++++++++ codemods/redirect-arg-order/src/workflow.ts | 41 +++++++++++++++++++ .../tests/expected/redirect.ts | 38 +++++++++++++++++ .../tests/input/redirect.ts | 38 +++++++++++++++++ codemods/redirect-arg-order/workflow.yaml | 28 +++++++++++++ 7 files changed, 213 insertions(+) create mode 100644 codemods/redirect-arg-order/README.md create mode 100644 codemods/redirect-arg-order/codemod.yaml create mode 100644 codemods/redirect-arg-order/package.json create mode 100644 codemods/redirect-arg-order/src/workflow.ts create mode 100644 codemods/redirect-arg-order/tests/expected/redirect.ts create mode 100644 codemods/redirect-arg-order/tests/input/redirect.ts create mode 100644 codemods/redirect-arg-order/workflow.yaml diff --git a/codemods/redirect-arg-order/README.md b/codemods/redirect-arg-order/README.md new file mode 100644 index 0000000..910be8d --- /dev/null +++ b/codemods/redirect-arg-order/README.md @@ -0,0 +1,22 @@ +# Migrate legacy `res.redirect(url, status)` + +Migrates usage of the legacy APIs `res.redirect(url, status)` to the new signature +`res.redirect(status, url)`. + +## Example + +### Migrating `res.redirect(url, status)` + +The migration involves replacing instances of `res.redirect(url, status)` with `res.redirect(status, url)`. + +```diff +app.get('/some-route', (req, res) => { + // Some logic here +- res.redirect(url, status); ++ res.redirect(status, url); +}); +``` + +## References + +- [Migration of res.redirect(url, status)](https://expressjs.com/en/guide/migrating-5.html#res.redirect) diff --git a/codemods/redirect-arg-order/codemod.yaml b/codemods/redirect-arg-order/codemod.yaml new file mode 100644 index 0000000..b897114 --- /dev/null +++ b/codemods/redirect-arg-order/codemod.yaml @@ -0,0 +1,24 @@ +schema_version: "1.0" +name: "@expressjs/redirect-arg-order" +version: "1.0.0" +description: Migrates usage of the legacy APIs `res.redirect(url, status)` to use the recommended argument order `res.redirect(status, url)`. +author: bjohansebas (Sebastian Beltran) +license: MIT +workflow: workflow.yaml +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + - express + - redirect + - location + +registry: + access: public + visibility: public \ No newline at end of file diff --git a/codemods/redirect-arg-order/package.json b/codemods/redirect-arg-order/package.json new file mode 100644 index 0000000..b76d0b5 --- /dev/null +++ b/codemods/redirect-arg-order/package.json @@ -0,0 +1,22 @@ +{ + "name": "@expressjs/redirect-arg-order", + "private": true, + "version": "1.0.0", + "description": "Migrates usage of the legacy APIs `res.redirect(url, status)` to use the recommended argument order `res.redirect(status, url)`.", + "type": "module", + "scripts": { + "test": "npx codemod jssg test -l typescript ./src/workflow.ts ./" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/expressjs/codemod.git", + "directory": "codemods/redirect-arg-order", + "bugs": "https://github.com/expressjs/codemod/issues" + }, + "author": "bjohansebas (Sebastian Beltran)", + "license": "MIT", + "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/redirect-arg-order/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } +} diff --git a/codemods/redirect-arg-order/src/workflow.ts b/codemods/redirect-arg-order/src/workflow.ts new file mode 100644 index 0000000..535a001 --- /dev/null +++ b/codemods/redirect-arg-order/src/workflow.ts @@ -0,0 +1,41 @@ +import type Js from '@codemod.com/jssg-types/src/langs/javascript' +import type { Edit, SgRoot } from '@codemod.com/jssg-types/src/main' + +async function transform(root: SgRoot): Promise { + const rootNode = root.root() + + const nodes = rootNode.findAll({ + rule: { + pattern: '$OBJ.$METHOD($$$ARG)', + }, + constraints: { + METHOD: { regex: '^(redirect)$' }, + }, + }) + + const edits: Edit[] = [] + + for (const call of nodes) { + const obj = call.getMatch('OBJ') + const args = call.getMultipleMatches('ARG') + if (!obj || args.length < 3) continue + + const objDef = obj.definition({ resolveExternal: false }) + if (!objDef) continue + + // $$$ARG yields argument nodes interleaved with separators, so arg nodes are at 0,2,4... + const first = args[0] + const second = args[2] + if (!first || !second) continue + + // Only transform legacy form redirect(url, status) where second is number + if (second.is('number') && !first.is('number')) { + edits.push(call.replace(`${obj.text()}.redirect(${second.text()}, ${first.text()})`)) + } + } + + if (edits.length === 0) return null + return rootNode.commitEdits(edits) +} + +export default transform diff --git a/codemods/redirect-arg-order/tests/expected/redirect.ts b/codemods/redirect-arg-order/tests/expected/redirect.ts new file mode 100644 index 0000000..3db2d52 --- /dev/null +++ b/codemods/redirect-arg-order/tests/expected/redirect.ts @@ -0,0 +1,38 @@ +import express from "express"; +import { redirect } from "somelibrary"; + +const app = express(); + +app.get("/", function (...arg) { + const [, res] = arg + res.redirect(); +}); + +app.get("/", function (...arg) { + const [, res] = arg + res.redirect(301, "/other-page"); +}); + +app.get("/", function (req, res) { + res.redirect(); +}); + +app.get("/", function (req, res) { + res.redirect(301, "/other-page"); +}); + +app.get("/", function (req, response) { + response.redirect(301, "/other-page"); +}); + +app.get("/", function (req, res) { + res.redirect(301, "/other-page"); +}); + +app.get("/", function (req, res) { + res.redirect("/other-page"); +}); + +app.get("/", function (req, res) { + redirect(301, "/other-page"); +}); \ No newline at end of file diff --git a/codemods/redirect-arg-order/tests/input/redirect.ts b/codemods/redirect-arg-order/tests/input/redirect.ts new file mode 100644 index 0000000..805d5a5 --- /dev/null +++ b/codemods/redirect-arg-order/tests/input/redirect.ts @@ -0,0 +1,38 @@ +import express from "express"; +import { redirect } from "somelibrary"; + +const app = express(); + +app.get("/", function (...arg) { + const [, res] = arg + res.redirect(); +}); + +app.get("/", function (...arg) { + const [, res] = arg + res.redirect("/other-page", 301); +}); + +app.get("/", function (req, res) { + res.redirect(); +}); + +app.get("/", function (req, res) { + res.redirect("/other-page", 301); +}); + +app.get("/", function (req, response) { + response.redirect("/other-page", 301); +}); + +app.get("/", function (req, res) { + res.redirect(301, "/other-page"); +}); + +app.get("/", function (req, res) { + res.redirect("/other-page"); +}); + +app.get("/", function (req, res) { + redirect(301, "/other-page"); +}); \ No newline at end of file diff --git a/codemods/redirect-arg-order/workflow.yaml b/codemods/redirect-arg-order/workflow.yaml new file mode 100644 index 0000000..08a3b89 --- /dev/null +++ b/codemods/redirect-arg-order/workflow.yaml @@ -0,0 +1,28 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/codemod-com/codemod/refs/heads/main/schemas/workflow.json + +version: "1" + +nodes: + - id: apply-transforms + name: Apply AST Transformations + type: automatic + runtime: + type: direct + steps: + - name: Migrates usage of the legacy APIs `res.redirect(url, status)` to use the recommended argument order `res.redirect(status, url)`. + js-ast-grep: + js_file: src/workflow.ts + base_path: . + semantic_analysis: file + include: + - "**/*.cjs" + - "**/*.js" + - "**/*.jsx" + - "**/*.mjs" + - "**/*.cts" + - "**/*.mts" + - "**/*.ts" + - "**/*.tsx" + exclude: + - "**/node_modules/**" + language: typescript \ No newline at end of file From 9840bb2b08976bb3e7f5b015cd47e4fc3f7b6f62 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 9 Jan 2026 18:02:45 -0500 Subject: [PATCH 2/4] refactor(workflow): simplify early return checks for nodes and edits --- codemods/redirect-arg-order/src/workflow.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/codemods/redirect-arg-order/src/workflow.ts b/codemods/redirect-arg-order/src/workflow.ts index 535a001..58135f8 100644 --- a/codemods/redirect-arg-order/src/workflow.ts +++ b/codemods/redirect-arg-order/src/workflow.ts @@ -13,6 +13,8 @@ async function transform(root: SgRoot): Promise { }, }) + if (!nodes.length) return null + const edits: Edit[] = [] for (const call of nodes) { @@ -34,7 +36,8 @@ async function transform(root: SgRoot): Promise { } } - if (edits.length === 0) return null + if (!edits.length) return null + return rootNode.commitEdits(edits) } From 65f672c1f8000cb6dcdc39c3537b4d31d20fdf69 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 9 Jan 2026 18:02:52 -0500 Subject: [PATCH 3/4] fix(codemod.yaml): update repository URL for redirect-arg-order migration --- codemods/redirect-arg-order/codemod.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/codemods/redirect-arg-order/codemod.yaml b/codemods/redirect-arg-order/codemod.yaml index b897114..2af0c34 100644 --- a/codemods/redirect-arg-order/codemod.yaml +++ b/codemods/redirect-arg-order/codemod.yaml @@ -5,6 +5,7 @@ description: Migrates usage of the legacy APIs `res.redirect(url, status)` to us author: bjohansebas (Sebastian Beltran) license: MIT workflow: workflow.yaml +repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/redirect-arg-order" category: migration targets: From d88cf3b6027fc4cfcc96a2cf0ab98d18a5a1b2d9 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 13 Jan 2026 10:06:21 -0500 Subject: [PATCH 4/4] fix(docs): clarify deprecation notice for res.redirect argument order migration --- codemods/redirect-arg-order/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codemods/redirect-arg-order/README.md b/codemods/redirect-arg-order/README.md index 910be8d..05e90b3 100644 --- a/codemods/redirect-arg-order/README.md +++ b/codemods/redirect-arg-order/README.md @@ -1,7 +1,7 @@ # Migrate legacy `res.redirect(url, status)` Migrates usage of the legacy APIs `res.redirect(url, status)` to the new signature -`res.redirect(status, url)`. +`res.redirect(status, url)`. This usage was deprecated in Express 4, in Express 5 you must use the new signature `res.redirect(status, url)`. ## Example