-
-
Notifications
You must be signed in to change notification settings - Fork 9
feat(redirect-arg-order): add migration codemod for res.redirect argument order #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bjohansebas
wants to merge
4
commits into
main
Choose a base branch
from
redirect-order
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
05a1fcd
feat(redirect-arg-order): add migration codemod for res.redirect argu…
bjohansebas 5988e64
Merge branch 'main' of github.com:expressjs/codemod into redirect-order
bjohansebas 9840bb2
refactor(workflow): simplify early return checks for nodes and edits
bjohansebas 65f672c
fix(codemod.yaml): update repository URL for redirect-arg-order migra…
bjohansebas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| 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 | ||
| repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/redirect-arg-order" | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
| - express | ||
| - redirect | ||
| - location | ||
|
|
||
| registry: | ||
| access: public | ||
| visibility: public |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 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<Js>): Promise<string | null> { | ||
| const rootNode = root.root() | ||
|
|
||
| const nodes = rootNode.findAll({ | ||
| rule: { | ||
| pattern: '$OBJ.$METHOD($$$ARG)', | ||
| }, | ||
| constraints: { | ||
| METHOD: { regex: '^(redirect)$' }, | ||
| }, | ||
| }) | ||
|
|
||
| if (!nodes.length) return null | ||
|
|
||
| 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) return null | ||
|
|
||
| return rootNode.commitEdits(edits) | ||
| } | ||
|
|
||
| export default transform |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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"); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to mention the affected version