Skip to content

Commit 9bb7718

Browse files
committed
feat(pluralize-deprecated-methods): migrate pluralize-methods to codemod.com
1 parent ba25f2f commit 9bb7718

File tree

11 files changed

+268
-0
lines changed

11 files changed

+268
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Migrate pluralized request methods
2+
3+
Migrates deprecated request methods to their pluralized versions that were deprecated in Express 4 and removed in Express 5.
4+
5+
## Example
6+
7+
### Migrating `req.acceptsCharset(charset)`
8+
9+
The migration involves replacing instances of `req.acceptsCharset(charset)` with `req.acceptsCharsets(charset)`.
10+
11+
```diff
12+
app.get('/', (req, res) => {
13+
- const charset = req.acceptsCharset('utf-8');
14+
+ const charset = req.acceptsCharsets('utf-8');
15+
res.json({ charset });
16+
});
17+
```
18+
### Migrating `req.acceptsEncoding(encoding)
19+
20+
The migration involves replacing instances of `req.acceptsEncoding(encoding)` with `req.acceptsEncodings(encoding)`.
21+
22+
```diff
23+
app.get('/', (req, res) => {
24+
- const encoding = req.acceptsEncoding('gzip');
25+
+ const encoding = req.acceptsEncodings('gzip');
26+
res.json({ encoding });
27+
});
28+
```
29+
30+
### Migrating `req.acceptsLanguage(language)`
31+
32+
The migration involves replacing instances of `req.acceptsLanguage(language)` with `req.acceptsLanguages(language)`.
33+
34+
```diff
35+
app.get('/', (req, res) => {
36+
- const language = req.acceptsLanguage('en');
37+
+ const language = req.acceptsLanguages('en');
38+
res.json({ language });
39+
});
40+
```
41+
42+
## References
43+
44+
- [Migration of pluralized request methods](https://expressjs.com/en/guide/migrating-5.html#plural)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
schema_version: "1.0"
2+
name: "@expressjs/pluralize-deprecated-methods"
3+
version: "1.0.0"
4+
description: Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications.
5+
author: bjohansebas (Sebastian Beltran)
6+
license: MIT
7+
workflow: workflow.yaml
8+
category: migration
9+
10+
targets:
11+
languages:
12+
- javascript
13+
- typescript
14+
15+
keywords:
16+
- transformation
17+
- migration
18+
- express
19+
- accepts
20+
- accepetsCharsets
21+
- acceptsEncodings
22+
- acceptsLanguages
23+
24+
registry:
25+
access: public
26+
visibility: public
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@expressjs/pluralize-deprecated-methods",
3+
"private": true,
4+
"version": "1.0.0",
5+
"description": "Migrates usage of deprecated singular request methods to their pluralized versions in Express.js applications.",
6+
"type": "module",
7+
"scripts": {
8+
"test": "npx codemod jssg test -l typescript ./src/workflow.ts ./"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/expressjs/codemod.git",
13+
"directory": "codemods/pluralize-deprecated-methods",
14+
"bugs": "https://github.com/expressjs/codemod/issues"
15+
},
16+
"author": "bjohansebas (Sebastian Beltran)",
17+
"license": "MIT",
18+
"homepage": "https://github.com/expressjs/codemod/blob/main/codemods/pluralize-deprecated-methods/README.md",
19+
"devDependencies": {
20+
"@codemod.com/jssg-types": "^1.3.1"
21+
}
22+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type Js from '@codemod.com/jssg-types/src/langs/javascript'
2+
import type { Edit, SgRoot } from '@codemod.com/jssg-types/src/main'
3+
4+
async function transform(root: SgRoot<Js>): Promise<string | null> {
5+
const rootNode = root.root()
6+
7+
const nodes = rootNode.findAll({
8+
rule: {
9+
any: [
10+
{ pattern: '$OBJ.$METHOD($ARG)' },
11+
{
12+
pattern: '$OBJ.$METHOD()',
13+
},
14+
],
15+
},
16+
constraints: {
17+
METHOD: { regex: '^(acceptsCharset|acceptsEncoding|acceptsLanguage)$' },
18+
},
19+
})
20+
21+
const edits: Edit[] = []
22+
23+
for (const call of nodes) {
24+
const method = call.getMatch('METHOD')
25+
const obj = call.getMatch('OBJ')
26+
if (!obj || !method) continue
27+
28+
const objDef = obj.definition({ resolveExternal: false })
29+
if (!objDef) continue
30+
31+
edits.push(method.replace(`${method.text()}s`))
32+
}
33+
34+
if (edits.length === 0) return null
35+
return rootNode.commitEdits(edits)
36+
}
37+
38+
export default transform
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const charset = req.acceptsCharsets();
7+
res.json({ charset });
8+
});
9+
10+
11+
app.get('/', (req, res) => {
12+
const charset = req.acceptsCharsets('utf-8');
13+
res.json({ charset });
14+
});
15+
16+
app.get('/', function (request, response) {
17+
const charset = request.acceptsCharsets('utf-8');
18+
response.json({ charset });
19+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const encoding = req.acceptsEncodings();
7+
res.json({ encoding });
8+
});
9+
10+
app.get('/', (req, res) => {
11+
const encoding = req.acceptsEncodings('gzip');
12+
res.json({ encoding });
13+
});
14+
15+
app.get('/', function (request, response) {
16+
const encoding = request.acceptsEncodings('gzip');
17+
response.json({ encoding });
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const encoding = req.acceptsLanguages();
7+
res.json({ encoding });
8+
});
9+
10+
app.get('/', (req, res) => {
11+
const encoding = req.acceptsLanguages('gzip');
12+
res.json({ encoding });
13+
});
14+
15+
app.get('/', function (request, response) {
16+
const encoding = request.acceptsLanguages('gzip');
17+
response.json({ encoding });
18+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const charset = req.acceptsCharset();
7+
res.json({ charset });
8+
});
9+
10+
11+
app.get('/', (req, res) => {
12+
const charset = req.acceptsCharset('utf-8');
13+
res.json({ charset });
14+
});
15+
16+
app.get('/', function (request, response) {
17+
const charset = request.acceptsCharset('utf-8');
18+
response.json({ charset });
19+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const encoding = req.acceptsEncoding();
7+
res.json({ encoding });
8+
});
9+
10+
app.get('/', (req, res) => {
11+
const encoding = req.acceptsEncoding('gzip');
12+
res.json({ encoding });
13+
});
14+
15+
app.get('/', function (request, response) {
16+
const encoding = request.acceptsEncoding('gzip');
17+
response.json({ encoding });
18+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import express from 'express'
2+
3+
const app = express()
4+
5+
app.get('/', (req, res) => {
6+
const encoding = req.acceptsLanguage();
7+
res.json({ encoding });
8+
});
9+
10+
app.get('/', (req, res) => {
11+
const encoding = req.acceptsLanguage('gzip');
12+
res.json({ encoding });
13+
});
14+
15+
app.get('/', function (request, response) {
16+
const encoding = request.acceptsLanguage('gzip');
17+
response.json({ encoding });
18+
});

0 commit comments

Comments
 (0)