@opendesignai/harness 0.0.0-stage → 0.1.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 +127 -2
- package/dist/cli.d.mts +1 -0
- package/dist/cli.mjs +380 -0
- package/dist/eslint-comments.d.mts +24 -0
- package/dist/eslint-comments.mjs +50 -0
- package/dist/paths-DBoYPrGy.mjs +59 -0
- package/dist/plugin.d.mts +25 -0
- package/dist/plugin.mjs +1040 -0
- package/docs/adopting.md +157 -0
- package/docs/decisions/0001-oxlint.md +74 -0
- package/docs/decisions/0002-react-compiler-rules.md +34 -0
- package/docs/ratchet.md +49 -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 +55 -0
- package/docs/rules/no-locale-branch.md +51 -0
- package/docs/rules/no-process-env.md +27 -0
- package/docs/rules/no-product-literal.md +35 -0
- package/docs/rules/no-raw-fetch.md +33 -0
- package/docs/rules/no-raw-storage.md +41 -0
- package/docs/rules/no-shell-imports-product.md +46 -0
- package/docs/rules/no-swallowed-catch.md +75 -0
- package/docs/rules/no-window-bus.md +35 -0
- package/docs/size.md +51 -0
- package/package.json +73 -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 +47 -0
- package/presets/rules/platform-boundary.json +6 -0
- package/presets/rules/product.json +5 -0
package/dist/plugin.mjs
ADDED
|
@@ -0,0 +1,1040 @@
|
|
|
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
|
+
/** True when `name` at `node` refers to the global binding (not a local, parameter or import). */
|
|
65
|
+
function isGlobalName(context, node, name) {
|
|
66
|
+
const variable = findVariable(context, node, name);
|
|
67
|
+
return !variable || variable.defs.length === 0;
|
|
68
|
+
}
|
|
69
|
+
const GLOBAL_OBJECTS = /* @__PURE__ */ new Set([
|
|
70
|
+
"window",
|
|
71
|
+
"globalThis",
|
|
72
|
+
"self"
|
|
73
|
+
]);
|
|
74
|
+
/** `window`, `globalThis` or `self` (or any of `extra`) as an unshadowed global identifier. */
|
|
75
|
+
function isGlobalObject(context, node, extra = []) {
|
|
76
|
+
const n = unwrap(node);
|
|
77
|
+
if (!n || n.type !== "Identifier") return false;
|
|
78
|
+
if (!GLOBAL_OBJECTS.has(n.name) && !extra.includes(n.name)) return false;
|
|
79
|
+
return isGlobalName(context, n, n.name);
|
|
80
|
+
}
|
|
81
|
+
/** Direct children of a node, using the parser's visitor keys. */
|
|
82
|
+
function childNodes(node, visitorKeys) {
|
|
83
|
+
const record = node;
|
|
84
|
+
const keys = visitorKeys[node.type] ?? Object.keys(record);
|
|
85
|
+
const out = [];
|
|
86
|
+
for (const key of keys) {
|
|
87
|
+
if (key === "parent" || key === "loc" || key === "range") continue;
|
|
88
|
+
const value = record[key];
|
|
89
|
+
if (Array.isArray(value)) {
|
|
90
|
+
for (const item of value) if (isNode(item)) out.push(item);
|
|
91
|
+
} else if (isNode(value)) out.push(value);
|
|
92
|
+
}
|
|
93
|
+
return out;
|
|
94
|
+
}
|
|
95
|
+
function isNode(value) {
|
|
96
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
97
|
+
}
|
|
98
|
+
/** Depth-first search below (and including) `root`. */
|
|
99
|
+
function someNode(root, visitorKeys, predicate) {
|
|
100
|
+
const stack = [root];
|
|
101
|
+
while (stack.length > 0) {
|
|
102
|
+
const node = stack.pop();
|
|
103
|
+
if (predicate(node)) return true;
|
|
104
|
+
stack.push(...childNodes(node, visitorKeys));
|
|
105
|
+
}
|
|
106
|
+
return false;
|
|
107
|
+
}
|
|
108
|
+
const TYPE_CONTAINERS = /* @__PURE__ */ new Set([
|
|
109
|
+
"TSTypeAnnotation",
|
|
110
|
+
"TSTypeQuery",
|
|
111
|
+
"TSTypeParameterInstantiation",
|
|
112
|
+
"TSTypeParameterDeclaration",
|
|
113
|
+
"TSInterfaceDeclaration",
|
|
114
|
+
"TSTypeAliasDeclaration",
|
|
115
|
+
"TSPropertySignature",
|
|
116
|
+
"TSIndexSignature"
|
|
117
|
+
]);
|
|
118
|
+
/** True for nodes that only exist in type positions (`type X = "a"`, `typeof x`). */
|
|
119
|
+
function isInTypePosition(node) {
|
|
120
|
+
let current = node.parent;
|
|
121
|
+
while (current) {
|
|
122
|
+
const type = current.type;
|
|
123
|
+
if (TYPE_CONTAINERS.has(type) || type.startsWith("TS") && type.endsWith("Type")) return true;
|
|
124
|
+
if (type === "Program" || type.endsWith("Statement") || type.endsWith("Declaration")) return false;
|
|
125
|
+
current = current.parent;
|
|
126
|
+
}
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
/** True when a string literal is a module specifier (`import x from "a"`, `import("a")`). */
|
|
130
|
+
function isModuleSource(node) {
|
|
131
|
+
const parent = node.parent;
|
|
132
|
+
if (!parent) return false;
|
|
133
|
+
switch (parent.type) {
|
|
134
|
+
case "ImportDeclaration":
|
|
135
|
+
case "ExportAllDeclaration":
|
|
136
|
+
case "ExportNamedDeclaration":
|
|
137
|
+
case "ImportExpression": return parent.source === node;
|
|
138
|
+
case "TSExternalModuleReference":
|
|
139
|
+
case "TSImportType": return true;
|
|
140
|
+
case "ImportAttribute":
|
|
141
|
+
case "TSModuleDeclaration": return true;
|
|
142
|
+
default: return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function compileRegExp(pattern, flags, option) {
|
|
146
|
+
try {
|
|
147
|
+
return new RegExp(pattern, flags);
|
|
148
|
+
} catch (error) {
|
|
149
|
+
throw new Error(`Invalid regular expression in option "${option}": ${String(error)}`, { cause: error });
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// src/plugin/rules/no-api-path-literal.ts
|
|
153
|
+
const RULE$11 = "no-api-path-literal";
|
|
154
|
+
const DEFAULT_PREFIXES = ["/api/", "/motion-api/"];
|
|
155
|
+
const noApiPathLiteral = {
|
|
156
|
+
meta: {
|
|
157
|
+
type: "problem",
|
|
158
|
+
docs: {
|
|
159
|
+
description: "Disallow API path literals outside the API client layer",
|
|
160
|
+
url: docsUrl(RULE$11)
|
|
161
|
+
},
|
|
162
|
+
schema: [{
|
|
163
|
+
type: "object",
|
|
164
|
+
properties: {
|
|
165
|
+
prefixes: {
|
|
166
|
+
type: "array",
|
|
167
|
+
items: { type: "string" }
|
|
168
|
+
},
|
|
169
|
+
client: { type: "string" }
|
|
170
|
+
},
|
|
171
|
+
additionalProperties: false
|
|
172
|
+
}],
|
|
173
|
+
messages: { apiPath: message(RULE$11, {
|
|
174
|
+
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.",
|
|
175
|
+
fix: "build the URL with a helper in {{client}} (add one if it does not exist) and call that helper.",
|
|
176
|
+
exemption: CONFIG_EXEMPTION
|
|
177
|
+
}) }
|
|
178
|
+
},
|
|
179
|
+
create(context) {
|
|
180
|
+
const options = context.options[0] ?? {};
|
|
181
|
+
const prefixes = options.prefixes ?? DEFAULT_PREFIXES;
|
|
182
|
+
const client = options.client ? `the API client in ${options.client}` : "the typed API client";
|
|
183
|
+
function matchPrefix(text) {
|
|
184
|
+
for (const prefix of prefixes) {
|
|
185
|
+
const bare = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
186
|
+
if (text.startsWith(prefix) || text === bare) return prefix;
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
function check(node, text) {
|
|
191
|
+
if (isModuleSource(node) || isInTypePosition(node)) return;
|
|
192
|
+
const prefix = matchPrefix(text);
|
|
193
|
+
if (prefix !== null) context.report({
|
|
194
|
+
node,
|
|
195
|
+
messageId: "apiPath",
|
|
196
|
+
data: {
|
|
197
|
+
prefix: `"${prefix}"`,
|
|
198
|
+
client
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return {
|
|
203
|
+
Literal(node) {
|
|
204
|
+
if (typeof node.value === "string") check(node, node.value);
|
|
205
|
+
},
|
|
206
|
+
TemplateLiteral(node) {
|
|
207
|
+
const first = node.quasis[0];
|
|
208
|
+
if (first) check(node, first.value.cooked ?? first.value.raw);
|
|
209
|
+
}
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
// src/plugin/rules/no-cjk-literal.ts
|
|
214
|
+
const RULE$10 = "no-cjk-literal";
|
|
215
|
+
const DEFAULT_PATTERN$1 = "[\\p{Script=Han}\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Hangul}]";
|
|
216
|
+
const noCjkLiteral = {
|
|
217
|
+
meta: {
|
|
218
|
+
type: "problem",
|
|
219
|
+
docs: {
|
|
220
|
+
description: "Disallow CJK text in source literals outside the i18n catalogs",
|
|
221
|
+
url: docsUrl(RULE$10)
|
|
222
|
+
},
|
|
223
|
+
schema: [{
|
|
224
|
+
type: "object",
|
|
225
|
+
properties: { pattern: { type: "string" } },
|
|
226
|
+
additionalProperties: false
|
|
227
|
+
}],
|
|
228
|
+
messages: { cjk: message(RULE$10, {
|
|
229
|
+
violation: "CJK text in a source literal: {{sample}}. Copy hard-coded in code skips translation review and cannot follow the user's locale.",
|
|
230
|
+
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.",
|
|
231
|
+
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."
|
|
232
|
+
}) }
|
|
233
|
+
},
|
|
234
|
+
create(context) {
|
|
235
|
+
const re = compileRegExp((context.options[0] ?? {}).pattern ?? DEFAULT_PATTERN$1, "u", "pattern");
|
|
236
|
+
function check(node, text) {
|
|
237
|
+
const match = re.exec(text);
|
|
238
|
+
if (!match) return;
|
|
239
|
+
const start = Math.max(0, match.index - 8);
|
|
240
|
+
const sample = JSON.stringify(text.slice(start, match.index + 12).trim());
|
|
241
|
+
context.report({
|
|
242
|
+
node,
|
|
243
|
+
messageId: "cjk",
|
|
244
|
+
data: { sample }
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
return {
|
|
248
|
+
Literal(node) {
|
|
249
|
+
if (typeof node.value !== "string") return;
|
|
250
|
+
if (isModuleSource(node) || isInTypePosition(node)) return;
|
|
251
|
+
check(node, node.value);
|
|
252
|
+
},
|
|
253
|
+
TemplateLiteral(node) {
|
|
254
|
+
if (isInTypePosition(node)) return;
|
|
255
|
+
check(node, node.quasis.map((q) => q.value.cooked ?? q.value.raw).join("${}"));
|
|
256
|
+
},
|
|
257
|
+
JSXText(node) {
|
|
258
|
+
check(node, node.value);
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
// src/plugin/rules/no-constant-flag.ts
|
|
264
|
+
const RULE$9 = "no-constant-flag";
|
|
265
|
+
const DEFAULT_PATTERN = [
|
|
266
|
+
"[A-Za-z0-9]*(?:Enabled|Disabled|Flag)",
|
|
267
|
+
"[A-Z0-9_]*_(?:ENABLED|DISABLED|FLAG)",
|
|
268
|
+
"(?:ENABLE|DISABLE|FEATURE|FF|SHOW|HIDE|USE)_[A-Z0-9_]+",
|
|
269
|
+
"(?:enable|disable|show|hide)[A-Z][A-Za-z0-9]*"
|
|
270
|
+
].map((alt) => `(?:${alt})`).join("|");
|
|
271
|
+
/** `true`, `false`, `!0`, `!1`, possibly wrapped in `as const` / `satisfies`. */
|
|
272
|
+
function booleanValue(input) {
|
|
273
|
+
const node = unwrap(input);
|
|
274
|
+
if (!node) return null;
|
|
275
|
+
if (node.type === "Literal" && typeof node.value === "boolean") return String(node.value);
|
|
276
|
+
if (node.type === "UnaryExpression" && node.operator === "!") {
|
|
277
|
+
const arg = unwrap(node.argument);
|
|
278
|
+
if (arg?.type === "Literal" && (arg.value === 0 || arg.value === 1)) return String(!arg.value);
|
|
279
|
+
}
|
|
280
|
+
return null;
|
|
281
|
+
}
|
|
282
|
+
const noConstantFlag = {
|
|
283
|
+
meta: {
|
|
284
|
+
type: "problem",
|
|
285
|
+
docs: {
|
|
286
|
+
description: "Disallow module-level constant feature flags",
|
|
287
|
+
url: docsUrl(RULE$9)
|
|
288
|
+
},
|
|
289
|
+
schema: [{
|
|
290
|
+
type: "object",
|
|
291
|
+
properties: { pattern: { type: "string" } },
|
|
292
|
+
additionalProperties: false
|
|
293
|
+
}],
|
|
294
|
+
messages: { constantFlag: message(RULE$9, {
|
|
295
|
+
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.",
|
|
296
|
+
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.",
|
|
297
|
+
exemption: "none inline (disable comments are rejected). A constant that is not a toggle should be renamed so it does not read like one."
|
|
298
|
+
}) }
|
|
299
|
+
},
|
|
300
|
+
create(context) {
|
|
301
|
+
const re = compileRegExp(`^(?:${(context.options[0] ?? {}).pattern ?? DEFAULT_PATTERN})$`, "", "pattern");
|
|
302
|
+
return { VariableDeclaration(node) {
|
|
303
|
+
if (node.kind !== "const") return;
|
|
304
|
+
const parent = node.parent;
|
|
305
|
+
if (!(parent?.type === "Program" || parent?.type === "ExportNamedDeclaration" && parent.parent?.type === "Program")) return;
|
|
306
|
+
for (const declarator of node.declarations) {
|
|
307
|
+
if (declarator.id.type !== "Identifier" || !re.test(declarator.id.name)) continue;
|
|
308
|
+
const value = booleanValue(declarator.init);
|
|
309
|
+
if (value === null) continue;
|
|
310
|
+
context.report({
|
|
311
|
+
node: declarator,
|
|
312
|
+
messageId: "constantFlag",
|
|
313
|
+
data: {
|
|
314
|
+
name: declarator.id.name,
|
|
315
|
+
value
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
} };
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
// src/plugin/rules/no-error-message-match.ts
|
|
323
|
+
const RULE$8 = "no-error-message-match";
|
|
324
|
+
const DEFAULT_IDENTIFIERS = "^(?:msg|err(?:or)?_?(?:msg|message|text))$";
|
|
325
|
+
const DEFAULT_ERROR_NAMES = "^(?:e|err|error|ex|exc|exception|cause)$";
|
|
326
|
+
const MATCH_METHODS = /* @__PURE__ */ new Set([
|
|
327
|
+
"includes",
|
|
328
|
+
"startsWith",
|
|
329
|
+
"endsWith",
|
|
330
|
+
"indexOf",
|
|
331
|
+
"match",
|
|
332
|
+
"search"
|
|
333
|
+
]);
|
|
334
|
+
const TRANSFORMS = /* @__PURE__ */ new Set([
|
|
335
|
+
"toLowerCase",
|
|
336
|
+
"toUpperCase",
|
|
337
|
+
"toLocaleLowerCase",
|
|
338
|
+
"toLocaleUpperCase",
|
|
339
|
+
"trim",
|
|
340
|
+
"trimStart",
|
|
341
|
+
"trimEnd",
|
|
342
|
+
"normalize"
|
|
343
|
+
]);
|
|
344
|
+
const EQ_OPS$2 = /* @__PURE__ */ new Set([
|
|
345
|
+
"===",
|
|
346
|
+
"!==",
|
|
347
|
+
"==",
|
|
348
|
+
"!="
|
|
349
|
+
]);
|
|
350
|
+
function isRegex(node) {
|
|
351
|
+
const n = unwrap(node);
|
|
352
|
+
return n?.type === "Literal" && "regex" in n && n.regex !== void 0;
|
|
353
|
+
}
|
|
354
|
+
const noErrorMessageMatch = {
|
|
355
|
+
meta: {
|
|
356
|
+
type: "problem",
|
|
357
|
+
docs: {
|
|
358
|
+
description: "Disallow decisions based on matching error message text",
|
|
359
|
+
url: docsUrl(RULE$8)
|
|
360
|
+
},
|
|
361
|
+
schema: [{
|
|
362
|
+
type: "object",
|
|
363
|
+
properties: {
|
|
364
|
+
identifierPattern: { type: "string" },
|
|
365
|
+
errorPattern: { type: "string" },
|
|
366
|
+
trackAliases: { type: "boolean" }
|
|
367
|
+
},
|
|
368
|
+
additionalProperties: false
|
|
369
|
+
}],
|
|
370
|
+
messages: { messageMatch: message(RULE$8, {
|
|
371
|
+
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.",
|
|
372
|
+
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.",
|
|
373
|
+
exemption: `adapters for third-party errors that expose nothing but text belong in one module that maps them to codes; ${CONFIG_EXEMPTION}`
|
|
374
|
+
}) }
|
|
375
|
+
},
|
|
376
|
+
create(context) {
|
|
377
|
+
const options = context.options[0] ?? {};
|
|
378
|
+
const idRe = compileRegExp(options.identifierPattern ?? DEFAULT_IDENTIFIERS, "i", "identifierPattern");
|
|
379
|
+
const errRe = compileRegExp(options.errorPattern ?? DEFAULT_ERROR_NAMES, "", "errorPattern");
|
|
380
|
+
const trackAliases = options.trackAliases !== false;
|
|
381
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
382
|
+
/** An expression holding error message text. */
|
|
383
|
+
function isMessage(input) {
|
|
384
|
+
const node = unwrap(input);
|
|
385
|
+
if (!node) return false;
|
|
386
|
+
switch (node.type) {
|
|
387
|
+
case "MemberExpression": return propertyName(node) === "message";
|
|
388
|
+
case "Identifier": return idRe.test(node.name) || aliases.has(node.name);
|
|
389
|
+
case "CallExpression": {
|
|
390
|
+
const callee = unwrap(node.callee);
|
|
391
|
+
if (callee?.type === "MemberExpression" && TRANSFORMS.has(propertyName(callee) ?? "")) return isMessage(callee.object);
|
|
392
|
+
if (callee?.type === "Identifier" && callee.name === "String") {
|
|
393
|
+
const arg = unwrap(node.arguments[0]);
|
|
394
|
+
return arg?.type === "Identifier" && errRe.test(arg.name) || isMessage(arg);
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
case "LogicalExpression": return isMessage(node.left);
|
|
399
|
+
case "ConditionalExpression": return isMessage(node.consequent) || isMessage(node.alternate);
|
|
400
|
+
default: return false;
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
function report(node) {
|
|
404
|
+
context.report({
|
|
405
|
+
node,
|
|
406
|
+
messageId: "messageMatch"
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
const pending = [];
|
|
410
|
+
return {
|
|
411
|
+
VariableDeclarator(node) {
|
|
412
|
+
if (trackAliases && node.id.type === "Identifier" && isMessage(node.init)) aliases.add(node.id.name);
|
|
413
|
+
},
|
|
414
|
+
CallExpression(node) {
|
|
415
|
+
pending.push(node);
|
|
416
|
+
},
|
|
417
|
+
BinaryExpression(node) {
|
|
418
|
+
if (EQ_OPS$2.has(node.operator)) pending.push(node);
|
|
419
|
+
},
|
|
420
|
+
"Program:exit"() {
|
|
421
|
+
for (const node of pending) {
|
|
422
|
+
if (node.type === "BinaryExpression") {
|
|
423
|
+
const left = node.left;
|
|
424
|
+
if (isMessage(left) && staticString(node.right) !== null || isMessage(node.right) && staticString(left) !== null) report(node);
|
|
425
|
+
continue;
|
|
426
|
+
}
|
|
427
|
+
if (node.type !== "CallExpression") continue;
|
|
428
|
+
const callee = unwrap(node.callee);
|
|
429
|
+
const method = callee ? calleeName(callee) : null;
|
|
430
|
+
if (callee?.type !== "MemberExpression" || method === null) continue;
|
|
431
|
+
const arg = node.arguments[0];
|
|
432
|
+
if (MATCH_METHODS.has(method)) {
|
|
433
|
+
if ((staticString(arg) !== null || isRegex(arg)) && isMessage(callee.object)) report(node);
|
|
434
|
+
} else if (method === "test" && isRegex(callee.object) && isMessage(arg)) report(node);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
// src/plugin/rules/no-locale-branch.ts
|
|
441
|
+
const RULE$7 = "no-locale-branch";
|
|
442
|
+
const DEFAULT_ALIAS_PATTERN = "^is(Zh|Chinese|Cn|CN|En|English)$";
|
|
443
|
+
const PREDICATE_METHODS = /* @__PURE__ */ new Set([
|
|
444
|
+
"startsWith",
|
|
445
|
+
"endsWith",
|
|
446
|
+
"includes"
|
|
447
|
+
]);
|
|
448
|
+
const EQ_OPS$1 = /* @__PURE__ */ new Set([
|
|
449
|
+
"===",
|
|
450
|
+
"!==",
|
|
451
|
+
"==",
|
|
452
|
+
"!="
|
|
453
|
+
]);
|
|
454
|
+
function isStringish(node) {
|
|
455
|
+
if (node.type === "Literal" && typeof node.value === "string") return node.value.trim() !== "";
|
|
456
|
+
if (node.type === "TemplateLiteral") return node.quasis.some((q) => (q.value.cooked ?? q.value.raw).trim() !== "");
|
|
457
|
+
if (node.type === "JSXText") return node.value.trim() !== "";
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
const noLocaleBranch = {
|
|
461
|
+
meta: {
|
|
462
|
+
type: "problem",
|
|
463
|
+
docs: {
|
|
464
|
+
description: "Disallow hard-coded copy in branches derived from the locale",
|
|
465
|
+
url: docsUrl(RULE$7)
|
|
466
|
+
},
|
|
467
|
+
schema: [{
|
|
468
|
+
type: "object",
|
|
469
|
+
properties: {
|
|
470
|
+
trackAliases: { type: "boolean" },
|
|
471
|
+
aliasPattern: { type: "string" }
|
|
472
|
+
},
|
|
473
|
+
additionalProperties: false
|
|
474
|
+
}],
|
|
475
|
+
messages: { localeBranch: message(RULE$7, {
|
|
476
|
+
violation: "this branch picks hard-coded copy by locale (isZh / locale === ... / startsWith('zh')).",
|
|
477
|
+
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.",
|
|
478
|
+
exemption: `values that are not copy (URLs, format strings) belong in a locale-keyed table, not a branch; ${CONFIG_EXEMPTION}`
|
|
479
|
+
}) }
|
|
480
|
+
},
|
|
481
|
+
create(context) {
|
|
482
|
+
const options = context.options[0] ?? {};
|
|
483
|
+
const trackAliases = options.trackAliases !== false;
|
|
484
|
+
const aliasRe = compileRegExp(options.aliasPattern ?? DEFAULT_ALIAS_PATTERN, "", "aliasPattern");
|
|
485
|
+
const visitorKeys = context.sourceCode.visitorKeys;
|
|
486
|
+
const aliases = /* @__PURE__ */ new Set();
|
|
487
|
+
const candidates = [];
|
|
488
|
+
function mentionsLocale(node) {
|
|
489
|
+
return someNode(node, visitorKeys, (n) => n.type === "Identifier" && (n.name === "locale" || aliasRe.test(n.name) || aliases.has(n.name)));
|
|
490
|
+
}
|
|
491
|
+
function isLocaleTest(input) {
|
|
492
|
+
const node = unwrap(input);
|
|
493
|
+
if (!node) return false;
|
|
494
|
+
if (node.type === "Identifier") return aliasRe.test(node.name) || aliases.has(node.name);
|
|
495
|
+
if (node.type === "UnaryExpression" && node.operator === "!") return isLocaleTest(node.argument);
|
|
496
|
+
if (node.type === "LogicalExpression") return isLocaleTest(node.left) || isLocaleTest(node.right);
|
|
497
|
+
return (node.type === "BinaryExpression" && EQ_OPS$1.has(node.operator) || node.type === "CallExpression" && node.callee.type === "MemberExpression" && !node.callee.computed && node.callee.property.type === "Identifier" && PREDICATE_METHODS.has(node.callee.property.name)) && mentionsLocale(node);
|
|
498
|
+
}
|
|
499
|
+
return {
|
|
500
|
+
VariableDeclarator(node) {
|
|
501
|
+
if (trackAliases && node.id.type === "Identifier" && isLocaleTest(node.init)) aliases.add(node.id.name);
|
|
502
|
+
},
|
|
503
|
+
ConditionalExpression(node) {
|
|
504
|
+
candidates.push([node, [node.consequent, node.alternate]]);
|
|
505
|
+
},
|
|
506
|
+
IfStatement(node) {
|
|
507
|
+
const branches = [node.consequent];
|
|
508
|
+
if (node.alternate) branches.push(node.alternate);
|
|
509
|
+
candidates.push([node, branches]);
|
|
510
|
+
},
|
|
511
|
+
"Program:exit"() {
|
|
512
|
+
for (const [node, branches] of candidates) {
|
|
513
|
+
const test = node.test;
|
|
514
|
+
if (!isLocaleTest(test)) continue;
|
|
515
|
+
if (!branches.some((b) => someNode(b, visitorKeys, isStringish))) continue;
|
|
516
|
+
context.report({
|
|
517
|
+
node: test,
|
|
518
|
+
messageId: "localeBranch"
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
};
|
|
525
|
+
// src/plugin/rules/no-process-env.ts
|
|
526
|
+
const RULE$6 = "no-process-env";
|
|
527
|
+
const noProcessEnv = {
|
|
528
|
+
meta: {
|
|
529
|
+
type: "problem",
|
|
530
|
+
docs: {
|
|
531
|
+
description: "Disallow process.env outside the env registry",
|
|
532
|
+
url: docsUrl(RULE$6)
|
|
533
|
+
},
|
|
534
|
+
schema: [{
|
|
535
|
+
type: "object",
|
|
536
|
+
properties: { registry: { type: "string" } },
|
|
537
|
+
additionalProperties: false
|
|
538
|
+
}],
|
|
539
|
+
messages: { processEnv: message(RULE$6, {
|
|
540
|
+
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.",
|
|
541
|
+
fix: "declare the variable once in {{registry}} (name, type, default, description) and import the typed value from there.",
|
|
542
|
+
exemption: CONFIG_EXEMPTION
|
|
543
|
+
}) }
|
|
544
|
+
},
|
|
545
|
+
create(context) {
|
|
546
|
+
const options = context.options[0] ?? {};
|
|
547
|
+
const registry = options.registry ? `the env registry (${options.registry})` : "the env registry module";
|
|
548
|
+
return { MemberExpression(node) {
|
|
549
|
+
if (propertyName(node) !== "env") return;
|
|
550
|
+
const object = unwrap(node.object);
|
|
551
|
+
if (object?.type !== "Identifier" || object.name !== "process") return;
|
|
552
|
+
if (!isGlobalName(context, object, "process")) return;
|
|
553
|
+
context.report({
|
|
554
|
+
node,
|
|
555
|
+
messageId: "processEnv",
|
|
556
|
+
data: { registry }
|
|
557
|
+
});
|
|
558
|
+
} };
|
|
559
|
+
}
|
|
560
|
+
};
|
|
561
|
+
// src/plugin/rules/no-product-literal.ts
|
|
562
|
+
const RULE$5 = "no-product-literal";
|
|
563
|
+
const EQ_OPS = /* @__PURE__ */ new Set([
|
|
564
|
+
"===",
|
|
565
|
+
"!==",
|
|
566
|
+
"==",
|
|
567
|
+
"!="
|
|
568
|
+
]);
|
|
569
|
+
const DEFAULT_PRODUCTS = ["opengame", "openmotion"];
|
|
570
|
+
const DEFAULT_ACCESSORS = ["currentProductId"];
|
|
571
|
+
const noProductLiteral = {
|
|
572
|
+
meta: {
|
|
573
|
+
type: "problem",
|
|
574
|
+
docs: {
|
|
575
|
+
description: "Disallow branching on product identity literals",
|
|
576
|
+
url: docsUrl(RULE$5)
|
|
577
|
+
},
|
|
578
|
+
schema: [{
|
|
579
|
+
type: "object",
|
|
580
|
+
properties: {
|
|
581
|
+
products: {
|
|
582
|
+
type: "array",
|
|
583
|
+
items: { type: "string" }
|
|
584
|
+
},
|
|
585
|
+
accessors: {
|
|
586
|
+
type: "array",
|
|
587
|
+
items: { type: "string" }
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
additionalProperties: false
|
|
591
|
+
}],
|
|
592
|
+
messages: { productLiteral: message(RULE$5, {
|
|
593
|
+
violation: "code branches on product identity ({{what}}).",
|
|
594
|
+
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.",
|
|
595
|
+
exemption: CONFIG_EXEMPTION
|
|
596
|
+
}) }
|
|
597
|
+
},
|
|
598
|
+
create(context) {
|
|
599
|
+
const options = context.options[0] ?? {};
|
|
600
|
+
const products = new Set(options.products ?? DEFAULT_PRODUCTS);
|
|
601
|
+
const accessors = new Set(options.accessors ?? DEFAULT_ACCESSORS);
|
|
602
|
+
function productLiteral(node) {
|
|
603
|
+
const value = staticString(node);
|
|
604
|
+
return value !== null && products.has(value) ? value : null;
|
|
605
|
+
}
|
|
606
|
+
function accessorCall(node) {
|
|
607
|
+
const n = unwrap(node);
|
|
608
|
+
if (!n || n.type !== "CallExpression") return null;
|
|
609
|
+
const name = calleeName(n.callee);
|
|
610
|
+
return name !== null && accessors.has(name) ? name : null;
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
BinaryExpression(node) {
|
|
614
|
+
if (!EQ_OPS.has(node.operator)) return;
|
|
615
|
+
const literal = productLiteral(node.left) ?? productLiteral(node.right);
|
|
616
|
+
const accessor = accessorCall(node.left) ?? accessorCall(node.right);
|
|
617
|
+
if (literal === null && accessor === null) return;
|
|
618
|
+
const what = literal !== null ? `"${literal}"` : `${accessor}()`;
|
|
619
|
+
context.report({
|
|
620
|
+
node,
|
|
621
|
+
messageId: "productLiteral",
|
|
622
|
+
data: { what }
|
|
623
|
+
});
|
|
624
|
+
},
|
|
625
|
+
SwitchStatement(node) {
|
|
626
|
+
const accessor = accessorCall(node.discriminant);
|
|
627
|
+
for (const c of node.cases) {
|
|
628
|
+
const literal = c.test ? productLiteral(c.test) : null;
|
|
629
|
+
if (literal !== null) context.report({
|
|
630
|
+
node: c,
|
|
631
|
+
messageId: "productLiteral",
|
|
632
|
+
data: { what: `"${literal}"` }
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
if (accessor !== null && !node.cases.some((c) => c.test && productLiteral(c.test))) context.report({
|
|
636
|
+
node: node.discriminant,
|
|
637
|
+
messageId: "productLiteral",
|
|
638
|
+
data: { what: `${accessor}()` }
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
// src/plugin/rules/no-raw-fetch.ts
|
|
645
|
+
const RULE$4 = "no-raw-fetch";
|
|
646
|
+
const noRawFetch = {
|
|
647
|
+
meta: {
|
|
648
|
+
type: "problem",
|
|
649
|
+
docs: {
|
|
650
|
+
description: "Disallow the global fetch() outside the API client layer",
|
|
651
|
+
url: docsUrl(RULE$4)
|
|
652
|
+
},
|
|
653
|
+
schema: [{
|
|
654
|
+
type: "object",
|
|
655
|
+
properties: { client: { type: "string" } },
|
|
656
|
+
additionalProperties: false
|
|
657
|
+
}],
|
|
658
|
+
messages: { rawFetch: message(RULE$4, {
|
|
659
|
+
violation: "raw fetch() call outside the API client layer (no shared auth, base URL, error mapping or retries).",
|
|
660
|
+
fix: "call the endpoint through {{client}}; if the endpoint is missing there, add a typed function for it in the client first.",
|
|
661
|
+
exemption: CONFIG_EXEMPTION
|
|
662
|
+
}) }
|
|
663
|
+
},
|
|
664
|
+
create(context) {
|
|
665
|
+
const options = context.options[0] ?? {};
|
|
666
|
+
const client = options.client ? `the API client in ${options.client}` : "the typed API client";
|
|
667
|
+
return { CallExpression(node) {
|
|
668
|
+
const callee = unwrap(node.callee);
|
|
669
|
+
if (!callee) return;
|
|
670
|
+
if (callee.type === "Identifier" && callee.name === "fetch" && isGlobalName(context, callee, "fetch") || callee.type === "MemberExpression" && propertyName(callee) === "fetch" && isGlobalObject(context, callee.object)) context.report({
|
|
671
|
+
node,
|
|
672
|
+
messageId: "rawFetch",
|
|
673
|
+
data: { client }
|
|
674
|
+
});
|
|
675
|
+
} };
|
|
676
|
+
}
|
|
677
|
+
};
|
|
678
|
+
// src/plugin/rules/no-raw-storage.ts
|
|
679
|
+
const RULE$3 = "no-raw-storage";
|
|
680
|
+
const STORAGE_NAMES = /* @__PURE__ */ new Set(["localStorage", "sessionStorage"]);
|
|
681
|
+
const DECLARATION_PARENTS = /* @__PURE__ */ new Set([
|
|
682
|
+
"ImportSpecifier",
|
|
683
|
+
"ImportDefaultSpecifier",
|
|
684
|
+
"ImportNamespaceSpecifier",
|
|
685
|
+
"ExportSpecifier",
|
|
686
|
+
"TSPropertySignature",
|
|
687
|
+
"TSQualifiedName",
|
|
688
|
+
"LabeledStatement"
|
|
689
|
+
]);
|
|
690
|
+
const noRawStorage = {
|
|
691
|
+
meta: {
|
|
692
|
+
type: "problem",
|
|
693
|
+
docs: {
|
|
694
|
+
description: "Disallow localStorage / sessionStorage outside the storage wrapper",
|
|
695
|
+
url: docsUrl(RULE$3)
|
|
696
|
+
},
|
|
697
|
+
schema: [{
|
|
698
|
+
type: "object",
|
|
699
|
+
properties: { wrapper: { type: "string" } },
|
|
700
|
+
additionalProperties: false
|
|
701
|
+
}],
|
|
702
|
+
messages: { rawStorage: message(RULE$3, {
|
|
703
|
+
violation: "direct use of {{name}} bypasses the storage wrapper (no key prefixing, no quota or privacy-mode handling).",
|
|
704
|
+
fix: "read and write through {{wrapper}}; once products run in the shell, use the shell SDK storage capability, which prefixes keys per product.",
|
|
705
|
+
exemption: CONFIG_EXEMPTION
|
|
706
|
+
}) }
|
|
707
|
+
},
|
|
708
|
+
create(context) {
|
|
709
|
+
const options = context.options[0] ?? {};
|
|
710
|
+
const wrapper = options.wrapper ? `the wrapper in ${options.wrapper}` : "the repository's storage wrapper";
|
|
711
|
+
return { Identifier(node) {
|
|
712
|
+
if (!STORAGE_NAMES.has(node.name)) return;
|
|
713
|
+
const parent = node.parent;
|
|
714
|
+
if (!parent) return;
|
|
715
|
+
if (parent.type === "MemberExpression" && parent.property === node) {
|
|
716
|
+
if (parent.computed || !isGlobalObject(context, parent.object)) return;
|
|
717
|
+
} else {
|
|
718
|
+
if (DECLARATION_PARENTS.has(parent.type)) return;
|
|
719
|
+
if ((parent.type === "Property" || parent.type === "MethodDefinition" || parent.type === "PropertyDefinition") && parent.key === node && !(parent.type === "Property" && parent.shorthand)) return;
|
|
720
|
+
if (parent.type === "VariableDeclarator" && parent.id === node) return;
|
|
721
|
+
if ((parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") && parent.params.includes(node)) return;
|
|
722
|
+
if (isInTypePosition(node)) return;
|
|
723
|
+
if (!isGlobalName(context, node, node.name)) return;
|
|
724
|
+
}
|
|
725
|
+
context.report({
|
|
726
|
+
node,
|
|
727
|
+
messageId: "rawStorage",
|
|
728
|
+
data: {
|
|
729
|
+
name: node.name,
|
|
730
|
+
wrapper
|
|
731
|
+
}
|
|
732
|
+
});
|
|
733
|
+
} };
|
|
734
|
+
}
|
|
735
|
+
};
|
|
736
|
+
// src/plugin/rules/no-shell-imports-product.ts
|
|
737
|
+
const RULE$2 = "no-shell-imports-product";
|
|
738
|
+
function isTypeOnly(node) {
|
|
739
|
+
if (node.type === "ImportDeclaration") {
|
|
740
|
+
if (node.importKind === "type") return true;
|
|
741
|
+
return node.specifiers.length > 0 && node.specifiers.every((s) => s.type === "ImportSpecifier" && s.importKind === "type");
|
|
742
|
+
}
|
|
743
|
+
if (node.exportKind === "type") return true;
|
|
744
|
+
if (node.type === "ExportNamedDeclaration") return node.specifiers.length > 0 && node.specifiers.every((s) => s.exportKind === "type");
|
|
745
|
+
return false;
|
|
746
|
+
}
|
|
747
|
+
/**
|
|
748
|
+
* Aliases match whole path segments: `@` matches `@` and `@/x` but not the package `@scope/x`;
|
|
749
|
+
* a prefix that ends in `/` (`@/`) matches what starts with it.
|
|
750
|
+
*/
|
|
751
|
+
function aliasMatches(spec, prefix) {
|
|
752
|
+
if (prefix.endsWith("/")) return spec.startsWith(prefix);
|
|
753
|
+
return spec === prefix || spec.startsWith(`${prefix}/`);
|
|
754
|
+
}
|
|
755
|
+
const noShellImportsProduct = {
|
|
756
|
+
meta: {
|
|
757
|
+
type: "problem",
|
|
758
|
+
docs: {
|
|
759
|
+
description: "Disallow shell files from statically importing product directories",
|
|
760
|
+
url: docsUrl(RULE$2)
|
|
761
|
+
},
|
|
762
|
+
schema: [{
|
|
763
|
+
type: "object",
|
|
764
|
+
properties: {
|
|
765
|
+
shell: {
|
|
766
|
+
type: "array",
|
|
767
|
+
items: { type: "string" }
|
|
768
|
+
},
|
|
769
|
+
product: {
|
|
770
|
+
type: "array",
|
|
771
|
+
items: { type: "string" }
|
|
772
|
+
},
|
|
773
|
+
aliases: {
|
|
774
|
+
type: "object",
|
|
775
|
+
additionalProperties: { type: "string" }
|
|
776
|
+
}
|
|
777
|
+
},
|
|
778
|
+
additionalProperties: false
|
|
779
|
+
}],
|
|
780
|
+
messages: { shellImportsProduct: message(RULE$2, {
|
|
781
|
+
violation: "shell file imports product code from {{target}}.",
|
|
782
|
+
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.",
|
|
783
|
+
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)."
|
|
784
|
+
}) }
|
|
785
|
+
},
|
|
786
|
+
create(context) {
|
|
787
|
+
const options = context.options[0] ?? {};
|
|
788
|
+
if (!options.shell?.length || !options.product?.length) return {};
|
|
789
|
+
const isShell = compilePathPatterns(options.shell);
|
|
790
|
+
const isProduct = compilePathPatterns(options.product);
|
|
791
|
+
const rel = repoRelativePath(context);
|
|
792
|
+
if (!isShell(rel) || isProduct(rel)) return {};
|
|
793
|
+
const dir = path.posix.dirname(rel);
|
|
794
|
+
const aliases = Object.entries(options.aliases ?? {}).sort((a, b) => b[0].length - a[0].length);
|
|
795
|
+
function resolve(spec) {
|
|
796
|
+
if (spec.startsWith(".")) return path.posix.normalize(path.posix.join(dir, spec));
|
|
797
|
+
for (const [prefix, target] of aliases) if (aliasMatches(spec, prefix)) return path.posix.normalize(target + spec.slice(prefix.length));
|
|
798
|
+
return null;
|
|
799
|
+
}
|
|
800
|
+
function check(node) {
|
|
801
|
+
if (!node.source || typeof node.source.value !== "string" || isTypeOnly(node)) return;
|
|
802
|
+
const target = resolve(node.source.value);
|
|
803
|
+
if (target !== null && isProduct(target)) context.report({
|
|
804
|
+
node,
|
|
805
|
+
messageId: "shellImportsProduct",
|
|
806
|
+
data: { target }
|
|
807
|
+
});
|
|
808
|
+
}
|
|
809
|
+
return {
|
|
810
|
+
ImportDeclaration: check,
|
|
811
|
+
ExportNamedDeclaration: check,
|
|
812
|
+
ExportAllDeclaration: check
|
|
813
|
+
};
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
// src/plugin/rules/no-swallowed-catch.ts
|
|
817
|
+
const RULE$1 = "no-swallowed-catch";
|
|
818
|
+
const FAIL_OPEN = /^\s*fail-open:\s*\S/;
|
|
819
|
+
const DEFAULT_CLEANUP = [
|
|
820
|
+
"abort",
|
|
821
|
+
"cancel",
|
|
822
|
+
"close",
|
|
823
|
+
"destroy",
|
|
824
|
+
"disconnect",
|
|
825
|
+
"dispose",
|
|
826
|
+
"kill",
|
|
827
|
+
"release",
|
|
828
|
+
"removeItem",
|
|
829
|
+
"revokeObjectURL",
|
|
830
|
+
"rm",
|
|
831
|
+
"rmSync",
|
|
832
|
+
"rmdir",
|
|
833
|
+
"rmdirSync",
|
|
834
|
+
"terminate",
|
|
835
|
+
"unlink",
|
|
836
|
+
"unlinkSync"
|
|
837
|
+
];
|
|
838
|
+
/** `null`, `undefined`, `void 0`, `false`, `{}` or `[]`. */
|
|
839
|
+
function isEmptyValue(input) {
|
|
840
|
+
const node = unwrap(input);
|
|
841
|
+
if (!node) return true;
|
|
842
|
+
if (node.type === "Literal") return node.value === null || node.value === false;
|
|
843
|
+
if (node.type === "Identifier") return node.name === "undefined";
|
|
844
|
+
if (node.type === "UnaryExpression") return node.operator === "void";
|
|
845
|
+
if (node.type === "ObjectExpression") return node.properties.length === 0;
|
|
846
|
+
if (node.type === "ArrayExpression") return node.elements.length === 0;
|
|
847
|
+
return false;
|
|
848
|
+
}
|
|
849
|
+
function isSwallowingHandler(input) {
|
|
850
|
+
const fn = unwrap(input);
|
|
851
|
+
if (!fn || fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return false;
|
|
852
|
+
if (fn.body.type !== "BlockStatement") return isEmptyValue(fn.body);
|
|
853
|
+
const statements = fn.body.body;
|
|
854
|
+
if (statements.length === 0) return true;
|
|
855
|
+
const only = statements[0];
|
|
856
|
+
return statements.length === 1 && only?.type === "ReturnStatement" && isEmptyValue(only.argument);
|
|
857
|
+
}
|
|
858
|
+
const noSwallowedCatch = {
|
|
859
|
+
meta: {
|
|
860
|
+
type: "problem",
|
|
861
|
+
docs: {
|
|
862
|
+
description: "Disallow catch blocks and .catch() handlers that silently discard errors",
|
|
863
|
+
url: docsUrl(RULE$1)
|
|
864
|
+
},
|
|
865
|
+
schema: [{
|
|
866
|
+
type: "object",
|
|
867
|
+
properties: { cleanupCallees: {
|
|
868
|
+
type: "array",
|
|
869
|
+
items: { type: "string" }
|
|
870
|
+
} },
|
|
871
|
+
additionalProperties: false
|
|
872
|
+
}],
|
|
873
|
+
messages: { swallowed: message(RULE$1, {
|
|
874
|
+
violation: "{{what}} discards the error without logging, reporting or rethrowing it, so failures (including failed writes) vanish silently.",
|
|
875
|
+
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.",
|
|
876
|
+
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."
|
|
877
|
+
}) }
|
|
878
|
+
},
|
|
879
|
+
create(context) {
|
|
880
|
+
const options = context.options[0] ?? {};
|
|
881
|
+
const cleanup = new Set(options.cleanupCallees ?? DEFAULT_CLEANUP);
|
|
882
|
+
let failOpen = null;
|
|
883
|
+
function failOpenComments() {
|
|
884
|
+
failOpen ??= context.sourceCode.getAllComments().filter((c) => FAIL_OPEN.test(c.value));
|
|
885
|
+
return failOpen;
|
|
886
|
+
}
|
|
887
|
+
/** A `fail-open:` comment inside `inside`, or on `line` / the line before it. */
|
|
888
|
+
function hasFailOpen(inside, line) {
|
|
889
|
+
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);
|
|
890
|
+
}
|
|
891
|
+
function isCleanupCall(input) {
|
|
892
|
+
let node = unwrap(input);
|
|
893
|
+
if (node?.type === "AwaitExpression") node = unwrap(node.argument);
|
|
894
|
+
if (node?.type === "UnaryExpression" && node.operator === "void") node = unwrap(node.argument);
|
|
895
|
+
if (!node || node.type !== "CallExpression") return false;
|
|
896
|
+
const name = calleeName(node.callee);
|
|
897
|
+
return name !== null && cleanup.has(name);
|
|
898
|
+
}
|
|
899
|
+
return {
|
|
900
|
+
CatchClause(node) {
|
|
901
|
+
if (node.body.body.length > 0) return;
|
|
902
|
+
const tryStatement = node.parent;
|
|
903
|
+
if (tryStatement?.type === "TryStatement" && tryStatement.block.body.length > 0 && tryStatement.block.body.every((s) => s.type === "ExpressionStatement" && isCleanupCall(s.expression))) return;
|
|
904
|
+
if (hasFailOpen(node.body, node.loc.start.line)) return;
|
|
905
|
+
context.report({
|
|
906
|
+
node,
|
|
907
|
+
messageId: "swallowed",
|
|
908
|
+
data: { what: "this empty catch block" }
|
|
909
|
+
});
|
|
910
|
+
},
|
|
911
|
+
CallExpression(node) {
|
|
912
|
+
const callee = unwrap(node.callee);
|
|
913
|
+
if (!callee || callee.type !== "MemberExpression" || propertyName(callee) !== "catch") return;
|
|
914
|
+
const handler = node.arguments[0];
|
|
915
|
+
if (!handler || !isSwallowingHandler(handler)) return;
|
|
916
|
+
if (isCleanupCall(callee.object)) return;
|
|
917
|
+
if (hasFailOpen(handler, callee.property.loc.start.line)) return;
|
|
918
|
+
context.report({
|
|
919
|
+
node: handler,
|
|
920
|
+
messageId: "swallowed",
|
|
921
|
+
data: { what: "this .catch() handler" }
|
|
922
|
+
});
|
|
923
|
+
}
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
};
|
|
927
|
+
// src/plugin/rules/no-window-bus.ts
|
|
928
|
+
const RULE = "no-window-bus";
|
|
929
|
+
const LISTENER_METHODS = /* @__PURE__ */ new Set(["addEventListener", "removeEventListener"]);
|
|
930
|
+
const DOM_EVENT_NAME = /^[A-Za-z]+$/;
|
|
931
|
+
const noWindowBus = {
|
|
932
|
+
meta: {
|
|
933
|
+
type: "problem",
|
|
934
|
+
docs: {
|
|
935
|
+
description: "Disallow window/document as an untyped global event bus",
|
|
936
|
+
url: docsUrl(RULE)
|
|
937
|
+
},
|
|
938
|
+
schema: [{
|
|
939
|
+
type: "object",
|
|
940
|
+
properties: { allowEvents: {
|
|
941
|
+
type: "array",
|
|
942
|
+
items: { type: "string" }
|
|
943
|
+
} },
|
|
944
|
+
additionalProperties: false
|
|
945
|
+
}],
|
|
946
|
+
messages: { windowBus: message(RULE, {
|
|
947
|
+
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.",
|
|
948
|
+
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.",
|
|
949
|
+
exemption: `events declared in the registry can be listed in the rule's \`allowEvents\` option; ${CONFIG_EXEMPTION}`
|
|
950
|
+
}) }
|
|
951
|
+
},
|
|
952
|
+
create(context) {
|
|
953
|
+
const options = context.options[0] ?? {};
|
|
954
|
+
const allowed = new Set(options.allowEvents ?? []);
|
|
955
|
+
function isGlobalTarget(node) {
|
|
956
|
+
return isGlobalObject(context, node, ["document"]);
|
|
957
|
+
}
|
|
958
|
+
/** The event name when it is a string literal or a same-file string constant. */
|
|
959
|
+
function eventName(arg) {
|
|
960
|
+
const direct = staticString(arg);
|
|
961
|
+
if (direct !== null) return direct;
|
|
962
|
+
const id = unwrap(arg);
|
|
963
|
+
if (!id || id.type !== "Identifier") return null;
|
|
964
|
+
const def = findVariable(context, id, id.name)?.defs[0];
|
|
965
|
+
if (!def || def.node.type !== "VariableDeclarator") return null;
|
|
966
|
+
const declaration = def.node.parent;
|
|
967
|
+
if (declaration?.type !== "VariableDeclaration" || declaration.kind !== "const") return null;
|
|
968
|
+
return staticString(def.node.init);
|
|
969
|
+
}
|
|
970
|
+
function report(node, what) {
|
|
971
|
+
context.report({
|
|
972
|
+
node,
|
|
973
|
+
messageId: "windowBus",
|
|
974
|
+
data: { what }
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
return {
|
|
978
|
+
CallExpression(node) {
|
|
979
|
+
const callee = unwrap(node.callee);
|
|
980
|
+
if (!callee) return;
|
|
981
|
+
let method = null;
|
|
982
|
+
let target = "window";
|
|
983
|
+
if (callee.type === "Identifier") {
|
|
984
|
+
if (!isGlobalName(context, callee, callee.name)) return;
|
|
985
|
+
method = callee.name;
|
|
986
|
+
} else if (callee.type === "MemberExpression" && isGlobalTarget(callee.object)) {
|
|
987
|
+
method = propertyName(callee);
|
|
988
|
+
target = unwrap(callee.object).name;
|
|
989
|
+
}
|
|
990
|
+
if (method === "dispatchEvent") {
|
|
991
|
+
const event = unwrap(node.arguments[0]);
|
|
992
|
+
const name = event?.type === "NewExpression" ? eventName(event.arguments[0]) : null;
|
|
993
|
+
if (name !== null && allowed.has(name)) return;
|
|
994
|
+
report(node, `${target}.dispatchEvent()`);
|
|
995
|
+
} else if (method !== null && LISTENER_METHODS.has(method)) {
|
|
996
|
+
const name = eventName(node.arguments[0]);
|
|
997
|
+
if (name === null || DOM_EVENT_NAME.test(name) || allowed.has(name)) return;
|
|
998
|
+
report(node, `${target}.${method}("${name}")`);
|
|
999
|
+
}
|
|
1000
|
+
},
|
|
1001
|
+
NewExpression(node) {
|
|
1002
|
+
const callee = unwrap(node.callee);
|
|
1003
|
+
if (callee?.type !== "Identifier" || callee.name !== "CustomEvent") return;
|
|
1004
|
+
if (!isGlobalName(context, callee, "CustomEvent")) return;
|
|
1005
|
+
const name = eventName(node.arguments[0]);
|
|
1006
|
+
if (name !== null && allowed.has(name)) return;
|
|
1007
|
+
const parent = node.parent;
|
|
1008
|
+
if (parent?.type === "CallExpression" && parent.arguments[0] === node) {
|
|
1009
|
+
const outer = unwrap(parent.callee);
|
|
1010
|
+
if (outer?.type === "Identifier" && outer.name === "dispatchEvent" || outer?.type === "MemberExpression" && propertyName(outer) === "dispatchEvent" && isGlobalTarget(outer.object)) return;
|
|
1011
|
+
}
|
|
1012
|
+
report(node, "new CustomEvent()");
|
|
1013
|
+
}
|
|
1014
|
+
};
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
// src/plugin/index.ts
|
|
1018
|
+
/** Kept in sync with package.json by test/plugin.test.ts. */
|
|
1019
|
+
const VERSION = "0.1.0-alpha.0";
|
|
1020
|
+
const plugin = {
|
|
1021
|
+
meta: {
|
|
1022
|
+
name: "od",
|
|
1023
|
+
version: VERSION
|
|
1024
|
+
},
|
|
1025
|
+
rules: {
|
|
1026
|
+
"no-product-literal": noProductLiteral,
|
|
1027
|
+
"no-shell-imports-product": noShellImportsProduct,
|
|
1028
|
+
"no-locale-branch": noLocaleBranch,
|
|
1029
|
+
"no-raw-storage": noRawStorage,
|
|
1030
|
+
"no-swallowed-catch": noSwallowedCatch,
|
|
1031
|
+
"no-raw-fetch": noRawFetch,
|
|
1032
|
+
"no-api-path-literal": noApiPathLiteral,
|
|
1033
|
+
"no-window-bus": noWindowBus,
|
|
1034
|
+
"no-process-env": noProcessEnv,
|
|
1035
|
+
"no-error-message-match": noErrorMessageMatch,
|
|
1036
|
+
"no-cjk-literal": noCjkLiteral,
|
|
1037
|
+
"no-constant-flag": noConstantFlag
|
|
1038
|
+
}
|
|
1039
|
+
};
|
|
1040
|
+
export { VERSION, plugin as default };
|