Skip to content

Commit d9782a6

Browse files
authored
feat(pluralize-deprecated-methods): migrate pluralize-methods to codemod.com (#95)
* feat(pluralize-deprecated-methods): migrate pluralize-methods to codemod.com * rename codemod * apply suggestions * chore: update repository URL in codemod.yaml
1 parent 5856cfd commit d9782a6

File tree

12 files changed

+284
-0
lines changed

12 files changed

+284
-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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
schema_version: "1.0"
2+
name: "@expressjs/pluralize-method-names"
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+
repository: "https://github.com/expressjs/codemod/tree/HEAD/codemods/pluralize-method-names"
9+
category: migration
10+
11+
targets:
12+
languages:
13+
- javascript
14+
- typescript
15+
16+
keywords:
17+
- transformation
18+
- migration
19+
- express
20+
- accepts
21+
- accepetsCharsets
22+
- acceptsEncodings
23+
- acceptsLanguages
24+
25+
registry:
26+
access: public
27+
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-method-names",
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-method-names",
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-method-names/README.md",
19+
"devDependencies": {
20+
"@codemod.com/jssg-types": "^1.3.1"
21+
}
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
if (!nodes.length) return null
22+
23+
const edits: Edit[] = []
24+
25+
for (const call of nodes) {
26+
const method = call.getMatch('METHOD')
27+
const obj = call.getMatch('OBJ')
28+
if (!obj || !method) continue
29+
30+
const objDef = obj.definition({ resolveExternal: false })
31+
if (!objDef) continue
32+
33+
edits.push(method.replace(`${method.text()}s`))
34+
}
35+
36+
if (!edits.length) return null
37+
38+
return rootNode.commitEdits(edits)
39+
}
40+
41+
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)