Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions codemods/req-param/README.md
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)
23 changes: 23 additions & 0 deletions codemods/req-param/codemod.yaml
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
22 changes: 22 additions & 0 deletions codemods/req-param/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "@expressjs/req-param",
"private": true,
Copy link
Member Author

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?

"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"
}
}
71 changes: 71 additions & 0 deletions codemods/req-param/src/workflow.ts
Copy link
Member Author

Choose a reason for hiding this comment

The 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

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
39 changes: 39 additions & 0 deletions codemods/req-param/tests/expected/params.ts
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
})
39 changes: 39 additions & 0 deletions codemods/req-param/tests/input/params.ts
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
})
28 changes: 28 additions & 0 deletions codemods/req-param/workflow.yaml
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