-
-
Notifications
You must be signed in to change notification settings - Fork 8
feat(req-param): migrate req-param to codemod.com #94
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
2
commits into
main
Choose a base branch
from
req-param-migration
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
2 commits
Select commit
Hold shift + click to select a range
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,38 @@ | ||
| # Migrate legacy `req.param(name)` | ||
|
|
||
| The `req.param(name)` helper that used to magically look up values from multiple places has been removed. This potentially confusing and dangerous method of retrieving form data has been removed. You will now need to specifically look for the submitted parameter name in the `req.params`, `req.body`, or `req.query` object. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Replacing `req.param('body')` and `req.param('query')` | ||
|
|
||
| Replace `req.param('body')` with `req.body` and | ||
| `req.param('query')` with `req.query`. | ||
|
|
||
| ```diff | ||
| app.get('/', (req, res) => { | ||
| // Before | ||
| - const reqBody = req.param('body'); | ||
| - const reqQuery = req.param('query'); | ||
| // After | ||
| + const reqBody = req.body; | ||
| + const reqQuery = req.query; | ||
| }); | ||
| ``` | ||
|
|
||
| ### Replacing `req.param('paramName')` | ||
|
|
||
| Replace `req.param('paramName')` with `req.params.paramName`. | ||
|
|
||
| ```diff | ||
| app.get('/user/:id', (req, res) => { | ||
| // Before | ||
| - const userId = req.param('id'); | ||
| // After | ||
| + const userId = req.params.id; | ||
| }); | ||
| ``` | ||
|
|
||
| ## References | ||
|
|
||
| - [Migration of `req.param()`](https://expressjs.com/en/guide/migrating-5.html#req.param) |
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,23 @@ | ||
| schema_version: "1.0" | ||
| name: "@expressjs/req-param" | ||
| version: "1.0.0" | ||
| description: Migrates usage of the legacy APIs `req.param(name)` to the current recommended approaches | ||
| author: bjohansebas (Sebastian Beltran) | ||
| license: MIT | ||
| workflow: workflow.yaml | ||
| category: migration | ||
|
|
||
| targets: | ||
| languages: | ||
| - javascript | ||
| - typescript | ||
|
|
||
| keywords: | ||
| - transformation | ||
| - migration | ||
| - express | ||
| - params | ||
|
|
||
| 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/req-param", | ||
| "private": true, | ||
| "version": "1.0.0", | ||
| "description": "Migrates usage of the legacy APIs `req.param(name)` to the current recommended approaches.", | ||
| "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/req-param", | ||
| "bugs": "https://github.com/expressjs/codemod/issues" | ||
| }, | ||
| "author": "bjohansebas (Sebastian Beltran)", | ||
| "license": "MIT", | ||
| "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/req-param/README.md", | ||
| "devDependencies": { | ||
| "@codemod.com/jssg-types": "^1.3.1" | ||
| } | ||
| } | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Basically, this code is repeated from the previous codemods, probably could be factored out into a utils package, though I'm wondering how this would behave for codemod registration, being a separate package and not published to npm |
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,71 @@ | ||
| import type Js from '@codemod.com/jssg-types/src/langs/javascript' | ||
| import type { Edit, SgNode, SgRoot } from '@codemod.com/jssg-types/src/main' | ||
|
|
||
| function getStringLiteralValue(node: SgNode<Js>): string | null { | ||
| if (!node.is('string')) return null | ||
|
|
||
| const fragments = node.findAll({ rule: { kind: 'string_fragment' } }) | ||
| if (fragments.length !== 1) return null | ||
| return fragments[0]?.text() ?? null | ||
| } | ||
|
|
||
| function findParentFunctionParameters(node: SgNode<Js>): SgNode<Js, 'formal_parameters'> | null { | ||
| let parent = node.parent() | ||
| while (parent) { | ||
| if (parent.is('formal_parameters')) return parent | ||
| parent = parent.parent() | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| 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: '^(param)$' }, | ||
| }, | ||
| }) | ||
|
|
||
| const edits: Edit[] = [] | ||
|
|
||
| for (const call of nodes) { | ||
| const arg = call.getMatch('ARG') | ||
| const obj = call.getMatch('OBJ') | ||
|
|
||
| if (!arg || !obj) continue | ||
| const objDef = obj.definition({ resolveExternal: false }) | ||
| if (!objDef) continue | ||
|
|
||
| const isParameter = objDef.node.matches({ | ||
| rule: { inside: { kind: 'formal_parameters', stopBy: 'end' } }, | ||
| }) | ||
| if (!isParameter) continue | ||
|
|
||
| const parameters = findParentFunctionParameters(objDef.node) | ||
| if (!parameters) continue | ||
| const firstParameter = parameters.find({ rule: { kind: 'identifier' } }) | ||
|
|
||
| if (!firstParameter) continue | ||
| const requestName = firstParameter.text() | ||
|
|
||
| const argValue = getStringLiteralValue(arg) | ||
|
|
||
| if (argValue === 'body') { | ||
| edits.push(call.replace(`${requestName}.body`)) | ||
| } else if (argValue === 'query') { | ||
| edits.push(call.replace(`${requestName}.query`)) | ||
| } else if (argValue) { | ||
| edits.push(call.replace(`${requestName}.params.${argValue}`)) | ||
| } | ||
| } | ||
|
|
||
| if (edits.length === 0) 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,39 @@ | ||
| import express from "express"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.get("/", function (req, res) { | ||
| const reqBody = req.body; | ||
| const reqQuery = req.query; | ||
| const reqQueryTest = req.query.test; | ||
| const reqOther = req.params.other; | ||
| const reqOtherNested = req.params.other.nested; | ||
| }); | ||
|
|
||
| app.get("/", function (request, response) { | ||
| const reqBody = request.body; | ||
| const reqQuery = request.query; | ||
| const reqQueryTest = request.query.test; | ||
| const reqOther = request.params.other; | ||
| const reqOtherNested = request.params.other.nested; | ||
| }); | ||
|
|
||
| app.get("/", (req, res) => { | ||
| const reqBody = req.body; | ||
| const reqQuery = req.query; | ||
| const reqQueryTest = req.query.test; | ||
| const reqOther = req.params.other; | ||
| const reqOtherNested = req.params.other.nested; | ||
| }); | ||
|
|
||
| app.get("/", (request, response) => { | ||
| const reqBody = request.body; | ||
| const reqQuery = request.query; | ||
| const reqQueryTest = request.query.test; | ||
| const reqOther = request.params.other; | ||
| const reqOtherNested = request.params.other.nested; | ||
| }); | ||
|
|
||
| app.param(function () { | ||
| // my important logic | ||
| }) |
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,39 @@ | ||
| import express from "express"; | ||
|
|
||
| const app = express(); | ||
|
|
||
| app.get("/", function (req, res) { | ||
| const reqBody = req.param('body'); | ||
| const reqQuery = req.param('query'); | ||
| const reqQueryTest = req.param('query').test; | ||
| const reqOther = req.param('other'); | ||
| const reqOtherNested = req.param('other').nested; | ||
| }); | ||
|
|
||
| app.get("/", function (request, response) { | ||
| const reqBody = request.param('body'); | ||
| const reqQuery = request.param('query'); | ||
| const reqQueryTest = request.param('query').test; | ||
| const reqOther = request.param('other'); | ||
| const reqOtherNested = request.param('other').nested; | ||
| }); | ||
|
|
||
| app.get("/", (req, res) => { | ||
| const reqBody = req.param('body'); | ||
| const reqQuery = req.param('query'); | ||
| const reqQueryTest = req.param('query').test; | ||
| const reqOther = req.param('other'); | ||
| const reqOtherNested = req.param('other').nested; | ||
| }); | ||
|
|
||
| app.get("/", (request, response) => { | ||
| const reqBody = request.param('body'); | ||
| const reqQuery = request.param('query'); | ||
| const reqQueryTest = request.param('query').test; | ||
| const reqOther = request.param('other'); | ||
| const reqOtherNested = request.param('other').nested; | ||
| }); | ||
|
|
||
| app.param(function () { | ||
| // my important logic | ||
| }) |
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 `req.param(name)` to the current recommended approaches | ||
| 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 |
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.
By the way, I'm setting this to private so we don't accidentally publish this to npm, but does this affect codemod registration?