From 9bb77186a20610c533ac2c2da7fd72709dd1288d Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Tue, 23 Dec 2025 23:27:05 -0500 Subject: [PATCH 1/4] feat(pluralize-deprecated-methods): migrate pluralize-methods to codemod.com --- .../pluralize-deprecated-methods/README.md | 44 +++++++++++++++++++ .../pluralize-deprecated-methods/codemod.yaml | 26 +++++++++++ .../pluralize-deprecated-methods/package.json | 22 ++++++++++ .../src/workflow.ts | 38 ++++++++++++++++ .../tests/expected/charset.ts | 19 ++++++++ .../tests/expected/encoding.ts | 18 ++++++++ .../tests/expected/language.ts | 18 ++++++++ .../tests/input/charset.ts | 19 ++++++++ .../tests/input/encoding.ts | 18 ++++++++ .../tests/input/language.ts | 18 ++++++++ .../workflow.yaml | 28 ++++++++++++ 11 files changed, 268 insertions(+) create mode 100644 codemods/pluralize-deprecated-methods/README.md create mode 100644 codemods/pluralize-deprecated-methods/codemod.yaml create mode 100644 codemods/pluralize-deprecated-methods/package.json create mode 100644 codemods/pluralize-deprecated-methods/src/workflow.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/expected/charset.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/expected/encoding.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/expected/language.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/input/charset.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/input/encoding.ts create mode 100644 codemods/pluralize-deprecated-methods/tests/input/language.ts create mode 100644 codemods/pluralize-deprecated-methods/workflow.yaml diff --git a/codemods/pluralize-deprecated-methods/README.md b/codemods/pluralize-deprecated-methods/README.md new file mode 100644 index 0000000..f345f23 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/README.md @@ -0,0 +1,44 @@ +# Migrate pluralized request methods + +Migrates deprecated request methods to their pluralized versions that were deprecated in Express 4 and removed in Express 5. + +## Example + +### Migrating `req.acceptsCharset(charset)` + +The migration involves replacing instances of `req.acceptsCharset(charset)` with `req.acceptsCharsets(charset)`. + +```diff +app.get('/', (req, res) => { +- const charset = req.acceptsCharset('utf-8'); ++ const charset = req.acceptsCharsets('utf-8'); + res.json({ charset }); +}); +``` +### Migrating `req.acceptsEncoding(encoding) + +The migration involves replacing instances of `req.acceptsEncoding(encoding)` with `req.acceptsEncodings(encoding)`. + +```diff +app.get('/', (req, res) => { +- const encoding = req.acceptsEncoding('gzip'); ++ const encoding = req.acceptsEncodings('gzip'); + res.json({ encoding }); +}); +``` + +### Migrating `req.acceptsLanguage(language)` + +The migration involves replacing instances of `req.acceptsLanguage(language)` with `req.acceptsLanguages(language)`. + +```diff +app.get('/', (req, res) => { +- const language = req.acceptsLanguage('en'); ++ const language = req.acceptsLanguages('en'); + res.json({ language }); +}); +``` + +## References + +- [Migration of pluralized request methods](https://expressjs.com/en/guide/migrating-5.html#plural) diff --git a/codemods/pluralize-deprecated-methods/codemod.yaml b/codemods/pluralize-deprecated-methods/codemod.yaml new file mode 100644 index 0000000..301ab57 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/codemod.yaml @@ -0,0 +1,26 @@ +schema_version: "1.0" +name: "@expressjs/pluralize-deprecated-methods" +version: "1.0.0" +description: Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications. +author: bjohansebas (Sebastian Beltran) +license: MIT +workflow: workflow.yaml +category: migration + +targets: + languages: + - javascript + - typescript + +keywords: + - transformation + - migration + - express + - accepts + - accepetsCharsets + - acceptsEncodings + - acceptsLanguages + +registry: + access: public + visibility: public \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/package.json b/codemods/pluralize-deprecated-methods/package.json new file mode 100644 index 0000000..abc8571 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/package.json @@ -0,0 +1,22 @@ +{ + "name": "@expressjs/pluralize-deprecated-methods", + "private": true, + "version": "1.0.0", + "description": "Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications.", + "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/pluralize-deprecated-methods", + "bugs": "https://github.com/expressjs/codemod/issues" + }, + "author": "bjohansebas (Sebastian Beltran)", + "license": "MIT", + "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/pluralize-deprecated-methods/README.md", + "devDependencies": { + "@codemod.com/jssg-types": "^1.3.1" + } +} diff --git a/codemods/pluralize-deprecated-methods/src/workflow.ts b/codemods/pluralize-deprecated-methods/src/workflow.ts new file mode 100644 index 0000000..4b5b41e --- /dev/null +++ b/codemods/pluralize-deprecated-methods/src/workflow.ts @@ -0,0 +1,38 @@ +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: { + any: [ + { pattern: '$OBJ.$METHOD($ARG)' }, + { + pattern: '$OBJ.$METHOD()', + }, + ], + }, + constraints: { + METHOD: { regex: '^(acceptsCharset|acceptsEncoding|acceptsLanguage)$' }, + }, + }) + + const edits: Edit[] = [] + + for (const call of nodes) { + const method = call.getMatch('METHOD') + const obj = call.getMatch('OBJ') + if (!obj || !method) continue + + const objDef = obj.definition({ resolveExternal: false }) + if (!objDef) continue + + edits.push(method.replace(`${method.text()}s`)) + } + + if (edits.length === 0) return null + return rootNode.commitEdits(edits) +} + +export default transform diff --git a/codemods/pluralize-deprecated-methods/tests/expected/charset.ts b/codemods/pluralize-deprecated-methods/tests/expected/charset.ts new file mode 100644 index 0000000..73713dd --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/expected/charset.ts @@ -0,0 +1,19 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const charset = req.acceptsCharsets(); + res.json({ charset }); +}); + + +app.get('/', (req, res) => { + const charset = req.acceptsCharsets('utf-8'); + res.json({ charset }); +}); + +app.get('/', function (request, response) { + const charset = request.acceptsCharsets('utf-8'); + response.json({ charset }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/tests/expected/encoding.ts b/codemods/pluralize-deprecated-methods/tests/expected/encoding.ts new file mode 100644 index 0000000..bf2c3f4 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/expected/encoding.ts @@ -0,0 +1,18 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const encoding = req.acceptsEncodings(); + res.json({ encoding }); +}); + +app.get('/', (req, res) => { + const encoding = req.acceptsEncodings('gzip'); + res.json({ encoding }); +}); + +app.get('/', function (request, response) { + const encoding = request.acceptsEncodings('gzip'); + response.json({ encoding }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/tests/expected/language.ts b/codemods/pluralize-deprecated-methods/tests/expected/language.ts new file mode 100644 index 0000000..5c4f2b9 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/expected/language.ts @@ -0,0 +1,18 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const encoding = req.acceptsLanguages(); + res.json({ encoding }); +}); + +app.get('/', (req, res) => { + const encoding = req.acceptsLanguages('gzip'); + res.json({ encoding }); +}); + +app.get('/', function (request, response) { + const encoding = request.acceptsLanguages('gzip'); + response.json({ encoding }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/tests/input/charset.ts b/codemods/pluralize-deprecated-methods/tests/input/charset.ts new file mode 100644 index 0000000..5e7c962 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/input/charset.ts @@ -0,0 +1,19 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const charset = req.acceptsCharset(); + res.json({ charset }); +}); + + +app.get('/', (req, res) => { + const charset = req.acceptsCharset('utf-8'); + res.json({ charset }); +}); + +app.get('/', function (request, response) { + const charset = request.acceptsCharset('utf-8'); + response.json({ charset }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/tests/input/encoding.ts b/codemods/pluralize-deprecated-methods/tests/input/encoding.ts new file mode 100644 index 0000000..fa130ca --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/input/encoding.ts @@ -0,0 +1,18 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const encoding = req.acceptsEncoding(); + res.json({ encoding }); +}); + +app.get('/', (req, res) => { + const encoding = req.acceptsEncoding('gzip'); + res.json({ encoding }); +}); + +app.get('/', function (request, response) { + const encoding = request.acceptsEncoding('gzip'); + response.json({ encoding }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/tests/input/language.ts b/codemods/pluralize-deprecated-methods/tests/input/language.ts new file mode 100644 index 0000000..a719ed3 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/tests/input/language.ts @@ -0,0 +1,18 @@ +import express from 'express' + +const app = express() + +app.get('/', (req, res) => { + const encoding = req.acceptsLanguage(); + res.json({ encoding }); +}); + +app.get('/', (req, res) => { + const encoding = req.acceptsLanguage('gzip'); + res.json({ encoding }); +}); + +app.get('/', function (request, response) { + const encoding = request.acceptsLanguage('gzip'); + response.json({ encoding }); +}); \ No newline at end of file diff --git a/codemods/pluralize-deprecated-methods/workflow.yaml b/codemods/pluralize-deprecated-methods/workflow.yaml new file mode 100644 index 0000000..4dcf1d6 --- /dev/null +++ b/codemods/pluralize-deprecated-methods/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 deprecated singular request methods to their pluralized versions in Express.js applications. + 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 89a2840d0277f91da4d04892a81b65d2b01f11e9 Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Thu, 25 Dec 2025 22:36:57 -0500 Subject: [PATCH 2/4] rename codemod --- .../README.md | 0 .../codemod.yaml | 2 +- .../package.json | 6 +++--- .../src/workflow.ts | 0 .../tests/expected/charset.ts | 0 .../tests/expected/encoding.ts | 0 .../tests/expected/language.ts | 0 .../tests/input/charset.ts | 0 .../tests/input/encoding.ts | 0 .../tests/input/language.ts | 0 .../workflow.yaml | 0 11 files changed, 4 insertions(+), 4 deletions(-) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/README.md (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/codemod.yaml (91%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/package.json (80%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/src/workflow.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/expected/charset.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/expected/encoding.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/expected/language.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/input/charset.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/input/encoding.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/tests/input/language.ts (100%) rename codemods/{pluralize-deprecated-methods => pluralize-method-names}/workflow.yaml (100%) diff --git a/codemods/pluralize-deprecated-methods/README.md b/codemods/pluralize-method-names/README.md similarity index 100% rename from codemods/pluralize-deprecated-methods/README.md rename to codemods/pluralize-method-names/README.md diff --git a/codemods/pluralize-deprecated-methods/codemod.yaml b/codemods/pluralize-method-names/codemod.yaml similarity index 91% rename from codemods/pluralize-deprecated-methods/codemod.yaml rename to codemods/pluralize-method-names/codemod.yaml index 301ab57..5dd23c2 100644 --- a/codemods/pluralize-deprecated-methods/codemod.yaml +++ b/codemods/pluralize-method-names/codemod.yaml @@ -1,5 +1,5 @@ schema_version: "1.0" -name: "@expressjs/pluralize-deprecated-methods" +name: "@expressjs/pluralize-method-names" version: "1.0.0" description: Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications. author: bjohansebas (Sebastian Beltran) diff --git a/codemods/pluralize-deprecated-methods/package.json b/codemods/pluralize-method-names/package.json similarity index 80% rename from codemods/pluralize-deprecated-methods/package.json rename to codemods/pluralize-method-names/package.json index abc8571..1c3cd1c 100644 --- a/codemods/pluralize-deprecated-methods/package.json +++ b/codemods/pluralize-method-names/package.json @@ -1,5 +1,5 @@ { - "name": "@expressjs/pluralize-deprecated-methods", + "name": "@expressjs/pluralize-method-names", "private": true, "version": "1.0.0", "description": "Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications.", @@ -10,12 +10,12 @@ "repository": { "type": "git", "url": "git+https://github.com/expressjs/codemod.git", - "directory": "codemods/pluralize-deprecated-methods", + "directory": "codemods/pluralize-method-names", "bugs": "https://github.com/expressjs/codemod/issues" }, "author": "bjohansebas (Sebastian Beltran)", "license": "MIT", - "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/pluralize-deprecated-methods/README.md", + "homepage": "https://github.com/expressjs/codemod/blob/main/codemods/pluralize-method-names/README.md", "devDependencies": { "@codemod.com/jssg-types": "^1.3.1" } diff --git a/codemods/pluralize-deprecated-methods/src/workflow.ts b/codemods/pluralize-method-names/src/workflow.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/src/workflow.ts rename to codemods/pluralize-method-names/src/workflow.ts diff --git a/codemods/pluralize-deprecated-methods/tests/expected/charset.ts b/codemods/pluralize-method-names/tests/expected/charset.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/expected/charset.ts rename to codemods/pluralize-method-names/tests/expected/charset.ts diff --git a/codemods/pluralize-deprecated-methods/tests/expected/encoding.ts b/codemods/pluralize-method-names/tests/expected/encoding.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/expected/encoding.ts rename to codemods/pluralize-method-names/tests/expected/encoding.ts diff --git a/codemods/pluralize-deprecated-methods/tests/expected/language.ts b/codemods/pluralize-method-names/tests/expected/language.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/expected/language.ts rename to codemods/pluralize-method-names/tests/expected/language.ts diff --git a/codemods/pluralize-deprecated-methods/tests/input/charset.ts b/codemods/pluralize-method-names/tests/input/charset.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/input/charset.ts rename to codemods/pluralize-method-names/tests/input/charset.ts diff --git a/codemods/pluralize-deprecated-methods/tests/input/encoding.ts b/codemods/pluralize-method-names/tests/input/encoding.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/input/encoding.ts rename to codemods/pluralize-method-names/tests/input/encoding.ts diff --git a/codemods/pluralize-deprecated-methods/tests/input/language.ts b/codemods/pluralize-method-names/tests/input/language.ts similarity index 100% rename from codemods/pluralize-deprecated-methods/tests/input/language.ts rename to codemods/pluralize-method-names/tests/input/language.ts diff --git a/codemods/pluralize-deprecated-methods/workflow.yaml b/codemods/pluralize-method-names/workflow.yaml similarity index 100% rename from codemods/pluralize-deprecated-methods/workflow.yaml rename to codemods/pluralize-method-names/workflow.yaml From 997bdc756a2cc2aa918b03aaaa51aaeebaf9106e Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 9 Jan 2026 17:24:50 -0500 Subject: [PATCH 3/4] apply suggestions --- codemods/pluralize-method-names/src/workflow.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/codemods/pluralize-method-names/src/workflow.ts b/codemods/pluralize-method-names/src/workflow.ts index 4b5b41e..1baf93a 100644 --- a/codemods/pluralize-method-names/src/workflow.ts +++ b/codemods/pluralize-method-names/src/workflow.ts @@ -18,6 +18,8 @@ async function transform(root: SgRoot): Promise { }, }) + if (!nodes.length) return null + const edits: Edit[] = [] for (const call of nodes) { @@ -31,7 +33,8 @@ async function transform(root: SgRoot): Promise { edits.push(method.replace(`${method.text()}s`)) } - if (edits.length === 0) return null + if (!edits.length) return null + return rootNode.commitEdits(edits) } From 09a35ec8d832b1c21c31ed65f4ce8972413e479a Mon Sep 17 00:00:00 2001 From: Sebastian Beltran Date: Fri, 9 Jan 2026 17:26:42 -0500 Subject: [PATCH 4/4] chore: update repository URL in codemod.yaml --- codemods/pluralize-method-names/codemod.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/codemods/pluralize-method-names/codemod.yaml b/codemods/pluralize-method-names/codemod.yaml index 5dd23c2..56ba749 100644 --- a/codemods/pluralize-method-names/codemod.yaml +++ b/codemods/pluralize-method-names/codemod.yaml @@ -5,6 +5,7 @@ description: Migrates usage of deprecated singular request methods to their plur author: bjohansebas (Sebastian Beltran) license: MIT workflow: workflow.yaml +repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/pluralize-method-names" category: migration targets: