Skip to content

Commit 75f33dc

Browse files
authored
feat: req.param transform (#5)
* start transform for req.param method to object * replace callExpression with memberExpression for req.param * add tests for req.param nested object keys * fix lint --------- Co-authored-by: Filip Kudła <[email protected]>
1 parent 4fb1e92 commit 75f33dc

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export const TRANSFORM_OPTIONS = [
99
value: 'pluralized-methods',
1010
version: '5.0.0',
1111
},
12-
{
13-
description: 'Transform the deprecated signatures in Express v4',
14-
value: 'v4-deprecated-signatures',
15-
version: '5.0.0'
12+
{
13+
description: 'Transform the deprecated signatures in Express v4',
14+
value: 'v4-deprecated-signatures',
15+
version: '5.0.0',
1616
},
1717
]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { testSpecBuilder } from './util'
2+
3+
testSpecBuilder('req-param')
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import express from "express";
2+
3+
const app = express();
4+
5+
app.get("/", function (req, res) {
6+
const reqBody = req.param('body');
7+
const reqQuery = req.param('query');
8+
const reqQueryTest = req.param('query').test;
9+
const reqOther = req.param('other');
10+
const reqOtherNested = req.param('other').nested;
11+
});
12+
13+
app.get("/", function (request, response) {
14+
const reqBody = request.param('body');
15+
const reqQuery = request.param('query');
16+
const reqQueryTest = request.param('query').test;
17+
const reqOther = request.param('other');
18+
const reqOtherNested = request.param('other').nested;
19+
});
20+
21+
app.get("/", (req, res) => {
22+
const reqBody = req.param('body');
23+
const reqQuery = req.param('query');
24+
const reqQueryTest = req.param('query').test;
25+
const reqOther = req.param('other');
26+
const reqOtherNested = req.param('other').nested;
27+
});
28+
29+
app.get("/", (request, response) => {
30+
const reqBody = request.param('body');
31+
const reqQuery = request.param('query');
32+
const reqQueryTest = request.param('query').test;
33+
const reqOther = request.param('other');
34+
const reqOtherNested = request.param('other').nested;
35+
});
36+
37+
app.param(function () {
38+
// my important logic
39+
})
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import express from "express";
2+
3+
const app = express();
4+
5+
app.get("/", function (req, res) {
6+
const reqBody = req.body;
7+
const reqQuery = req.query;
8+
const reqQueryTest = req.query.test;
9+
const reqOther = req.params.other;
10+
const reqOtherNested = req.params.other.nested;
11+
});
12+
13+
app.get("/", function (request, response) {
14+
const reqBody = request.body;
15+
const reqQuery = request.query;
16+
const reqQueryTest = request.query.test;
17+
const reqOther = request.params.other;
18+
const reqOtherNested = request.params.other.nested;
19+
});
20+
21+
app.get("/", (req, res) => {
22+
const reqBody = req.body;
23+
const reqQuery = req.query;
24+
const reqQueryTest = req.query.test;
25+
const reqOther = req.params.other;
26+
const reqOtherNested = req.params.other.nested;
27+
});
28+
29+
app.get("/", (request, response) => {
30+
const reqBody = request.body;
31+
const reqQuery = request.query;
32+
const reqQueryTest = request.query.test;
33+
const reqOther = request.params.other;
34+
const reqOtherNested = request.params.other.nested;
35+
});
36+
37+
app.param(function () {
38+
// my important logic
39+
})

transforms/param.ts renamed to transforms/app-param.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ export default function transformer(file: FileInfo, _api: API): string {
99
.find(CallExpression, {
1010
callee: {
1111
property: {
12-
name: 'json',
12+
name: 'param',
1313
},
1414
},
1515
})
16-
// TODO: app.param(fn): This method has been deprecated. Instead, access parameters directly via req.params, or use req.body or req.query as needed.
16+
// TODO: The app.param(fn) signature was used for modifying the behavior of the app.param(name, fn) function
1717
// Add comment line before with this information
1818
.toSource()
1919
)

transforms/req-param.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import type { API, FileInfo } from 'jscodeshift'
2+
import { CallExpression, identifier, memberExpression, withParser } from 'jscodeshift'
3+
import { recursiveParent } from '../utils/recursiveParent'
4+
5+
export default function transformer(file: FileInfo, _api: API): string {
6+
const parser = withParser('ts')
7+
8+
return parser(file.source)
9+
.find(CallExpression, {
10+
callee: {
11+
property: {
12+
name: 'param',
13+
},
14+
},
15+
})
16+
.filter((path) => Boolean(recursiveParent(path.parentPath)))
17+
.replaceWith((path) => {
18+
const pathArguments = path.node.arguments
19+
20+
if (pathArguments.length > 1) {
21+
return path
22+
}
23+
24+
if (pathArguments[0].type === 'StringLiteral') {
25+
if (pathArguments[0].value === 'query') {
26+
// convert to req.query
27+
return memberExpression(identifier(recursiveParent(path.parentPath) || 'req'), identifier('query'))
28+
}
29+
if (pathArguments[0].value === 'body') {
30+
// convert to req.body
31+
return memberExpression(identifier(recursiveParent(path.parentPath) || 'req'), identifier('body'))
32+
}
33+
34+
// convert to req.params.[value]
35+
return memberExpression(
36+
memberExpression(identifier(recursiveParent(path.parentPath) || 'req'), identifier('params')),
37+
identifier(pathArguments[0].value),
38+
)
39+
}
40+
41+
return path
42+
})
43+
.toSource()
44+
}

0 commit comments

Comments
 (0)