@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.
- package/LICENSE +22 -0
- package/README.md +64 -0
- package/package.json +41 -0
- package/src/effect/index.ts +59 -0
- package/src/effect/rules/guarded-op-must-return-effect.ts +236 -0
- package/src/effect/rules/guarded-op-must-return-option.ts +97 -0
- package/src/effect/rules/no-cascading-layer-provide.ts +80 -0
- package/src/effect/rules/no-decode-unknown-option.ts +41 -0
- package/src/effect/rules/no-direct-browser-storage.ts +91 -0
- package/src/effect/rules/no-direct-fetch.ts +44 -0
- package/src/effect/rules/no-manual-field-guards.ts +144 -0
- package/src/effect/rules/no-nested-layer-provide.ts +51 -0
- package/src/effect/rules/no-schema-class-modeling.ts +59 -0
- package/src/effect/rules/no-service-constructor-imports.ts +50 -0
- package/src/effect/rules/no-silent-error-swallow.ts +99 -0
- package/src/effect/rules/no-static-effect-service-forwarders.ts +227 -0
- package/src/effect/rules/no-try-catch.ts +32 -0
- package/src/effect/rules/no-unguarded-json-parse.ts +211 -0
- package/src/effect/rules/option-core-needs-effect-public.ts +81 -0
- package/src/effect/rules/prefer-die-for-precondition-defects.ts +118 -0
- package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +76 -0
- package/src/effect/rules/prefer-effect-match.ts +78 -0
- package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +79 -0
- package/src/effect/rules/prefer-named-guard-predicate.ts +105 -0
- package/src/effect/rules/prefer-option-pipeline.ts +95 -0
- package/src/effect/rules/prefer-ordering-match.ts +152 -0
- package/src/effect/rules/prefer-tagged-enum.ts +68 -0
- package/src/effect/rules/require-schema-type-derivation.ts +83 -0
- package/src/index.ts +106 -0
- package/src/meta/index.ts +25 -0
- package/src/meta/rules/no-disable-directives.ts +38 -0
- package/src/meta/rules/no-manual-ancestor-walks.ts +27 -0
- package/src/meta/rules/prefer-before-file-scope.ts +57 -0
- package/src/meta/rules/require-create-once.ts +51 -0
- package/src/meta/rules/require-rule-tester.ts +57 -0
- package/src/rules/no-anonymous-wide-tuples.ts +38 -0
- package/src/rules/no-barrel-export-star.ts +30 -0
- package/src/rules/no-boolean-field-signals.ts +305 -0
- package/src/rules/no-builtin-throws.ts +83 -0
- package/src/rules/no-chained-type-assertions.ts +65 -0
- package/src/rules/no-conditional-empty-object-spread.ts +42 -0
- package/src/rules/no-duplicated-literal-union.ts +66 -0
- package/src/rules/no-exported-mutable-state.ts +111 -0
- package/src/rules/no-generic-export-names.ts +149 -0
- package/src/rules/no-impossible-branch-throw.ts +55 -0
- package/src/rules/no-known-value-widening.ts +225 -0
- package/src/rules/no-module-mocking.ts +95 -0
- package/src/rules/no-multiple-function-params.ts +192 -0
- package/src/rules/no-nondeterministic-core.ts +73 -0
- package/src/rules/no-object-parameters.ts +127 -0
- package/src/rules/no-optional-function-parameters.ts +49 -0
- package/src/rules/no-product-of-state-booleans.ts +80 -0
- package/src/rules/no-redundant-derived-field.ts +192 -0
- package/src/rules/no-reflect-apply.ts +32 -0
- package/src/rules/no-reflect-get.ts +32 -0
- package/src/rules/no-reinterpret-cast.ts +78 -0
- package/src/rules/no-runtime-typeof.ts +59 -0
- package/src/rules/no-sentinel-comparison-union.ts +148 -0
- package/src/rules/no-shape-in-symbol-names.ts +45 -0
- package/src/rules/no-single-use-private-functions.ts +329 -0
- package/src/rules/no-stacked-jsdoc-blocks.ts +65 -0
- package/src/rules/no-stray-inline-comments.ts +79 -0
- package/src/rules/no-swappable-primitive-params.ts +109 -0
- package/src/rules/no-tag-ladder-assertions.ts +71 -0
- package/src/rules/no-transposed-field-reads.ts +135 -0
- package/src/rules/no-unit-return-validators.ts +65 -0
- package/src/rules/no-unknown-parameters.ts +97 -0
- package/src/rules/no-unknown-returns.ts +120 -0
- package/src/rules/no-unknown-type-aliases.ts +70 -0
- package/src/rules/no-unsafe-dictionary-type.ts +132 -0
- package/src/rules/no-vague-test-filenames.ts +67 -0
- package/src/rules/no-widen-then-assert.ts +345 -0
- package/src/rules/prefer-payload-brand.ts +67 -0
- package/src/rules/prefer-property-tests.ts +43 -0
- package/src/rules/require-canonical-stringify-for-identity.ts +80 -0
- package/src/rules/require-deprecated-tag-for-legacy-comments.ts +45 -0
- package/src/rules/require-exhaustive-tag-switch.ts +42 -0
- package/src/rules/require-fc-block-predicate.ts +42 -0
- package/src/rules/require-jsdoc-on-exported.ts +74 -0
- package/src/rules/require-published-order.ts +109 -0
- package/src/rules/require-safety-comment-for-type-assertion.ts +61 -0
- package/src/rules/require-sort-comparator.ts +41 -0
- package/src/rules/test-utils.ts +68 -0
- package/src/shared/ancestors.ts +21 -0
- package/src/shared/child-nodes.ts +27 -0
- package/src/shared/dictionary-types.ts +524 -0
- package/src/shared/is-node.ts +13 -0
- package/src/shared/lexical-type-parameters.ts +62 -0
- package/src/shared/reflect-method.ts +41 -0
- package/src/shared/rule-options.ts +23 -0
- package/src/shared/structural.ts +81 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/testable-rule.test.ts +68 -0
- package/src/testing/testable-rule.ts +86 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
import { getAllComments } from "../shared/structural.ts";
|
|
6
|
+
|
|
7
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
8
|
+
|
|
9
|
+
interface CommentLike {
|
|
10
|
+
type: string;
|
|
11
|
+
value: string;
|
|
12
|
+
loc?: { end?: { line?: number } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Every exported function carries a complete JSDoc contract. */
|
|
16
|
+
export const requireJsdocOnExportedRule = defineRule({
|
|
17
|
+
meta: {
|
|
18
|
+
type: "problem",
|
|
19
|
+
docs: {
|
|
20
|
+
description:
|
|
21
|
+
"Require exported functions (declarations and arrow/function-expression consts) to carry an attached JSDoc block; the jsdoc rules only check blocks that exist, so undocumented exports otherwise escape the contract rules entirely.",
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
missingJsdoc:
|
|
25
|
+
"Exported function `{{name}}` has no JSDoc block. Document the contract with @param/@returns so the jsdoc rules can enforce it.",
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
createOnce(context) {
|
|
29
|
+
const jsdocFollowsLines = new Set<number>();
|
|
30
|
+
return {
|
|
31
|
+
before() {
|
|
32
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
33
|
+
jsdocFollowsLines.clear();
|
|
34
|
+
},
|
|
35
|
+
Program(node) {
|
|
36
|
+
jsdocFollowsLines.clear();
|
|
37
|
+
for (const comment of getAllComments(context.sourceCode)) {
|
|
38
|
+
if (comment.type !== "Block" || !comment.value.startsWith("*")) continue;
|
|
39
|
+
const endLine = comment.loc?.end?.line;
|
|
40
|
+
if (endLine !== undefined) jsdocFollowsLines.add(endLine + 1);
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
ExportNamedDeclaration(node) {
|
|
44
|
+
const declaration = node.declaration;
|
|
45
|
+
if (declaration === null || declaration === undefined) return;
|
|
46
|
+
const startLine = declaration.loc?.start?.line;
|
|
47
|
+
if (startLine === undefined || jsdocFollowsLines.has(startLine)) return;
|
|
48
|
+
if (declaration.type === "FunctionDeclaration" && declaration.id !== null) {
|
|
49
|
+
context.report({
|
|
50
|
+
node: declaration,
|
|
51
|
+
messageId: "missingJsdoc",
|
|
52
|
+
data: { name: declaration.id.name },
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (declaration.type !== "VariableDeclaration") return;
|
|
57
|
+
for (const declarator of declaration.declarations) {
|
|
58
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
59
|
+
const init = declarator.init;
|
|
60
|
+
if (
|
|
61
|
+
init?.type === "ArrowFunctionExpression" ||
|
|
62
|
+
init?.type === "FunctionExpression"
|
|
63
|
+
) {
|
|
64
|
+
context.report({
|
|
65
|
+
node: declarator.id,
|
|
66
|
+
messageId: "missingJsdoc",
|
|
67
|
+
data: { name: declarator.id.name },
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
/** True for the numeric ordering sentinels -1, 0 and 1 in a type position.
|
|
6
|
+
*
|
|
7
|
+
* @param {ESTree.TSType} type - The union constituent to inspect.
|
|
8
|
+
* @returns {boolean} True when the type is one of the sentinel literals.
|
|
9
|
+
*/
|
|
10
|
+
function isSentinelLiteral(type: ESTree.TSType): boolean {
|
|
11
|
+
if (type.type !== "TSLiteralType") return false;
|
|
12
|
+
const literal = type.literal;
|
|
13
|
+
if (literal.type === "UnaryExpression") {
|
|
14
|
+
return (
|
|
15
|
+
literal.operator === "-" &&
|
|
16
|
+
literal.argument.type === "Literal" &&
|
|
17
|
+
literal.argument.value === 1
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
return literal.type === "Literal" && (literal.value === 0 || literal.value === 1);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** True for a reference to Effect's Ordering (or its -1 | 0 | 1 spelling).
|
|
24
|
+
*
|
|
25
|
+
* @param {ESTree.TSType} type - The annotation to inspect.
|
|
26
|
+
* @returns {boolean} True when the type names Ordering.
|
|
27
|
+
*/
|
|
28
|
+
function isOrderingReference(type: ESTree.TSType): boolean {
|
|
29
|
+
if (type.type !== "TSTypeReference") return false;
|
|
30
|
+
const name = type.typeName;
|
|
31
|
+
if (name.type === "Identifier") return name.name === "Ordering";
|
|
32
|
+
return (
|
|
33
|
+
name.type === "TSQualifiedName" &&
|
|
34
|
+
name.left.type === "Identifier" &&
|
|
35
|
+
name.left.name === "Ordering" &&
|
|
36
|
+
name.right.type === "Identifier" &&
|
|
37
|
+
name.right.name === "Ordering"
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function returnsComparatorOrdering(
|
|
42
|
+
returnType: ESTree.TSTypeAnnotation | null | undefined,
|
|
43
|
+
): boolean {
|
|
44
|
+
if (returnType === null || returnType === undefined) return false;
|
|
45
|
+
const annotation = returnType.typeAnnotation;
|
|
46
|
+
if (annotation.type === "TSUnionType") return annotation.types.every(isSentinelLiteral);
|
|
47
|
+
return isOrderingReference(annotation);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A module exporting comparators must publish a named Order built from them. */
|
|
51
|
+
export const requirePublishedOrderRule = defineRule({
|
|
52
|
+
meta: {
|
|
53
|
+
type: "problem",
|
|
54
|
+
docs: {
|
|
55
|
+
description:
|
|
56
|
+
"Require modules that export comparators to publish a named Order (Order.make) so call sites compose through one total order instead of hand-rolling comparisons.",
|
|
57
|
+
},
|
|
58
|
+
messages: {
|
|
59
|
+
missingPublishedOrder:
|
|
60
|
+
"`{{name}}` exposes an ordering but the module publishes no named order. Export `export const Order = Order.make({{name}})` so comparisons compose through one total order.",
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
createOnce(context) {
|
|
64
|
+
return {
|
|
65
|
+
Program(node) {
|
|
66
|
+
let hasPublishedOrder = false;
|
|
67
|
+
const comparators: Array<{ name: string; node: ESTree.Node }> = [];
|
|
68
|
+
for (const statement of node.body) {
|
|
69
|
+
const declaration =
|
|
70
|
+
statement.type === "ExportNamedDeclaration" ||
|
|
71
|
+
statement.type === "ExportDefaultDeclaration"
|
|
72
|
+
? (statement.declaration ?? null)
|
|
73
|
+
: statement;
|
|
74
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
75
|
+
for (const declarator of declaration.declarations) {
|
|
76
|
+
if (declarator.id.type !== "Identifier") continue;
|
|
77
|
+
const name = declarator.id.name;
|
|
78
|
+
if (name === "Order") hasPublishedOrder = true;
|
|
79
|
+
if (!/^compare/u.test(name)) continue;
|
|
80
|
+
const init = declarator.init;
|
|
81
|
+
if (
|
|
82
|
+
(init?.type === "ArrowFunctionExpression" ||
|
|
83
|
+
init?.type === "FunctionExpression") &&
|
|
84
|
+
returnsComparatorOrdering(init.returnType)
|
|
85
|
+
) {
|
|
86
|
+
comparators.push({ name, node: declarator.id });
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id !== null) {
|
|
91
|
+
const name = declaration.id.name;
|
|
92
|
+
if (!/^compare/u.test(name)) continue;
|
|
93
|
+
if (returnsComparatorOrdering(declaration.returnType)) {
|
|
94
|
+
comparators.push({ name, node: declaration.id });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (hasPublishedOrder) return;
|
|
99
|
+
for (const comparator of comparators) {
|
|
100
|
+
context.report({
|
|
101
|
+
node: comparator.node,
|
|
102
|
+
messageId: "missingPublishedOrder",
|
|
103
|
+
data: { name: comparator.name },
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
},
|
|
109
|
+
});
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
+
import { ancestorsOf } from "../shared/ancestors.ts";
|
|
5
|
+
|
|
6
|
+
type TypeAssertion = ESTree.TSAsExpression | ESTree.TSTypeAssertion;
|
|
7
|
+
|
|
8
|
+
const commentOwnerKinds = new Set([
|
|
9
|
+
"ExpressionStatement",
|
|
10
|
+
"PropertyDefinition",
|
|
11
|
+
"ReturnStatement",
|
|
12
|
+
"ThrowStatement",
|
|
13
|
+
"VariableDeclaration",
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
/** Require every non-const type assertion to state the invariant TypeScript cannot express. */
|
|
17
|
+
export const requireSafetyCommentForTypeAssertionRule = defineRule({
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
docs: {
|
|
21
|
+
description:
|
|
22
|
+
"Require a nearby SAFETY comment for every TypeScript type assertion except const assertions.",
|
|
23
|
+
},
|
|
24
|
+
messages: {
|
|
25
|
+
missingSafetyComment:
|
|
26
|
+
"This type assertion has no `SAFETY:` justification. State the checked invariant immediately before the assertion or its containing statement.",
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
createOnce(context) {
|
|
30
|
+
const checkAssertion = (node: TypeAssertion) => {
|
|
31
|
+
const isConstAssertion =
|
|
32
|
+
node.typeAnnotation.type === "TSTypeReference" &&
|
|
33
|
+
node.typeAnnotation.typeName.type === "Identifier" &&
|
|
34
|
+
node.typeAnnotation.typeName.name === "const";
|
|
35
|
+
if (isConstAssertion) return;
|
|
36
|
+
const ancestors = ancestorsOf(context.sourceCode, node);
|
|
37
|
+
const chain: Array<ESTree.Node> = [node, ...[...ancestors].reverse()];
|
|
38
|
+
let justified = false;
|
|
39
|
+
for (let index = 0; index < chain.length; index += 1) {
|
|
40
|
+
const current = chain[index]!;
|
|
41
|
+
if (
|
|
42
|
+
context.sourceCode
|
|
43
|
+
.getCommentsBefore(current)
|
|
44
|
+
.some((comment) => comment.end <= node.start && /\bSAFETY\s*:/u.test(comment.value))
|
|
45
|
+
) {
|
|
46
|
+
justified = true;
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (commentOwnerKinds.has(current.type)) break;
|
|
50
|
+
const parent = chain[index + 1];
|
|
51
|
+
if (parent === undefined || parent.type === "Program") break;
|
|
52
|
+
}
|
|
53
|
+
if (!justified) context.report({ node, messageId: "missingSafetyComment" });
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
TSAsExpression: checkAssertion,
|
|
58
|
+
TSTypeAssertion: checkAssertion,
|
|
59
|
+
};
|
|
60
|
+
},
|
|
61
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Canonical form requires intentional order. The default sort coerces
|
|
7
|
+
* elements to strings and compares lexicographically, so the "natural"
|
|
8
|
+
* ordering silently depends on element encoding; an explicit comparator
|
|
9
|
+
* makes the canonical order deliberate and reviewable.
|
|
10
|
+
*/
|
|
11
|
+
export const requireSortComparatorRule = defineRule({
|
|
12
|
+
meta: {
|
|
13
|
+
type: "problem",
|
|
14
|
+
docs: {
|
|
15
|
+
description:
|
|
16
|
+
"Require an explicit comparator on .sort()/.toSorted(); the default lexicographic sort makes canonical order accidental.",
|
|
17
|
+
},
|
|
18
|
+
messages: {
|
|
19
|
+
missingComparator:
|
|
20
|
+
"This sort has no comparator, so ordering defaults to string comparison and the canonical order is accidental. Pass an explicit comparator so the intended order is stated here.",
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
createOnce(context) {
|
|
24
|
+
return {
|
|
25
|
+
CallExpression(node) {
|
|
26
|
+
const callee = node.callee;
|
|
27
|
+
if (
|
|
28
|
+
callee.type !== "MemberExpression" ||
|
|
29
|
+
callee.computed ||
|
|
30
|
+
callee.property.type !== "Identifier" ||
|
|
31
|
+
(callee.property.name !== "sort" && callee.property.name !== "toSorted")
|
|
32
|
+
) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
if (node.arguments.length === 0) {
|
|
36
|
+
context.report({ node, messageId: "missingComparator" });
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { defineRule, eslintCompatPlugin } from "@oxlint/plugins";
|
|
2
|
+
import fc from "fast-check";
|
|
3
|
+
import { describe, it } from "vitest";
|
|
4
|
+
import { RuleTester } from "oxlint/plugins-dev";
|
|
5
|
+
|
|
6
|
+
import type { ESTree, Rule } from "@oxlint/plugins";
|
|
7
|
+
|
|
8
|
+
type Recorded = { messageId: string };
|
|
9
|
+
|
|
10
|
+
/** Wrap a raw createOnce rule in the compat layer so it can run anywhere.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} name - The rule name to register under.
|
|
13
|
+
* @param {unknown} rule - The raw rule implementation.
|
|
14
|
+
* @returns {Rule} The compat-wrapped rule.
|
|
15
|
+
*/
|
|
16
|
+
export function wrap(name: string, rule: unknown): Rule {
|
|
17
|
+
const plugin = eslintCompatPlugin({ meta: { name: "plumb-test" }, rules: { [name]: rule as unknown as Rule } });
|
|
18
|
+
return plugin.rules[name] as unknown as Rule;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Drive a createOnce rule visitor directly against a synthetic node.
|
|
22
|
+
*
|
|
23
|
+
* @param {Rule} rule - The rule to probe.
|
|
24
|
+
* @param {string} visitorKey - The visitor method name to invoke.
|
|
25
|
+
* @param {unknown} node - The synthetic node passed to the visitor.
|
|
26
|
+
* @returns {Array<Recorded>} Reports collected from the probe.
|
|
27
|
+
*/
|
|
28
|
+
export function collectReports(
|
|
29
|
+
rule: Rule,
|
|
30
|
+
visitorKey: string,
|
|
31
|
+
node: unknown,
|
|
32
|
+
): Array<Recorded> {
|
|
33
|
+
const reports: Array<Recorded> = [];
|
|
34
|
+
const visitor = wrap("probe", rule) as unknown as {
|
|
35
|
+
create: (context: unknown) => Record<string, (n: unknown) => void>;
|
|
36
|
+
};
|
|
37
|
+
visitor.create({ report: (d: Recorded) => reports.push(d) })[visitorKey]?.(node);
|
|
38
|
+
return reports;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Bind vitest reporting into RuleTester so suites run under the project runner.
|
|
42
|
+
*
|
|
43
|
+
* @returns {RuleTester} A RuleTester wired to vitest's describe/it.
|
|
44
|
+
*/
|
|
45
|
+
export function makeTester(): RuleTester {
|
|
46
|
+
RuleTester.describe = (name, fn) => describe(name, fn);
|
|
47
|
+
RuleTester.it = (name, fn) => it(name, fn);
|
|
48
|
+
return new RuleTester({ languageOptions: { parserOptions: { lang: "ts" } } });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Build an ESTree member expression on a fixed `node` object for visitor probes.
|
|
52
|
+
*
|
|
53
|
+
* @param {boolean} computed - Whether the member access is computed.
|
|
54
|
+
* @param {string} propertyName_ - The property identifier name.
|
|
55
|
+
* @returns {ESTree.Node} The synthetic member expression node.
|
|
56
|
+
*/
|
|
57
|
+
export function memberExpression(computed: boolean, propertyName_: string): ESTree.Node {
|
|
58
|
+
return {
|
|
59
|
+
type: "MemberExpression",
|
|
60
|
+
computed,
|
|
61
|
+
object: { type: "Identifier", name: "node" },
|
|
62
|
+
property: { type: "Identifier", name: propertyName_ },
|
|
63
|
+
} as unknown as ESTree.Node;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export const identifiers: fc.Arbitrary<string> = fc
|
|
67
|
+
.stringMatching(/^[a-zA-Z_$][a-zA-Z0-9_$]{0,7}$/u)
|
|
68
|
+
.filter((s) => !["const", "let", "var", "function", "class"].includes(s));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Ancestor chain accessor returning oxlint's native order: outermost-first
|
|
5
|
+
* (Program at index 0, immediate parent last). Reverse it to walk outward
|
|
6
|
+
* from a node.
|
|
7
|
+
*
|
|
8
|
+
* Oxlint types `getAncestors` as returning an opaque span-only `Node`, not
|
|
9
|
+
* the exported ESTree union; recover that union once here.
|
|
10
|
+
*
|
|
11
|
+
* Oxlint's `sourceCode.getAncestors` returns an internal Node type rather than
|
|
12
|
+
* the exported ESTree union; recover the union once here so rule code can
|
|
13
|
+
* narrow on `type` discriminants without per-site casts.
|
|
14
|
+
*
|
|
15
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
16
|
+
* @param {ESTree.Node} node - The node whose ancestor chain is requested.
|
|
17
|
+
* @returns {ReadonlyArray<ESTree.Node>} Nearest-first ancestor chain.
|
|
18
|
+
*/
|
|
19
|
+
export function ancestorsOf(sourceCode: SourceCode, node: ESTree.Node): ReadonlyArray<ESTree.Node> {
|
|
20
|
+
return sourceCode.getAncestors(node) as unknown as ReadonlyArray<ESTree.Node>;
|
|
21
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { isNode } from "./is-node.ts";
|
|
4
|
+
import { readField } from "./structural.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Child nodes of an ESTree node via reflection over its fields.
|
|
8
|
+
*
|
|
9
|
+
* SAFETY: node fields are owned by the AST grammar (see NodeFieldValue); the
|
|
10
|
+
* object/array discrimination here is the field-to-child boundary. Prefer
|
|
11
|
+
* declared visitors; use this only for custom descent orders.
|
|
12
|
+
*
|
|
13
|
+
* @param {ESTree.Node} node - The parent AST node.
|
|
14
|
+
* @returns {Array<ESTree.Node>} Direct child nodes, in field order.
|
|
15
|
+
*/
|
|
16
|
+
export function childNodes(node: ESTree.Node): Array<ESTree.Node> {
|
|
17
|
+
const children: Array<ESTree.Node> = [];
|
|
18
|
+
for (const key of Object.keys(node)) {
|
|
19
|
+
if (key === "parent" || key === "loc" || key === "range") continue;
|
|
20
|
+
const value = readField(node, key);
|
|
21
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
22
|
+
for (const candidate of candidates) {
|
|
23
|
+
if (isNode(candidate)) children.push(candidate);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return children;
|
|
27
|
+
}
|