1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
const UPPERCASE_PATTERN = /\b[A-Z]/
const IGNORED_JSX_PARENTS = new Set(["code", "Code"])
const CHECKED_JSX_ATTRIBUTES = new Set(["placeholder", "alt", "title"])
const NOTIFY_NAMES = new Set(["notify"])
function isInsideCodeElement(node) {
let current = node.parent
while (current) {
if (
current.type === "JSXElement" &&
current.openingElement?.name?.name &&
IGNORED_JSX_PARENTS.has(current.openingElement.name.name)
) {
return true
}
current = current.parent
}
return false
}
function isClassName(node) {
return (
node.parent?.type === "JSXAttribute" &&
node.parent.name?.name === "className"
)
}
function isHrefOrSrc(node) {
return (
node.parent?.type === "JSXAttribute" &&
(node.parent.name?.name === "href" || node.parent.name?.name === "src")
)
}
function isFetchMethod(node) {
if (node.parent?.type !== "Property") return false
const key = node.parent.key
return key?.type === "Identifier" && key.name === "method"
}
function isObjectKey(node) {
return (
node.parent?.type === "Property" &&
node.parent.key === node
)
}
function isImportSource(node) {
return (
node.parent?.type === "ImportDeclaration" &&
node.parent.source === node
)
}
function isTypeContext(node) {
return (
node.parent?.type === "TSTypeReference" ||
node.parent?.type === "TSLiteralType" ||
node.parent?.type === "TSPropertySignature"
)
}
function isQueryKey(node) {
return (
node.parent?.type === "ArrayExpression" &&
node.parent.parent?.type === "Property" &&
node.parent.parent.key?.name === "queryKey"
)
}
function isContentTypeHeader(node) {
if (node.parent?.type !== "Property") return false
const key = node.parent.key
return key?.type === "Literal" && key.value === "Content-Type"
}
function isHeaderValue(node) {
if (node.parent?.type !== "Property") return false
const key = node.parent.key
if (key?.type === "Literal") {
const keyValue = String(key.value).toLowerCase()
return keyValue === "content-type" || keyValue === "authorization"
}
return false
}
function isComparisonTarget(node) {
return (
node.parent?.type === "BinaryExpression" &&
(node.parent.operator === "===" || node.parent.operator === "!==") &&
node.parent.right === node
)
}
function looksLikeCode(value) {
if (value.startsWith("Bearer ")) return true
if (value.startsWith("Authorization:")) return true
if (/^https?:\/\//.test(value)) return true
if (value.includes("::")) return true
return false
}
const rule = {
meta: {
type: "suggestion",
docs: {
description: "enforce lowercase user-facing strings",
},
messages: {
uppercaseString:
"user-facing string contains uppercase: \"{{value}}\". all user-facing text must be lowercase.",
uppercaseJsxText:
"jsx text contains uppercase: \"{{value}}\". all user-facing text must be lowercase.",
},
schema: [],
},
create(context) {
function checkStringLiteral(node, value, messageId) {
if (!UPPERCASE_PATTERN.test(value)) return
if (looksLikeCode(value)) return
context.report({
node,
messageId,
data: {
value: value.length > 60 ? value.slice(0, 57) + "..." : value,
},
})
}
return {
JSXText(node) {
const trimmed = node.value.trim()
if (!trimmed) return
if (!UPPERCASE_PATTERN.test(trimmed)) return
if (isInsideCodeElement(node)) return
context.report({
node,
messageId: "uppercaseJsxText",
data: {
value:
trimmed.length > 60 ? trimmed.slice(0, 57) + "..." : trimmed,
},
})
},
Literal(node) {
if (typeof node.value !== "string") return
if (!UPPERCASE_PATTERN.test(node.value)) return
if (isInsideCodeElement(node)) return
if (isClassName(node)) return
if (isHrefOrSrc(node)) return
if (isFetchMethod(node)) return
if (isObjectKey(node)) return
if (isImportSource(node)) return
if (isTypeContext(node)) return
if (isQueryKey(node)) return
if (isContentTypeHeader(node)) return
if (isHeaderValue(node)) return
if (isComparisonTarget(node)) return
if (
node.parent?.type === "CallExpression" &&
node.parent.callee?.type === "Identifier" &&
NOTIFY_NAMES.has(node.parent.callee.name)
) {
checkStringLiteral(node, node.value, "uppercaseString")
return
}
if (
node.parent?.type === "NewExpression" &&
node.parent.callee?.type === "Identifier" &&
node.parent.callee.name === "Error"
) {
checkStringLiteral(node, node.value, "uppercaseString")
return
}
if (
node.parent?.type === "JSXAttribute" &&
CHECKED_JSX_ATTRIBUTES.has(node.parent.name?.name)
) {
checkStringLiteral(node, node.value, "uppercaseString")
return
}
if (node.parent?.type === "JSXExpressionContainer") {
if (isInsideCodeElement(node.parent)) return
checkStringLiteral(node, node.value, "uppercaseString")
return
}
if (
node.parent?.type === "Property" &&
node.parent.value === node &&
node.parent.key?.type === "Identifier"
) {
const keyName = node.parent.key.name
if (
keyName === "label" ||
keyName === "description" ||
keyName === "title" ||
keyName === "error"
) {
checkStringLiteral(node, node.value, "uppercaseString")
return
}
}
},
TemplateLiteral(node) {
const isNotifyArg =
node.parent?.type === "CallExpression" &&
node.parent.callee?.type === "Identifier" &&
NOTIFY_NAMES.has(node.parent.callee.name)
const isErrorArg =
node.parent?.type === "NewExpression" &&
node.parent.callee?.type === "Identifier" &&
node.parent.callee.name === "Error"
const isJsxExpression =
node.parent?.type === "JSXExpressionContainer"
if (!isNotifyArg && !isErrorArg && !isJsxExpression) return
if (isInsideCodeElement(node)) return
for (const quasi of node.quasis) {
const raw = quasi.value.raw
if (UPPERCASE_PATTERN.test(raw) && !looksLikeCode(raw)) {
context.report({
node: quasi,
messageId: "uppercaseString",
data: {
value:
raw.length > 60 ? raw.slice(0, 57) + "..." : raw,
},
})
}
}
},
}
},
}
const plugin = {
meta: {
name: "asa-lowercase",
version: "1.0.0",
},
rules: {
"lowercase-strings": rule,
},
}
export default plugin
|