|
1
|
import * as path from "path" |
|
2
|
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils" |
|
3
|
import { createRule } from "../../create-rule" |
|
4
|
import { isUnderApiDir } from "../../util/api-route" |
|
5
|
|
|
6
|
type MessageIds = "lowercaseHandler" |
|
7
|
|
|
8
|
const LOWERCASE_HTTP_METHODS = new Set([ |
|
9
|
"get", |
|
10
|
"post", |
|
11
|
"put", |
|
12
|
"patch", |
|
13
|
"delete", |
|
14
|
"head", |
|
15
|
"options", |
|
16
|
]) |
|
17
|
|
|
18
|
const ROUTE_FILE_BASENAMES = new Set(["route.ts", "route.js"]) |
|
19
|
|
|
20
|
export const rule = createRule<[], MessageIds>({ |
|
21
|
name: "route-handler-exports-uppercase", |
|
22
|
meta: { |
|
23
|
type: "problem", |
|
24
|
docs: { |
|
25
|
description: |
|
26
|
"API route handler exports must be uppercase HTTP method names (GET, POST, ...). Lowercase exports are ignored by the framework.", |
|
27
|
}, |
|
28
|
fixable: "code", |
|
29
|
messages: { |
|
30
|
lowercaseHandler: |
|
31
|
"Route handler exports must be uppercase. Rename `{{name}}` to `{{upper}}` — lowercase exports are ignored by the framework.", |
|
32
|
}, |
|
33
|
schema: [], |
|
34
|
}, |
|
35
|
defaultOptions: [], |
|
36
|
create(context) { |
|
37
|
const filename = context.filename |
|
38
|
if (!filename || filename.startsWith("<")) { |
|
39
|
return {} |
|
40
|
} |
|
41
|
if (!isUnderApiDir(filename)) { |
|
42
|
return {} |
|
43
|
} |
|
44
|
if (!ROUTE_FILE_BASENAMES.has(path.basename(filename))) { |
|
45
|
return {} |
|
46
|
} |
|
47
|
|
|
48
|
function reportIdentifier(id: TSESTree.Identifier) { |
|
49
|
const name = id.name |
|
50
|
if (!LOWERCASE_HTTP_METHODS.has(name)) { |
|
51
|
return |
|
52
|
} |
|
53
|
const upper = name.toUpperCase() |
|
54
|
context.report({ |
|
55
|
node: id, |
|
56
|
messageId: "lowercaseHandler", |
|
57
|
data: { name, upper }, |
|
58
|
fix(fixer) { |
|
59
|
return fixer.replaceText(id, upper) |
|
60
|
}, |
|
61
|
}) |
|
62
|
} |
|
63
|
|
|
64
|
function reportStringLiteral(node: TSESTree.StringLiteral, name: string) { |
|
65
|
const upper = name.toUpperCase() |
|
66
|
context.report({ |
|
67
|
node, |
|
68
|
messageId: "lowercaseHandler", |
|
69
|
data: { name, upper }, |
|
70
|
fix(fixer) { |
|
71
|
return fixer.replaceText(node, JSON.stringify(upper)) |
|
72
|
}, |
|
73
|
}) |
|
74
|
} |
|
75
|
|
|
76
|
return { |
|
77
|
ExportNamedDeclaration(node) { |
|
78
|
if (node.declaration) { |
|
79
|
const decl = node.declaration |
|
80
|
if (decl.type === AST_NODE_TYPES.FunctionDeclaration && decl.id) { |
|
81
|
reportIdentifier(decl.id) |
|
82
|
} else if (decl.type === AST_NODE_TYPES.VariableDeclaration) { |
|
83
|
for (const d of decl.declarations) { |
|
84
|
if (d.id.type === AST_NODE_TYPES.Identifier) { |
|
85
|
reportIdentifier(d.id) |
|
86
|
} |
|
87
|
} |
|
88
|
} |
|
89
|
} |
|
90
|
|
|
91
|
for (const spec of node.specifiers) { |
|
92
|
if (spec.type !== AST_NODE_TYPES.ExportSpecifier) { |
|
93
|
continue |
|
94
|
} |
|
95
|
const exported = spec.exported |
|
96
|
if (exported.type === AST_NODE_TYPES.Identifier) { |
|
97
|
reportIdentifier(exported) |
|
98
|
} else if ( |
|
99
|
exported.type === AST_NODE_TYPES.Literal && |
|
100
|
typeof exported.value === "string" && |
|
101
|
LOWERCASE_HTTP_METHODS.has(exported.value) |
|
102
|
) { |
|
103
|
reportStringLiteral( |
|
104
|
exported as TSESTree.StringLiteral, |
|
105
|
exported.value |
|
106
|
) |
|
107
|
} |
|
108
|
} |
|
109
|
}, |
|
110
|
} |
|
111
|
}, |
|
112
|
}) |
|
113
|
|
|
114
|
export default rule |