@autotelic/plumb 0.3.1

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.
Files changed (94) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +64 -0
  3. package/package.json +41 -0
  4. package/src/effect/index.ts +59 -0
  5. package/src/effect/rules/guarded-op-must-return-effect.ts +236 -0
  6. package/src/effect/rules/guarded-op-must-return-option.ts +97 -0
  7. package/src/effect/rules/no-cascading-layer-provide.ts +80 -0
  8. package/src/effect/rules/no-decode-unknown-option.ts +41 -0
  9. package/src/effect/rules/no-direct-browser-storage.ts +91 -0
  10. package/src/effect/rules/no-direct-fetch.ts +44 -0
  11. package/src/effect/rules/no-manual-field-guards.ts +144 -0
  12. package/src/effect/rules/no-nested-layer-provide.ts +51 -0
  13. package/src/effect/rules/no-schema-class-modeling.ts +59 -0
  14. package/src/effect/rules/no-service-constructor-imports.ts +50 -0
  15. package/src/effect/rules/no-silent-error-swallow.ts +99 -0
  16. package/src/effect/rules/no-static-effect-service-forwarders.ts +227 -0
  17. package/src/effect/rules/no-try-catch.ts +32 -0
  18. package/src/effect/rules/no-unguarded-json-parse.ts +211 -0
  19. package/src/effect/rules/option-core-needs-effect-public.ts +81 -0
  20. package/src/effect/rules/prefer-die-for-precondition-defects.ts +118 -0
  21. package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +76 -0
  22. package/src/effect/rules/prefer-effect-match.ts +78 -0
  23. package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +79 -0
  24. package/src/effect/rules/prefer-named-guard-predicate.ts +105 -0
  25. package/src/effect/rules/prefer-option-pipeline.ts +95 -0
  26. package/src/effect/rules/prefer-ordering-match.ts +152 -0
  27. package/src/effect/rules/prefer-tagged-enum.ts +68 -0
  28. package/src/effect/rules/require-schema-type-derivation.ts +83 -0
  29. package/src/index.ts +106 -0
  30. package/src/meta/index.ts +25 -0
  31. package/src/meta/rules/no-disable-directives.ts +38 -0
  32. package/src/meta/rules/no-manual-ancestor-walks.ts +27 -0
  33. package/src/meta/rules/prefer-before-file-scope.ts +57 -0
  34. package/src/meta/rules/require-create-once.ts +51 -0
  35. package/src/meta/rules/require-rule-tester.ts +57 -0
  36. package/src/rules/no-anonymous-wide-tuples.ts +38 -0
  37. package/src/rules/no-barrel-export-star.ts +30 -0
  38. package/src/rules/no-boolean-field-signals.ts +305 -0
  39. package/src/rules/no-builtin-throws.ts +83 -0
  40. package/src/rules/no-chained-type-assertions.ts +65 -0
  41. package/src/rules/no-conditional-empty-object-spread.ts +42 -0
  42. package/src/rules/no-duplicated-literal-union.ts +66 -0
  43. package/src/rules/no-exported-mutable-state.ts +111 -0
  44. package/src/rules/no-generic-export-names.ts +149 -0
  45. package/src/rules/no-impossible-branch-throw.ts +55 -0
  46. package/src/rules/no-known-value-widening.ts +225 -0
  47. package/src/rules/no-module-mocking.ts +95 -0
  48. package/src/rules/no-multiple-function-params.ts +192 -0
  49. package/src/rules/no-nondeterministic-core.ts +73 -0
  50. package/src/rules/no-object-parameters.ts +127 -0
  51. package/src/rules/no-optional-function-parameters.ts +49 -0
  52. package/src/rules/no-product-of-state-booleans.ts +80 -0
  53. package/src/rules/no-redundant-derived-field.ts +192 -0
  54. package/src/rules/no-reflect-apply.ts +32 -0
  55. package/src/rules/no-reflect-get.ts +32 -0
  56. package/src/rules/no-reinterpret-cast.ts +78 -0
  57. package/src/rules/no-runtime-typeof.ts +59 -0
  58. package/src/rules/no-sentinel-comparison-union.ts +148 -0
  59. package/src/rules/no-shape-in-symbol-names.ts +45 -0
  60. package/src/rules/no-single-use-private-functions.ts +329 -0
  61. package/src/rules/no-stacked-jsdoc-blocks.ts +65 -0
  62. package/src/rules/no-stray-inline-comments.ts +79 -0
  63. package/src/rules/no-swappable-primitive-params.ts +109 -0
  64. package/src/rules/no-tag-ladder-assertions.ts +71 -0
  65. package/src/rules/no-transposed-field-reads.ts +135 -0
  66. package/src/rules/no-unit-return-validators.ts +65 -0
  67. package/src/rules/no-unknown-parameters.ts +97 -0
  68. package/src/rules/no-unknown-returns.ts +120 -0
  69. package/src/rules/no-unknown-type-aliases.ts +70 -0
  70. package/src/rules/no-unsafe-dictionary-type.ts +132 -0
  71. package/src/rules/no-vague-test-filenames.ts +67 -0
  72. package/src/rules/no-widen-then-assert.ts +345 -0
  73. package/src/rules/prefer-payload-brand.ts +67 -0
  74. package/src/rules/prefer-property-tests.ts +43 -0
  75. package/src/rules/require-canonical-stringify-for-identity.ts +80 -0
  76. package/src/rules/require-deprecated-tag-for-legacy-comments.ts +45 -0
  77. package/src/rules/require-exhaustive-tag-switch.ts +42 -0
  78. package/src/rules/require-fc-block-predicate.ts +42 -0
  79. package/src/rules/require-jsdoc-on-exported.ts +74 -0
  80. package/src/rules/require-published-order.ts +109 -0
  81. package/src/rules/require-safety-comment-for-type-assertion.ts +61 -0
  82. package/src/rules/require-sort-comparator.ts +41 -0
  83. package/src/rules/test-utils.ts +68 -0
  84. package/src/shared/ancestors.ts +21 -0
  85. package/src/shared/child-nodes.ts +27 -0
  86. package/src/shared/dictionary-types.ts +524 -0
  87. package/src/shared/is-node.ts +13 -0
  88. package/src/shared/lexical-type-parameters.ts +62 -0
  89. package/src/shared/reflect-method.ts +41 -0
  90. package/src/shared/rule-options.ts +23 -0
  91. package/src/shared/structural.ts +81 -0
  92. package/src/testing/index.ts +3 -0
  93. package/src/testing/testable-rule.test.ts +68 -0
  94. package/src/testing/testable-rule.ts +86 -0
@@ -0,0 +1,192 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+ import { isRecordObject, isString } from "../shared/structural.ts";
5
+
6
+ const COUNT_SUFFIX = /(?:Count|Length|Size)$/u;
7
+ const COUNT_PREFIX = /^(?:count|num|numberOf|total)(?=[A-Z])/u;
8
+ const KEYED_COLLECTION_NAMES = new Set(["Array", "Map", "Record", "ReadonlyArray", "ReadonlyMap", "ReadonlySet", "Set"]);
9
+
10
+ function lastTypeNameSegment(typeName: ESTree.Node): string | null {
11
+ if (typeName.type === "Identifier") return typeName.name;
12
+ if (typeName.type === "TSQualifiedName") return lastTypeNameSegment(typeName.right);
13
+ return null;
14
+ }
15
+
16
+ function propertyName(member: ESTree.TSSignature): string | null {
17
+ if (member.type !== "TSPropertySignature") return null;
18
+ const key = member.key;
19
+ return key.type === "Identifier" ? key.name : null;
20
+ }
21
+
22
+ function propertyAnnotation(member: ESTree.TSSignature): ESTree.TSType | null | undefined {
23
+ if (member.type !== "TSPropertySignature") return null;
24
+ return member.typeAnnotation?.typeAnnotation ?? null;
25
+ }
26
+
27
+ /** Lowercase, strip separators, and naive-singularise so `seatOrder`/`seats`, `entryIndex`/`entries` stems can be compared.
28
+ *
29
+ * @param {string} name - The field or property name to fold.
30
+ * @returns {string} The comparison stem.
31
+ */
32
+ function normaliseName(name: string): string {
33
+ const folded = name.replaceAll(/[^a-zA-Z0-9]/gu, "").toLowerCase();
34
+ if (folded.endsWith("ies") && folded.length > 4) return `${folded.slice(0, -3)}y`;
35
+ return folded.endsWith("s") && folded.length > 3 ? folded.slice(0, -1) : folded;
36
+ }
37
+
38
+ /** Extract the `x` from `xCount`, `countX`, `numberOfX`, … otherwise null.
39
+ *
40
+ * @param {string} name - The candidate count-field name.
41
+ * @returns {string | null} The counted stem, or null when the name isn't a count pattern.
42
+ */
43
+ function countStem(name: string): string | null {
44
+ const suffixMatch = name.match(COUNT_SUFFIX);
45
+ if (suffixMatch !== null && suffixMatch.index !== null) return name.slice(0, suffixMatch.index);
46
+ const prefixMatch = name.match(COUNT_PREFIX);
47
+ if (prefixMatch !== null) {
48
+ const stem = name.slice(prefixMatch[0].length);
49
+ return stem.charAt(0).toLowerCase() + stem.slice(1);
50
+ }
51
+ return null;
52
+ }
53
+
54
+ /** Whether the annotation is any array/map/set/record shape.
55
+ *
56
+ * @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
57
+ * @returns {boolean} True when the annotation denotes a keyed collection.
58
+ */
59
+ function isCollectionLike(annotation: ESTree.TSType | null | undefined): boolean {
60
+ if (annotation === null || annotation === undefined) return false;
61
+ if (annotation.type === "TSTypeReference") {
62
+ const name = lastTypeNameSegment(annotation.typeName);
63
+ return name !== null && KEYED_COLLECTION_NAMES.has(name);
64
+ }
65
+ return annotation.type === "TSTypeOperator" && annotation.operator === "readonly"
66
+ ? isCollectionLike(annotation.typeAnnotation)
67
+ : annotation.type === "TSArrayType";
68
+ }
69
+
70
+ /** Whether the annotation is an array (or readonly array reference) whose element is a primitive keyword.
71
+ *
72
+ * @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
73
+ * @returns {"string" | "number" | null} The primitive keyword of the element type, or null.
74
+ */
75
+ function arrayOfPrimitiveKeys(annotation: ESTree.TSType | null | undefined): "string" | "number" | null {
76
+ if (annotation === null || annotation === undefined) return null;
77
+ let elementType: ESTree.TSType | undefined;
78
+ if (annotation.type === "TSArrayType") {
79
+ elementType = annotation.elementType;
80
+ } else if (
81
+ annotation.type === "TSTypeOperator" &&
82
+ annotation.operator === "readonly" &&
83
+ annotation.typeAnnotation.type === "TSArrayType"
84
+ ) {
85
+ elementType = annotation.typeAnnotation.elementType;
86
+ } else if (
87
+ annotation.type === "TSTypeReference" &&
88
+ annotation.typeArguments != null &&
89
+ annotation.typeArguments.params.length === 1
90
+ ) {
91
+ const name = lastTypeNameSegment(annotation.typeName);
92
+ if (name === "Array" || name === "ReadonlyArray") elementType = annotation.typeArguments.params[0];
93
+ }
94
+ if (elementType === undefined) return null;
95
+ if (elementType.type === "TSStringKeyword") return "string";
96
+ if (elementType.type === "TSNumberKeyword") return "number";
97
+ return null;
98
+ }
99
+
100
+ /** First type argument's primitive keyword for a keyed collection like `Record<string, T>` / `Map<string, T>`.
101
+ *
102
+ * @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
103
+ * @returns {"string" | "number" | null} The key keyword, or null when not a keyed reference.
104
+ */
105
+ function keyKeywordOf(annotation: ESTree.TSType | null | undefined): "string" | "number" | null {
106
+ if (annotation?.type !== "TSTypeReference") return null;
107
+ const first = annotation.typeArguments?.params[0] ?? null;
108
+ if (first === null || first === undefined) return null;
109
+ if (first.type === "TSStringKeyword") return "string";
110
+ if (first.type === "TSNumberKeyword") return "number";
111
+ return null;
112
+ }
113
+
114
+ /**
115
+ * One fact, one place: a type that stores data *and* a field derivable from
116
+ * that data keeps two sources of truth that drift apart independently. Counts
117
+ * belong to read sites; ordering belongs in the structure itself.
118
+ */
119
+ export const noRedundantDerivedFieldRule = defineRule({
120
+ meta: {
121
+ type: "suggestion",
122
+ docs: {
123
+ description:
124
+ "Disallow fields that duplicate information derivable from a sibling field on the same type (derived counts, parallel key/order arrays); compute or encode them instead.",
125
+ },
126
+ messages: {
127
+ redundantCount:
128
+ "`{{name}}` restates what is derivable from `{{sibling}}`, giving the type two sources of truth that can drift. Drop it and compute the count at read time.",
129
+ parallelKeyArray:
130
+ "`{{name}}` mirrors the keys of `{{sibling}}` as a second source of truth that can drift. Encode the canonical form (e.g. ordering) in the collection itself instead of a parallel array.",
131
+ },
132
+ },
133
+ createOnce(context) {
134
+ function checkMembers(members: ESTree.TSSignature[]): void {
135
+ for (const member of members) {
136
+ const name = propertyName(member);
137
+ if (name === null) continue;
138
+ const annotation = propertyAnnotation(member);
139
+
140
+ const stem = countStem(name);
141
+ if (stem !== null && annotation?.type === "TSNumberKeyword") {
142
+ const stemNorm = normaliseName(stem);
143
+ const sibling = members.find((other) => {
144
+ if (other === member) return false;
145
+ const otherName = propertyName(other);
146
+ return otherName !== null && normaliseName(otherName) === stemNorm && isCollectionLike(propertyAnnotation(other));
147
+ });
148
+ if (sibling !== undefined) {
149
+ context.report({
150
+ node: member,
151
+ messageId: "redundantCount",
152
+ data: { name, sibling: propertyName(sibling) ?? "" },
153
+ });
154
+ continue;
155
+ }
156
+ }
157
+
158
+ const elementKey = arrayOfPrimitiveKeys(annotation);
159
+ if (elementKey === null) continue;
160
+ const nameNorm = normaliseName(name);
161
+ const sibling = members.find((other) => {
162
+ if (other === member) return false;
163
+ const otherName = propertyName(other);
164
+ if (otherName === null) return false;
165
+ if (keyKeywordOf(propertyAnnotation(other)) !== elementKey) return false;
166
+ const otherNorm = normaliseName(otherName);
167
+ return otherNorm.includes(nameNorm) || nameNorm.includes(otherNorm);
168
+ });
169
+ if (sibling !== undefined) {
170
+ context.report({
171
+ node: member,
172
+ messageId: "parallelKeyArray",
173
+ data: { name, sibling: propertyName(sibling) ?? "" },
174
+ });
175
+ }
176
+ }
177
+ }
178
+
179
+ function memberList(bodyNode: ESTree.TSInterfaceBody): ESTree.TSSignature[] {
180
+ return [...bodyNode.body];
181
+ }
182
+
183
+ return {
184
+ TSInterfaceDeclaration(node) {
185
+ checkMembers(memberList(node.body));
186
+ },
187
+ TSTypeAliasDeclaration(node) {
188
+ if (node.typeAnnotation.type === "TSTypeLiteral") checkMembers(node.typeAnnotation.members);
189
+ },
190
+ };
191
+ },
192
+ });
@@ -0,0 +1,32 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { isGlobalReflectMethodCall } from "../shared/reflect-method.ts";
4
+
5
+ /** Ban Reflect.apply, which bypasses ordinary typed function calls. */
6
+ export const noReflectApplyRule = defineRule({
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description:
11
+ "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface.",
12
+ },
13
+ messages: {
14
+ reflectApply:
15
+ "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface.",
16
+ },
17
+ },
18
+ createOnce(context) {
19
+ return {
20
+ CallExpression(node) {
21
+ if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
22
+ if (isGlobalReflectMethodCall({
23
+ sourceCode: context.sourceCode,
24
+ callee: node.callee,
25
+ methodName: "apply",
26
+ })) {
27
+ context.report({ node, messageId: "reflectApply" });
28
+ }
29
+ },
30
+ };
31
+ },
32
+ });
@@ -0,0 +1,32 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import { isGlobalReflectMethodCall } from "../shared/reflect-method.ts";
4
+
5
+ /** Ban Reflect.get, which bypasses ordinary property access and useful type evidence. */
6
+ export const noReflectGetRule = defineRule({
7
+ meta: {
8
+ type: "problem",
9
+ docs: {
10
+ description:
11
+ "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type.",
12
+ },
13
+ messages: {
14
+ reflectGet:
15
+ "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it.",
16
+ },
17
+ },
18
+ createOnce(context) {
19
+ return {
20
+ CallExpression(node) {
21
+ if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
22
+ if (isGlobalReflectMethodCall({
23
+ sourceCode: context.sourceCode,
24
+ callee: node.callee,
25
+ methodName: "get",
26
+ })) {
27
+ context.report({ node, messageId: "reflectGet" });
28
+ }
29
+ },
30
+ };
31
+ },
32
+ });
@@ -0,0 +1,78 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+
5
+ interface CommentLike {
6
+ value: string;
7
+ }
8
+
9
+ /** Whether a function-like body consists solely of returning an as-cast.
10
+ *
11
+ * @param {ESTree.Node | null | undefined} body - The function body to inspect.
12
+ * @returns {boolean} True when the body only returns an as-cast expression.
13
+ */
14
+ function isSoleReturnOfAssertion(body: ESTree.Node | null | undefined): boolean {
15
+ if (body === null || body === undefined) return false;
16
+ if (body.type === "BlockStatement") {
17
+ if (body.body.length !== 1) return false;
18
+ const only = body.body[0];
19
+ return only?.type === "ReturnStatement" && only.argument?.type === "TSAsExpression";
20
+ }
21
+ return body.type === "TSAsExpression";
22
+ }
23
+
24
+ /** Whether the assertion node has an adjacent `// SAFETY:` justification.
25
+ *
26
+ * @param {{ getCommentsBefore(n: ESTree.Node): Array<CommentLike> }} sourceCode - The rule's source-code accessor.
27
+ * @param {ESTree.Node} node - The assertion node.
28
+ * @returns {boolean} True when a SAFETY comment precedes the assertion.
29
+ */
30
+ function hasAdjacentSafetyComment(sourceCode: { getCommentsBefore(n: ESTree.Node): Array<CommentLike> }, node: ESTree.Node): boolean {
31
+ return sourceCode.getCommentsBefore(node).some((comment) => /\bSAFETY\s*:/u.test(comment.value));
32
+ }
33
+
34
+ /** Type assertions erase evidence; `as const` is exempt and `// SAFETY:` documents rare verified invariants. */
35
+ export const noReinterpretCastRule = defineRule({
36
+ meta: {
37
+ type: "problem",
38
+ docs: {
39
+ description:
40
+ "Disallow type reinterpretation via `as`; absence must be modeled by the type system, with SAFETY-commented exceptions.",
41
+ },
42
+ messages: {
43
+ noReinterpret:
44
+ "Type assertion discards type evidence. Narrow with discriminants/`in` checks or a type predicate instead.",
45
+ noLaundering:
46
+ "`{{name}}` exists only to perform a type assertion: a laundering wrapper that hides the cast from review and tooling.",
47
+ },
48
+ },
49
+ createOnce(context) {
50
+ return {
51
+ TSAsExpression(node) {
52
+ const annotation = node.typeAnnotation;
53
+ if (
54
+ annotation.type === "TSTypeReference" &&
55
+ annotation.typeName.type === "Identifier" &&
56
+ annotation.typeName.name === "const"
57
+ )
58
+ return;
59
+ if (hasAdjacentSafetyComment(context.sourceCode, node)) return;
60
+ context.report({ node, messageId: "noReinterpret" });
61
+ },
62
+ TSTypeAssertion(node) {
63
+ if (hasAdjacentSafetyComment(context.sourceCode, node)) return;
64
+ context.report({ node, messageId: "noReinterpret" });
65
+ },
66
+ FunctionDeclaration(node) {
67
+ if (node.body !== null && isSoleReturnOfAssertion(node.body)) {
68
+ context.report({ node, messageId: "noLaundering", data: { name: node.id?.name ?? "<anonymous>" } });
69
+ }
70
+ },
71
+ ArrowFunctionExpression(node) {
72
+ if (node.body.type === "TSAsExpression") {
73
+ context.report({ node, messageId: "noLaundering", data: { name: "<arrow>" } });
74
+ }
75
+ },
76
+ };
77
+ },
78
+ });
@@ -0,0 +1,59 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+ import { readField } from "../shared/structural.ts";
5
+ import { ancestorsOf } from "../shared/ancestors.ts";
6
+
7
+ import { firstOptionRecord } from "../shared/rule-options.ts";
8
+
9
+ /** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
10
+ export const noRuntimeTypeofRule = defineRule({
11
+ meta: {
12
+ type: "problem",
13
+ docs: {
14
+ description:
15
+ "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.",
16
+ },
17
+ messages: {
18
+ runtimeTypeof:
19
+ "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
20
+ },
21
+ schema: [
22
+ {
23
+ type: "object",
24
+ properties: {
25
+ allowInTypeGuards: { type: "boolean" },
26
+ },
27
+ additionalProperties: false,
28
+ },
29
+ ],
30
+ defaultOptions: [{ allowInTypeGuards: false }],
31
+ },
32
+ createOnce(context) {
33
+ return {
34
+ UnaryExpression(node) {
35
+ if (node.operator !== "typeof") return;
36
+ const option = firstOptionRecord(context.options);
37
+ const allowInTypeGuards = option.allowInTypeGuards === true;
38
+ let insideTypeGuard = false;
39
+ if (allowInTypeGuards) {
40
+ const ancestors = ancestorsOf(context.sourceCode, node);
41
+ for (let index = ancestors.length - 1; index >= 0; index--) {
42
+ const current = ancestors[index]!;
43
+ if (current.type === "Program") break;
44
+ if (
45
+ current.type === "ArrowFunctionExpression" ||
46
+ current.type === "FunctionDeclaration" ||
47
+ current.type === "FunctionExpression"
48
+ ) {
49
+ insideTypeGuard =
50
+ current.returnType?.typeAnnotation.type === "TSTypePredicate";
51
+ break;
52
+ }
53
+ }
54
+ }
55
+ if (!insideTypeGuard) context.report({ node, messageId: "runtimeTypeof" });
56
+ },
57
+ };
58
+ },
59
+ });
@@ -0,0 +1,148 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+ import { readField } from "../shared/structural.ts";
5
+ import { ancestorsOf } from "../shared/ancestors.ts";
6
+
7
+ const SENTINELS = new Set<number>([-1, 0, 1]);
8
+ const ORDERING_OPERATORS = new Set([">", "<", ">=", "<="]);
9
+
10
+ function sentinelValue(expression: ESTree.Expression): number | null {
11
+ if (expression.type === "Literal") {
12
+ const numeric = readField<number>(expression, "value");
13
+ return numeric !== undefined && SENTINELS.has(numeric) ? numeric : null;
14
+ }
15
+ if (
16
+ expression.type === "UnaryExpression" &&
17
+ expression.operator === "-" &&
18
+ expression.argument.type === "Literal" &&
19
+ expression.argument.value === -1
20
+ ) {
21
+ return -1;
22
+ }
23
+ return null;
24
+ }
25
+
26
+ /** True when every leaf of an expression tree is one of the ordering sentinels.
27
+ * @param {ESTree.Expression} expression - Candidate expression to inspect.
28
+ * @returns {boolean} True when the expression (or both conditional branches) are sentinels. */
29
+ function chainIsSentinel(expression: ESTree.Expression): boolean {
30
+ if (sentinelValue(expression) !== null) return true;
31
+ if (expression.type === "ConditionalExpression") {
32
+ return chainIsSentinel(expression.consequent) && chainIsSentinel(expression.alternate);
33
+ }
34
+ return false;
35
+ }
36
+
37
+ function hasOrderingComparison(expression: ESTree.Expression): boolean {
38
+ if (expression.type === "BinaryExpression") {
39
+ return ORDERING_OPERATORS.has(expression.operator);
40
+ }
41
+ if (expression.type === "LogicalExpression") {
42
+ return hasOrderingComparison(expression.left) || hasOrderingComparison(expression.right);
43
+ }
44
+ if (expression.type === "UnaryExpression") {
45
+ return hasOrderingComparison(expression.argument);
46
+ }
47
+ if (expression.type === "ParenthesizedExpression") {
48
+ return hasOrderingComparison(expression.expression);
49
+ }
50
+ if (expression.type === "ConditionalExpression") {
51
+ return (
52
+ hasOrderingComparison(expression.test) ||
53
+ hasOrderingComparison(expression.consequent) ||
54
+ hasOrderingComparison(expression.alternate)
55
+ );
56
+ }
57
+ return false;
58
+ }
59
+
60
+ /** True when the conditional is nested under a larger sentinel chain already reported at its root.
61
+ * @param {ESTree.ConditionalExpression} node - The conditional being checked.
62
+ * @param {ReadonlyArray<ESTree.Node>} ancestors - Nearest-first ancestor chain from the traversal engine.
63
+ * @returns {boolean} True when an enclosing conditional already forms a reported sentinel chain. */
64
+ function coveredByOuterSentinelChain(node: ESTree.ConditionalExpression, ancestors: ReadonlyArray<ESTree.Node>): boolean {
65
+ for (let index = ancestors.length - 1; index >= 0; index--) {
66
+ const current = ancestors[index]!;
67
+ if (current.type === "ConditionalExpression") {
68
+ if (chainIsSentinel(current)) return true;
69
+ } else if (
70
+ current.type === "ArrowFunctionExpression" ||
71
+ current.type === "FunctionDeclaration" ||
72
+ current.type === "FunctionExpression" ||
73
+ current.type === "Program"
74
+ ) {
75
+ return false;
76
+ }
77
+ }
78
+ return false;
79
+ }
80
+
81
+ function asSingleReturnSentinel(statement: ESTree.Statement): boolean {
82
+ const target =
83
+ statement.type === "BlockStatement" && statement.body.length === 1
84
+ ? statement.body[0] ?? statement
85
+ : statement;
86
+ return (
87
+ target.type === "ReturnStatement" &&
88
+ target.argument !== null &&
89
+ sentinelValue(target.argument) !== null
90
+ );
91
+ }
92
+
93
+ /** True when an if/else-if/else chain returns only ordering sentinels from every branch.
94
+ * @param {ESTree.IfStatement} node - The root if-statement of the chain.
95
+ * @returns {boolean} True when every branch returns a sentinel comparison. */
96
+ function ifChainIsSentinel(node: ESTree.IfStatement): boolean {
97
+ let branch: ESTree.IfStatement | null = node;
98
+ while (branch !== null) {
99
+ if (!asSingleReturnSentinel(branch.consequent)) return false;
100
+ const alternate: ESTree.Statement | null = branch.alternate;
101
+ if (alternate === null) return false;
102
+ if (alternate.type === "IfStatement") {
103
+ branch = alternate;
104
+ continue;
105
+ }
106
+ return asSingleReturnSentinel(alternate);
107
+ }
108
+ return false;
109
+ }
110
+
111
+ /** Ban hand-built -1/0/1 comparison results; ordering must go through a named order. */
112
+ export const noSentinelComparisonUnionRule = defineRule({
113
+ meta: {
114
+ type: "problem",
115
+ docs: {
116
+ description:
117
+ "Disallow hand-built -1/0/1 ordering results from comparison expressions; use a named Order/Ordering or a domain comparison instead.",
118
+ },
119
+ messages: {
120
+ sentinelComparisonUnion:
121
+ "This comparison hand-builds a -1/0/1 result. Publish a named order (Order.compare + Ordering.match) or return the compared values directly.",
122
+ },
123
+ },
124
+ createOnce(context) {
125
+ return {
126
+ ConditionalExpression(node) {
127
+ if (!chainIsSentinel(node)) return;
128
+ if (coveredByOuterSentinelChain(node, ancestorsOf(context.sourceCode, node))) return;
129
+ if (!hasOrderingComparison(node)) return;
130
+ context.report({ node, messageId: "sentinelComparisonUnion" });
131
+ },
132
+ IfStatement(node) {
133
+ const parent = ancestorsOf(context.sourceCode, node).at(-1);
134
+ if (parent?.type === "IfStatement" && parent.alternate === node) return;
135
+ if (!ifChainIsSentinel(node)) return;
136
+ const conditions = [node.test];
137
+ let branch: ESTree.Statement | null = node.alternate;
138
+ while (branch !== null) {
139
+ if (branch.type !== "IfStatement") break;
140
+ conditions.push(branch.test);
141
+ branch = branch.alternate;
142
+ }
143
+ if (!conditions.some(hasOrderingComparison)) return;
144
+ context.report({ node, messageId: "sentinelComparisonUnion" });
145
+ },
146
+ };
147
+ },
148
+ });
@@ -0,0 +1,45 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ const FORBIDDEN_SYMBOL_NAME = "shape";
5
+
6
+ /**
7
+ * Whether a symbol name contains the forbidden substring.
8
+ *
9
+ * @param {string} name - The symbol name to inspect.
10
+ * @returns {boolean} True when the name contains the forbidden term.
11
+ */
12
+ function containsForbiddenSymbolName(name: string): boolean {
13
+ return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
14
+ }
15
+
16
+ /** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
17
+ export const noForbiddenTermInSymbolNamesRule = defineRule({
18
+ meta: {
19
+ type: "problem",
20
+ docs: {
21
+ description:
22
+ 'Disallow the case-insensitive substring "shape" in JavaScript, TypeScript, private, and JSX symbol names.',
23
+ },
24
+ messages: {
25
+ forbiddenSymbolName:
26
+ 'Rename symbol "{{name}}" for its domain role; "shape" describes structure rather than ownership.',
27
+ },
28
+ },
29
+ createOnce(context) {
30
+ const reportForbiddenSymbolName = (node: ESTree.Node & { name: string }) => {
31
+ if (!containsForbiddenSymbolName(node.name)) return;
32
+ context.report({
33
+ node,
34
+ messageId: "forbiddenSymbolName",
35
+ data: { name: node.name },
36
+ });
37
+ };
38
+
39
+ return {
40
+ Identifier: reportForbiddenSymbolName,
41
+ PrivateIdentifier: reportForbiddenSymbolName,
42
+ JSXIdentifier: reportForbiddenSymbolName,
43
+ };
44
+ },
45
+ });