Skip to content

Commit 716d99e

Browse files
committed
feat(redirect): enhance redirect handlers with additional test cases
1 parent fc04f72 commit 716d99e

File tree

3 files changed

+71
-10
lines changed

3 files changed

+71
-10
lines changed

recipes/magic-redirect/src/workflow.ts

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,47 @@ async function transform(root: SgRoot<Js>): Promise<string> {
66

77
// Helper function to find the request parameter name
88
function findRequestParamName(node: any): string {
9-
// Start from the call expression and traverse up to find function parameters
109
let current = node
10+
const funcKinds = new Set([
11+
'function_declaration',
12+
'function_expression',
13+
'function',
14+
'arrow_function',
15+
'method_definition',
16+
])
17+
1118
while (current) {
1219
const parent = current.parent()
1320
if (!parent) break
14-
// Check if we're in a function declaration or arrow function
15-
if (parent.kind() === 'function_declaration' || parent.kind() === 'arrow_function') {
16-
const params = parent.field('parameters')
1721

18-
if (params && params.children().length > 0) {
19-
const firstParam = params.children()[1]
20-
if (firstParam.kind() === 'required_parameter') {
21-
const pattern = firstParam.field('pattern')
22-
if (pattern && pattern.kind() === 'identifier') {
23-
return pattern.text()
22+
const kind = parent?.kind()
23+
if (kind && funcKinds.has(kind)) {
24+
const candidateFields = ['parameters', 'parameter', 'formal_parameters', 'params']
25+
let params: any = null
26+
27+
for (const f of candidateFields) {
28+
params = parent?.field(f)
29+
if (params) break
30+
}
31+
32+
if (params) {
33+
const children = typeof params.children === 'function' ? params.children() : []
34+
if (children && children.length > 0) {
35+
const first = children[1]
36+
37+
if (first?.kind() === 'required_parameter') {
38+
const pattern = first?.field('pattern')
39+
if (pattern && typeof pattern.kind === 'function' && pattern.kind() === 'identifier') {
40+
return pattern.text()
41+
}
2442
}
2543
}
2644
}
2745
}
2846

2947
current = parent
3048
}
49+
3150
return 'req' // default fallback
3251
}
3352

recipes/magic-redirect/tests/expected/redirect.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,33 @@ app.get("/", function (req, res) {
99
app.get("/", (req, res) => {
1010
res.redirect(req.get("Referrer") || "/");
1111
});
12+
app.get("/", (req, res) => {
13+
res.redirect("testing");
14+
});
15+
app.get("/", (req, res) => {
16+
res.redirect();
17+
});
1218
app.get("/articles", function (request, response) {
1319
response.redirect(request.get("Referrer") || "/");
1420
});
1521
app.get("/articles", (request, response) => {
1622
response.redirect(request.get("Referrer") || "/");
1723
});
24+
app.get("/articles", function (request, response) {
25+
response.redirect("testing");
26+
});
1827
app.get("/articles", function (_req, _res) {
1928
redirect("back");
2029
});
30+
31+
export function handler(requests, response) {
32+
response.redirect(requests.get("Referrer") || "/");
33+
}
34+
35+
export function handleRedirect(req: any) {
36+
req.redirect(req.get("Referrer") || "/");
37+
}
38+
39+
export function handlerWith(req: any, res: any) {
40+
res.redirect(req.get("Referrer") || "/");
41+
}

recipes/magic-redirect/tests/input/redirect.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,33 @@ app.get("/", function (req, res) {
99
app.get("/", (req, res) => {
1010
res.redirect("back");
1111
});
12+
app.get("/", (req, res) => {
13+
res.redirect("testing");
14+
});
15+
app.get("/", (req, res) => {
16+
res.redirect();
17+
});
1218
app.get("/articles", function (request, response) {
1319
response.redirect("back");
1420
});
1521
app.get("/articles", (request, response) => {
1622
response.redirect("back");
1723
});
24+
app.get("/articles", function (request, response) {
25+
response.redirect("testing");
26+
});
1827
app.get("/articles", function (_req, _res) {
1928
redirect("back");
2029
});
30+
31+
export function handler(requests, response) {
32+
response.redirect('back');
33+
}
34+
35+
export function handleRedirect(req: any) {
36+
req.redirect('back');
37+
}
38+
39+
export function handlerWith(req: any, res: any) {
40+
res.redirect('back');
41+
}

0 commit comments

Comments
 (0)