@opendesignai/harness 0.1.0-alpha.0 → 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/README.md +17 -12
- package/dist/cli.mjs +1438 -95
- package/dist/eslint-comments.d.mts +17 -1
- package/dist/eslint-comments.mjs +134 -2
- package/dist/plugin.d.mts +20 -4
- package/dist/plugin.mjs +429 -59
- package/docs/adopting.md +84 -17
- package/docs/baseline-move.md +56 -0
- package/docs/clones.md +51 -0
- package/docs/decisions/0003-agent-harness.md +37 -0
- package/docs/hooks.md +128 -0
- package/docs/ratchet.md +25 -9
- package/docs/rules/no-api-path-literal.md +1 -1
- package/docs/rules/no-cjk-literal.md +1 -1
- package/docs/rules/no-error-message-match.md +11 -3
- package/docs/rules/no-locale-branch.md +9 -5
- package/docs/rules/no-process-env.md +11 -1
- package/docs/rules/no-product-literal.md +6 -0
- package/docs/rules/no-raw-storage.md +4 -2
- package/docs/rules/no-shell-imports-product.md +4 -2
- package/docs/rules/no-swallowed-catch.md +5 -4
- package/docs/size.md +1 -0
- package/docs/unused.md +76 -0
- package/package.json +6 -4
- package/presets/rules/base.json +5 -3
package/dist/plugin.mjs
CHANGED
|
@@ -61,6 +61,36 @@ function findVariable(context, node, name) {
|
|
|
61
61
|
}
|
|
62
62
|
return null;
|
|
63
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
|
+
}
|
|
64
94
|
/** True when `name` at `node` refers to the global binding (not a local, parameter or import). */
|
|
65
95
|
function isGlobalName(context, node, name) {
|
|
66
96
|
const variable = findVariable(context, node, name);
|
|
@@ -113,15 +143,42 @@ const TYPE_CONTAINERS = /* @__PURE__ */ new Set([
|
|
|
113
143
|
"TSInterfaceDeclaration",
|
|
114
144
|
"TSTypeAliasDeclaration",
|
|
115
145
|
"TSPropertySignature",
|
|
116
|
-
"TSIndexSignature"
|
|
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"
|
|
117
159
|
]);
|
|
118
|
-
/**
|
|
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
|
+
*/
|
|
119
168
|
function isInTypePosition(node) {
|
|
169
|
+
let child = node;
|
|
120
170
|
let current = node.parent;
|
|
171
|
+
let statement = false;
|
|
121
172
|
while (current) {
|
|
122
173
|
const type = current.type;
|
|
123
|
-
if (
|
|
124
|
-
if (
|
|
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;
|
|
125
182
|
current = current.parent;
|
|
126
183
|
}
|
|
127
184
|
return false;
|
|
@@ -347,6 +404,26 @@ const EQ_OPS$2 = /* @__PURE__ */ new Set([
|
|
|
347
404
|
"==",
|
|
348
405
|
"!="
|
|
349
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
|
+
}
|
|
350
427
|
function isRegex(node) {
|
|
351
428
|
const n = unwrap(node);
|
|
352
429
|
return n?.type === "Literal" && "regex" in n && n.regex !== void 0;
|
|
@@ -378,14 +455,27 @@ const noErrorMessageMatch = {
|
|
|
378
455
|
const idRe = compileRegExp(options.identifierPattern ?? DEFAULT_IDENTIFIERS, "i", "identifierPattern");
|
|
379
456
|
const errRe = compileRegExp(options.errorPattern ?? DEFAULT_ERROR_NAMES, "", "errorPattern");
|
|
380
457
|
const trackAliases = options.trackAliases !== false;
|
|
458
|
+
/** Simple declarators whose value is message text. */
|
|
381
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
|
+
}
|
|
382
472
|
/** An expression holding error message text. */
|
|
383
473
|
function isMessage(input) {
|
|
384
474
|
const node = unwrap(input);
|
|
385
475
|
if (!node) return false;
|
|
386
476
|
switch (node.type) {
|
|
387
477
|
case "MemberExpression": return propertyName(node) === "message";
|
|
388
|
-
case "Identifier": return idRe.test(node.name) ||
|
|
478
|
+
case "Identifier": return idRe.test(node.name) || isAlias(node);
|
|
389
479
|
case "CallExpression": {
|
|
390
480
|
const callee = unwrap(node.callee);
|
|
391
481
|
if (callee?.type === "MemberExpression" && TRANSFORMS.has(propertyName(callee) ?? "")) return isMessage(callee.object);
|
|
@@ -393,7 +483,7 @@ const noErrorMessageMatch = {
|
|
|
393
483
|
const arg = unwrap(node.arguments[0]);
|
|
394
484
|
return arg?.type === "Identifier" && errRe.test(arg.name) || isMessage(arg);
|
|
395
485
|
}
|
|
396
|
-
return
|
|
486
|
+
return callee !== null && MESSAGE_GETTER.test(calleeName(callee) ?? "");
|
|
397
487
|
}
|
|
398
488
|
case "LogicalExpression": return isMessage(node.left);
|
|
399
489
|
case "ConditionalExpression": return isMessage(node.consequent) || isMessage(node.alternate);
|
|
@@ -407,21 +497,49 @@ const noErrorMessageMatch = {
|
|
|
407
497
|
});
|
|
408
498
|
}
|
|
409
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
|
+
}
|
|
410
519
|
return {
|
|
411
520
|
VariableDeclarator(node) {
|
|
412
|
-
if (trackAliases && node.id.type === "Identifier" &&
|
|
521
|
+
if (trackAliases && node.id.type === "Identifier" && node.init) declarators.push(node);
|
|
413
522
|
},
|
|
414
523
|
CallExpression(node) {
|
|
415
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]);
|
|
416
528
|
},
|
|
417
529
|
BinaryExpression(node) {
|
|
418
530
|
if (EQ_OPS$2.has(node.operator)) pending.push(node);
|
|
419
531
|
},
|
|
420
532
|
"Program:exit"() {
|
|
533
|
+
let grew = true;
|
|
534
|
+
while (grew) grew = collect();
|
|
421
535
|
for (const node of pending) {
|
|
422
536
|
if (node.type === "BinaryExpression") {
|
|
423
537
|
const left = node.left;
|
|
424
|
-
|
|
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);
|
|
425
543
|
continue;
|
|
426
544
|
}
|
|
427
545
|
if (node.type !== "CallExpression") continue;
|
|
@@ -445,12 +563,25 @@ const PREDICATE_METHODS = /* @__PURE__ */ new Set([
|
|
|
445
563
|
"endsWith",
|
|
446
564
|
"includes"
|
|
447
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"]);
|
|
448
568
|
const EQ_OPS$1 = /* @__PURE__ */ new Set([
|
|
449
569
|
"===",
|
|
450
570
|
"!==",
|
|
451
571
|
"==",
|
|
452
572
|
"!="
|
|
453
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
|
+
}
|
|
454
585
|
function isStringish(node) {
|
|
455
586
|
if (node.type === "Literal" && typeof node.value === "string") return node.value.trim() !== "";
|
|
456
587
|
if (node.type === "TemplateLiteral") return node.quasis.some((q) => (q.value.cooked ?? q.value.raw).trim() !== "");
|
|
@@ -483,22 +614,113 @@ const noLocaleBranch = {
|
|
|
483
614
|
const trackAliases = options.trackAliases !== false;
|
|
484
615
|
const aliasRe = compileRegExp(options.aliasPattern ?? DEFAULT_ALIAS_PATTERN, "", "aliasPattern");
|
|
485
616
|
const visitorKeys = context.sourceCode.visitorKeys;
|
|
617
|
+
/** Declarators of variables that hold a locale test (`const zh = locale.startsWith("zh")`). */
|
|
486
618
|
const aliases = /* @__PURE__ */ new Set();
|
|
487
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
|
+
}
|
|
488
626
|
function mentionsLocale(node) {
|
|
489
|
-
return someNode(node, visitorKeys, (n) => n.type === "Identifier" && (n.name === "locale" || aliasRe.test(n.name) ||
|
|
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);
|
|
490
710
|
}
|
|
491
711
|
function isLocaleTest(input) {
|
|
492
712
|
const node = unwrap(input);
|
|
493
713
|
if (!node) return false;
|
|
494
|
-
if (node.type === "Identifier") return aliasRe.test(node.name) ||
|
|
714
|
+
if (node.type === "Identifier") return aliasRe.test(node.name) || isAlias(node);
|
|
495
715
|
if (node.type === "UnaryExpression" && node.operator === "!") return isLocaleTest(node.argument);
|
|
496
716
|
if (node.type === "LogicalExpression") return isLocaleTest(node.left) || isLocaleTest(node.right);
|
|
497
|
-
|
|
717
|
+
if (node.type === "BinaryExpression") return EQ_OPS$1.has(node.operator) && comparesWithFixed(node);
|
|
718
|
+
return isCallOf(node, PREDICATE_METHODS) && matchesFixed(node);
|
|
498
719
|
}
|
|
720
|
+
const declarators = [];
|
|
499
721
|
return {
|
|
500
722
|
VariableDeclarator(node) {
|
|
501
|
-
if (trackAliases && node.id.type === "Identifier" &&
|
|
723
|
+
if (trackAliases && node.id.type === "Identifier" && node.init) declarators.push(node);
|
|
502
724
|
},
|
|
503
725
|
ConditionalExpression(node) {
|
|
504
726
|
candidates.push([node, [node.consequent, node.alternate]]);
|
|
@@ -509,6 +731,14 @@ const noLocaleBranch = {
|
|
|
509
731
|
candidates.push([node, branches]);
|
|
510
732
|
},
|
|
511
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
|
+
}
|
|
512
742
|
for (const [node, branches] of candidates) {
|
|
513
743
|
const test = node.test;
|
|
514
744
|
if (!isLocaleTest(test)) continue;
|
|
@@ -524,6 +754,28 @@ const noLocaleBranch = {
|
|
|
524
754
|
};
|
|
525
755
|
// src/plugin/rules/no-process-env.ts
|
|
526
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
|
+
}
|
|
527
779
|
const noProcessEnv = {
|
|
528
780
|
meta: {
|
|
529
781
|
type: "problem",
|
|
@@ -545,17 +797,49 @@ const noProcessEnv = {
|
|
|
545
797
|
create(context) {
|
|
546
798
|
const options = context.options[0] ?? {};
|
|
547
799
|
const registry = options.registry ? `the env registry (${options.registry})` : "the env registry module";
|
|
548
|
-
|
|
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;
|
|
800
|
+
function report(node) {
|
|
553
801
|
context.report({
|
|
554
802
|
node,
|
|
555
803
|
messageId: "processEnv",
|
|
556
804
|
data: { registry }
|
|
557
805
|
});
|
|
558
|
-
}
|
|
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
|
+
};
|
|
559
843
|
}
|
|
560
844
|
};
|
|
561
845
|
// src/plugin/rules/no-product-literal.ts
|
|
@@ -678,6 +962,7 @@ const noRawFetch = {
|
|
|
678
962
|
// src/plugin/rules/no-raw-storage.ts
|
|
679
963
|
const RULE$3 = "no-raw-storage";
|
|
680
964
|
const STORAGE_NAMES = /* @__PURE__ */ new Set(["localStorage", "sessionStorage"]);
|
|
965
|
+
/** Parents of an identifier that is a name, not a reference to the global. */
|
|
681
966
|
const DECLARATION_PARENTS = /* @__PURE__ */ new Set([
|
|
682
967
|
"ImportSpecifier",
|
|
683
968
|
"ImportDefaultSpecifier",
|
|
@@ -685,8 +970,26 @@ const DECLARATION_PARENTS = /* @__PURE__ */ new Set([
|
|
|
685
970
|
"ExportSpecifier",
|
|
686
971
|
"TSPropertySignature",
|
|
687
972
|
"TSQualifiedName",
|
|
688
|
-
"
|
|
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"
|
|
689
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
|
+
}
|
|
690
993
|
const noRawStorage = {
|
|
691
994
|
meta: {
|
|
692
995
|
type: "problem",
|
|
@@ -708,29 +1011,61 @@ const noRawStorage = {
|
|
|
708
1011
|
create(context) {
|
|
709
1012
|
const options = context.options[0] ?? {};
|
|
710
1013
|
const wrapper = options.wrapper ? `the wrapper in ${options.wrapper}` : "the repository's storage wrapper";
|
|
711
|
-
|
|
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
|
-
}
|
|
1014
|
+
function report(node, name) {
|
|
725
1015
|
context.report({
|
|
726
1016
|
node,
|
|
727
1017
|
messageId: "rawStorage",
|
|
728
1018
|
data: {
|
|
729
|
-
name
|
|
1019
|
+
name,
|
|
730
1020
|
wrapper
|
|
731
1021
|
}
|
|
732
1022
|
});
|
|
733
|
-
}
|
|
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
|
+
};
|
|
734
1069
|
}
|
|
735
1070
|
};
|
|
736
1071
|
// src/plugin/rules/no-shell-imports-product.ts
|
|
@@ -752,6 +1087,15 @@ function aliasMatches(spec, prefix) {
|
|
|
752
1087
|
if (prefix.endsWith("/")) return spec.startsWith(prefix);
|
|
753
1088
|
return spec === prefix || spec.startsWith(`${prefix}/`);
|
|
754
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
|
+
}
|
|
755
1099
|
const noShellImportsProduct = {
|
|
756
1100
|
meta: {
|
|
757
1101
|
type: "problem",
|
|
@@ -786,20 +1130,20 @@ const noShellImportsProduct = {
|
|
|
786
1130
|
create(context) {
|
|
787
1131
|
const options = context.options[0] ?? {};
|
|
788
1132
|
if (!options.shell?.length || !options.product?.length) return {};
|
|
789
|
-
const isShell = compilePathPatterns(options.shell);
|
|
790
|
-
const isProduct = compilePathPatterns(options.product);
|
|
1133
|
+
const isShell = compilePathPatterns(options.shell.map(repoPattern));
|
|
1134
|
+
const isProduct = compilePathPatterns(options.product.map(repoPattern));
|
|
791
1135
|
const rel = repoRelativePath(context);
|
|
792
1136
|
if (!isShell(rel) || isProduct(rel)) return {};
|
|
793
1137
|
const dir = path.posix.dirname(rel);
|
|
794
1138
|
const aliases = Object.entries(options.aliases ?? {}).sort((a, b) => b[0].length - a[0].length);
|
|
795
1139
|
function resolve(spec) {
|
|
796
|
-
if (spec.startsWith(".")) return path.posix.
|
|
797
|
-
for (const [prefix, target] of aliases) if (aliasMatches(spec, prefix)) return path.posix.
|
|
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));
|
|
798
1142
|
return null;
|
|
799
1143
|
}
|
|
800
1144
|
function check(node) {
|
|
801
1145
|
if (!node.source || typeof node.source.value !== "string" || isTypeOnly(node)) return;
|
|
802
|
-
const target = resolve(node.source.value);
|
|
1146
|
+
const target = resolve(withoutQuery(node.source.value));
|
|
803
1147
|
if (target !== null && isProduct(target)) context.report({
|
|
804
1148
|
node,
|
|
805
1149
|
messageId: "shellImportsProduct",
|
|
@@ -835,25 +1179,50 @@ const DEFAULT_CLEANUP = [
|
|
|
835
1179
|
"unlink",
|
|
836
1180
|
"unlinkSync"
|
|
837
1181
|
];
|
|
838
|
-
/**
|
|
839
|
-
|
|
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) {
|
|
840
1187
|
const node = unwrap(input);
|
|
841
1188
|
if (!node) return true;
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
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;
|
|
848
1211
|
}
|
|
849
1212
|
function isSwallowingHandler(input) {
|
|
850
1213
|
const fn = unwrap(input);
|
|
851
1214
|
if (!fn || fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression") return false;
|
|
852
|
-
if (fn.body.type !== "BlockStatement") return
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
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;
|
|
857
1226
|
}
|
|
858
1227
|
const noSwallowedCatch = {
|
|
859
1228
|
meta: {
|
|
@@ -898,7 +1267,7 @@ const noSwallowedCatch = {
|
|
|
898
1267
|
}
|
|
899
1268
|
return {
|
|
900
1269
|
CatchClause(node) {
|
|
901
|
-
if (node.body
|
|
1270
|
+
if (swallows(node.body) !== "empty") return;
|
|
902
1271
|
const tryStatement = node.parent;
|
|
903
1272
|
if (tryStatement?.type === "TryStatement" && tryStatement.block.body.length > 0 && tryStatement.block.body.every((s) => s.type === "ExpressionStatement" && isCleanupCall(s.expression))) return;
|
|
904
1273
|
if (hasFailOpen(node.body, node.loc.start.line)) return;
|
|
@@ -909,16 +1278,17 @@ const noSwallowedCatch = {
|
|
|
909
1278
|
});
|
|
910
1279
|
},
|
|
911
1280
|
CallExpression(node) {
|
|
1281
|
+
const found = rejectionHandler(node);
|
|
1282
|
+
if (!found) return;
|
|
1283
|
+
const [handler, what] = found;
|
|
1284
|
+
if (!isSwallowingHandler(handler)) return;
|
|
912
1285
|
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
1286
|
if (isCleanupCall(callee.object)) return;
|
|
917
1287
|
if (hasFailOpen(handler, callee.property.loc.start.line)) return;
|
|
918
1288
|
context.report({
|
|
919
1289
|
node: handler,
|
|
920
1290
|
messageId: "swallowed",
|
|
921
|
-
data: { what
|
|
1291
|
+
data: { what }
|
|
922
1292
|
});
|
|
923
1293
|
}
|
|
924
1294
|
};
|
|
@@ -1016,7 +1386,7 @@ const noWindowBus = {
|
|
|
1016
1386
|
};
|
|
1017
1387
|
// src/plugin/index.ts
|
|
1018
1388
|
/** Kept in sync with package.json by test/plugin.test.ts. */
|
|
1019
|
-
const VERSION = "0.
|
|
1389
|
+
const VERSION = "0.2.0-alpha.0";
|
|
1020
1390
|
const plugin = {
|
|
1021
1391
|
meta: {
|
|
1022
1392
|
name: "od",
|