glosslint 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +120 -0
- package/NOTICE +31 -0
- package/README.md +98 -2
- package/dist/cli.d.mts +111 -0
- package/dist/cli.mjs +591 -0
- package/dist/eslint.d.mts +29 -0
- package/dist/eslint.mjs +39 -0
- package/dist/format-CQ9xdJYe.mjs +66 -0
- package/dist/glossary-DmTK_e1d.d.mts +32 -0
- package/dist/glossary-lk7EwGey.mjs +1872 -0
- package/dist/inclusive-CP6oU8NW.mjs +520 -0
- package/dist/index.d.mts +75 -0
- package/dist/index.mjs +3 -0
- package/dist/oxlint.d.mts +26 -0
- package/dist/oxlint.mjs +33 -0
- package/dist/presets/inclusive.d.mts +3 -0
- package/dist/presets/inclusive.mjs +1 -0
- package/dist/presets/tech.d.mts +3 -0
- package/dist/presets/tech.mjs +1 -0
- package/dist/rules-D-PrDySk.mjs +768 -0
- package/dist/tech-D0v3ss45.mjs +2401 -0
- package/dist/types-Bbp_cYfZ.d.mts +192 -0
- package/dist/x18n.d.mts +28 -0
- package/dist/x18n.mjs +164 -0
- package/package.json +134 -2
|
@@ -0,0 +1,768 @@
|
|
|
1
|
+
import { d as createMatcher, f as caseModeOf, p as classify, v as splitWords } from "./glossary-lk7EwGey.mjs";
|
|
2
|
+
// src/eslint/offsets.ts
|
|
3
|
+
/**
|
|
4
|
+
* Maps from a string's cooked value back to its source text. Each map has one entry per UTF-16 unit of
|
|
5
|
+
* the cooked value plus one for its end, each an offset into the raw text (the source between the
|
|
6
|
+
* quotes). A map is `undefined` when the raw text can't be read back exactly, and then no fix is offered.
|
|
7
|
+
*/
|
|
8
|
+
const JS_SIMPLE = {
|
|
9
|
+
n: "\n",
|
|
10
|
+
t: " ",
|
|
11
|
+
r: "\r",
|
|
12
|
+
b: "\b",
|
|
13
|
+
f: "\f",
|
|
14
|
+
v: "\v",
|
|
15
|
+
"0": "\0"
|
|
16
|
+
};
|
|
17
|
+
const YAML_SIMPLE = {
|
|
18
|
+
"0": "\0",
|
|
19
|
+
a: "\x07",
|
|
20
|
+
b: "\b",
|
|
21
|
+
t: " ",
|
|
22
|
+
" ": " ",
|
|
23
|
+
n: "\n",
|
|
24
|
+
v: "\v",
|
|
25
|
+
f: "\f",
|
|
26
|
+
r: "\r",
|
|
27
|
+
e: "\x1B",
|
|
28
|
+
" ": " ",
|
|
29
|
+
"\"": "\"",
|
|
30
|
+
"/": "/",
|
|
31
|
+
"\\": "\\",
|
|
32
|
+
N: "
",
|
|
33
|
+
_: "\xA0",
|
|
34
|
+
L: "\u2028",
|
|
35
|
+
P: "\u2029"
|
|
36
|
+
};
|
|
37
|
+
const HEX = {
|
|
38
|
+
x: 2,
|
|
39
|
+
u: 4,
|
|
40
|
+
U: 8
|
|
41
|
+
};
|
|
42
|
+
/** Reads the escapes of a JS string literal or template quasi. */
|
|
43
|
+
function jsMap(raw, cooked) {
|
|
44
|
+
const map = [];
|
|
45
|
+
let out = "";
|
|
46
|
+
const emit = (text, at) => {
|
|
47
|
+
for (let unit = 0; unit < text.length; unit++) map.push(at);
|
|
48
|
+
out += text;
|
|
49
|
+
};
|
|
50
|
+
for (let index = 0; index < raw.length;) {
|
|
51
|
+
const char = raw[index];
|
|
52
|
+
if (char !== "\\") {
|
|
53
|
+
emit(char, index);
|
|
54
|
+
index++;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const next = raw[index + 1];
|
|
58
|
+
if (next === void 0) return void 0;
|
|
59
|
+
if (next === "\r" && raw[index + 2] === "\n") {
|
|
60
|
+
index += 3;
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
if ("\n\r\u2028\u2029".includes(next)) {
|
|
64
|
+
index += 2;
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (next === "x" || next === "u" && raw[index + 2] !== "{") {
|
|
68
|
+
const size = next === "x" ? 2 : 4;
|
|
69
|
+
const hex = raw.slice(index + 2, index + 2 + size);
|
|
70
|
+
if (!/^[\da-f]+$/i.test(hex) || hex.length !== size) return void 0;
|
|
71
|
+
emit(String.fromCharCode(Number.parseInt(hex, 16)), index);
|
|
72
|
+
index += 2 + size;
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
if (next === "u") {
|
|
76
|
+
const close = raw.indexOf("}", index);
|
|
77
|
+
const hex = raw.slice(index + 3, close);
|
|
78
|
+
if (close === -1 || !/^[\da-f]+$/i.test(hex)) return void 0;
|
|
79
|
+
emit(String.fromCodePoint(Number.parseInt(hex, 16)), index);
|
|
80
|
+
index = close + 1;
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (/\d/.test(next) && (next !== "0" || /\d/.test(raw[index + 2] ?? ""))) return void 0;
|
|
84
|
+
emit(JS_SIMPLE[next] ?? next, index);
|
|
85
|
+
index += 2;
|
|
86
|
+
}
|
|
87
|
+
map.push(raw.length);
|
|
88
|
+
return out === cooked ? map : void 0;
|
|
89
|
+
}
|
|
90
|
+
/** Reads a single-quoted YAML scalar on one line, where `''` is a quote. */
|
|
91
|
+
function yamlSingleMap(raw, cooked) {
|
|
92
|
+
if (/[\r\n]/.test(raw)) return void 0;
|
|
93
|
+
const map = [];
|
|
94
|
+
let out = "";
|
|
95
|
+
for (let index = 0; index < raw.length; index++) {
|
|
96
|
+
map.push(index);
|
|
97
|
+
out += raw[index];
|
|
98
|
+
if (raw[index] === "'" && raw[index + 1] === "'") index++;
|
|
99
|
+
}
|
|
100
|
+
map.push(raw.length);
|
|
101
|
+
return out === cooked ? map : void 0;
|
|
102
|
+
}
|
|
103
|
+
/** Reads a double-quoted YAML scalar on one line, with its backslash escapes. */
|
|
104
|
+
function yamlDoubleMap(raw, cooked) {
|
|
105
|
+
if (/[\r\n]/.test(raw)) return void 0;
|
|
106
|
+
const map = [];
|
|
107
|
+
let out = "";
|
|
108
|
+
const emit = (text, at) => {
|
|
109
|
+
for (let unit = 0; unit < text.length; unit++) map.push(at);
|
|
110
|
+
out += text;
|
|
111
|
+
};
|
|
112
|
+
for (let index = 0; index < raw.length;) {
|
|
113
|
+
const char = raw[index];
|
|
114
|
+
if (char !== "\\") {
|
|
115
|
+
emit(char, index);
|
|
116
|
+
index++;
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
const next = raw[index + 1] ?? "";
|
|
120
|
+
const size = HEX[next];
|
|
121
|
+
if (size) {
|
|
122
|
+
const hex = raw.slice(index + 2, index + 2 + size);
|
|
123
|
+
if (!/^[\da-f]+$/i.test(hex) || hex.length !== size) return void 0;
|
|
124
|
+
emit(String.fromCodePoint(Number.parseInt(hex, 16)), index);
|
|
125
|
+
index += 2 + size;
|
|
126
|
+
continue;
|
|
127
|
+
}
|
|
128
|
+
const simple = YAML_SIMPLE[next];
|
|
129
|
+
if (simple === void 0) return void 0;
|
|
130
|
+
emit(simple, index);
|
|
131
|
+
index += 2;
|
|
132
|
+
}
|
|
133
|
+
map.push(raw.length);
|
|
134
|
+
return out === cooked ? map : void 0;
|
|
135
|
+
}
|
|
136
|
+
/** The identity map, for text whose source is the value itself. */
|
|
137
|
+
function identityMap(raw, cooked) {
|
|
138
|
+
if (raw !== cooked) return void 0;
|
|
139
|
+
return Array.from({ length: raw.length + 1 }, (_, index) => index);
|
|
140
|
+
}
|
|
141
|
+
// src/eslint/sites.ts
|
|
142
|
+
const ALWAYS_IGNORED = [
|
|
143
|
+
"t",
|
|
144
|
+
"$t",
|
|
145
|
+
"te",
|
|
146
|
+
"tm"
|
|
147
|
+
];
|
|
148
|
+
/** The callees whose first argument `ignoreCallees` replaces by default. */
|
|
149
|
+
const DEFAULT_IGNORE_CALLEES = [
|
|
150
|
+
"endsWith",
|
|
151
|
+
"startsWith",
|
|
152
|
+
"includes",
|
|
153
|
+
"querySelector*"
|
|
154
|
+
];
|
|
155
|
+
const SKIPPED_ATTRIBUTES = /* @__PURE__ */ new Set([
|
|
156
|
+
"class",
|
|
157
|
+
"style",
|
|
158
|
+
"id",
|
|
159
|
+
"key",
|
|
160
|
+
"ref",
|
|
161
|
+
"name",
|
|
162
|
+
"to",
|
|
163
|
+
"href",
|
|
164
|
+
"src",
|
|
165
|
+
"icon",
|
|
166
|
+
"type",
|
|
167
|
+
"variant",
|
|
168
|
+
"color",
|
|
169
|
+
"size",
|
|
170
|
+
"slot",
|
|
171
|
+
"is",
|
|
172
|
+
"for",
|
|
173
|
+
"aria-labelledby",
|
|
174
|
+
"aria-describedby",
|
|
175
|
+
"aria-controls",
|
|
176
|
+
"aria-owns"
|
|
177
|
+
]);
|
|
178
|
+
const COMPARISONS = /* @__PURE__ */ new Set([
|
|
179
|
+
"==",
|
|
180
|
+
"===",
|
|
181
|
+
"!=",
|
|
182
|
+
"!==",
|
|
183
|
+
"<",
|
|
184
|
+
">",
|
|
185
|
+
"<=",
|
|
186
|
+
">=",
|
|
187
|
+
"in",
|
|
188
|
+
"instanceof"
|
|
189
|
+
]);
|
|
190
|
+
/** Nodes a string passes through without changing what it is. */
|
|
191
|
+
const WRAPPERS = /* @__PURE__ */ new Set([
|
|
192
|
+
"TSAsExpression",
|
|
193
|
+
"TSSatisfiesExpression",
|
|
194
|
+
"TSNonNullExpression",
|
|
195
|
+
"TSTypeAssertion",
|
|
196
|
+
"ParenthesizedExpression",
|
|
197
|
+
"ChainExpression"
|
|
198
|
+
]);
|
|
199
|
+
/** Nodes a string passes through on its way to the name it is assigned to. */
|
|
200
|
+
const NAME_WRAPPERS = /* @__PURE__ */ new Set([
|
|
201
|
+
...WRAPPERS,
|
|
202
|
+
"ConditionalExpression",
|
|
203
|
+
"LogicalExpression",
|
|
204
|
+
"ArrayExpression",
|
|
205
|
+
"TemplateLiteral",
|
|
206
|
+
"BinaryExpression"
|
|
207
|
+
]);
|
|
208
|
+
/** A string with no spaces, all in lowercase, shaped like a key, slug, path or single code word. */
|
|
209
|
+
const LOWER_SHAPE = /^[\p{Ll}\p{N}_\-./:#@~]+$/u;
|
|
210
|
+
/** A camelCase name, such as a column id. Capitalised words and hyphenated labels don't match. */
|
|
211
|
+
const CAMEL_SHAPE = /^\p{Ll}[\p{Ll}\p{N}]*\p{Lu}[\p{L}\p{N}_\-./:]*$/u;
|
|
212
|
+
/** Whether a JS string or attribute value is shaped like code, so it is skipped (decision 7). */
|
|
213
|
+
function isCodeShape(text) {
|
|
214
|
+
return LOWER_SHAPE.test(text) || CAMEL_SHAPE.test(text);
|
|
215
|
+
}
|
|
216
|
+
function isSkippedAttribute(name) {
|
|
217
|
+
if (name === void 0) return false;
|
|
218
|
+
const lower = name.toLowerCase();
|
|
219
|
+
return SKIPPED_ATTRIBUTES.has(lower) || lower.startsWith("data-");
|
|
220
|
+
}
|
|
221
|
+
function nameOf(node) {
|
|
222
|
+
if (!node) return void 0;
|
|
223
|
+
if (node.type === "Identifier" || node.type === "PrivateIdentifier") return node.name;
|
|
224
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value;
|
|
225
|
+
if (node.type === "MemberExpression" && !node.computed) return nameOf(node.property);
|
|
226
|
+
}
|
|
227
|
+
function calleeName(node) {
|
|
228
|
+
const callee = node.callee;
|
|
229
|
+
if (callee.type === "Identifier") return callee.name;
|
|
230
|
+
if (callee.type === "MemberExpression") return nameOf(callee.property);
|
|
231
|
+
}
|
|
232
|
+
function matchesCallee(name, patterns) {
|
|
233
|
+
return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
|
|
234
|
+
}
|
|
235
|
+
/** The attribute or directive argument name of a Vue attribute. */
|
|
236
|
+
function attributeName(attribute) {
|
|
237
|
+
const key = attribute.key;
|
|
238
|
+
if (!attribute.directive) return key.rawName ?? key.name;
|
|
239
|
+
const argument = key.argument;
|
|
240
|
+
return argument?.type === "VIdentifier" ? argument.rawName ?? argument.name : void 0;
|
|
241
|
+
}
|
|
242
|
+
/** Whether a string literal or template literal sits where decision 7 says it is code. */
|
|
243
|
+
function isSkippedString(node, ignoreCallees) {
|
|
244
|
+
let child = node;
|
|
245
|
+
let parent = node.parent;
|
|
246
|
+
while (parent && (WRAPPERS.has(parent.type) || parent.type === "BinaryExpression" && parent.operator === "+")) {
|
|
247
|
+
child = parent;
|
|
248
|
+
parent = parent.parent;
|
|
249
|
+
}
|
|
250
|
+
if (!parent) return false;
|
|
251
|
+
switch (parent.type) {
|
|
252
|
+
case "ImportDeclaration":
|
|
253
|
+
case "ExportAllDeclaration":
|
|
254
|
+
case "ExportNamedDeclaration":
|
|
255
|
+
case "ImportExpression":
|
|
256
|
+
case "TSExternalModuleReference":
|
|
257
|
+
case "TSImportType":
|
|
258
|
+
case "ImportAttribute":
|
|
259
|
+
case "ImportSpecifier":
|
|
260
|
+
case "ExportSpecifier":
|
|
261
|
+
case "TSLiteralType":
|
|
262
|
+
case "TaggedTemplateExpression": return true;
|
|
263
|
+
case "ExpressionStatement": return parent.directive !== void 0;
|
|
264
|
+
case "Property":
|
|
265
|
+
case "PropertyDefinition":
|
|
266
|
+
case "MethodDefinition":
|
|
267
|
+
case "AccessorProperty":
|
|
268
|
+
case "TSPropertySignature":
|
|
269
|
+
case "TSAbstractPropertyDefinition": return parent.key === child && !(parent.type === "Property" && isTextKey(child));
|
|
270
|
+
case "TSEnumMember": return parent.id === child;
|
|
271
|
+
case "MemberExpression": return parent.property === child;
|
|
272
|
+
case "BinaryExpression": return COMPARISONS.has(parent.operator);
|
|
273
|
+
case "SwitchCase": return parent.test === child;
|
|
274
|
+
case "CallExpression":
|
|
275
|
+
case "NewExpression": {
|
|
276
|
+
if (parent.arguments[0] !== child) return false;
|
|
277
|
+
const name = calleeName(parent);
|
|
278
|
+
return name !== void 0 && (ALWAYS_IGNORED.includes(name) || matchesCallee(name, ignoreCallees));
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
return false;
|
|
282
|
+
}
|
|
283
|
+
function isTextKey(node) {
|
|
284
|
+
return node.type === "Literal" && typeof node.value === "string" && /\s/.test(node.value);
|
|
285
|
+
}
|
|
286
|
+
/** The name `kindOf` reads for a string: the key, variable, attribute or YAML key it belongs to. */
|
|
287
|
+
function stringName(node) {
|
|
288
|
+
let child = node;
|
|
289
|
+
let parent = node.parent;
|
|
290
|
+
while (parent && NAME_WRAPPERS.has(parent.type)) {
|
|
291
|
+
if (parent.type === "BinaryExpression" && parent.operator !== "+") break;
|
|
292
|
+
child = parent;
|
|
293
|
+
parent = parent.parent;
|
|
294
|
+
}
|
|
295
|
+
if (!parent) return void 0;
|
|
296
|
+
switch (parent.type) {
|
|
297
|
+
case "Property":
|
|
298
|
+
case "PropertyDefinition": return parent.value === child ? nameOf(parent.key) : void 0;
|
|
299
|
+
case "VariableDeclarator": return nameOf(parent.id);
|
|
300
|
+
case "AssignmentExpression":
|
|
301
|
+
case "AssignmentPattern": return nameOf(parent.left);
|
|
302
|
+
case "VExpressionContainer": {
|
|
303
|
+
const holder = parent.parent;
|
|
304
|
+
if (holder?.type === "VAttribute") return attributeName(holder);
|
|
305
|
+
return "text";
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
/** The Vue attribute a template expression belongs to, if any. */
|
|
310
|
+
function enclosingAttribute(node) {
|
|
311
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
312
|
+
if (current.type === "VAttribute") return current;
|
|
313
|
+
if (current.type === "VElement") return void 0;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
function noneOf(...forbidden) {
|
|
317
|
+
return (replace) => !forbidden.some((text) => replace.includes(text));
|
|
318
|
+
}
|
|
319
|
+
const NEWLINES = ["\n", "\r"];
|
|
320
|
+
/** Builds the sites of a JS string literal or template literal, or none when it is skipped. */
|
|
321
|
+
function esSites(node, source, ignoreCallees, inTemplate, decide) {
|
|
322
|
+
if (isSkippedString(node, ignoreCallees)) return [];
|
|
323
|
+
if (inTemplate && isSkippedAttribute(attributeNameOf(node))) return [];
|
|
324
|
+
const name = stringName(node);
|
|
325
|
+
if (node.type === "Literal") {
|
|
326
|
+
const text = node.value;
|
|
327
|
+
if (isCodeShape(text)) return [];
|
|
328
|
+
const raw = node.raw.slice(1, -1);
|
|
329
|
+
const quote = node.raw[0];
|
|
330
|
+
const isKey = node.parent?.type === "Property" && node.parent.key === node;
|
|
331
|
+
return [{
|
|
332
|
+
text,
|
|
333
|
+
...decide({
|
|
334
|
+
name,
|
|
335
|
+
text
|
|
336
|
+
}),
|
|
337
|
+
base: node.range[0] + 1,
|
|
338
|
+
map: jsMap(raw, text),
|
|
339
|
+
within: node.range,
|
|
340
|
+
safe: isKey ? () => false : noneOf(quote, "\\", ...NEWLINES)
|
|
341
|
+
}];
|
|
342
|
+
}
|
|
343
|
+
const quasis = node.quasis;
|
|
344
|
+
const cookedOf = (quasi) => quasi.value.cooked;
|
|
345
|
+
if (isCodeShape(quasis.map((quasi) => cookedOf(quasi) ?? "").join(""))) return [];
|
|
346
|
+
const { kind, guessed } = decide({
|
|
347
|
+
name,
|
|
348
|
+
text: quasis.map((quasi) => cookedOf(quasi) ?? "").join("x")
|
|
349
|
+
});
|
|
350
|
+
const sites = [];
|
|
351
|
+
for (const quasi of quasis) {
|
|
352
|
+
const cooked = cookedOf(quasi);
|
|
353
|
+
const raw = quasi.value.raw;
|
|
354
|
+
if (cooked === null || cooked.trim() === "") continue;
|
|
355
|
+
const base = quasi.range[0] + 1;
|
|
356
|
+
const exact = source.slice(base, base + raw.length) === raw;
|
|
357
|
+
sites.push({
|
|
358
|
+
text: cooked,
|
|
359
|
+
kind,
|
|
360
|
+
guessed,
|
|
361
|
+
base,
|
|
362
|
+
map: exact ? jsMap(raw, cooked) : void 0,
|
|
363
|
+
within: quasi.range,
|
|
364
|
+
safe: noneOf("`", "${", "\\")
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
return sites;
|
|
368
|
+
}
|
|
369
|
+
function attributeNameOf(node) {
|
|
370
|
+
const attribute = enclosingAttribute(node);
|
|
371
|
+
return attribute ? attributeName(attribute) : void 0;
|
|
372
|
+
}
|
|
373
|
+
/** The name a Vue text node's element gives it: the slot of a `<template #header>`, else the tag. */
|
|
374
|
+
function elementName(node) {
|
|
375
|
+
const element = node.parent;
|
|
376
|
+
if (element?.type !== "VElement") return void 0;
|
|
377
|
+
const tag = String(element.rawName ?? element.name ?? "");
|
|
378
|
+
if (tag !== "template") return tag;
|
|
379
|
+
const attributes = element.startTag?.attributes ?? [];
|
|
380
|
+
for (const attribute of attributes) {
|
|
381
|
+
const key = attribute.key;
|
|
382
|
+
if (!attribute.directive || (key?.name)?.name !== "slot") continue;
|
|
383
|
+
const argument = key?.argument;
|
|
384
|
+
if (argument?.type === "VIdentifier") return String(argument.rawName ?? argument.name);
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
/** Builds the site of a Vue template text node. */
|
|
388
|
+
function textSite(node, source, decide) {
|
|
389
|
+
const text = node.value;
|
|
390
|
+
if (text.trim() === "") return void 0;
|
|
391
|
+
const raw = source.slice(node.range[0], node.range[1]);
|
|
392
|
+
return {
|
|
393
|
+
text,
|
|
394
|
+
...decide({
|
|
395
|
+
name: elementName(node),
|
|
396
|
+
text,
|
|
397
|
+
shown: true
|
|
398
|
+
}),
|
|
399
|
+
base: node.range[0],
|
|
400
|
+
map: identityMap(raw, text),
|
|
401
|
+
within: node.range,
|
|
402
|
+
safe: noneOf("<", "&", "{{", "}}")
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
/** Builds the site of a static Vue attribute value. */
|
|
406
|
+
function attributeSite(attribute, source, decide) {
|
|
407
|
+
const value = attribute.value;
|
|
408
|
+
if (attribute.directive || !value || value.type !== "VLiteral") return void 0;
|
|
409
|
+
const name = attributeName(attribute);
|
|
410
|
+
const text = value.value;
|
|
411
|
+
if (isSkippedAttribute(name) || text.trim() === "" || isCodeShape(text)) return void 0;
|
|
412
|
+
let raw = source.slice(value.range[0], value.range[1]);
|
|
413
|
+
let base = value.range[0];
|
|
414
|
+
const quote = /^["']/.test(raw) ? raw[0] : "";
|
|
415
|
+
if (quote) {
|
|
416
|
+
raw = raw.slice(1, -1);
|
|
417
|
+
base++;
|
|
418
|
+
}
|
|
419
|
+
return {
|
|
420
|
+
text,
|
|
421
|
+
...decide({
|
|
422
|
+
name,
|
|
423
|
+
text
|
|
424
|
+
}),
|
|
425
|
+
base,
|
|
426
|
+
map: identityMap(raw, text),
|
|
427
|
+
within: value.range,
|
|
428
|
+
safe: quote ? noneOf("&", "\"", "'", "<", ...NEWLINES) : noneOf("&", "\"", "'", "<", ">", "=", "`", " ", " ", ...NEWLINES)
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
function yamlKeyName(node) {
|
|
432
|
+
for (let current = node.parent; current; current = current.parent) {
|
|
433
|
+
if (current.type !== "YAMLPair") continue;
|
|
434
|
+
const key = current.key;
|
|
435
|
+
if (key?.type === "YAMLScalar" && key.value !== null) return String(key.value);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
/** Builds the site of a YAML string value; keys are never checked. */
|
|
440
|
+
function yamlSite(node, decide) {
|
|
441
|
+
if (typeof node.value !== "string" || node.value.trim() === "") return void 0;
|
|
442
|
+
const parent = node.parent;
|
|
443
|
+
if (parent?.type === "YAMLPair" && parent.key === node) return void 0;
|
|
444
|
+
const text = node.value;
|
|
445
|
+
const name = yamlKeyName(node);
|
|
446
|
+
const raw = typeof node.raw === "string" ? node.raw : "";
|
|
447
|
+
const site = {
|
|
448
|
+
text,
|
|
449
|
+
...decide({
|
|
450
|
+
name,
|
|
451
|
+
text,
|
|
452
|
+
shown: true
|
|
453
|
+
}),
|
|
454
|
+
within: node.range
|
|
455
|
+
};
|
|
456
|
+
switch (node.style) {
|
|
457
|
+
case "plain": return {
|
|
458
|
+
...site,
|
|
459
|
+
base: node.range[0],
|
|
460
|
+
map: /[\r\n]/.test(raw) ? void 0 : identityMap(raw, text),
|
|
461
|
+
safe: noneOf("#", ":", ",", "[", "]", "{", "}", ...NEWLINES)
|
|
462
|
+
};
|
|
463
|
+
case "single-quoted": return {
|
|
464
|
+
...site,
|
|
465
|
+
base: node.range[0] + 1,
|
|
466
|
+
map: yamlSingleMap(raw.slice(1, -1), text),
|
|
467
|
+
safe: noneOf("'", ...NEWLINES)
|
|
468
|
+
};
|
|
469
|
+
case "double-quoted": return {
|
|
470
|
+
...site,
|
|
471
|
+
base: node.range[0] + 1,
|
|
472
|
+
map: yamlDoubleMap(raw.slice(1, -1), text),
|
|
473
|
+
safe: noneOf("\"", "\\", ...NEWLINES)
|
|
474
|
+
};
|
|
475
|
+
default: return {
|
|
476
|
+
...site,
|
|
477
|
+
base: node.range[0],
|
|
478
|
+
safe: () => false
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
function escapeRegExp(text) {
|
|
483
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Places a site's findings in the source. With a map each offset is exact; without one each finding's
|
|
487
|
+
* text is searched for in the site's source, in order, and no fix is offered.
|
|
488
|
+
*/
|
|
489
|
+
function place(site, source, findings) {
|
|
490
|
+
let cursor = site.within[0];
|
|
491
|
+
return findings.map((finding) => {
|
|
492
|
+
const map = site.map;
|
|
493
|
+
if (map) return {
|
|
494
|
+
range: [site.base + map[finding.start], site.base + map[finding.end]],
|
|
495
|
+
fixable: !finding.fix || site.safe(finding.fix.replace)
|
|
496
|
+
};
|
|
497
|
+
const pattern = new RegExp(escapeRegExp(finding.text.trim()).replace(/\s+/g, "\\s+"), "gu");
|
|
498
|
+
pattern.lastIndex = cursor;
|
|
499
|
+
const area = source.slice(0, site.within[1]);
|
|
500
|
+
const match = pattern.exec(area);
|
|
501
|
+
if (!match) return {
|
|
502
|
+
range: site.within,
|
|
503
|
+
fixable: false
|
|
504
|
+
};
|
|
505
|
+
cursor = match.index + match[0].length;
|
|
506
|
+
return {
|
|
507
|
+
range: [match.index, cursor],
|
|
508
|
+
fixable: false
|
|
509
|
+
};
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
/** Builds the listener that hands every checkable string of a file to `onSite`. */
|
|
513
|
+
function stringListener(context, ignoreCallees, kindOf, onSite) {
|
|
514
|
+
const source = context.sourceCode.text;
|
|
515
|
+
const file = context.filename;
|
|
516
|
+
const decide = (site) => classify({
|
|
517
|
+
...site,
|
|
518
|
+
file
|
|
519
|
+
}, kindOf);
|
|
520
|
+
const emit = (site) => site && onSite(site);
|
|
521
|
+
const es = (inTemplate) => (node) => {
|
|
522
|
+
const n = node;
|
|
523
|
+
if (n.type === "Literal" && typeof n.value !== "string") return;
|
|
524
|
+
for (const site of esSites(n, source, ignoreCallees, inTemplate, decide)) onSite(site);
|
|
525
|
+
};
|
|
526
|
+
const script = {
|
|
527
|
+
Literal: es(false),
|
|
528
|
+
TemplateLiteral: es(false),
|
|
529
|
+
YAMLScalar: (node) => emit(yamlSite(node, decide))
|
|
530
|
+
};
|
|
531
|
+
const services = context.sourceCode.parserServices;
|
|
532
|
+
if (!services?.defineTemplateBodyVisitor) return script;
|
|
533
|
+
return services.defineTemplateBodyVisitor({
|
|
534
|
+
VText: (node) => emit(textSite(node, source, decide)),
|
|
535
|
+
VAttribute: (node) => emit(attributeSite(node, source, decide)),
|
|
536
|
+
Literal: es(true),
|
|
537
|
+
TemplateLiteral: es(true)
|
|
538
|
+
}, script);
|
|
539
|
+
}
|
|
540
|
+
// src/eslint/rules.ts
|
|
541
|
+
const matchers = /* @__PURE__ */ new WeakMap();
|
|
542
|
+
const identifierMatchers = /* @__PURE__ */ new WeakMap();
|
|
543
|
+
/** One matcher per corpus, and a second one per corpus for identifiers when avoid words count there. */
|
|
544
|
+
function matcherFor(corpus, identifierAvoid = false) {
|
|
545
|
+
const cache = identifierAvoid ? identifierMatchers : matchers;
|
|
546
|
+
let matcher = cache.get(corpus);
|
|
547
|
+
if (!matcher) {
|
|
548
|
+
matcher = createMatcher(corpus, { identifierAvoid });
|
|
549
|
+
cache.set(corpus, matcher);
|
|
550
|
+
}
|
|
551
|
+
return matcher;
|
|
552
|
+
}
|
|
553
|
+
const messages = {
|
|
554
|
+
GLOSSLINT_TERM: "{{message}}",
|
|
555
|
+
GLOSSLINT_AVOID: "{{message}}",
|
|
556
|
+
GLOSSLINT_CASE: "{{message}}",
|
|
557
|
+
suggest: "Change to \"{{replace}}\"."
|
|
558
|
+
};
|
|
559
|
+
const oneLine = (text) => text.replace(/\s+/g, " ").trim();
|
|
560
|
+
function docs(description) {
|
|
561
|
+
return {
|
|
562
|
+
description,
|
|
563
|
+
url: "https://github.com/onmax/glosslint#eslint"
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
/**
|
|
567
|
+
* Reports findings of a site, with an autofix for TERM and CASE and a suggestion for AVOID. Where only the
|
|
568
|
+
* text's shape decided its kind, a `context` term's fix is a suggestion too, since the guess may be wrong.
|
|
569
|
+
*/
|
|
570
|
+
function report(context, site, findings, codes) {
|
|
571
|
+
const wanted = findings.filter((finding) => codes.has(finding.code));
|
|
572
|
+
if (wanted.length === 0) return;
|
|
573
|
+
const { sourceCode } = context;
|
|
574
|
+
const placed = place(site, sourceCode.text, wanted);
|
|
575
|
+
for (const [index, finding] of wanted.entries()) {
|
|
576
|
+
const { range, fixable } = placed[index];
|
|
577
|
+
const loc = {
|
|
578
|
+
start: sourceCode.getLocFromIndex(range[0]),
|
|
579
|
+
end: sourceCode.getLocFromIndex(range[1])
|
|
580
|
+
};
|
|
581
|
+
const replace = finding.fix?.replace;
|
|
582
|
+
const fix = fixable && replace !== void 0 ? (fixer) => fixer.replaceTextRange(range, replace) : void 0;
|
|
583
|
+
const base = {
|
|
584
|
+
loc,
|
|
585
|
+
messageId: finding.code,
|
|
586
|
+
data: { message: oneLine(finding.message) }
|
|
587
|
+
};
|
|
588
|
+
if (finding.fix?.kind === "autofix" && !(site.guessed && caseModeOf(finding.term) === "context")) context.report({
|
|
589
|
+
...base,
|
|
590
|
+
fix: fix ?? null
|
|
591
|
+
});
|
|
592
|
+
else context.report({
|
|
593
|
+
...base,
|
|
594
|
+
suggest: fix ? [{
|
|
595
|
+
messageId: "suggest",
|
|
596
|
+
data: { replace },
|
|
597
|
+
fix
|
|
598
|
+
}] : []
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
const STRING_CODES = /* @__PURE__ */ new Set(["GLOSSLINT_TERM", "GLOSSLINT_CASE"]);
|
|
603
|
+
const AVOID_CODES = /* @__PURE__ */ new Set(["GLOSSLINT_AVOID"]);
|
|
604
|
+
const ALL_CODES = /* @__PURE__ */ new Set([
|
|
605
|
+
"GLOSSLINT_TERM",
|
|
606
|
+
"GLOSSLINT_AVOID",
|
|
607
|
+
"GLOSSLINT_CASE"
|
|
608
|
+
]);
|
|
609
|
+
function stringRule(bind, codes, meta) {
|
|
610
|
+
return {
|
|
611
|
+
meta: {
|
|
612
|
+
schema: [],
|
|
613
|
+
messages,
|
|
614
|
+
...meta
|
|
615
|
+
},
|
|
616
|
+
create(context) {
|
|
617
|
+
const { corpus, settings } = bind(context);
|
|
618
|
+
const matcher = matcherFor(corpus);
|
|
619
|
+
return stringListener(context, settings.ignoreCallees, settings.kindOf, (site) => report(context, site, matcher.check(site.text, site.kind), codes));
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
}
|
|
623
|
+
/** Comments that configure a tool rather than say something. */
|
|
624
|
+
const DIRECTIVE = /^\s*(?:eslint|global|globals|exported|jshint|istanbul|c8|v8|prettier-ignore|oxlint|biome|@ts-|ts-|#?(?:end)?region\b|\/\s*<reference)/;
|
|
625
|
+
function commentRule(bind) {
|
|
626
|
+
return {
|
|
627
|
+
meta: {
|
|
628
|
+
type: "suggestion",
|
|
629
|
+
docs: docs("Check comments against the corpus: spellings, avoid words and exact casing."),
|
|
630
|
+
fixable: "code",
|
|
631
|
+
hasSuggestions: true,
|
|
632
|
+
schema: [],
|
|
633
|
+
messages
|
|
634
|
+
},
|
|
635
|
+
create(context) {
|
|
636
|
+
const matcher = matcherFor(bind(context).corpus);
|
|
637
|
+
const { sourceCode } = context;
|
|
638
|
+
const source = sourceCode.text;
|
|
639
|
+
const check = (comment) => {
|
|
640
|
+
if (!comment.range || DIRECTIVE.test(comment.value)) return;
|
|
641
|
+
const base = source.indexOf(comment.value, comment.range[0]);
|
|
642
|
+
if (base === -1) return;
|
|
643
|
+
const text = comment.value;
|
|
644
|
+
report(context, {
|
|
645
|
+
text,
|
|
646
|
+
kind: "comment",
|
|
647
|
+
guessed: false,
|
|
648
|
+
base,
|
|
649
|
+
map: Array.from({ length: text.length + 1 }, (_, index) => index),
|
|
650
|
+
within: comment.range,
|
|
651
|
+
safe: (replace) => comment.type === "Line" || comment.type === "YAMLComment" ? !/[\r\n]/.test(replace) : !replace.includes("*/") && !replace.includes("-->")
|
|
652
|
+
}, matcher.check(text, "comment"), ALL_CODES);
|
|
653
|
+
};
|
|
654
|
+
return { Program() {
|
|
655
|
+
for (const comment of sourceCode.getAllComments()) check(comment);
|
|
656
|
+
const ast = sourceCode.ast;
|
|
657
|
+
for (const comment of ast.templateBody?.comments ?? []) check(comment);
|
|
658
|
+
} };
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
/** The identifiers a declaration introduces: variables, functions, params, classes and properties. */
|
|
663
|
+
function declaredNames(node) {
|
|
664
|
+
const names = [];
|
|
665
|
+
const binding = (target) => {
|
|
666
|
+
if (!target) return;
|
|
667
|
+
if (target.type === "Identifier") names.push(target);
|
|
668
|
+
else if (target.type === "AssignmentPattern") binding(target.left);
|
|
669
|
+
else if (target.type === "RestElement") binding(target.argument);
|
|
670
|
+
else if (target.type === "TSParameterProperty") binding(target.parameter);
|
|
671
|
+
else if (target.type === "ArrayPattern") for (const element of target.elements) binding(element);
|
|
672
|
+
else if (target.type === "ObjectPattern") {
|
|
673
|
+
for (const property of target.properties) if (property.type === "RestElement") binding(property);
|
|
674
|
+
else if (!property.shorthand) binding(property.value);
|
|
675
|
+
}
|
|
676
|
+
};
|
|
677
|
+
switch (node.type) {
|
|
678
|
+
case "VariableDeclarator":
|
|
679
|
+
binding(node.id);
|
|
680
|
+
break;
|
|
681
|
+
case "FunctionDeclaration":
|
|
682
|
+
case "FunctionExpression":
|
|
683
|
+
case "ArrowFunctionExpression":
|
|
684
|
+
binding(node.id);
|
|
685
|
+
for (const param of node.params) binding(param);
|
|
686
|
+
break;
|
|
687
|
+
case "ClassDeclaration":
|
|
688
|
+
case "ClassExpression":
|
|
689
|
+
binding(node.id);
|
|
690
|
+
break;
|
|
691
|
+
case "Property":
|
|
692
|
+
if (node.parent?.type === "ObjectExpression" && !node.computed && !node.shorthand) {
|
|
693
|
+
if (node.key.type === "Identifier") names.push(node.key);
|
|
694
|
+
}
|
|
695
|
+
break;
|
|
696
|
+
case "PropertyDefinition":
|
|
697
|
+
case "MethodDefinition":
|
|
698
|
+
case "AccessorProperty": if (!node.computed && node.kind !== "constructor") {
|
|
699
|
+
if (node.key.type === "Identifier" || node.key.type === "PrivateIdentifier") names.push(node.key);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
return names;
|
|
703
|
+
}
|
|
704
|
+
function identifierRule(bind) {
|
|
705
|
+
return {
|
|
706
|
+
meta: {
|
|
707
|
+
type: "suggestion",
|
|
708
|
+
docs: docs("Check how declared names split into words. Suggests a rename, never fixes."),
|
|
709
|
+
schema: [],
|
|
710
|
+
messages
|
|
711
|
+
},
|
|
712
|
+
create(context) {
|
|
713
|
+
const { corpus, settings } = bind(context);
|
|
714
|
+
const matcher = matcherFor(corpus, settings.identifierAvoid);
|
|
715
|
+
const { sourceCode } = context;
|
|
716
|
+
const seen = /* @__PURE__ */ new Set();
|
|
717
|
+
const check = (node) => {
|
|
718
|
+
for (const id of declaredNames(node)) {
|
|
719
|
+
if (seen.has(id)) continue;
|
|
720
|
+
seen.add(id);
|
|
721
|
+
const name = id.name;
|
|
722
|
+
const start = id.range[0] + (id.type === "PrivateIdentifier" ? 1 : 0);
|
|
723
|
+
if (!sourceCode.text.startsWith(name, start)) continue;
|
|
724
|
+
const findings = matcher.check(name, "identifier").filter((finding) => finding.code !== "GLOSSLINT_TERM" || splitWords(finding.term.term).length > 1);
|
|
725
|
+
for (const finding of findings) context.report({
|
|
726
|
+
loc: {
|
|
727
|
+
start: sourceCode.getLocFromIndex(start + finding.start),
|
|
728
|
+
end: sourceCode.getLocFromIndex(start + finding.end)
|
|
729
|
+
},
|
|
730
|
+
messageId: finding.code,
|
|
731
|
+
data: { message: oneLine(finding.message) }
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
};
|
|
735
|
+
return {
|
|
736
|
+
VariableDeclarator: check,
|
|
737
|
+
FunctionDeclaration: check,
|
|
738
|
+
FunctionExpression: check,
|
|
739
|
+
ArrowFunctionExpression: check,
|
|
740
|
+
ClassDeclaration: check,
|
|
741
|
+
ClassExpression: check,
|
|
742
|
+
Property: check,
|
|
743
|
+
PropertyDefinition: check,
|
|
744
|
+
MethodDefinition: check,
|
|
745
|
+
AccessorProperty: check
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
};
|
|
749
|
+
}
|
|
750
|
+
/** The four rules, with the corpus and settings `bind` gives them on each file. */
|
|
751
|
+
function createRules(bind) {
|
|
752
|
+
return {
|
|
753
|
+
term: stringRule(bind, STRING_CODES, {
|
|
754
|
+
type: "suggestion",
|
|
755
|
+
docs: docs("Spell terms as the corpus does, with the casing it asks for."),
|
|
756
|
+
fixable: "code",
|
|
757
|
+
hasSuggestions: true
|
|
758
|
+
}),
|
|
759
|
+
avoid: stringRule(bind, AVOID_CODES, {
|
|
760
|
+
type: "suggestion",
|
|
761
|
+
docs: docs("Report words the corpus says to avoid, and suggest the term."),
|
|
762
|
+
hasSuggestions: true
|
|
763
|
+
}),
|
|
764
|
+
comment: commentRule(bind),
|
|
765
|
+
identifier: identifierRule(bind)
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
export { DEFAULT_IGNORE_CALLEES as n, createRules as t };
|