|
1
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
2
|
import { createRule } from "../../create-rule" |
|
3
|
|
|
4
|
type MessageIds = "trailingSlash" |
|
5
|
|
|
6
|
const DEFINE_MIDDLEWARES = "defineMiddlewares" |
|
7
|
|
|
8
|
function collectMatcherLiterals( |
|
9
|
objectArg: TSESTree.Node, |
|
10
|
out: TSESTree.Literal[] |
|
11
|
): void { |
|
12
|
const stack: TSESTree.Node[] = [objectArg] |
|
13
|
while (stack.length) { |
|
14
|
const node = stack.pop()! |
|
15
|
if (node.type === AST_NODE_TYPES.Property) { |
|
16
|
const key = node.key |
|
17
|
const isMatcherKey = |
|
18
|
(key.type === AST_NODE_TYPES.Identifier && key.name === "matcher") || |
|
19
|
(key.type === AST_NODE_TYPES.Literal && key.value === "matcher") |
|
20
|
if ( |
|
21
|
isMatcherKey && |
|
22
|
node.value.type === AST_NODE_TYPES.Literal && |
|
23
|
typeof node.value.value === "string" |
|
24
|
) { |
|
25
|
out.push(node.value) |
|
26
|
continue |
|
27
|
} |
|
28
|
} |
|
29
|
for (const key of Object.keys(node) as Array<keyof typeof node>) { |
|
30
|
if (key === "parent") { |
|
31
|
continue |
|
32
|
} |
|
33
|
const value = (node as unknown as Record<string, unknown>)[key as string] |
|
34
|
if (!value) { |
|
35
|
continue |
|
36
|
} |
|
37
|
if (Array.isArray(value)) { |
|
38
|
for (const child of value) { |
|
39
|
if (child && typeof child === "object" && "type" in child) { |
|
40
|
stack.push(child as TSESTree.Node) |
|
41
|
} |
|
42
|
} |
|
43
|
} else if (typeof value === "object" && "type" in (value as object)) { |
|
44
|
stack.push(value as TSESTree.Node) |
|
45
|
} |
|
46
|
} |
|
47
|
} |
|
48
|
} |
|
49
|
|
|
50
|
export const rule = createRule<[], MessageIds>({ |
|
51
|
name: "no-trailing-slash-in-route-matcher", |
|
52
|
meta: { |
|
53
|
type: "problem", |
|
54
|
docs: { |
|
55
|
description: |
|
56
|
"Route matchers passed to `defineMiddlewares(...)` should not end with a trailing slash; matchers without trailing slashes won't match URLs with trailing slashes.", |
|
57
|
}, |
|
58
|
messages: { |
|
59
|
trailingSlash: |
|
60
|
"Route matcher should not end with a trailing slash. Remove the trailing `/`.", |
|
61
|
}, |
|
62
|
fixable: "code", |
|
63
|
schema: [], |
|
64
|
}, |
|
65
|
defaultOptions: [], |
|
66
|
create(context) { |
|
67
|
return { |
|
68
|
CallExpression(node) { |
|
69
|
if ( |
|
70
|
node.callee.type !== AST_NODE_TYPES.Identifier || |
|
71
|
node.callee.name !== DEFINE_MIDDLEWARES |
|
72
|
) { |
|
73
|
return |
|
74
|
} |
|
75
|
const arg = node.arguments[0] |
|
76
|
if (!arg || arg.type !== AST_NODE_TYPES.ObjectExpression) { |
|
77
|
return |
|
78
|
} |
|
79
|
|
|
80
|
const matchers: TSESTree.Literal[] = [] |
|
81
|
collectMatcherLiterals(arg, matchers) |
|
82
|
|
|
83
|
for (const lit of matchers) { |
|
84
|
const value = lit.value as string |
|
85
|
if (value.length <= 1 || !value.endsWith("/")) { |
|
86
|
continue |
|
87
|
} |
|
88
|
context.report({ |
|
89
|
node: lit, |
|
90
|
messageId: "trailingSlash", |
|
91
|
fix(fixer) { |
|
92
|
const raw = lit.raw |
|
93
|
const quote = raw[0] |
|
94
|
const inner = raw.slice(1, -1).replace(/\/+$/, "") |
|
95
|
return fixer.replaceText(lit, `${quote}${inner}${quote}`) |
|
96
|
}, |
|
97
|
}) |
|
98
|
} |
|
99
|
}, |
|
100
|
} |
|
101
|
}, |
|
102
|
}) |
|
103
|
|
|
104
|
export default rule |