@opendesignai/harness 0.0.0-stage → 0.2.0-alpha.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 +201 -0
- package/README.md +132 -2
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +1723 -0
- package/dist/eslint-comments.d.mts +40 -0
- package/dist/eslint-comments.mjs +182 -0
- package/dist/paths-DBoYPrGy.mjs +59 -0
- package/dist/plugin.d.mts +41 -0
- package/dist/plugin.mjs +1410 -0
- package/docs/adopting.md +224 -0
- package/docs/baseline-move.md +56 -0
- package/docs/clones.md +51 -0
- package/docs/decisions/0001-oxlint.md +74 -0
- package/docs/decisions/0002-react-compiler-rules.md +34 -0
- package/docs/decisions/0003-agent-harness.md +37 -0
- package/docs/hooks.md +128 -0
- package/docs/ratchet.md +65 -0
- package/docs/rules/no-api-path-literal.md +35 -0
- package/docs/rules/no-cjk-literal.md +38 -0
- package/docs/rules/no-constant-flag.md +33 -0
- package/docs/rules/no-error-message-match.md +63 -0
- package/docs/rules/no-locale-branch.md +55 -0
- package/docs/rules/no-process-env.md +37 -0
- package/docs/rules/no-product-literal.md +41 -0
- package/docs/rules/no-raw-fetch.md +33 -0
- package/docs/rules/no-raw-storage.md +43 -0
- package/docs/rules/no-shell-imports-product.md +48 -0
- package/docs/rules/no-swallowed-catch.md +76 -0
- package/docs/rules/no-window-bus.md +35 -0
- package/docs/size.md +52 -0
- package/docs/unused.md +76 -0
- package/package.json +75 -3
- package/presets/base.json +7 -0
- package/presets/platform-boundary.json +4 -0
- package/presets/product.json +4 -0
- package/presets/react-compiler.json +13 -0
- package/presets/rules/base.json +49 -0
- package/presets/rules/platform-boundary.json +6 -0
- package/presets/rules/product.json +5 -0
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,1410 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import { n as repoRelativePath, t as compilePathPatterns } from "./paths-DBoYPrGy.mjs";
|
|
3
|
+
// src/plugin/meta.ts
|
|
4
|
+
const DOCS_BASE = "https://github.com/nexu-io/od-harness/blob/main/docs/rules";
|
|
5
|
+
function docsUrl(rule) {
|
|
6
|
+
return `${DOCS_BASE}/${rule}.md`;
|
|
7
|
+
}
|
|
8
|
+
function message(rule, parts) {
|
|
9
|
+
return `Violation: ${parts.violation} Fix: ${parts.fix} Exemption: ${parts.exemption} Docs: ${docsUrl(rule)}`;
|
|
10
|
+
}
|
|
11
|
+
/** The exemption sentence shared by rules whose exemptions live in the lint config. */
|
|
12
|
+
const CONFIG_EXEMPTION = "inline disable comments are rejected; a file that legitimately needs this (for example the one wrapper that implements it) is exempted by an `overrides` entry in the lint config, which the owner reviews. Existing debt stays in oxlint-suppressions.json and may only shrink.";
|
|
13
|
+
// src/plugin/utils/ast.ts
|
|
14
|
+
const WRAPPERS = /* @__PURE__ */ new Set([
|
|
15
|
+
"TSAsExpression",
|
|
16
|
+
"TSNonNullExpression",
|
|
17
|
+
"TSSatisfiesExpression",
|
|
18
|
+
"TSTypeAssertion",
|
|
19
|
+
"TSInstantiationExpression",
|
|
20
|
+
"ParenthesizedExpression",
|
|
21
|
+
"ChainExpression"
|
|
22
|
+
]);
|
|
23
|
+
/** Look through TS-only wrappers, parentheses and optional chains. */
|
|
24
|
+
function unwrap(node) {
|
|
25
|
+
let current = node ?? null;
|
|
26
|
+
while (current && WRAPPERS.has(current.type)) current = current.expression;
|
|
27
|
+
return current;
|
|
28
|
+
}
|
|
29
|
+
/** The string value of a string literal or of a template literal without expressions. */
|
|
30
|
+
function staticString(node) {
|
|
31
|
+
const n = unwrap(node);
|
|
32
|
+
if (!n) return null;
|
|
33
|
+
if (n.type === "Literal" && typeof n.value === "string") return n.value;
|
|
34
|
+
if (n.type === "TemplateLiteral" && n.expressions.length === 0) {
|
|
35
|
+
const quasi = n.quasis[0];
|
|
36
|
+
return quasi ? quasi.value.cooked ?? quasi.value.raw : "";
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
/** Name of a non-computed member property (`a.b` → "b", `a["b"]` → "b"). */
|
|
41
|
+
function propertyName(node) {
|
|
42
|
+
if (node.type !== "MemberExpression") return null;
|
|
43
|
+
if (!node.computed && node.property.type === "Identifier") return node.property.name;
|
|
44
|
+
if (node.computed) return staticString(node.property);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
/** Last name of a callee: `rm()` → "rm", `fs.promises.rm()` → "rm". */
|
|
48
|
+
function calleeName(callee) {
|
|
49
|
+
const c = unwrap(callee);
|
|
50
|
+
if (!c) return null;
|
|
51
|
+
if (c.type === "Identifier") return c.name;
|
|
52
|
+
if (c.type === "MemberExpression") return propertyName(c);
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
function findVariable(context, node, name) {
|
|
56
|
+
let scope = context.sourceCode.getScope(node);
|
|
57
|
+
while (scope) {
|
|
58
|
+
const variable = scope.set.get(name);
|
|
59
|
+
if (variable) return variable;
|
|
60
|
+
scope = scope.upper;
|
|
61
|
+
}
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The definition an identifier reference resolves to, following scopes. Undefined for a global
|
|
66
|
+
* and for names that are not references (`a.x`, `{ x: 1 }`).
|
|
67
|
+
*/
|
|
68
|
+
function definitionOf(context, node) {
|
|
69
|
+
if (node.type !== "Identifier") return void 0;
|
|
70
|
+
const parent = node.parent;
|
|
71
|
+
if (parent?.type === "MemberExpression" && parent.property === node && !parent.computed) return;
|
|
72
|
+
if (parent?.type === "Property" && parent.key === node && !parent.computed && !parent.shorthand) return;
|
|
73
|
+
return findVariable(context, node, node.name)?.defs[0];
|
|
74
|
+
}
|
|
75
|
+
/** `const x = <init>`: a declarator that binds one identifier (not a destructuring pattern). */
|
|
76
|
+
function isSimpleDeclarator(node) {
|
|
77
|
+
return node.type === "VariableDeclarator" && node.id.type === "Identifier";
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The function a call invokes when it is a same-file function declaration or a variable bound
|
|
81
|
+
* to a function expression (`function f() {}`, `const f = () => {}`).
|
|
82
|
+
*/
|
|
83
|
+
function calledFunction(context, call) {
|
|
84
|
+
const callee = unwrap(call.callee);
|
|
85
|
+
if (callee?.type !== "Identifier") return null;
|
|
86
|
+
const def = findVariable(context, callee, callee.name)?.defs[0];
|
|
87
|
+
if (!def) return null;
|
|
88
|
+
if (def.type === "FunctionName" && def.node.type === "FunctionDeclaration") return def.node;
|
|
89
|
+
if (!isSimpleDeclarator(def.node)) return null;
|
|
90
|
+
const init = unwrap(def.node.init);
|
|
91
|
+
if (init?.type === "ArrowFunctionExpression" || init?.type === "FunctionExpression") return init;
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
/** True when `name` at `node` refers to the global binding (not a local, parameter or import). */
|
|
95
|
+
function isGlobalName(context, node, name) {
|
|
96
|
+
const variable = findVariable(context, node, name);
|
|
97
|
+
return !variable || variable.defs.length === 0;
|
|
98
|
+
}
|
|
99
|
+
const GLOBAL_OBJECTS = /* @__PURE__ */ new Set([
|
|
100
|
+
"window",
|
|
101
|
+
"globalThis",
|
|
102
|
+
"self"
|
|
103
|
+
]);
|
|
104
|
+
/** `window`, `globalThis` or `self` (or any of `extra`) as an unshadowed global identifier. */
|
|
105
|
+
function isGlobalObject(context, node, extra = []) {
|
|
106
|
+
const n = unwrap(node);
|
|
107
|
+
if (!n || n.type !== "Identifier") return false;
|
|
108
|
+
if (!GLOBAL_OBJECTS.has(n.name) && !extra.includes(n.name)) return false;
|
|
109
|
+
return isGlobalName(context, n, n.name);
|
|
110
|
+
}
|
|
111
|
+
/** Direct children of a node, using the parser's visitor keys. */
|
|
112
|
+
function childNodes(node, visitorKeys) {
|
|
113
|
+
const record = node;
|
|
114
|
+
const keys = visitorKeys[node.type] ?? Object.keys(record);
|
|
115
|
+
const out = [];
|
|
116
|
+
for (const key of keys) {
|
|
117
|
+
if (key === "parent" || key === "loc" || key === "range") continue;
|
|
118
|
+
const value = record[key];
|
|
119
|
+
if (Array.isArray(value)) {
|
|
120
|
+
for (const item of value) if (isNode(item)) out.push(item);
|
|
121
|
+
} else if (isNode(value)) out.push(value);
|
|
122
|
+
}
|
|
123
|
+
return out;
|
|
124
|
+
}
|
|
125
|
+
function isNode(value) {
|
|
126
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
127
|
+
}
|
|
128
|
+
/** Depth-first search below (and including) `root`. */
|
|
129
|
+
function someNode(root, visitorKeys, predicate) {
|
|
130
|
+
const stack = [root];
|
|
131
|
+
while (stack.length > 0) {
|
|
132
|
+
const node = stack.pop();
|
|
133
|
+
if (predicate(node)) return true;
|
|
134
|
+
stack.push(...childNodes(node, visitorKeys));
|
|
135
|
+
}
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const TYPE_CONTAINERS = /* @__PURE__ */ new Set([
|
|
139
|
+
"TSTypeAnnotation",
|
|
140
|
+
"TSTypeQuery",
|
|
141
|
+
"TSTypeParameterInstantiation",
|
|
142
|
+
"TSTypeParameterDeclaration",
|
|
143
|
+
"TSInterfaceDeclaration",
|
|
144
|
+
"TSTypeAliasDeclaration",
|
|
145
|
+
"TSPropertySignature",
|
|
146
|
+
"TSIndexSignature",
|
|
147
|
+
"TSTypeLiteral",
|
|
148
|
+
"TSMethodSignature",
|
|
149
|
+
"TSCallSignatureDeclaration",
|
|
150
|
+
"TSConstructSignatureDeclaration",
|
|
151
|
+
"TSTypePredicate",
|
|
152
|
+
"TSTypeReference"
|
|
153
|
+
]);
|
|
154
|
+
/** Expressions whose `typeAnnotation` child is a type: `x as T`, `x satisfies T`, `<T>x`. */
|
|
155
|
+
const TYPE_OPERATORS = /* @__PURE__ */ new Set([
|
|
156
|
+
"TSAsExpression",
|
|
157
|
+
"TSSatisfiesExpression",
|
|
158
|
+
"TSTypeAssertion"
|
|
159
|
+
]);
|
|
160
|
+
/** An ambient declaration (`declare const`, `declare class`, `declare module`, `declare global`). */
|
|
161
|
+
function isAmbient(node) {
|
|
162
|
+
return node.declare === true;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* True for nodes that only exist in type positions (`type X = "a"`, `typeof x`, the type of
|
|
166
|
+
* `x as { a: "b" }`) or in ambient declarations, which emit no code (`declare const x = "a"`).
|
|
167
|
+
*/
|
|
168
|
+
function isInTypePosition(node) {
|
|
169
|
+
let child = node;
|
|
170
|
+
let current = node.parent;
|
|
171
|
+
let statement = false;
|
|
172
|
+
while (current) {
|
|
173
|
+
const type = current.type;
|
|
174
|
+
if (isAmbient(current)) return true;
|
|
175
|
+
if (!statement) {
|
|
176
|
+
if (TYPE_CONTAINERS.has(type) || type.startsWith("TS") && type.endsWith("Type")) return true;
|
|
177
|
+
if (TYPE_OPERATORS.has(type) && current.typeAnnotation === child) return true;
|
|
178
|
+
statement = type.endsWith("Statement") || type.endsWith("Declaration");
|
|
179
|
+
}
|
|
180
|
+
if (type === "Program") return false;
|
|
181
|
+
child = current;
|
|
182
|
+
current = current.parent;
|
|
183
|
+
}
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
/** True when a string literal is a module specifier (`import x from "a"`, `import("a")`). */
|
|
187
|
+
function isModuleSource(node) {
|
|
188
|
+
const parent = node.parent;
|
|
189
|
+
if (!parent) return false;
|
|
190
|
+
switch (parent.type) {
|
|
191
|
+
case "ImportDeclaration":
|
|
192
|
+
case "ExportAllDeclaration":
|
|
193
|
+
case "ExportNamedDeclaration":
|
|
194
|
+
case "ImportExpression": return parent.source === node;
|
|
195
|
+
case "TSExternalModuleReference":
|
|
196
|
+
case "TSImportType": return true;
|
|
197
|
+
case "ImportAttribute":
|
|
198
|
+
case "TSModuleDeclaration": return true;
|
|
199
|
+
default: return false;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
function compileRegExp(pattern, flags, option) {
|
|
203
|
+
try {
|
|
204
|
+
return new RegExp(pattern, flags);
|
|
205
|
+
} catch (error) {
|
|
206
|
+
throw new Error(`Invalid regular expression in option "${option}": ${String(error)}`, { cause: error });
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
// src/plugin/rules/no-api-path-literal.ts
|
|
210
|
+
const RULE$11 = "no-api-path-literal";
|
|
211
|
+
const DEFAULT_PREFIXES = ["/api/", "/motion-api/"];
|
|
212
|
+
const noApiPathLiteral = {
|
|
213
|
+
meta: {
|
|
214
|
+
type: "problem",
|
|
215
|
+
docs: {
|
|
216
|
+
description: "Disallow API path literals outside the API client layer",
|
|
217
|
+
url: docsUrl(RULE$11)
|
|
218
|
+
},
|
|
219
|
+
schema: [{
|
|
220
|
+
type: "object",
|
|
221
|
+
properties: {
|
|
222
|
+
prefixes: {
|
|
223
|
+
type: "array",
|
|
224
|
+
items: { type: "string" }
|
|
225
|
+
},
|
|
226
|
+
client: { type: "string" }
|
|
227
|
+
},
|
|
228
|
+
additionalProperties: false
|
|
229
|
+
}],
|
|
230
|
+
messages: { apiPath: message(RULE$11, {
|
|
231
|
+
violation: "API path literal starting with {{prefix}} outside the API client layer; when the backend prefix or parameters change, every copy has to be found by hand.",
|
|
232
|
+
fix: "build the URL with a helper in {{client}} (add one if it does not exist) and call that helper.",
|
|
233
|
+
exemption: CONFIG_EXEMPTION
|
|
234
|
+
}) }
|
|
235
|
+
},
|
|
236
|
+
create(context) {
|
|
237
|
+
const options = context.options[0] ?? {};
|
|
238
|
+
const prefixes = options.prefixes ?? DEFAULT_PREFIXES;
|
|
239
|
+
const client = options.client ? `the API client in ${options.client}` : "the typed API client";
|
|
240
|
+
function matchPrefix(text) {
|
|
241
|
+
for (const prefix of prefixes) {
|
|
242
|
+
const bare = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
243
|
+
if (text.startsWith(prefix) || text === bare) return prefix;
|
|
244
|
+
}
|
|
245
|
+
return null;
|
|
246
|
+
}
|
|
247
|
+
function check(node, text) {
|
|
248
|
+
if (isModuleSource(node) || isInTypePosition(node)) return;
|
|
249
|
+
const prefix = matchPrefix(text);
|
|
250
|
+
if (prefix !== null) context.report({
|
|
251
|
+
node,
|
|
252
|
+
messageId: "apiPath",
|
|
253
|
+
data: {
|
|
254
|
+
prefix: `"${prefix}"`,
|
|
255
|
+
client
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
return {
|
|
260
|
+
Literal(node) {
|
|
261
|
+
if (typeof node.value === "string") check(node, node.value);
|
|
262
|
+
},
|
|
263
|
+
TemplateLiteral(node) {
|
|
264
|
+
const first = node.quasis[0];
|
|
265
|
+
if (first) check(node, first.value.cooked ?? first.value.raw);
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
// src/plugin/rules/no-cjk-literal.ts
|
|
271
|
+
const RULE$10 = "no-cjk-literal";
|
|
272
|
+
const DEFAULT_PATTERN$1 = "[\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}]";
|
|
273
|
+
const noCjkLiteral = {
|
|
274
|
+
meta: {
|
|
275
|
+
type: "problem",
|
|
276
|
+
docs: {
|
|
277
|
+
description: "Disallow CJK text in source literals outside the i18n catalogs",
|
|
278
|
+
url: docsUrl(RULE$10)
|
|
279
|
+
},
|
|
280
|
+
schema: [{
|
|
281
|
+
type: "object",
|
|
282
|
+
properties: { pattern: { type: "string" } },
|
|
283
|
+
additionalProperties: false
|
|
284
|
+
}],
|
|
285
|
+
messages: { cjk: message(RULE$10, {
|
|
286
|
+
violation: "CJK text in a source literal: {{sample}}. Copy hard-coded in code skips translation review and cannot follow the user's locale.",
|
|
287
|
+
fix: "move user-facing copy into the i18n catalog (every supported locale) and render it with `t('<key>')`; write logs, error messages and identifiers in English.",
|
|
288
|
+
exemption: "i18n catalogs, tests and fixtures are excluded by the lint config (`ignorePatterns` / `overrides`, owner review); inline disable comments are rejected and existing debt stays in oxlint-suppressions.json."
|
|
289
|
+
}) }
|
|
290
|
+
},
|
|
291
|
+
create(context) {
|
|
292
|
+
const re = compileRegExp((context.options[0] ?? {}).pattern ?? DEFAULT_PATTERN$1, "u", "pattern");
|
|
293
|
+
function check(node, text) {
|
|
294
|
+
const match = re.exec(text);
|
|
295
|
+
if (!match) return;
|
|
296
|
+
const start = Math.max(0, match.index - 8);
|
|
297
|
+
const sample = JSON.stringify(text.slice(start, match.index + 12).trim());
|
|
298
|
+
context.report({
|
|
299
|
+
node,
|
|
300
|
+
messageId: "cjk",
|
|
301
|
+
data: { sample }
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
return {
|
|
305
|
+
Literal(node) {
|
|
306
|
+
if (typeof node.value !== "string") return;
|
|
307
|
+
if (isModuleSource(node) || isInTypePosition(node)) return;
|
|
308
|
+
check(node, node.value);
|
|
309
|
+
},
|
|
310
|
+
TemplateLiteral(node) {
|
|
311
|
+
if (isInTypePosition(node)) return;
|
|
312
|
+
check(node, node.quasis.map((q) => q.value.cooked ?? q.value.raw).join("${}"));
|
|
313
|
+
},
|
|
314
|
+
JSXText(node) {
|
|
315
|
+
check(node, node.value);
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
// src/plugin/rules/no-constant-flag.ts
|
|
321
|
+
const RULE$9 = "no-constant-flag";
|
|
322
|
+
const DEFAULT_PATTERN = [
|
|
323
|
+
"[A-Za-z0-9]*(?:Enabled|Disabled|Flag)",
|
|
324
|
+
"[A-Z0-9_]*_(?:ENABLED|DISABLED|FLAG)",
|
|
325
|
+
"(?:ENABLE|DISABLE|FEATURE|FF|SHOW|HIDE|USE)_[A-Z0-9_]+",
|
|
326
|
+
"(?:enable|disable|show|hide)[A-Z][A-Za-z0-9]*"
|
|
327
|
+
].map((alt) => `(?:${alt})`).join("|");
|
|
328
|
+
/** `true`, `false`, `!0`, `!1`, possibly wrapped in `as const` / `satisfies`. */
|
|
329
|
+
function booleanValue(input) {
|
|
330
|
+
const node = unwrap(input);
|
|
331
|
+
if (!node) return null;
|
|
332
|
+
if (node.type === "Literal" && typeof node.value === "boolean") return String(node.value);
|
|
333
|
+
if (node.type === "UnaryExpression" && node.operator === "!") {
|
|
334
|
+
const arg = unwrap(node.argument);
|
|
335
|
+
if (arg?.type === "Literal" && (arg.value === 0 || arg.value === 1)) return String(!arg.value);
|
|
336
|
+
}
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
const noConstantFlag = {
|
|
340
|
+
meta: {
|
|
341
|
+
type: "problem",
|
|
342
|
+
docs: {
|
|
343
|
+
description: "Disallow module-level constant feature flags",
|
|
344
|
+
url: docsUrl(RULE$9)
|
|
345
|
+
},
|
|
346
|
+
schema: [{
|
|
347
|
+
type: "object",
|
|
348
|
+
properties: { pattern: { type: "string" } },
|
|
349
|
+
additionalProperties: false
|
|
350
|
+
}],
|
|
351
|
+
messages: { constantFlag: message(RULE$9, {
|
|
352
|
+
violation: "`{{name}} = {{value}}` is a feature toggle hard-coded at module level: it has no owner or expiry, cannot be switched per environment, and the dead branch stays in the bundle.",
|
|
353
|
+
fix: "register the flag in the feature-flag registry (owner, expiry date, default off) and read it from there; if the decision is final, delete the constant and the branch it guards.",
|
|
354
|
+
exemption: "none inline (disable comments are rejected). A constant that is not a toggle should be renamed so it does not read like one."
|
|
355
|
+
}) }
|
|
356
|
+
},
|
|
357
|
+
create(context) {
|
|
358
|
+
const re = compileRegExp(`^(?:${(context.options[0] ?? {}).pattern ?? DEFAULT_PATTERN})$`, "", "pattern");
|
|
359
|
+
return { VariableDeclaration(node) {
|
|
360
|
+
if (node.kind !== "const") return;
|
|
361
|
+
const parent = node.parent;
|
|
362
|
+
if (!(parent?.type === "Program" || parent?.type === "ExportNamedDeclaration" && parent.parent?.type === "Program")) return;
|
|
363
|
+
for (const declarator of node.declarations) {
|
|
364
|
+
if (declarator.id.type !== "Identifier" || !re.test(declarator.id.name)) continue;
|
|
365
|
+
const value = booleanValue(declarator.init);
|
|
366
|
+
if (value === null) continue;
|
|
367
|
+
context.report({
|
|
368
|
+
node: declarator,
|
|
369
|
+
messageId: "constantFlag",
|
|
370
|
+
data: {
|
|
371
|
+
name: declarator.id.name,
|
|
372
|
+
value
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
} };
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
// src/plugin/rules/no-error-message-match.ts
|
|
380
|
+
const RULE$8 = "no-error-message-match";
|
|
381
|
+
const DEFAULT_IDENTIFIERS = "^(?:msg|err(?:or)?_?(?:msg|message|text))$";
|
|
382
|
+
const DEFAULT_ERROR_NAMES = "^(?:e|err|error|ex|exc|exception|cause)$";
|
|
383
|
+
const MATCH_METHODS = /* @__PURE__ */ new Set([
|
|
384
|
+
"includes",
|
|
385
|
+
"startsWith",
|
|
386
|
+
"endsWith",
|
|
387
|
+
"indexOf",
|
|
388
|
+
"match",
|
|
389
|
+
"search"
|
|
390
|
+
]);
|
|
391
|
+
const TRANSFORMS = /* @__PURE__ */ new Set([
|
|
392
|
+
"toLowerCase",
|
|
393
|
+
"toUpperCase",
|
|
394
|
+
"toLocaleLowerCase",
|
|
395
|
+
"toLocaleUpperCase",
|
|
396
|
+
"trim",
|
|
397
|
+
"trimStart",
|
|
398
|
+
"trimEnd",
|
|
399
|
+
"normalize"
|
|
400
|
+
]);
|
|
401
|
+
const EQ_OPS$2 = /* @__PURE__ */ new Set([
|
|
402
|
+
"===",
|
|
403
|
+
"!==",
|
|
404
|
+
"==",
|
|
405
|
+
"!="
|
|
406
|
+
]);
|
|
407
|
+
/** Functions that return message text by name: `getErrorMessage(e)`, `archiveErrorMessage(err)`. */
|
|
408
|
+
const MESSAGE_GETTER = /err(?:or)?_?(?:msg|message|text)$/i;
|
|
409
|
+
/** The identifier a parameter binds: `x` or `x = default`. */
|
|
410
|
+
function parameterName(param) {
|
|
411
|
+
if (param?.type === "Identifier") return param;
|
|
412
|
+
if (param?.type === "AssignmentPattern" && param.left.type === "Identifier") return param.left;
|
|
413
|
+
return null;
|
|
414
|
+
}
|
|
415
|
+
function isVariableDeclarator(node) {
|
|
416
|
+
return node.type === "VariableDeclarator";
|
|
417
|
+
}
|
|
418
|
+
function isDestructuredFromErrorCall(node, errRe) {
|
|
419
|
+
if (!isVariableDeclarator(node)) return false;
|
|
420
|
+
const init = unwrap(node.init);
|
|
421
|
+
if (init?.type !== "CallExpression") return false;
|
|
422
|
+
return init.arguments.some((argument) => {
|
|
423
|
+
const arg = unwrap(argument);
|
|
424
|
+
return arg?.type === "Identifier" && errRe.test(arg.name);
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
function isRegex(node) {
|
|
428
|
+
const n = unwrap(node);
|
|
429
|
+
return n?.type === "Literal" && "regex" in n && n.regex !== void 0;
|
|
430
|
+
}
|
|
431
|
+
const noErrorMessageMatch = {
|
|
432
|
+
meta: {
|
|
433
|
+
type: "problem",
|
|
434
|
+
docs: {
|
|
435
|
+
description: "Disallow decisions based on matching error message text",
|
|
436
|
+
url: docsUrl(RULE$8)
|
|
437
|
+
},
|
|
438
|
+
schema: [{
|
|
439
|
+
type: "object",
|
|
440
|
+
properties: {
|
|
441
|
+
identifierPattern: { type: "string" },
|
|
442
|
+
errorPattern: { type: "string" },
|
|
443
|
+
trackAliases: { type: "boolean" }
|
|
444
|
+
},
|
|
445
|
+
additionalProperties: false
|
|
446
|
+
}],
|
|
447
|
+
messages: { messageMatch: message(RULE$8, {
|
|
448
|
+
violation: "this decision depends on the wording of an error message; when the producer rewords it, the branch (or the telemetry attribution) silently stops matching.",
|
|
449
|
+
fix: "give the error a stable machine-readable signal at the source (an error `code`, a typed error class checked with instanceof, or a structured result) and branch on that.",
|
|
450
|
+
exemption: `adapters for third-party errors that expose nothing but text belong in one module that maps them to codes; ${CONFIG_EXEMPTION}`
|
|
451
|
+
}) }
|
|
452
|
+
},
|
|
453
|
+
create(context) {
|
|
454
|
+
const options = context.options[0] ?? {};
|
|
455
|
+
const idRe = compileRegExp(options.identifierPattern ?? DEFAULT_IDENTIFIERS, "i", "identifierPattern");
|
|
456
|
+
const errRe = compileRegExp(options.errorPattern ?? DEFAULT_ERROR_NAMES, "", "errorPattern");
|
|
457
|
+
const trackAliases = options.trackAliases !== false;
|
|
458
|
+
/** Simple declarators whose value is message text. */
|
|
459
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
460
|
+
/** Their names. */
|
|
461
|
+
const aliasNames = /* @__PURE__ */ new Set();
|
|
462
|
+
/** Identifiers of parameters that receive message text. */
|
|
463
|
+
const messageParams = /* @__PURE__ */ new Set();
|
|
464
|
+
function isAlias(node) {
|
|
465
|
+
const def = definitionOf(context, node);
|
|
466
|
+
if (!def) return false;
|
|
467
|
+
if (isSimpleDeclarator(def.node)) return aliases.has(def.node);
|
|
468
|
+
if (def.type === "Parameter") return messageParams.has(def.name);
|
|
469
|
+
if (def.type === "Variable" && isDestructuredFromErrorCall(def.node, errRe)) return aliasNames.has(node.name);
|
|
470
|
+
return false;
|
|
471
|
+
}
|
|
472
|
+
/** An expression holding error message text. */
|
|
473
|
+
function isMessage(input) {
|
|
474
|
+
const node = unwrap(input);
|
|
475
|
+
if (!node) return false;
|
|
476
|
+
switch (node.type) {
|
|
477
|
+
case "MemberExpression": return propertyName(node) === "message";
|
|
478
|
+
case "Identifier": return idRe.test(node.name) || isAlias(node);
|
|
479
|
+
case "CallExpression": {
|
|
480
|
+
const callee = unwrap(node.callee);
|
|
481
|
+
if (callee?.type === "MemberExpression" && TRANSFORMS.has(propertyName(callee) ?? "")) return isMessage(callee.object);
|
|
482
|
+
if (callee?.type === "Identifier" && callee.name === "String") {
|
|
483
|
+
const arg = unwrap(node.arguments[0]);
|
|
484
|
+
return arg?.type === "Identifier" && errRe.test(arg.name) || isMessage(arg);
|
|
485
|
+
}
|
|
486
|
+
return callee !== null && MESSAGE_GETTER.test(calleeName(callee) ?? "");
|
|
487
|
+
}
|
|
488
|
+
case "LogicalExpression": return isMessage(node.left);
|
|
489
|
+
case "ConditionalExpression": return isMessage(node.consequent) || isMessage(node.alternate);
|
|
490
|
+
default: return false;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
function report(node) {
|
|
494
|
+
context.report({
|
|
495
|
+
node,
|
|
496
|
+
messageId: "messageMatch"
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
const pending = [];
|
|
500
|
+
const declarators = [];
|
|
501
|
+
const calls = [];
|
|
502
|
+
/** One round of alias collection; true when it found a new alias or message parameter. */
|
|
503
|
+
function collect() {
|
|
504
|
+
let grew = false;
|
|
505
|
+
for (const node of declarators) {
|
|
506
|
+
if (aliases.has(node) || !isMessage(node.init)) continue;
|
|
507
|
+
aliases.add(node);
|
|
508
|
+
aliasNames.add(node.id.name);
|
|
509
|
+
grew = true;
|
|
510
|
+
}
|
|
511
|
+
for (const [call, fn] of calls) call.arguments.forEach((arg, index) => {
|
|
512
|
+
const param = parameterName(fn.params[index]);
|
|
513
|
+
if (!param || messageParams.has(param) || !isMessage(arg)) return;
|
|
514
|
+
messageParams.add(param);
|
|
515
|
+
grew = true;
|
|
516
|
+
});
|
|
517
|
+
return grew;
|
|
518
|
+
}
|
|
519
|
+
return {
|
|
520
|
+
VariableDeclarator(node) {
|
|
521
|
+
if (trackAliases && node.id.type === "Identifier" && node.init) declarators.push(node);
|
|
522
|
+
},
|
|
523
|
+
CallExpression(node) {
|
|
524
|
+
pending.push(node);
|
|
525
|
+
if (!trackAliases || node.arguments.length === 0) return;
|
|
526
|
+
const fn = calledFunction(context, node);
|
|
527
|
+
if (fn) calls.push([node, fn]);
|
|
528
|
+
},
|
|
529
|
+
BinaryExpression(node) {
|
|
530
|
+
if (EQ_OPS$2.has(node.operator)) pending.push(node);
|
|
531
|
+
},
|
|
532
|
+
"Program:exit"() {
|
|
533
|
+
let grew = true;
|
|
534
|
+
while (grew) grew = collect();
|
|
535
|
+
for (const node of pending) {
|
|
536
|
+
if (node.type === "BinaryExpression") {
|
|
537
|
+
const left = node.left;
|
|
538
|
+
const wording = (other) => {
|
|
539
|
+
const text = staticString(other);
|
|
540
|
+
return text !== null && text !== "";
|
|
541
|
+
};
|
|
542
|
+
if (isMessage(left) && wording(node.right) || isMessage(node.right) && wording(left)) report(node);
|
|
543
|
+
continue;
|
|
544
|
+
}
|
|
545
|
+
if (node.type !== "CallExpression") continue;
|
|
546
|
+
const callee = unwrap(node.callee);
|
|
547
|
+
const method = callee ? calleeName(callee) : null;
|
|
548
|
+
if (callee?.type !== "MemberExpression" || method === null) continue;
|
|
549
|
+
const arg = node.arguments[0];
|
|
550
|
+
if (MATCH_METHODS.has(method)) {
|
|
551
|
+
if ((staticString(arg) !== null || isRegex(arg)) && isMessage(callee.object)) report(node);
|
|
552
|
+
} else if (method === "test" && isRegex(callee.object) && isMessage(arg)) report(node);
|
|
553
|
+
}
|
|
554
|
+
}
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
};
|
|
558
|
+
// src/plugin/rules/no-locale-branch.ts
|
|
559
|
+
const RULE$7 = "no-locale-branch";
|
|
560
|
+
const DEFAULT_ALIAS_PATTERN = "^is(Zh|Chinese|Cn|CN|En|English)$";
|
|
561
|
+
const PREDICATE_METHODS = /* @__PURE__ */ new Set([
|
|
562
|
+
"startsWith",
|
|
563
|
+
"endsWith",
|
|
564
|
+
"includes"
|
|
565
|
+
]);
|
|
566
|
+
/** Matches whose result is a position, so they only become a test in a comparison. */
|
|
567
|
+
const INDEX_METHODS = /* @__PURE__ */ new Set(["indexOf"]);
|
|
568
|
+
const EQ_OPS$1 = /* @__PURE__ */ new Set([
|
|
569
|
+
"===",
|
|
570
|
+
"!==",
|
|
571
|
+
"==",
|
|
572
|
+
"!="
|
|
573
|
+
]);
|
|
574
|
+
/** Names written as constants: `DEFAULT_LOCALE`, and enum-like objects such as `Locale.ZhCN`. */
|
|
575
|
+
const CONSTANT_NAME = /^[A-Z][A-Z0-9_]*$/;
|
|
576
|
+
const TYPE_NAME = /^[A-Z]/;
|
|
577
|
+
/** A call written `x.method(…)` of one of `methods`. */
|
|
578
|
+
function isCallOf(node, methods) {
|
|
579
|
+
return node.type === "CallExpression" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && methods.has(node.callee.property.name);
|
|
580
|
+
}
|
|
581
|
+
function isNullLiteral(input) {
|
|
582
|
+
const node = unwrap(input);
|
|
583
|
+
return node?.type === "Literal" && node.raw === "null";
|
|
584
|
+
}
|
|
585
|
+
function isStringish(node) {
|
|
586
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value.trim() !== "";
|
|
587
|
+
if (node.type === "TemplateLiteral") return node.quasis.some((q) => (q.value.cooked ?? q.value.raw).trim() !== "");
|
|
588
|
+
if (node.type === "JSXText") return node.value.trim() !== "";
|
|
589
|
+
return false;
|
|
590
|
+
}
|
|
591
|
+
const noLocaleBranch = {
|
|
592
|
+
meta: {
|
|
593
|
+
type: "problem",
|
|
594
|
+
docs: {
|
|
595
|
+
description: "Disallow hard-coded copy in branches derived from the locale",
|
|
596
|
+
url: docsUrl(RULE$7)
|
|
597
|
+
},
|
|
598
|
+
schema: [{
|
|
599
|
+
type: "object",
|
|
600
|
+
properties: {
|
|
601
|
+
trackAliases: { type: "boolean" },
|
|
602
|
+
aliasPattern: { type: "string" }
|
|
603
|
+
},
|
|
604
|
+
additionalProperties: false
|
|
605
|
+
}],
|
|
606
|
+
messages: { localeBranch: message(RULE$7, {
|
|
607
|
+
violation: "this branch picks hard-coded copy by locale (isZh / locale === ... / startsWith('zh')).",
|
|
608
|
+
fix: "render the text with `t('<key>')` from useI18n() and add the key to the i18n catalog for every supported locale; never branch on the locale to choose copy.",
|
|
609
|
+
exemption: `values that are not copy (URLs, format strings) belong in a locale-keyed table, not a branch; ${CONFIG_EXEMPTION}`
|
|
610
|
+
}) }
|
|
611
|
+
},
|
|
612
|
+
create(context) {
|
|
613
|
+
const options = context.options[0] ?? {};
|
|
614
|
+
const trackAliases = options.trackAliases !== false;
|
|
615
|
+
const aliasRe = compileRegExp(options.aliasPattern ?? DEFAULT_ALIAS_PATTERN, "", "aliasPattern");
|
|
616
|
+
const visitorKeys = context.sourceCode.visitorKeys;
|
|
617
|
+
/** Declarators of variables that hold a locale test (`const zh = locale.startsWith("zh")`). */
|
|
618
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
619
|
+
const candidates = [];
|
|
620
|
+
/** A reference to a tracked alias in scope (a same-named variable elsewhere is not one). */
|
|
621
|
+
function isAlias(node) {
|
|
622
|
+
if (aliases.size === 0) return false;
|
|
623
|
+
const def = definitionOf(context, node);
|
|
624
|
+
return def !== void 0 && aliases.has(def.node);
|
|
625
|
+
}
|
|
626
|
+
function mentionsLocale(node) {
|
|
627
|
+
return someNode(node, visitorKeys, (n) => n.type === "Identifier" && (n.name === "locale" || aliasRe.test(n.name) || isAlias(n)));
|
|
628
|
+
}
|
|
629
|
+
/** An imported binding (`zhCN`) or a static member chain on one (`locales.zhCN`). */
|
|
630
|
+
function isImportedValue(input) {
|
|
631
|
+
const node = unwrap(input);
|
|
632
|
+
if (!node) return false;
|
|
633
|
+
if (node.type === "Identifier") return findVariable(context, node, node.name)?.defs[0]?.type === "ImportBinding";
|
|
634
|
+
if (node.type === "MemberExpression") return propertyName(node) !== null && isImportedValue(node.object);
|
|
635
|
+
return false;
|
|
636
|
+
}
|
|
637
|
+
/** The initializer of the same-file `const` an identifier resolves to. */
|
|
638
|
+
function constInit(node) {
|
|
639
|
+
const def = findVariable(context, node, node.name)?.defs[0];
|
|
640
|
+
if (!def || !isSimpleDeclarator(def.node)) return null;
|
|
641
|
+
const declaration = def.node.parent;
|
|
642
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const") return null;
|
|
643
|
+
return def.node.init;
|
|
644
|
+
}
|
|
645
|
+
/**
|
|
646
|
+
* A fixed value to compare the locale with: a literal, a static template, a constant name
|
|
647
|
+
* (`DEFAULT_LOCALE`, `Locale.ZhCN`), an imported binding or member on one (`zhCN`,
|
|
648
|
+
* `locales.zhCN`), or a same-file `const` bound to a string.
|
|
649
|
+
*/
|
|
650
|
+
function isFixed(input) {
|
|
651
|
+
const node = unwrap(input);
|
|
652
|
+
if (!node) return false;
|
|
653
|
+
if (node.type === "Literal" || staticString(node) !== null) return true;
|
|
654
|
+
if (isImportedValue(node)) return true;
|
|
655
|
+
if (node.type === "MemberExpression") {
|
|
656
|
+
const object = unwrap(node.object);
|
|
657
|
+
return object?.type === "Identifier" && TYPE_NAME.test(object.name) && propertyName(node) !== null;
|
|
658
|
+
}
|
|
659
|
+
if (node.type !== "Identifier") return false;
|
|
660
|
+
return CONSTANT_NAME.test(node.name) || staticString(constInit(node)) !== null;
|
|
661
|
+
}
|
|
662
|
+
/**
|
|
663
|
+
* A list that can be read in this file: a string, an array of fixed values, or a same-file
|
|
664
|
+
* `const` bound to either. What an imported or computed list holds is unknown, and asking
|
|
665
|
+
* whether the locale is in one (`SUPPORTED.includes(locale)`) is validation.
|
|
666
|
+
*/
|
|
667
|
+
function isWrittenList(input) {
|
|
668
|
+
let node = unwrap(input);
|
|
669
|
+
if (node?.type === "Identifier") node = unwrap(constInit(node));
|
|
670
|
+
if (node?.type === "ArrayExpression") return node.elements.length > 0 && node.elements.every((e) => e !== null && isFixed(e));
|
|
671
|
+
return Boolean(staticString(node));
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* `locale.startsWith("zh")` or `["zh-CN", "zh-TW"].includes(locale)` match the locale with
|
|
675
|
+
* something fixed. `supported.includes(locale)` and `locale.startsWith(prefix)` match one
|
|
676
|
+
* value with another, which picks no language.
|
|
677
|
+
*/
|
|
678
|
+
function matchesFixed(call) {
|
|
679
|
+
const receiver = call.callee.object;
|
|
680
|
+
const argument = call.arguments[0];
|
|
681
|
+
if (!argument) return false;
|
|
682
|
+
if (!mentionsLocale(receiver)) return mentionsLocale(argument) && isWrittenList(receiver);
|
|
683
|
+
return !mentionsLocale(argument) && isFixed(argument);
|
|
684
|
+
}
|
|
685
|
+
/**
|
|
686
|
+
* Whether comparing `input`, which mentions the locale, with `other` can ask which locale it
|
|
687
|
+
* is. `typeof locale` and `locale.length` ask what kind of value it is; `""` and `null` ask
|
|
688
|
+
* whether there is one (a call may return null for "no match": `locale.match(/^zh/)`); and
|
|
689
|
+
* `indexOf` is a match like `includes`.
|
|
690
|
+
*/
|
|
691
|
+
function asksWhichLocale(input, other) {
|
|
692
|
+
const subject = unwrap(input);
|
|
693
|
+
if (!subject) return false;
|
|
694
|
+
if (subject.type === "UnaryExpression" && subject.operator === "typeof") return false;
|
|
695
|
+
if (subject.type === "MemberExpression" && propertyName(subject) === "length") return false;
|
|
696
|
+
if (staticString(other) === "") return false;
|
|
697
|
+
if (isNullLiteral(other)) return subject.type === "CallExpression";
|
|
698
|
+
return !isCallOf(subject, INDEX_METHODS) || matchesFixed(subject);
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* `locale === "zh-CN"` picks a language; `next === locale` and `value !== locale` only ask
|
|
702
|
+
* whether two values are the same locale, whatever it is, so they do not count.
|
|
703
|
+
*/
|
|
704
|
+
function comparesWithFixed(node) {
|
|
705
|
+
const left = node.left;
|
|
706
|
+
const leftMentions = mentionsLocale(left);
|
|
707
|
+
if (leftMentions === mentionsLocale(node.right)) return false;
|
|
708
|
+
const [subject, other] = leftMentions ? [left, node.right] : [node.right, left];
|
|
709
|
+
return isFixed(other) && asksWhichLocale(subject, other);
|
|
710
|
+
}
|
|
711
|
+
function isLocaleTest(input) {
|
|
712
|
+
const node = unwrap(input);
|
|
713
|
+
if (!node) return false;
|
|
714
|
+
if (node.type === "Identifier") return aliasRe.test(node.name) || isAlias(node);
|
|
715
|
+
if (node.type === "UnaryExpression" && node.operator === "!") return isLocaleTest(node.argument);
|
|
716
|
+
if (node.type === "LogicalExpression") return isLocaleTest(node.left) || isLocaleTest(node.right);
|
|
717
|
+
if (node.type === "BinaryExpression") return EQ_OPS$1.has(node.operator) && comparesWithFixed(node);
|
|
718
|
+
return isCallOf(node, PREDICATE_METHODS) && matchesFixed(node);
|
|
719
|
+
}
|
|
720
|
+
const declarators = [];
|
|
721
|
+
return {
|
|
722
|
+
VariableDeclarator(node) {
|
|
723
|
+
if (trackAliases && node.id.type === "Identifier" && node.init) declarators.push(node);
|
|
724
|
+
},
|
|
725
|
+
ConditionalExpression(node) {
|
|
726
|
+
candidates.push([node, [node.consequent, node.alternate]]);
|
|
727
|
+
},
|
|
728
|
+
IfStatement(node) {
|
|
729
|
+
const branches = [node.consequent];
|
|
730
|
+
if (node.alternate) branches.push(node.alternate);
|
|
731
|
+
candidates.push([node, branches]);
|
|
732
|
+
},
|
|
733
|
+
"Program:exit"() {
|
|
734
|
+
for (let grew = true; grew;) {
|
|
735
|
+
grew = false;
|
|
736
|
+
for (const node of declarators) {
|
|
737
|
+
if (aliases.has(node) || !isLocaleTest(node.init)) continue;
|
|
738
|
+
aliases.add(node);
|
|
739
|
+
grew = true;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
for (const [node, branches] of candidates) {
|
|
743
|
+
const test = node.test;
|
|
744
|
+
if (!isLocaleTest(test)) continue;
|
|
745
|
+
if (!branches.some((b) => someNode(b, visitorKeys, isStringish))) continue;
|
|
746
|
+
context.report({
|
|
747
|
+
node: test,
|
|
748
|
+
messageId: "localeBranch"
|
|
749
|
+
});
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
};
|
|
755
|
+
// src/plugin/rules/no-process-env.ts
|
|
756
|
+
const RULE$6 = "no-process-env";
|
|
757
|
+
const PROCESS_MODULES = /* @__PURE__ */ new Set(["process", "node:process"]);
|
|
758
|
+
function moduleName(node) {
|
|
759
|
+
if (node?.type === "Identifier") return node.name;
|
|
760
|
+
return staticString(node);
|
|
761
|
+
}
|
|
762
|
+
/** An import or re-export declaration whose source is the process module. */
|
|
763
|
+
function fromProcessModule(declaration) {
|
|
764
|
+
return (declaration?.type === "ImportDeclaration" || declaration?.type === "ExportNamedDeclaration") && PROCESS_MODULES.has(String(declaration.source?.value));
|
|
765
|
+
}
|
|
766
|
+
/** A binding of the process module itself: `import process from`, `import * as p from`. */
|
|
767
|
+
function isProcessModuleBinding(variable) {
|
|
768
|
+
return variable.defs.some(({ node }) => {
|
|
769
|
+
if (!fromProcessModule(node.parent)) return false;
|
|
770
|
+
if (node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") return true;
|
|
771
|
+
return node.type === "ImportSpecifier" && moduleName(node.imported) === "default";
|
|
772
|
+
});
|
|
773
|
+
}
|
|
774
|
+
/** The name of a destructured property (`{ env }`, `{ env: e }`, `{ ["env"]: e }`). */
|
|
775
|
+
function keyName$1(property) {
|
|
776
|
+
if (!property.computed && property.key.type === "Identifier") return property.key.name;
|
|
777
|
+
return staticString(property.key);
|
|
778
|
+
}
|
|
779
|
+
const noProcessEnv = {
|
|
780
|
+
meta: {
|
|
781
|
+
type: "problem",
|
|
782
|
+
docs: {
|
|
783
|
+
description: "Disallow process.env outside the env registry",
|
|
784
|
+
url: docsUrl(RULE$6)
|
|
785
|
+
},
|
|
786
|
+
schema: [{
|
|
787
|
+
type: "object",
|
|
788
|
+
properties: { registry: { type: "string" } },
|
|
789
|
+
additionalProperties: false
|
|
790
|
+
}],
|
|
791
|
+
messages: { processEnv: message(RULE$6, {
|
|
792
|
+
violation: "process.env is read outside the env registry, so the variable has no declared type, default, owner or documentation, and parsing drifts between call sites.",
|
|
793
|
+
fix: "declare the variable once in {{registry}} (name, type, default, description) and import the typed value from there.",
|
|
794
|
+
exemption: CONFIG_EXEMPTION
|
|
795
|
+
}) }
|
|
796
|
+
},
|
|
797
|
+
create(context) {
|
|
798
|
+
const options = context.options[0] ?? {};
|
|
799
|
+
const registry = options.registry ? `the env registry (${options.registry})` : "the env registry module";
|
|
800
|
+
function report(node) {
|
|
801
|
+
context.report({
|
|
802
|
+
node,
|
|
803
|
+
messageId: "processEnv",
|
|
804
|
+
data: { registry }
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
/** The global `process`, `globalThis.process` (window, self, global) or the imported module. */
|
|
808
|
+
function isProcess(input) {
|
|
809
|
+
const node = unwrap(input);
|
|
810
|
+
if (node?.type === "Identifier") {
|
|
811
|
+
const variable = findVariable(context, node, node.name);
|
|
812
|
+
if (!variable || variable.defs.length === 0) return node.name === "process";
|
|
813
|
+
return isProcessModuleBinding(variable);
|
|
814
|
+
}
|
|
815
|
+
if (node?.type === "MemberExpression") return propertyName(node) === "process" && isGlobalObject(context, node.object, ["global"]);
|
|
816
|
+
return false;
|
|
817
|
+
}
|
|
818
|
+
function checkDestructuring(pattern, init) {
|
|
819
|
+
if (pattern.type !== "ObjectPattern" || !isProcess(init)) return;
|
|
820
|
+
for (const property of pattern.properties) if (property.type === "Property" && keyName$1(property) === "env") report(property);
|
|
821
|
+
}
|
|
822
|
+
function checkSpecifiers(node) {
|
|
823
|
+
if (!fromProcessModule(node)) return;
|
|
824
|
+
if (node.type === "ImportDeclaration" ? node.importKind === "type" : node.exportKind === "type") return;
|
|
825
|
+
for (const specifier of node.specifiers) {
|
|
826
|
+
if ("importKind" in specifier && specifier.importKind === "type") continue;
|
|
827
|
+
if ((specifier.type === "ImportSpecifier" ? moduleName(specifier.imported) : specifier.type === "ExportSpecifier" ? moduleName(specifier.local) : null) === "env") report(specifier);
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
return {
|
|
831
|
+
MemberExpression(node) {
|
|
832
|
+
if (propertyName(node) === "env" && isProcess(node.object)) report(node);
|
|
833
|
+
},
|
|
834
|
+
VariableDeclarator(node) {
|
|
835
|
+
checkDestructuring(node.id, node.init);
|
|
836
|
+
},
|
|
837
|
+
AssignmentExpression(node) {
|
|
838
|
+
checkDestructuring(node.left, node.right);
|
|
839
|
+
},
|
|
840
|
+
ImportDeclaration: checkSpecifiers,
|
|
841
|
+
ExportNamedDeclaration: checkSpecifiers
|
|
842
|
+
};
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
// src/plugin/rules/no-product-literal.ts
|
|
846
|
+
const RULE$5 = "no-product-literal";
|
|
847
|
+
const EQ_OPS = /* @__PURE__ */ new Set([
|
|
848
|
+
"===",
|
|
849
|
+
"!==",
|
|
850
|
+
"==",
|
|
851
|
+
"!="
|
|
852
|
+
]);
|
|
853
|
+
const DEFAULT_PRODUCTS = ["opengame", "openmotion"];
|
|
854
|
+
const DEFAULT_ACCESSORS = ["currentProductId"];
|
|
855
|
+
const noProductLiteral = {
|
|
856
|
+
meta: {
|
|
857
|
+
type: "problem",
|
|
858
|
+
docs: {
|
|
859
|
+
description: "Disallow branching on product identity literals",
|
|
860
|
+
url: docsUrl(RULE$5)
|
|
861
|
+
},
|
|
862
|
+
schema: [{
|
|
863
|
+
type: "object",
|
|
864
|
+
properties: {
|
|
865
|
+
products: {
|
|
866
|
+
type: "array",
|
|
867
|
+
items: { type: "string" }
|
|
868
|
+
},
|
|
869
|
+
accessors: {
|
|
870
|
+
type: "array",
|
|
871
|
+
items: { type: "string" }
|
|
872
|
+
}
|
|
873
|
+
},
|
|
874
|
+
additionalProperties: false
|
|
875
|
+
}],
|
|
876
|
+
messages: { productLiteral: message(RULE$5, {
|
|
877
|
+
violation: "code branches on product identity ({{what}}).",
|
|
878
|
+
fix: "describe the difference in the product manifest (open-design.json) or the product registry and read it from there, or check a capability; platform code must work for any product without naming one.",
|
|
879
|
+
exemption: CONFIG_EXEMPTION
|
|
880
|
+
}) }
|
|
881
|
+
},
|
|
882
|
+
create(context) {
|
|
883
|
+
const options = context.options[0] ?? {};
|
|
884
|
+
const products = new Set(options.products ?? DEFAULT_PRODUCTS);
|
|
885
|
+
const accessors = new Set(options.accessors ?? DEFAULT_ACCESSORS);
|
|
886
|
+
function productLiteral(node) {
|
|
887
|
+
const value = staticString(node);
|
|
888
|
+
return value !== null && products.has(value) ? value : null;
|
|
889
|
+
}
|
|
890
|
+
function accessorCall(node) {
|
|
891
|
+
const n = unwrap(node);
|
|
892
|
+
if (!n || n.type !== "CallExpression") return null;
|
|
893
|
+
const name = calleeName(n.callee);
|
|
894
|
+
return name !== null && accessors.has(name) ? name : null;
|
|
895
|
+
}
|
|
896
|
+
return {
|
|
897
|
+
BinaryExpression(node) {
|
|
898
|
+
if (!EQ_OPS.has(node.operator)) return;
|
|
899
|
+
const literal = productLiteral(node.left) ?? productLiteral(node.right);
|
|
900
|
+
const accessor = accessorCall(node.left) ?? accessorCall(node.right);
|
|
901
|
+
if (literal === null && accessor === null) return;
|
|
902
|
+
const what = literal !== null ? `"${literal}"` : `${accessor}()`;
|
|
903
|
+
context.report({
|
|
904
|
+
node,
|
|
905
|
+
messageId: "productLiteral",
|
|
906
|
+
data: { what }
|
|
907
|
+
});
|
|
908
|
+
},
|
|
909
|
+
SwitchStatement(node) {
|
|
910
|
+
const accessor = accessorCall(node.discriminant);
|
|
911
|
+
for (const c of node.cases) {
|
|
912
|
+
const literal = c.test ? productLiteral(c.test) : null;
|
|
913
|
+
if (literal !== null) context.report({
|
|
914
|
+
node: c,
|
|
915
|
+
messageId: "productLiteral",
|
|
916
|
+
data: { what: `"${literal}"` }
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
if (accessor !== null && !node.cases.some((c) => c.test && productLiteral(c.test))) context.report({
|
|
920
|
+
node: node.discriminant,
|
|
921
|
+
messageId: "productLiteral",
|
|
922
|
+
data: { what: `${accessor}()` }
|
|
923
|
+
});
|
|
924
|
+
}
|
|
925
|
+
};
|
|
926
|
+
}
|
|
927
|
+
};
|
|
928
|
+
// src/plugin/rules/no-raw-fetch.ts
|
|
929
|
+
const RULE$4 = "no-raw-fetch";
|
|
930
|
+
const noRawFetch = {
|
|
931
|
+
meta: {
|
|
932
|
+
type: "problem",
|
|
933
|
+
docs: {
|
|
934
|
+
description: "Disallow the global fetch() outside the API client layer",
|
|
935
|
+
url: docsUrl(RULE$4)
|
|
936
|
+
},
|
|
937
|
+
schema: [{
|
|
938
|
+
type: "object",
|
|
939
|
+
properties: { client: { type: "string" } },
|
|
940
|
+
additionalProperties: false
|
|
941
|
+
}],
|
|
942
|
+
messages: { rawFetch: message(RULE$4, {
|
|
943
|
+
violation: "raw fetch() call outside the API client layer (no shared auth, base URL, error mapping or retries).",
|
|
944
|
+
fix: "call the endpoint through {{client}}; if the endpoint is missing there, add a typed function for it in the client first.",
|
|
945
|
+
exemption: CONFIG_EXEMPTION
|
|
946
|
+
}) }
|
|
947
|
+
},
|
|
948
|
+
create(context) {
|
|
949
|
+
const options = context.options[0] ?? {};
|
|
950
|
+
const client = options.client ? `the API client in ${options.client}` : "the typed API client";
|
|
951
|
+
return { CallExpression(node) {
|
|
952
|
+
const callee = unwrap(node.callee);
|
|
953
|
+
if (!callee) return;
|
|
954
|
+
if (callee.type === "Identifier" && callee.name === "fetch" && isGlobalName(context, callee, "fetch") || callee.type === "MemberExpression" && propertyName(callee) === "fetch" && isGlobalObject(context, callee.object)) context.report({
|
|
955
|
+
node,
|
|
956
|
+
messageId: "rawFetch",
|
|
957
|
+
data: { client }
|
|
958
|
+
});
|
|
959
|
+
} };
|
|
960
|
+
}
|
|
961
|
+
};
|
|
962
|
+
// src/plugin/rules/no-raw-storage.ts
|
|
963
|
+
const RULE$3 = "no-raw-storage";
|
|
964
|
+
const STORAGE_NAMES = /* @__PURE__ */ new Set(["localStorage", "sessionStorage"]);
|
|
965
|
+
/** Parents of an identifier that is a name, not a reference to the global. */
|
|
966
|
+
const DECLARATION_PARENTS = /* @__PURE__ */ new Set([
|
|
967
|
+
"ImportSpecifier",
|
|
968
|
+
"ImportDefaultSpecifier",
|
|
969
|
+
"ImportNamespaceSpecifier",
|
|
970
|
+
"ExportSpecifier",
|
|
971
|
+
"TSPropertySignature",
|
|
972
|
+
"TSQualifiedName",
|
|
973
|
+
"TSEnumMember",
|
|
974
|
+
"LabeledStatement",
|
|
975
|
+
"BreakStatement",
|
|
976
|
+
"ContinueStatement"
|
|
977
|
+
]);
|
|
978
|
+
/** Class and object members whose non-computed key is a name. */
|
|
979
|
+
const KEYED_PARENTS = /* @__PURE__ */ new Set([
|
|
980
|
+
"Property",
|
|
981
|
+
"MethodDefinition",
|
|
982
|
+
"PropertyDefinition",
|
|
983
|
+
"AccessorProperty",
|
|
984
|
+
"TSAbstractMethodDefinition",
|
|
985
|
+
"TSAbstractPropertyDefinition",
|
|
986
|
+
"TSAbstractAccessorProperty"
|
|
987
|
+
]);
|
|
988
|
+
/** The name of a destructured property (`{ localStorage }`, `{ ["localStorage"]: s }`). */
|
|
989
|
+
function keyName(property) {
|
|
990
|
+
if (!property.computed && property.key.type === "Identifier") return property.key.name;
|
|
991
|
+
return staticString(property.key);
|
|
992
|
+
}
|
|
993
|
+
const noRawStorage = {
|
|
994
|
+
meta: {
|
|
995
|
+
type: "problem",
|
|
996
|
+
docs: {
|
|
997
|
+
description: "Disallow localStorage / sessionStorage outside the storage wrapper",
|
|
998
|
+
url: docsUrl(RULE$3)
|
|
999
|
+
},
|
|
1000
|
+
schema: [{
|
|
1001
|
+
type: "object",
|
|
1002
|
+
properties: { wrapper: { type: "string" } },
|
|
1003
|
+
additionalProperties: false
|
|
1004
|
+
}],
|
|
1005
|
+
messages: { rawStorage: message(RULE$3, {
|
|
1006
|
+
violation: "direct use of {{name}} bypasses the storage wrapper (no key prefixing, no quota or privacy-mode handling).",
|
|
1007
|
+
fix: "read and write through {{wrapper}}; once products run in the shell, use the shell SDK storage capability, which prefixes keys per product.",
|
|
1008
|
+
exemption: CONFIG_EXEMPTION
|
|
1009
|
+
}) }
|
|
1010
|
+
},
|
|
1011
|
+
create(context) {
|
|
1012
|
+
const options = context.options[0] ?? {};
|
|
1013
|
+
const wrapper = options.wrapper ? `the wrapper in ${options.wrapper}` : "the repository's storage wrapper";
|
|
1014
|
+
function report(node, name) {
|
|
1015
|
+
context.report({
|
|
1016
|
+
node,
|
|
1017
|
+
messageId: "rawStorage",
|
|
1018
|
+
data: {
|
|
1019
|
+
name,
|
|
1020
|
+
wrapper
|
|
1021
|
+
}
|
|
1022
|
+
});
|
|
1023
|
+
}
|
|
1024
|
+
/** `const { localStorage: s } = window`: report each storage key taken from a global. */
|
|
1025
|
+
function checkDestructuring(pattern, init) {
|
|
1026
|
+
if (pattern.type !== "ObjectPattern" || !init || !isGlobalObject(context, init)) return;
|
|
1027
|
+
for (const property of pattern.properties) {
|
|
1028
|
+
if (property.type !== "Property") continue;
|
|
1029
|
+
const name = keyName(property);
|
|
1030
|
+
if (name !== null && STORAGE_NAMES.has(name)) report(property.key, name);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
return {
|
|
1034
|
+
Identifier(node) {
|
|
1035
|
+
if (!STORAGE_NAMES.has(node.name)) return;
|
|
1036
|
+
const parent = node.parent;
|
|
1037
|
+
if (!parent) return;
|
|
1038
|
+
if (parent.type === "MemberExpression" && parent.property === node) {
|
|
1039
|
+
if (parent.computed || !isGlobalObject(context, parent.object)) return;
|
|
1040
|
+
} else {
|
|
1041
|
+
if (DECLARATION_PARENTS.has(parent.type)) return;
|
|
1042
|
+
if (KEYED_PARENTS.has(parent.type) && parent.key === node && !parent.computed && !(parent.type === "Property" && parent.shorthand)) return;
|
|
1043
|
+
if (parent.type === "Property" && parent.parent?.type === "ObjectPattern") {
|
|
1044
|
+
if (parent.value === node || !parent.computed) return;
|
|
1045
|
+
}
|
|
1046
|
+
if (parent.type === "VariableDeclarator" && parent.id === node) return;
|
|
1047
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") && parent.params.includes(node)) return;
|
|
1048
|
+
if (isInTypePosition(node)) return;
|
|
1049
|
+
if (!isGlobalName(context, node, node.name)) return;
|
|
1050
|
+
}
|
|
1051
|
+
report(node, node.name);
|
|
1052
|
+
},
|
|
1053
|
+
MemberExpression(node) {
|
|
1054
|
+
if (!node.computed) return;
|
|
1055
|
+
const name = staticString(node.property);
|
|
1056
|
+
if (name === null || !STORAGE_NAMES.has(name)) return;
|
|
1057
|
+
if (isGlobalObject(context, node.object)) report(node.property, name);
|
|
1058
|
+
},
|
|
1059
|
+
VariableDeclarator(node) {
|
|
1060
|
+
checkDestructuring(node.id, node.init);
|
|
1061
|
+
},
|
|
1062
|
+
AssignmentExpression(node) {
|
|
1063
|
+
checkDestructuring(node.left, node.right);
|
|
1064
|
+
},
|
|
1065
|
+
AssignmentPattern(node) {
|
|
1066
|
+
checkDestructuring(node.left, node.right);
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
};
|
|
1071
|
+
// src/plugin/rules/no-shell-imports-product.ts
|
|
1072
|
+
const RULE$2 = "no-shell-imports-product";
|
|
1073
|
+
function isTypeOnly(node) {
|
|
1074
|
+
if (node.type === "ImportDeclaration") {
|
|
1075
|
+
if (node.importKind === "type") return true;
|
|
1076
|
+
return node.specifiers.length > 0 && node.specifiers.every((s) => s.type === "ImportSpecifier" && s.importKind === "type");
|
|
1077
|
+
}
|
|
1078
|
+
if (node.exportKind === "type") return true;
|
|
1079
|
+
if (node.type === "ExportNamedDeclaration") return node.specifiers.length > 0 && node.specifiers.every((s) => s.exportKind === "type");
|
|
1080
|
+
return false;
|
|
1081
|
+
}
|
|
1082
|
+
/**
|
|
1083
|
+
* Aliases match whole path segments: `@` matches `@` and `@/x` but not the package `@scope/x`;
|
|
1084
|
+
* a prefix that ends in `/` (`@/`) matches what starts with it.
|
|
1085
|
+
*/
|
|
1086
|
+
function aliasMatches(spec, prefix) {
|
|
1087
|
+
if (prefix.endsWith("/")) return spec.startsWith(prefix);
|
|
1088
|
+
return spec === prefix || spec.startsWith(`${prefix}/`);
|
|
1089
|
+
}
|
|
1090
|
+
/** `./studio/src/` is written for `studio/src/`: paths are repo-relative either way. */
|
|
1091
|
+
function repoPattern(pattern) {
|
|
1092
|
+
return pattern.replace(/^(?:\.\/)+/, "");
|
|
1093
|
+
}
|
|
1094
|
+
/** Bundler queries name the same module: `./a.svg?url`, `./shader.glsl?raw` → the path alone. */
|
|
1095
|
+
function withoutQuery(spec) {
|
|
1096
|
+
const query = spec.indexOf("?");
|
|
1097
|
+
return query === -1 ? spec : spec.slice(0, query);
|
|
1098
|
+
}
|
|
1099
|
+
const noShellImportsProduct = {
|
|
1100
|
+
meta: {
|
|
1101
|
+
type: "problem",
|
|
1102
|
+
docs: {
|
|
1103
|
+
description: "Disallow shell files from statically importing product directories",
|
|
1104
|
+
url: docsUrl(RULE$2)
|
|
1105
|
+
},
|
|
1106
|
+
schema: [{
|
|
1107
|
+
type: "object",
|
|
1108
|
+
properties: {
|
|
1109
|
+
shell: {
|
|
1110
|
+
type: "array",
|
|
1111
|
+
items: { type: "string" }
|
|
1112
|
+
},
|
|
1113
|
+
product: {
|
|
1114
|
+
type: "array",
|
|
1115
|
+
items: { type: "string" }
|
|
1116
|
+
},
|
|
1117
|
+
aliases: {
|
|
1118
|
+
type: "object",
|
|
1119
|
+
additionalProperties: { type: "string" }
|
|
1120
|
+
}
|
|
1121
|
+
},
|
|
1122
|
+
additionalProperties: false
|
|
1123
|
+
}],
|
|
1124
|
+
messages: { shellImportsProduct: message(RULE$2, {
|
|
1125
|
+
violation: "shell file imports product code from {{target}}.",
|
|
1126
|
+
fix: "reach the product through the product registry (a lazy `import()` in the registry entry) and the shared contract or shell SDK; shell code must not name product paths.",
|
|
1127
|
+
exemption: "inline disable comments are rejected. Known edges during the migration stay in oxlint-suppressions.json until the boundary work removes them; the shell/product split itself is the `shell` and `product` options in the lint config (owner review)."
|
|
1128
|
+
}) }
|
|
1129
|
+
},
|
|
1130
|
+
create(context) {
|
|
1131
|
+
const options = context.options[0] ?? {};
|
|
1132
|
+
if (!options.shell?.length || !options.product?.length) return {};
|
|
1133
|
+
const isShell = compilePathPatterns(options.shell.map(repoPattern));
|
|
1134
|
+
const isProduct = compilePathPatterns(options.product.map(repoPattern));
|
|
1135
|
+
const rel = repoRelativePath(context);
|
|
1136
|
+
if (!isShell(rel) || isProduct(rel)) return {};
|
|
1137
|
+
const dir = path.posix.dirname(rel);
|
|
1138
|
+
const aliases = Object.entries(options.aliases ?? {}).sort((a, b) => b[0].length - a[0].length);
|
|
1139
|
+
function resolve(spec) {
|
|
1140
|
+
if (spec.startsWith(".")) return path.posix.join(dir, spec);
|
|
1141
|
+
for (const [prefix, target] of aliases) if (aliasMatches(spec, prefix)) return path.posix.join(target, spec.slice(prefix.length));
|
|
1142
|
+
return null;
|
|
1143
|
+
}
|
|
1144
|
+
function check(node) {
|
|
1145
|
+
if (!node.source || typeof node.source.value !== "string" || isTypeOnly(node)) return;
|
|
1146
|
+
const target = resolve(withoutQuery(node.source.value));
|
|
1147
|
+
if (target !== null && isProduct(target)) context.report({
|
|
1148
|
+
node,
|
|
1149
|
+
messageId: "shellImportsProduct",
|
|
1150
|
+
data: { target }
|
|
1151
|
+
});
|
|
1152
|
+
}
|
|
1153
|
+
return {
|
|
1154
|
+
ImportDeclaration: check,
|
|
1155
|
+
ExportNamedDeclaration: check,
|
|
1156
|
+
ExportAllDeclaration: check
|
|
1157
|
+
};
|
|
1158
|
+
}
|
|
1159
|
+
};
|
|
1160
|
+
// src/plugin/rules/no-swallowed-catch.ts
|
|
1161
|
+
const RULE$1 = "no-swallowed-catch";
|
|
1162
|
+
const FAIL_OPEN = /^\s*fail-open:\s*\S/;
|
|
1163
|
+
const DEFAULT_CLEANUP = [
|
|
1164
|
+
"abort",
|
|
1165
|
+
"cancel",
|
|
1166
|
+
"close",
|
|
1167
|
+
"destroy",
|
|
1168
|
+
"disconnect",
|
|
1169
|
+
"dispose",
|
|
1170
|
+
"kill",
|
|
1171
|
+
"release",
|
|
1172
|
+
"removeItem",
|
|
1173
|
+
"revokeObjectURL",
|
|
1174
|
+
"rm",
|
|
1175
|
+
"rmSync",
|
|
1176
|
+
"rmdir",
|
|
1177
|
+
"rmdirSync",
|
|
1178
|
+
"terminate",
|
|
1179
|
+
"unlink",
|
|
1180
|
+
"unlinkSync"
|
|
1181
|
+
];
|
|
1182
|
+
/**
|
|
1183
|
+
* A value that carries nothing about the error: no value, a literal (`null`, `false`, `0`, `""`),
|
|
1184
|
+
* `undefined`, `void …`, a template without expressions, `{}` or `[]`.
|
|
1185
|
+
*/
|
|
1186
|
+
function isConstant(input) {
|
|
1187
|
+
const node = unwrap(input);
|
|
1188
|
+
if (!node) return true;
|
|
1189
|
+
switch (node.type) {
|
|
1190
|
+
case "Literal": return !("regex" in node && node.regex);
|
|
1191
|
+
case "Identifier": return node.name === "undefined";
|
|
1192
|
+
case "UnaryExpression": return node.operator === "void" || node.operator === "-" && isConstant(node.argument);
|
|
1193
|
+
case "TemplateLiteral": return node.expressions.length === 0;
|
|
1194
|
+
case "ObjectExpression": return node.properties.length === 0;
|
|
1195
|
+
case "ArrayExpression": return node.elements.length === 0;
|
|
1196
|
+
default: return false;
|
|
1197
|
+
}
|
|
1198
|
+
}
|
|
1199
|
+
/** Statements without effect: `;` and empty blocks. */
|
|
1200
|
+
function isInert(statement) {
|
|
1201
|
+
if (statement.type === "EmptyStatement") return true;
|
|
1202
|
+
return statement.type === "BlockStatement" && statement.body.every(isInert);
|
|
1203
|
+
}
|
|
1204
|
+
/** What a block does once inert statements are dropped: nothing, or only `return <constant>`. */
|
|
1205
|
+
function swallows(block) {
|
|
1206
|
+
const statements = block.body.filter((s) => !isInert(s));
|
|
1207
|
+
if (statements.length === 0) return "empty";
|
|
1208
|
+
const only = statements[0];
|
|
1209
|
+
if (statements.length === 1 && only?.type === "ReturnStatement" && isConstant(only.argument)) return "constant";
|
|
1210
|
+
return null;
|
|
1211
|
+
}
|
|
1212
|
+
function isSwallowingHandler(input) {
|
|
1213
|
+
const fn = unwrap(input);
|
|
1214
|
+
if (!fn || fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return false;
|
|
1215
|
+
if (fn.body.type !== "BlockStatement") return isConstant(fn.body);
|
|
1216
|
+
return swallows(fn.body) !== null;
|
|
1217
|
+
}
|
|
1218
|
+
/** The rejection handler of `.catch(handler)` or `.then(onFulfilled, handler)`. */
|
|
1219
|
+
function rejectionHandler(node) {
|
|
1220
|
+
const callee = unwrap(node.callee);
|
|
1221
|
+
if (!callee || callee.type !== "MemberExpression") return null;
|
|
1222
|
+
const method = propertyName(callee);
|
|
1223
|
+
if (method === "catch" && node.arguments[0]) return [node.arguments[0], "this .catch() handler"];
|
|
1224
|
+
if (method === "then" && node.arguments[1]) return [node.arguments[1], "this .then() rejection handler"];
|
|
1225
|
+
return null;
|
|
1226
|
+
}
|
|
1227
|
+
const noSwallowedCatch = {
|
|
1228
|
+
meta: {
|
|
1229
|
+
type: "problem",
|
|
1230
|
+
docs: {
|
|
1231
|
+
description: "Disallow catch blocks and .catch() handlers that silently discard errors",
|
|
1232
|
+
url: docsUrl(RULE$1)
|
|
1233
|
+
},
|
|
1234
|
+
schema: [{
|
|
1235
|
+
type: "object",
|
|
1236
|
+
properties: { cleanupCallees: {
|
|
1237
|
+
type: "array",
|
|
1238
|
+
items: { type: "string" }
|
|
1239
|
+
} },
|
|
1240
|
+
additionalProperties: false
|
|
1241
|
+
}],
|
|
1242
|
+
messages: { swallowed: message(RULE$1, {
|
|
1243
|
+
violation: "{{what}} discards the error without logging, reporting or rethrowing it, so failures (including failed writes) vanish silently.",
|
|
1244
|
+
fix: "handle it: log with context and report it to telemetry, return an explicit error result, or rethrow; if the caller can proceed, say why in code.",
|
|
1245
|
+
exemption: "when ignoring the error is genuinely correct, put a `// fail-open: <reason>` comment inside the handler or on the line of (or just above) the catch; best-effort cleanup calls (close, cancel, rm, …) are allowed without a comment. Inline disable comments are rejected."
|
|
1246
|
+
}) }
|
|
1247
|
+
},
|
|
1248
|
+
create(context) {
|
|
1249
|
+
const options = context.options[0] ?? {};
|
|
1250
|
+
const cleanup = new Set(options.cleanupCallees ?? DEFAULT_CLEANUP);
|
|
1251
|
+
let failOpen = null;
|
|
1252
|
+
function failOpenComments() {
|
|
1253
|
+
failOpen ??= context.sourceCode.getAllComments().filter((c) => FAIL_OPEN.test(c.value));
|
|
1254
|
+
return failOpen;
|
|
1255
|
+
}
|
|
1256
|
+
/** A `fail-open:` comment inside `inside`, or on `line` / the line before it. */
|
|
1257
|
+
function hasFailOpen(inside, line) {
|
|
1258
|
+
return failOpenComments().some((c) => c.range[0] >= inside.range[0] && c.range[1] <= inside.range[1] || c.loc.end.line === line || c.loc.end.line === line - 1);
|
|
1259
|
+
}
|
|
1260
|
+
function isCleanupCall(input) {
|
|
1261
|
+
let node = unwrap(input);
|
|
1262
|
+
if (node?.type === "AwaitExpression") node = unwrap(node.argument);
|
|
1263
|
+
if (node?.type === "UnaryExpression" && node.operator === "void") node = unwrap(node.argument);
|
|
1264
|
+
if (!node || node.type !== "CallExpression") return false;
|
|
1265
|
+
const name = calleeName(node.callee);
|
|
1266
|
+
return name !== null && cleanup.has(name);
|
|
1267
|
+
}
|
|
1268
|
+
return {
|
|
1269
|
+
CatchClause(node) {
|
|
1270
|
+
if (swallows(node.body) !== "empty") return;
|
|
1271
|
+
const tryStatement = node.parent;
|
|
1272
|
+
if (tryStatement?.type === "TryStatement" && tryStatement.block.body.length > 0 && tryStatement.block.body.every((s) => s.type === "ExpressionStatement" && isCleanupCall(s.expression))) return;
|
|
1273
|
+
if (hasFailOpen(node.body, node.loc.start.line)) return;
|
|
1274
|
+
context.report({
|
|
1275
|
+
node,
|
|
1276
|
+
messageId: "swallowed",
|
|
1277
|
+
data: { what: "this empty catch block" }
|
|
1278
|
+
});
|
|
1279
|
+
},
|
|
1280
|
+
CallExpression(node) {
|
|
1281
|
+
const found = rejectionHandler(node);
|
|
1282
|
+
if (!found) return;
|
|
1283
|
+
const [handler, what] = found;
|
|
1284
|
+
if (!isSwallowingHandler(handler)) return;
|
|
1285
|
+
const callee = unwrap(node.callee);
|
|
1286
|
+
if (isCleanupCall(callee.object)) return;
|
|
1287
|
+
if (hasFailOpen(handler, callee.property.loc.start.line)) return;
|
|
1288
|
+
context.report({
|
|
1289
|
+
node: handler,
|
|
1290
|
+
messageId: "swallowed",
|
|
1291
|
+
data: { what }
|
|
1292
|
+
});
|
|
1293
|
+
}
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
};
|
|
1297
|
+
// src/plugin/rules/no-window-bus.ts
|
|
1298
|
+
const RULE = "no-window-bus";
|
|
1299
|
+
const LISTENER_METHODS = /* @__PURE__ */ new Set(["addEventListener", "removeEventListener"]);
|
|
1300
|
+
const DOM_EVENT_NAME = /^[A-Za-z]+$/;
|
|
1301
|
+
const noWindowBus = {
|
|
1302
|
+
meta: {
|
|
1303
|
+
type: "problem",
|
|
1304
|
+
docs: {
|
|
1305
|
+
description: "Disallow window/document as an untyped global event bus",
|
|
1306
|
+
url: docsUrl(RULE)
|
|
1307
|
+
},
|
|
1308
|
+
schema: [{
|
|
1309
|
+
type: "object",
|
|
1310
|
+
properties: { allowEvents: {
|
|
1311
|
+
type: "array",
|
|
1312
|
+
items: { type: "string" }
|
|
1313
|
+
} },
|
|
1314
|
+
additionalProperties: false
|
|
1315
|
+
}],
|
|
1316
|
+
messages: { windowBus: message(RULE, {
|
|
1317
|
+
violation: "{{what}} uses the global object as an event bus: untyped, no single owner, and nothing checks that every event has both a sender and a listener.",
|
|
1318
|
+
fix: "share the state through a typed store or context, or call the owning module's API directly; if a cross-app event is really needed, declare it in the event registry and use its typed emit/subscribe helpers.",
|
|
1319
|
+
exemption: `events declared in the registry can be listed in the rule's \`allowEvents\` option; ${CONFIG_EXEMPTION}`
|
|
1320
|
+
}) }
|
|
1321
|
+
},
|
|
1322
|
+
create(context) {
|
|
1323
|
+
const options = context.options[0] ?? {};
|
|
1324
|
+
const allowed = new Set(options.allowEvents ?? []);
|
|
1325
|
+
function isGlobalTarget(node) {
|
|
1326
|
+
return isGlobalObject(context, node, ["document"]);
|
|
1327
|
+
}
|
|
1328
|
+
/** The event name when it is a string literal or a same-file string constant. */
|
|
1329
|
+
function eventName(arg) {
|
|
1330
|
+
const direct = staticString(arg);
|
|
1331
|
+
if (direct !== null) return direct;
|
|
1332
|
+
const id = unwrap(arg);
|
|
1333
|
+
if (!id || id.type !== "Identifier") return null;
|
|
1334
|
+
const def = findVariable(context, id, id.name)?.defs[0];
|
|
1335
|
+
if (!def || def.node.type !== "VariableDeclarator") return null;
|
|
1336
|
+
const declaration = def.node.parent;
|
|
1337
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const") return null;
|
|
1338
|
+
return staticString(def.node.init);
|
|
1339
|
+
}
|
|
1340
|
+
function report(node, what) {
|
|
1341
|
+
context.report({
|
|
1342
|
+
node,
|
|
1343
|
+
messageId: "windowBus",
|
|
1344
|
+
data: { what }
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
return {
|
|
1348
|
+
CallExpression(node) {
|
|
1349
|
+
const callee = unwrap(node.callee);
|
|
1350
|
+
if (!callee) return;
|
|
1351
|
+
let method = null;
|
|
1352
|
+
let target = "window";
|
|
1353
|
+
if (callee.type === "Identifier") {
|
|
1354
|
+
if (!isGlobalName(context, callee, callee.name)) return;
|
|
1355
|
+
method = callee.name;
|
|
1356
|
+
} else if (callee.type === "MemberExpression" && isGlobalTarget(callee.object)) {
|
|
1357
|
+
method = propertyName(callee);
|
|
1358
|
+
target = unwrap(callee.object).name;
|
|
1359
|
+
}
|
|
1360
|
+
if (method === "dispatchEvent") {
|
|
1361
|
+
const event = unwrap(node.arguments[0]);
|
|
1362
|
+
const name = event?.type === "NewExpression" ? eventName(event.arguments[0]) : null;
|
|
1363
|
+
if (name !== null && allowed.has(name)) return;
|
|
1364
|
+
report(node, `${target}.dispatchEvent()`);
|
|
1365
|
+
} else if (method !== null && LISTENER_METHODS.has(method)) {
|
|
1366
|
+
const name = eventName(node.arguments[0]);
|
|
1367
|
+
if (name === null || DOM_EVENT_NAME.test(name) || allowed.has(name)) return;
|
|
1368
|
+
report(node, `${target}.${method}("${name}")`);
|
|
1369
|
+
}
|
|
1370
|
+
},
|
|
1371
|
+
NewExpression(node) {
|
|
1372
|
+
const callee = unwrap(node.callee);
|
|
1373
|
+
if (callee?.type !== "Identifier" || callee.name !== "CustomEvent") return;
|
|
1374
|
+
if (!isGlobalName(context, callee, "CustomEvent")) return;
|
|
1375
|
+
const name = eventName(node.arguments[0]);
|
|
1376
|
+
if (name !== null && allowed.has(name)) return;
|
|
1377
|
+
const parent = node.parent;
|
|
1378
|
+
if (parent?.type === "CallExpression" && parent.arguments[0] === node) {
|
|
1379
|
+
const outer = unwrap(parent.callee);
|
|
1380
|
+
if (outer?.type === "Identifier" && outer.name === "dispatchEvent" || outer?.type === "MemberExpression" && propertyName(outer) === "dispatchEvent" && isGlobalTarget(outer.object)) return;
|
|
1381
|
+
}
|
|
1382
|
+
report(node, "new CustomEvent()");
|
|
1383
|
+
}
|
|
1384
|
+
};
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
// src/plugin/index.ts
|
|
1388
|
+
/** Kept in sync with package.json by test/plugin.test.ts. */
|
|
1389
|
+
const VERSION = "0.2.0-alpha.0";
|
|
1390
|
+
const plugin = {
|
|
1391
|
+
meta: {
|
|
1392
|
+
name: "od",
|
|
1393
|
+
version: VERSION
|
|
1394
|
+
},
|
|
1395
|
+
rules: {
|
|
1396
|
+
"no-product-literal": noProductLiteral,
|
|
1397
|
+
"no-shell-imports-product": noShellImportsProduct,
|
|
1398
|
+
"no-locale-branch": noLocaleBranch,
|
|
1399
|
+
"no-raw-storage": noRawStorage,
|
|
1400
|
+
"no-swallowed-catch": noSwallowedCatch,
|
|
1401
|
+
"no-raw-fetch": noRawFetch,
|
|
1402
|
+
"no-api-path-literal": noApiPathLiteral,
|
|
1403
|
+
"no-window-bus": noWindowBus,
|
|
1404
|
+
"no-process-env": noProcessEnv,
|
|
1405
|
+
"no-error-message-match": noErrorMessageMatch,
|
|
1406
|
+
"no-cjk-literal": noCjkLiteral,
|
|
1407
|
+
"no-constant-flag": noConstantFlag
|
|
1408
|
+
}
|
|
1409
|
+
};
|
|
1410
|
+
export { VERSION, plugin as default };
|