@autotelic/plumb 0.3.1 → 0.4.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/dist/effect/index.d.ts +3 -0
- package/dist/effect/index.js +56 -0
- package/dist/effect/rules/guarded-op-must-return-effect.d.ts +6 -0
- package/dist/effect/rules/guarded-op-must-return-effect.js +235 -0
- package/dist/effect/rules/guarded-op-must-return-option.d.ts +2 -0
- package/dist/effect/rules/guarded-op-must-return-option.js +88 -0
- package/dist/effect/rules/no-cascading-layer-provide.d.ts +6 -0
- package/dist/effect/rules/no-cascading-layer-provide.js +73 -0
- package/dist/effect/rules/no-decode-unknown-option.d.ts +7 -0
- package/dist/effect/rules/no-decode-unknown-option.js +34 -0
- package/dist/effect/rules/no-direct-browser-storage.d.ts +2 -0
- package/dist/effect/rules/no-direct-browser-storage.js +88 -0
- package/dist/effect/rules/no-direct-fetch.d.ts +2 -0
- package/dist/effect/rules/no-direct-fetch.js +37 -0
- package/dist/effect/rules/no-manual-field-guards.d.ts +6 -0
- package/dist/effect/rules/no-manual-field-guards.js +129 -0
- package/dist/effect/rules/no-nested-layer-provide.d.ts +2 -0
- package/dist/effect/rules/no-nested-layer-provide.js +50 -0
- package/dist/effect/rules/no-schema-class-modeling.d.ts +2 -0
- package/dist/effect/rules/no-schema-class-modeling.js +52 -0
- package/dist/effect/rules/no-service-constructor-imports.d.ts +2 -0
- package/dist/effect/rules/no-service-constructor-imports.js +42 -0
- package/dist/effect/rules/no-silent-error-swallow.d.ts +2 -0
- package/dist/effect/rules/no-silent-error-swallow.js +95 -0
- package/dist/effect/rules/no-static-effect-service-forwarders.d.ts +6 -0
- package/dist/effect/rules/no-static-effect-service-forwarders.js +222 -0
- package/dist/effect/rules/no-try-catch.d.ts +2 -0
- package/dist/effect/rules/no-try-catch.js +30 -0
- package/dist/effect/rules/no-unguarded-json-parse.d.ts +5 -0
- package/dist/effect/rules/no-unguarded-json-parse.js +200 -0
- package/dist/effect/rules/option-core-needs-effect-public.d.ts +5 -0
- package/dist/effect/rules/option-core-needs-effect-public.js +76 -0
- package/dist/effect/rules/prefer-die-for-precondition-defects.d.ts +5 -0
- package/dist/effect/rules/prefer-die-for-precondition-defects.js +112 -0
- package/dist/effect/rules/prefer-effect-gen-for-guard-ladders.d.ts +5 -0
- package/dist/effect/rules/prefer-effect-gen-for-guard-ladders.js +72 -0
- package/dist/effect/rules/prefer-effect-match.d.ts +2 -0
- package/dist/effect/rules/prefer-effect-match.js +74 -0
- package/dist/effect/rules/prefer-effect-predicate-over-boolean-function.d.ts +2 -0
- package/dist/effect/rules/prefer-effect-predicate-over-boolean-function.js +75 -0
- package/dist/effect/rules/prefer-named-guard-predicate.d.ts +2 -0
- package/dist/effect/rules/prefer-named-guard-predicate.js +93 -0
- package/dist/effect/rules/prefer-option-pipeline.d.ts +2 -0
- package/dist/effect/rules/prefer-option-pipeline.js +87 -0
- package/dist/effect/rules/prefer-ordering-match.d.ts +2 -0
- package/dist/effect/rules/prefer-ordering-match.js +135 -0
- package/dist/effect/rules/prefer-tagged-enum.d.ts +2 -0
- package/dist/effect/rules/prefer-tagged-enum.js +66 -0
- package/dist/effect/rules/require-schema-type-derivation.d.ts +7 -0
- package/dist/effect/rules/require-schema-type-derivation.js +80 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +104 -0
- package/dist/meta/index.d.ts +7 -0
- package/{src/meta/index.ts → dist/meta/index.js} +13 -16
- package/dist/meta/rules/no-disable-directives.d.ts +6 -0
- package/dist/meta/rules/no-disable-directives.js +35 -0
- package/dist/meta/rules/no-manual-ancestor-walks.d.ts +2 -0
- package/dist/meta/rules/no-manual-ancestor-walks.js +26 -0
- package/dist/meta/rules/prefer-before-file-scope.d.ts +2 -0
- package/dist/meta/rules/prefer-before-file-scope.js +57 -0
- package/dist/meta/rules/require-create-once.d.ts +2 -0
- package/dist/meta/rules/require-create-once.js +51 -0
- package/dist/meta/rules/require-rule-tester.d.ts +2 -0
- package/dist/meta/rules/require-rule-tester.js +50 -0
- package/dist/rules/no-anonymous-wide-tuples.d.ts +7 -0
- package/dist/rules/no-anonymous-wide-tuples.js +34 -0
- package/dist/rules/no-barrel-export-star.d.ts +2 -0
- package/dist/rules/no-barrel-export-star.js +26 -0
- package/dist/rules/no-boolean-field-signals.d.ts +2 -0
- package/dist/rules/no-boolean-field-signals.js +279 -0
- package/dist/rules/no-builtin-throws.d.ts +6 -0
- package/dist/rules/no-builtin-throws.js +74 -0
- package/dist/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/rules/no-chained-type-assertions.js +54 -0
- package/dist/rules/no-conditional-empty-object-spread.d.ts +2 -0
- package/dist/rules/no-conditional-empty-object-spread.js +36 -0
- package/dist/rules/no-duplicated-literal-union.d.ts +6 -0
- package/dist/rules/no-duplicated-literal-union.js +62 -0
- package/dist/rules/no-exported-mutable-state.d.ts +7 -0
- package/dist/rules/no-exported-mutable-state.js +104 -0
- package/dist/rules/no-generic-export-names.d.ts +2 -0
- package/dist/rules/no-generic-export-names.js +141 -0
- package/dist/rules/no-impossible-branch-throw.d.ts +2 -0
- package/dist/rules/no-impossible-branch-throw.js +51 -0
- package/dist/rules/no-known-value-widening.d.ts +2 -0
- package/dist/rules/no-known-value-widening.js +176 -0
- package/dist/rules/no-module-mocking.d.ts +2 -0
- package/dist/rules/no-module-mocking.js +90 -0
- package/dist/rules/no-multiple-function-params.d.ts +2 -0
- package/dist/rules/no-multiple-function-params.js +155 -0
- package/dist/rules/no-mutable-environment-capture.d.ts +7 -0
- package/dist/rules/no-mutable-environment-capture.js +134 -0
- package/dist/rules/no-nondeterministic-core.d.ts +6 -0
- package/dist/rules/no-nondeterministic-core.js +63 -0
- package/dist/rules/no-object-parameters.d.ts +2 -0
- package/dist/rules/no-object-parameters.js +95 -0
- package/dist/rules/no-optional-function-parameters.d.ts +7 -0
- package/dist/rules/no-optional-function-parameters.js +44 -0
- package/dist/rules/no-product-of-state-booleans.d.ts +2 -0
- package/dist/rules/no-product-of-state-booleans.js +79 -0
- package/dist/rules/no-redundant-derived-field.d.ts +6 -0
- package/dist/rules/no-redundant-derived-field.js +194 -0
- package/dist/rules/no-reflect-apply.d.ts +2 -0
- package/dist/rules/no-reflect-apply.js +29 -0
- package/dist/rules/no-reflect-get.d.ts +2 -0
- package/dist/rules/no-reflect-get.js +29 -0
- package/dist/rules/no-reinterpret-cast.d.ts +2 -0
- package/dist/rules/no-reinterpret-cast.js +68 -0
- package/dist/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/rules/no-runtime-typeof.js +54 -0
- package/dist/rules/no-sentinel-comparison-union.d.ts +2 -0
- package/dist/rules/no-sentinel-comparison-union.js +139 -0
- package/dist/rules/no-shape-in-symbol-names.d.ts +2 -0
- package/dist/rules/no-shape-in-symbol-names.js +39 -0
- package/dist/rules/no-single-use-private-functions.d.ts +17 -0
- package/dist/rules/no-single-use-private-functions.js +419 -0
- package/dist/rules/no-stacked-jsdoc-blocks.d.ts +2 -0
- package/dist/rules/no-stacked-jsdoc-blocks.js +50 -0
- package/dist/rules/no-stray-inline-comments.d.ts +2 -0
- package/dist/rules/no-stray-inline-comments.js +74 -0
- package/dist/rules/no-swappable-primitive-params.d.ts +2 -0
- package/dist/rules/no-swappable-primitive-params.js +96 -0
- package/dist/rules/no-tag-ladder-assertions.d.ts +6 -0
- package/dist/rules/no-tag-ladder-assertions.js +63 -0
- package/dist/rules/no-transposed-field-reads.d.ts +8 -0
- package/dist/rules/no-transposed-field-reads.js +101 -0
- package/dist/rules/no-unit-return-validators.d.ts +2 -0
- package/dist/rules/no-unit-return-validators.js +64 -0
- package/dist/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/rules/no-unknown-parameters.js +82 -0
- package/dist/rules/no-unknown-returns.d.ts +2 -0
- package/dist/rules/no-unknown-returns.js +86 -0
- package/dist/rules/no-unknown-type-aliases.d.ts +2 -0
- package/dist/rules/no-unknown-type-aliases.js +64 -0
- package/dist/rules/no-unsafe-dictionary-type.d.ts +2 -0
- package/dist/rules/no-unsafe-dictionary-type.js +116 -0
- package/dist/rules/no-vague-test-filenames.d.ts +2 -0
- package/dist/rules/no-vague-test-filenames.js +61 -0
- package/dist/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/rules/no-widen-then-assert.js +268 -0
- package/dist/rules/prefer-payload-brand.d.ts +2 -0
- package/dist/rules/prefer-payload-brand.js +57 -0
- package/dist/rules/prefer-property-tests.d.ts +5 -0
- package/dist/rules/prefer-property-tests.js +39 -0
- package/dist/rules/require-canonical-stringify-for-identity.d.ts +8 -0
- package/dist/rules/require-canonical-stringify-for-identity.js +72 -0
- package/dist/rules/require-deprecated-tag-for-legacy-comments.d.ts +2 -0
- package/dist/rules/require-deprecated-tag-for-legacy-comments.js +34 -0
- package/dist/rules/require-exhaustive-tag-switch.d.ts +7 -0
- package/dist/rules/require-exhaustive-tag-switch.js +34 -0
- package/dist/rules/require-fc-block-predicate.d.ts +2 -0
- package/dist/rules/require-fc-block-predicate.js +37 -0
- package/dist/rules/require-jsdoc-on-exported.d.ts +2 -0
- package/dist/rules/require-jsdoc-on-exported.js +66 -0
- package/dist/rules/require-published-order.d.ts +2 -0
- package/dist/rules/require-published-order.js +102 -0
- package/dist/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
- package/dist/rules/require-safety-comment-for-type-assertion.js +53 -0
- package/dist/rules/require-sort-comparator.d.ts +7 -0
- package/dist/rules/require-sort-comparator.js +34 -0
- package/dist/rules/test-utils.d.ts +35 -0
- package/dist/rules/test-utils.js +53 -0
- package/{src/shared/ancestors.ts → dist/shared/ancestors.d.ts} +1 -4
- package/dist/shared/ancestors.js +19 -0
- package/dist/shared/child-nodes.d.ts +12 -0
- package/dist/shared/child-nodes.js +26 -0
- package/dist/shared/dictionary-types.d.ts +55 -0
- package/dist/shared/dictionary-types.js +413 -0
- package/{src/shared/is-node.ts → dist/shared/is-node.d.ts} +2 -6
- package/dist/shared/is-node.js +10 -0
- package/dist/shared/lexical-type-parameters.d.ts +12 -0
- package/dist/shared/lexical-type-parameters.js +50 -0
- package/dist/shared/reflect-method.d.ts +15 -0
- package/dist/shared/reflect-method.js +32 -0
- package/{src/shared/rule-options.ts → dist/shared/rule-options.d.ts} +2 -7
- package/dist/shared/rule-options.js +12 -0
- package/{src/shared/structural.ts → dist/shared/structural.d.ts} +17 -44
- package/dist/shared/structural.js +40 -0
- package/{src/testing/index.ts → dist/testing/index.d.ts} +0 -1
- package/dist/testing/index.js +1 -0
- package/dist/testing/testable-rule.d.ts +38 -0
- package/dist/testing/testable-rule.js +70 -0
- package/package.json +7 -6
- package/src/effect/index.ts +0 -59
- package/src/effect/rules/guarded-op-must-return-effect.ts +0 -236
- package/src/effect/rules/guarded-op-must-return-option.ts +0 -97
- package/src/effect/rules/no-cascading-layer-provide.ts +0 -80
- package/src/effect/rules/no-decode-unknown-option.ts +0 -41
- package/src/effect/rules/no-direct-browser-storage.ts +0 -91
- package/src/effect/rules/no-direct-fetch.ts +0 -44
- package/src/effect/rules/no-manual-field-guards.ts +0 -144
- package/src/effect/rules/no-nested-layer-provide.ts +0 -51
- package/src/effect/rules/no-schema-class-modeling.ts +0 -59
- package/src/effect/rules/no-service-constructor-imports.ts +0 -50
- package/src/effect/rules/no-silent-error-swallow.ts +0 -99
- package/src/effect/rules/no-static-effect-service-forwarders.ts +0 -227
- package/src/effect/rules/no-try-catch.ts +0 -32
- package/src/effect/rules/no-unguarded-json-parse.ts +0 -211
- package/src/effect/rules/option-core-needs-effect-public.ts +0 -81
- package/src/effect/rules/prefer-die-for-precondition-defects.ts +0 -118
- package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +0 -76
- package/src/effect/rules/prefer-effect-match.ts +0 -78
- package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +0 -79
- package/src/effect/rules/prefer-named-guard-predicate.ts +0 -105
- package/src/effect/rules/prefer-option-pipeline.ts +0 -95
- package/src/effect/rules/prefer-ordering-match.ts +0 -152
- package/src/effect/rules/prefer-tagged-enum.ts +0 -68
- package/src/effect/rules/require-schema-type-derivation.ts +0 -83
- package/src/index.ts +0 -106
- package/src/meta/rules/no-disable-directives.ts +0 -38
- package/src/meta/rules/no-manual-ancestor-walks.ts +0 -27
- package/src/meta/rules/prefer-before-file-scope.ts +0 -57
- package/src/meta/rules/require-create-once.ts +0 -51
- package/src/meta/rules/require-rule-tester.ts +0 -57
- package/src/rules/no-anonymous-wide-tuples.ts +0 -38
- package/src/rules/no-barrel-export-star.ts +0 -30
- package/src/rules/no-boolean-field-signals.ts +0 -305
- package/src/rules/no-builtin-throws.ts +0 -83
- package/src/rules/no-chained-type-assertions.ts +0 -65
- package/src/rules/no-conditional-empty-object-spread.ts +0 -42
- package/src/rules/no-duplicated-literal-union.ts +0 -66
- package/src/rules/no-exported-mutable-state.ts +0 -111
- package/src/rules/no-generic-export-names.ts +0 -149
- package/src/rules/no-impossible-branch-throw.ts +0 -55
- package/src/rules/no-known-value-widening.ts +0 -225
- package/src/rules/no-module-mocking.ts +0 -95
- package/src/rules/no-multiple-function-params.ts +0 -192
- package/src/rules/no-nondeterministic-core.ts +0 -73
- package/src/rules/no-object-parameters.ts +0 -127
- package/src/rules/no-optional-function-parameters.ts +0 -49
- package/src/rules/no-product-of-state-booleans.ts +0 -80
- package/src/rules/no-redundant-derived-field.ts +0 -192
- package/src/rules/no-reflect-apply.ts +0 -32
- package/src/rules/no-reflect-get.ts +0 -32
- package/src/rules/no-reinterpret-cast.ts +0 -78
- package/src/rules/no-runtime-typeof.ts +0 -59
- package/src/rules/no-sentinel-comparison-union.ts +0 -148
- package/src/rules/no-shape-in-symbol-names.ts +0 -45
- package/src/rules/no-single-use-private-functions.ts +0 -329
- package/src/rules/no-stacked-jsdoc-blocks.ts +0 -65
- package/src/rules/no-stray-inline-comments.ts +0 -79
- package/src/rules/no-swappable-primitive-params.ts +0 -109
- package/src/rules/no-tag-ladder-assertions.ts +0 -71
- package/src/rules/no-transposed-field-reads.ts +0 -135
- package/src/rules/no-unit-return-validators.ts +0 -65
- package/src/rules/no-unknown-parameters.ts +0 -97
- package/src/rules/no-unknown-returns.ts +0 -120
- package/src/rules/no-unknown-type-aliases.ts +0 -70
- package/src/rules/no-unsafe-dictionary-type.ts +0 -132
- package/src/rules/no-vague-test-filenames.ts +0 -67
- package/src/rules/no-widen-then-assert.ts +0 -345
- package/src/rules/prefer-payload-brand.ts +0 -67
- package/src/rules/prefer-property-tests.ts +0 -43
- package/src/rules/require-canonical-stringify-for-identity.ts +0 -80
- package/src/rules/require-deprecated-tag-for-legacy-comments.ts +0 -45
- package/src/rules/require-exhaustive-tag-switch.ts +0 -42
- package/src/rules/require-fc-block-predicate.ts +0 -42
- package/src/rules/require-jsdoc-on-exported.ts +0 -74
- package/src/rules/require-published-order.ts +0 -109
- package/src/rules/require-safety-comment-for-type-assertion.ts +0 -61
- package/src/rules/require-sort-comparator.ts +0 -41
- package/src/rules/test-utils.ts +0 -68
- package/src/shared/child-nodes.ts +0 -27
- package/src/shared/dictionary-types.ts +0 -524
- package/src/shared/lexical-type-parameters.ts +0 -62
- package/src/shared/reflect-method.ts +0 -41
- package/src/testing/testable-rule.test.ts +0 -68
- package/src/testing/testable-rule.ts +0 -86
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* The closed domain of values reachable from ESTree node fields.
|
|
5
4
|
*
|
|
@@ -7,17 +6,7 @@ import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
|
7
6
|
* yields exactly this union, so guards accept it instead of re-widening
|
|
8
7
|
* caller evidence to unknown.
|
|
9
8
|
*/
|
|
10
|
-
export type NodeFieldValue =
|
|
11
|
-
| ESTree.Node
|
|
12
|
-
| readonly ESTree.Node[]
|
|
13
|
-
| string
|
|
14
|
-
| number
|
|
15
|
-
| bigint
|
|
16
|
-
| boolean
|
|
17
|
-
| RegExp
|
|
18
|
-
| null
|
|
19
|
-
| undefined;
|
|
20
|
-
|
|
9
|
+
export type NodeFieldValue = ESTree.Node | readonly ESTree.Node[] | string | number | bigint | boolean | RegExp | null | undefined;
|
|
21
10
|
/**
|
|
22
11
|
* Read a field from an ESTree node without chained assertions.
|
|
23
12
|
*
|
|
@@ -29,53 +18,37 @@ export type NodeFieldValue =
|
|
|
29
18
|
* @param {string} key - The field name.
|
|
30
19
|
* @returns {T | undefined} The field value, or undefined when absent.
|
|
31
20
|
*/
|
|
32
|
-
export function readField<T = NodeFieldValue>(
|
|
33
|
-
node: object | null | undefined,
|
|
34
|
-
key: string,
|
|
35
|
-
): T | undefined {
|
|
36
|
-
if (node === null || node === undefined) return undefined;
|
|
37
|
-
return (node as Record<string, T>)[key];
|
|
38
|
-
}
|
|
39
|
-
|
|
21
|
+
export declare function readField<T = NodeFieldValue>(node: object | null | undefined, key: string): T | undefined;
|
|
40
22
|
/** Discriminate string-valued fields or expressions of an AST node.
|
|
41
23
|
*
|
|
42
24
|
* @param {NodeFieldValue | ESTree.Expression} value - The value to test.
|
|
43
25
|
* @returns {boolean} True when the value is a string.
|
|
44
26
|
*/
|
|
45
|
-
export function isString(value: NodeFieldValue | ESTree.Expression): value is string
|
|
46
|
-
return typeof value === "string";
|
|
47
|
-
}
|
|
48
|
-
|
|
27
|
+
export declare function isString(value: NodeFieldValue | ESTree.Expression): value is string;
|
|
49
28
|
/** Engine comment shape, normalized for rule consumption. */
|
|
50
29
|
export interface CommentLike {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
30
|
+
type: string;
|
|
31
|
+
value: string;
|
|
32
|
+
end?: number;
|
|
33
|
+
range?: readonly [number, number];
|
|
34
|
+
loc?: {
|
|
35
|
+
start?: {
|
|
36
|
+
line?: number;
|
|
37
|
+
};
|
|
38
|
+
end?: {
|
|
39
|
+
line?: number;
|
|
40
|
+
};
|
|
41
|
+
};
|
|
59
42
|
}
|
|
60
|
-
|
|
61
43
|
/** All comments in the file, typed for rule consumption.
|
|
62
44
|
*
|
|
63
45
|
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
64
46
|
* @returns {CommentLike[]} The file's comments.
|
|
65
47
|
*/
|
|
66
|
-
export function getAllComments(sourceCode: SourceCode): CommentLike[]
|
|
67
|
-
return sourceCode.getAllComments() as CommentLike[];
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
48
|
+
export declare function getAllComments(sourceCode: SourceCode): CommentLike[];
|
|
72
49
|
/** Discriminate object-valued fields of an AST node (child containers).
|
|
73
50
|
*
|
|
74
51
|
* @param {NodeFieldValue | Record<string, NodeFieldValue>} value - The field value to test.
|
|
75
52
|
* @returns {boolean} True when the value is a non-array object record.
|
|
76
53
|
*/
|
|
77
|
-
export function isRecordObject(
|
|
78
|
-
value: NodeFieldValue | Record<string, NodeFieldValue>,
|
|
79
|
-
): value is Record<string, NodeFieldValue> {
|
|
80
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
81
|
-
}
|
|
54
|
+
export declare function isRecordObject(value: NodeFieldValue | Record<string, NodeFieldValue>): value is Record<string, NodeFieldValue>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Read a field from an ESTree node without chained assertions.
|
|
3
|
+
*
|
|
4
|
+
* SAFETY: callers invoke this after a discriminant (or containment) check that
|
|
5
|
+
* establishes the key exists on the node's runtime shape; the value's domain is
|
|
6
|
+
* owned by the AST grammar (see NodeFieldValue), not by untrusted input.
|
|
7
|
+
*
|
|
8
|
+
* @param {object | null | undefined} node - The node whose field is read.
|
|
9
|
+
* @param {string} key - The field name.
|
|
10
|
+
* @returns {T | undefined} The field value, or undefined when absent.
|
|
11
|
+
*/
|
|
12
|
+
export function readField(node, key) {
|
|
13
|
+
if (node === null || node === undefined)
|
|
14
|
+
return undefined;
|
|
15
|
+
return node[key];
|
|
16
|
+
}
|
|
17
|
+
/** Discriminate string-valued fields or expressions of an AST node.
|
|
18
|
+
*
|
|
19
|
+
* @param {NodeFieldValue | ESTree.Expression} value - The value to test.
|
|
20
|
+
* @returns {boolean} True when the value is a string.
|
|
21
|
+
*/
|
|
22
|
+
export function isString(value) {
|
|
23
|
+
return typeof value === "string";
|
|
24
|
+
}
|
|
25
|
+
/** All comments in the file, typed for rule consumption.
|
|
26
|
+
*
|
|
27
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
28
|
+
* @returns {CommentLike[]} The file's comments.
|
|
29
|
+
*/
|
|
30
|
+
export function getAllComments(sourceCode) {
|
|
31
|
+
return sourceCode.getAllComments();
|
|
32
|
+
}
|
|
33
|
+
/** Discriminate object-valued fields of an AST node (child containers).
|
|
34
|
+
*
|
|
35
|
+
* @param {NodeFieldValue | Record<string, NodeFieldValue>} value - The field value to test.
|
|
36
|
+
* @returns {boolean} True when the value is a non-array object record.
|
|
37
|
+
*/
|
|
38
|
+
export function isRecordObject(value) {
|
|
39
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
40
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createTester, testableRule, wireRuleTester } from "./testable-rule.js";
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { RuleTester } from "oxlint/plugins-dev";
|
|
2
|
+
import type { Rule } from "@oxlint/plugins";
|
|
3
|
+
/**
|
|
4
|
+
* Structural surface of a test runner, satisfied by vitest's `describe`/`it`/
|
|
5
|
+
* `it.only` (and by jest/mocha equivalents).
|
|
6
|
+
*/
|
|
7
|
+
export interface TestRunner {
|
|
8
|
+
describe: (name: string, fn: () => void) => void;
|
|
9
|
+
it: (name: string, fn: () => void) => void;
|
|
10
|
+
itOnly?: (name: string, fn: () => void) => void;
|
|
11
|
+
}
|
|
12
|
+
/** Bind a test runner's hooks to RuleTester's statics.
|
|
13
|
+
*
|
|
14
|
+
* Call once at module scope of a test file (or setup file) before running suites.
|
|
15
|
+
*
|
|
16
|
+
* @param {TestRunner} runner - The ambient runner's hooks.
|
|
17
|
+
*/
|
|
18
|
+
export declare function wireRuleTester(runner: TestRunner): void;
|
|
19
|
+
/** A tester instance bound to the wired runner.
|
|
20
|
+
*
|
|
21
|
+
* @returns {RuleTester} A fresh RuleTester.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createTester(): RuleTester;
|
|
24
|
+
/**
|
|
25
|
+
* Adapt a `createOnce` rule for Oxlint's RuleTester.
|
|
26
|
+
*
|
|
27
|
+
* RuleTester drives rules through their ESLint-compatible `create` method and
|
|
28
|
+
* does not understand `before`/`after` lifecycle hooks. The adapter invokes
|
|
29
|
+
* `createOnce` once per linted file (fresh state per file, exactly like native
|
|
30
|
+
* Oxlint), honours the skip contract (`before` returning `false` skips the
|
|
31
|
+
* file), and chains the rule's own `Program:exit` visitor with the after hook
|
|
32
|
+
* instead of clobbering it.
|
|
33
|
+
*
|
|
34
|
+
* @param {CreateOnceRule | Rule} rule - The createOnce-style rule to adapt. A
|
|
35
|
+
* rule already in ESLint-style passes through untouched.
|
|
36
|
+
* @returns {Rule} An ESLint-compatible rule with an identical per-file lifecycle.
|
|
37
|
+
*/
|
|
38
|
+
export declare function testableRule(rule: Rule): Rule;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { RuleTester } from "oxlint/plugins-dev";
|
|
2
|
+
/** Narrow to the createOnce surface the tester adapts.
|
|
3
|
+
*
|
|
4
|
+
* @param {Rule} rule - The rule to test.
|
|
5
|
+
* @returns {boolean} True when the rule carries a createOnce factory.
|
|
6
|
+
*/
|
|
7
|
+
function isCreateOnceRule(rule) {
|
|
8
|
+
return "createOnce" in rule;
|
|
9
|
+
}
|
|
10
|
+
/** Bind a test runner's hooks to RuleTester's statics.
|
|
11
|
+
*
|
|
12
|
+
* Call once at module scope of a test file (or setup file) before running suites.
|
|
13
|
+
*
|
|
14
|
+
* @param {TestRunner} runner - The ambient runner's hooks.
|
|
15
|
+
*/
|
|
16
|
+
export function wireRuleTester(runner) {
|
|
17
|
+
RuleTester.describe = runner.describe;
|
|
18
|
+
RuleTester.it = runner.it;
|
|
19
|
+
if (runner.itOnly !== undefined) {
|
|
20
|
+
RuleTester.itOnly = runner.itOnly;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/** A tester instance bound to the wired runner.
|
|
24
|
+
*
|
|
25
|
+
* @returns {RuleTester} A fresh RuleTester.
|
|
26
|
+
*/
|
|
27
|
+
export function createTester() {
|
|
28
|
+
return new RuleTester();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Adapt a `createOnce` rule for Oxlint's RuleTester.
|
|
32
|
+
*
|
|
33
|
+
* RuleTester drives rules through their ESLint-compatible `create` method and
|
|
34
|
+
* does not understand `before`/`after` lifecycle hooks. The adapter invokes
|
|
35
|
+
* `createOnce` once per linted file (fresh state per file, exactly like native
|
|
36
|
+
* Oxlint), honours the skip contract (`before` returning `false` skips the
|
|
37
|
+
* file), and chains the rule's own `Program:exit` visitor with the after hook
|
|
38
|
+
* instead of clobbering it.
|
|
39
|
+
*
|
|
40
|
+
* @param {CreateOnceRule | Rule} rule - The createOnce-style rule to adapt. A
|
|
41
|
+
* rule already in ESLint-style passes through untouched.
|
|
42
|
+
* @returns {Rule} An ESLint-compatible rule with an identical per-file lifecycle.
|
|
43
|
+
*/
|
|
44
|
+
export function testableRule(rule) {
|
|
45
|
+
if (!isCreateOnceRule(rule))
|
|
46
|
+
return rule;
|
|
47
|
+
const adapter = {
|
|
48
|
+
meta: rule.meta,
|
|
49
|
+
create(context) {
|
|
50
|
+
const { after, before, ...visitor } = rule.createOnce(context);
|
|
51
|
+
// SAFETY: visitor fragments come from this plugin's own createOnce
|
|
52
|
+
// contract; method keys map one-to-one onto the tester's call shape.
|
|
53
|
+
const visitors = visitor;
|
|
54
|
+
const skip = before !== undefined && before() === false;
|
|
55
|
+
if (skip)
|
|
56
|
+
return {};
|
|
57
|
+
const previousExit = visitors["Program:exit"];
|
|
58
|
+
return {
|
|
59
|
+
...visitors,
|
|
60
|
+
"Program:exit"(...callArguments) {
|
|
61
|
+
previousExit?.(...callArguments);
|
|
62
|
+
after?.();
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
// SAFETY: the adapter reproduces Rule's meta/create surface; its visitors
|
|
68
|
+
// are the createOnce fragments the tester already consumes.
|
|
69
|
+
return adapter;
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,23 +1,24 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@autotelic/plumb",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Opinionated Oxlint rules that keep TypeScript true: generic and Effect-specific patterns, maintained as one source of truth for the autotelic repos.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"files": [
|
|
7
|
-
"
|
|
7
|
+
"dist"
|
|
8
8
|
],
|
|
9
9
|
"publishConfig": {
|
|
10
10
|
"access": "public"
|
|
11
11
|
},
|
|
12
12
|
"type": "module",
|
|
13
13
|
"exports": {
|
|
14
|
-
".": "./
|
|
15
|
-
"./effect": "./
|
|
16
|
-
"./meta": "./
|
|
17
|
-
"./testing": "./
|
|
14
|
+
".": "./dist/index.js",
|
|
15
|
+
"./effect": "./dist/effect/index.js",
|
|
16
|
+
"./meta": "./dist/meta/index.js",
|
|
17
|
+
"./testing": "./dist/testing/index.js"
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"check": "pnpm lint && pnpm typecheck && pnpm test && pnpm check:rule-tests && pnpm check:config",
|
|
21
|
+
"build": "tsc -p tsconfig.build.json",
|
|
21
22
|
"lint": "oxlint src",
|
|
22
23
|
"typecheck": "tsc --noEmit",
|
|
23
24
|
"test": "vitest run --passWithNoTests",
|
package/src/effect/index.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import { guardedOpMustReturnEffectRule } from "./rules/guarded-op-must-return-effect.ts";
|
|
4
|
-
import { guardedOpMustReturnOptionRule } from "./rules/guarded-op-must-return-option.ts";
|
|
5
|
-
import { noDecodeUnknownOptionRule } from "./rules/no-decode-unknown-option.ts";
|
|
6
|
-
import { noManualFieldGuardsRule } from "./rules/no-manual-field-guards.ts";
|
|
7
|
-
import { noUnguardedJsonParseRule } from "./rules/no-unguarded-json-parse.ts";
|
|
8
|
-
import { noSchemaClassModelingRule } from "./rules/no-schema-class-modeling.ts";
|
|
9
|
-
import { noServiceConstructorImportsRule } from "./rules/no-service-constructor-imports.ts";
|
|
10
|
-
import { optionCoreNeedsEffectPublicRule } from "./rules/option-core-needs-effect-public.ts";
|
|
11
|
-
import { preferDieForPreconditionDefectsRule } from "./rules/prefer-die-for-precondition-defects.ts";
|
|
12
|
-
import { preferEffectGenForGuardLaddersRule } from "./rules/prefer-effect-gen-for-guard-ladders.ts";
|
|
13
|
-
import { preferEffectPredicateOverBooleanFunctionRule } from "./rules/prefer-effect-predicate-over-boolean-function.ts";
|
|
14
|
-
import { preferNamedGuardPredicateRule } from "./rules/prefer-named-guard-predicate.ts";
|
|
15
|
-
import { preferOrderingMatchRule } from "./rules/prefer-ordering-match.ts";
|
|
16
|
-
import { preferOptionPipelineRule } from "./rules/prefer-option-pipeline.ts";
|
|
17
|
-
import { preferTaggedEnumRule } from "./rules/prefer-tagged-enum.ts";
|
|
18
|
-
import { requireSchemaTypeDerivationRule } from "./rules/require-schema-type-derivation.ts";
|
|
19
|
-
import { noDirectFetchRule } from "./rules/no-direct-fetch.ts";
|
|
20
|
-
import { noDirectBrowserStorageRule } from "./rules/no-direct-browser-storage.ts";
|
|
21
|
-
import { noTryCatchRule } from "./rules/no-try-catch.ts";
|
|
22
|
-
import { noSilentErrorSwallowRule } from "./rules/no-silent-error-swallow.ts";
|
|
23
|
-
import { preferEffectMatchRule } from "./rules/prefer-effect-match.ts";
|
|
24
|
-
import { noNestedLayerProvideRule } from "./rules/no-nested-layer-provide.ts";
|
|
25
|
-
import { noCascadingLayerProvideRule } from "./rules/no-cascading-layer-provide.ts";
|
|
26
|
-
import { noStaticEffectServiceForwardersRule } from "./rules/no-static-effect-service-forwarders.ts";
|
|
27
|
-
|
|
28
|
-
/** Opt-in Oxlint rules for Effect service and Layer architecture. */
|
|
29
|
-
const plumbEffectPlugin = eslintCompatPlugin({
|
|
30
|
-
meta: { name: "plumb-effect" },
|
|
31
|
-
rules: {
|
|
32
|
-
"guarded-op-must-return-effect": guardedOpMustReturnEffectRule,
|
|
33
|
-
"guarded-op-must-return-option": guardedOpMustReturnOptionRule,
|
|
34
|
-
"no-decode-unknown-option": noDecodeUnknownOptionRule,
|
|
35
|
-
"no-manual-field-guards": noManualFieldGuardsRule,
|
|
36
|
-
"no-unguarded-json-parse": noUnguardedJsonParseRule,
|
|
37
|
-
"option-core-needs-effect-public": optionCoreNeedsEffectPublicRule,
|
|
38
|
-
"no-schema-class-modeling": noSchemaClassModelingRule,
|
|
39
|
-
"no-service-constructor-imports": noServiceConstructorImportsRule,
|
|
40
|
-
"prefer-die-for-precondition-defects": preferDieForPreconditionDefectsRule,
|
|
41
|
-
"prefer-effect-gen-for-guard-ladders": preferEffectGenForGuardLaddersRule,
|
|
42
|
-
"prefer-effect-predicate-over-boolean-function": preferEffectPredicateOverBooleanFunctionRule,
|
|
43
|
-
"prefer-named-guard-predicate": preferNamedGuardPredicateRule,
|
|
44
|
-
"prefer-option-pipeline": preferOptionPipelineRule,
|
|
45
|
-
"prefer-ordering-match": preferOrderingMatchRule,
|
|
46
|
-
"prefer-tagged-enum": preferTaggedEnumRule,
|
|
47
|
-
"require-schema-type-derivation": requireSchemaTypeDerivationRule,
|
|
48
|
-
"no-direct-fetch": noDirectFetchRule,
|
|
49
|
-
"no-direct-browser-storage": noDirectBrowserStorageRule,
|
|
50
|
-
"no-try-catch": noTryCatchRule,
|
|
51
|
-
"no-silent-error-swallow": noSilentErrorSwallowRule,
|
|
52
|
-
"prefer-effect-match": preferEffectMatchRule,
|
|
53
|
-
"no-nested-layer-provide": noNestedLayerProvideRule,
|
|
54
|
-
"no-cascading-layer-provide": noCascadingLayerProvideRule,
|
|
55
|
-
"no-static-effect-service-forwarders": noStaticEffectServiceForwardersRule,
|
|
56
|
-
},
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
export default plumbEffectPlugin;
|
|
@@ -1,236 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
-
|
|
5
|
-
import { ancestorsOf } from "../../shared/ancestors.ts";
|
|
6
|
-
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
|
|
7
|
-
|
|
8
|
-
/** Whether the member chain ends in the Option namespace, e.g. `Option` or `O`.
|
|
9
|
-
*
|
|
10
|
-
* @param {ESTree.Expression | ESTree.Super} node - The callee object to inspect.
|
|
11
|
-
* @returns {boolean} True when the object resolves to the Option namespace.
|
|
12
|
-
*/
|
|
13
|
-
function isOptionObject(node: ESTree.Expression | ESTree.Super): boolean {
|
|
14
|
-
if (node.type === "Identifier") return node.name === "Option";
|
|
15
|
-
if (node.type === "MemberExpression" && !node.computed && node.property.type === "Identifier") {
|
|
16
|
-
return node.property.name === "Option";
|
|
17
|
-
}
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/** The name of the function whose body contains this node, if determinable.
|
|
22
|
-
*
|
|
23
|
-
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
24
|
-
* @param {ESTree.Node} node - The node whose enclosing function is sought.
|
|
25
|
-
* @returns {string | null} The enclosing function's name, or null.
|
|
26
|
-
*/
|
|
27
|
-
function enclosingFunctionName(sourceCode: SourceCode, node: ESTree.Node): string | null {
|
|
28
|
-
const chain = ancestorsOf(sourceCode, node);
|
|
29
|
-
for (let index = chain.length - 1; index >= 0; index--) {
|
|
30
|
-
const current = chain[index]!;
|
|
31
|
-
if (current.type === "FunctionDeclaration" && current.id !== null) return current.id.name;
|
|
32
|
-
if (
|
|
33
|
-
(current.type === "FunctionExpression" || current.type === "ArrowFunctionExpression") &&
|
|
34
|
-
node !== current
|
|
35
|
-
) {
|
|
36
|
-
const name =
|
|
37
|
-
current.type === "FunctionExpression" && current.id !== null
|
|
38
|
-
? current.id.name
|
|
39
|
-
: variableDeclaratorName(sourceCode, current);
|
|
40
|
-
if (name !== null) return name;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
return null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/** The declared name of the variable a function expression initialises, if any.
|
|
47
|
-
*
|
|
48
|
-
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
49
|
-
* @param {ESTree.Node} node - The function expression node.
|
|
50
|
-
* @returns {string | null} The initialising declarator's name, or null.
|
|
51
|
-
*/
|
|
52
|
-
function variableDeclaratorName(sourceCode: SourceCode, node: ESTree.Node): string | null {
|
|
53
|
-
const chain = ancestorsOf(sourceCode, node);
|
|
54
|
-
for (let index = chain.length - 1; index >= 0; index--) {
|
|
55
|
-
const current = chain[index]!;
|
|
56
|
-
if (current.type === "Program") break;
|
|
57
|
-
if (current.type === "VariableDeclarator" && current.id.type === "Identifier") return current.id.name;
|
|
58
|
-
}
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/** The doctrine binds public APIs: walk up to see if this node is exported.
|
|
63
|
-
*
|
|
64
|
-
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
65
|
-
* @param {ESTree.Node} node - The node to test for export ancestry.
|
|
66
|
-
* @returns {boolean} True when an export declaration encloses the node.
|
|
67
|
-
*/
|
|
68
|
-
function hasExportAncestor(sourceCode: SourceCode, node: ESTree.Node): boolean {
|
|
69
|
-
const chain = ancestorsOf(sourceCode, node);
|
|
70
|
-
for (let index = chain.length - 1; index >= 0; index--) {
|
|
71
|
-
const current = chain[index]!;
|
|
72
|
-
if (current.type === "ExportNamedDeclaration" || current.type === "ExportDefaultDeclaration") return true;
|
|
73
|
-
if (current.type === "Program") return false;
|
|
74
|
-
}
|
|
75
|
-
return false;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/** A return-type annotation naming Option<T>, e.g. `Option.Option<Account>`.
|
|
79
|
-
*
|
|
80
|
-
* @param {ESTree.TSType} type - The type node to inspect.
|
|
81
|
-
* @returns {string | null} The Option spelling used, or null when not an Option reference.
|
|
82
|
-
*/
|
|
83
|
-
function optionTypeReference(type: ESTree.TSType): string | null {
|
|
84
|
-
if (type.type !== "TSTypeReference") return null;
|
|
85
|
-
const name = type.typeName;
|
|
86
|
-
if (name.type === "Identifier" && name.name === "Option") return "Option";
|
|
87
|
-
if (
|
|
88
|
-
name.type === "TSQualifiedName" &&
|
|
89
|
-
name.right.type === "Identifier" &&
|
|
90
|
-
name.right.name === "Option"
|
|
91
|
-
) {
|
|
92
|
-
return "Option.Option";
|
|
93
|
-
}
|
|
94
|
-
return null;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Guarded operations must surface failure reasons as tagged MoneyError values
|
|
99
|
-
* through the Effect error channel, not as bare Option.None which conflates
|
|
100
|
-
* every possible why into one silent absence.
|
|
101
|
-
*/
|
|
102
|
-
export const guardedOpMustReturnEffectRule = defineRule({
|
|
103
|
-
meta: {
|
|
104
|
-
type: "problem",
|
|
105
|
-
docs: {
|
|
106
|
-
description:
|
|
107
|
-
"Require guarded operations to fail with typed MoneyError reasons via Effect/Either instead of returning bare Option values.",
|
|
108
|
-
},
|
|
109
|
-
messages: {
|
|
110
|
-
optionReturn:
|
|
111
|
-
"`{{found}}` hides why the operation failed. Return `Effect.Effect<T, MoneyError>` (or `Either<T, MoneyError>` in sync cores) with Schema.TaggedError reasons so callers can match Overflow vs InvalidInput vs ZeroDivisor. Options buried in tuples, records or arrays hide it just as well.",
|
|
112
|
-
bareNone:
|
|
113
|
-
"`Option.none()` here discards the reason for failure. Fail with a tagged MoneyError reason instead, or keep the Option strictly internal while a converted public API owns the error channel.",
|
|
114
|
-
},
|
|
115
|
-
},
|
|
116
|
-
createOnce(context) {
|
|
117
|
-
return {
|
|
118
|
-
before() {
|
|
119
|
-
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
120
|
-
},
|
|
121
|
-
ReturnStatement(node) {
|
|
122
|
-
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
123
|
-
if (enclosingFunctionName(context.sourceCode, node)?.endsWith("Option")) return;
|
|
124
|
-
const argument = node.argument;
|
|
125
|
-
if (argument === null || argument === undefined) return;
|
|
126
|
-
let call = argument;
|
|
127
|
-
while (call.type === "ParenthesizedExpression") call = call.expression;
|
|
128
|
-
if (call.type !== "CallExpression") return;
|
|
129
|
-
if (
|
|
130
|
-
call.callee.type === "MemberExpression" &&
|
|
131
|
-
call.callee.property.type === "Identifier" &&
|
|
132
|
-
call.callee.property.name === "none" &&
|
|
133
|
-
isOptionObject(call.callee.object)
|
|
134
|
-
) {
|
|
135
|
-
context.report({ node, messageId: "bareNone" });
|
|
136
|
-
}
|
|
137
|
-
},
|
|
138
|
-
ArrowFunctionExpression(node) {
|
|
139
|
-
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
140
|
-
const parentId = variableDeclaratorName(context.sourceCode, node);
|
|
141
|
-
if (parentId !== null && parentId.endsWith("Option")) return;
|
|
142
|
-
const annotation = node.returnType?.typeAnnotation;
|
|
143
|
-
if (annotation === undefined) return;
|
|
144
|
-
const found = optionInReturnType(annotation);
|
|
145
|
-
if (found !== null) {
|
|
146
|
-
context.report({ node: node.returnType ?? node, messageId: "optionReturn", data: { found } });
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
FunctionDeclaration(node) {
|
|
150
|
-
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
151
|
-
checkFunctionReturn(context, node, node.id?.name ?? null);
|
|
152
|
-
},
|
|
153
|
-
FunctionExpression(node) {
|
|
154
|
-
if (!hasExportAncestor(context.sourceCode, node)) return;
|
|
155
|
-
checkFunctionReturn(
|
|
156
|
-
context,
|
|
157
|
-
node,
|
|
158
|
-
node.id?.name ?? variableDeclaratorName(context.sourceCode, node),
|
|
159
|
-
);
|
|
160
|
-
},
|
|
161
|
-
};
|
|
162
|
-
},
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
function checkFunctionReturn(
|
|
166
|
-
context: { report(diagnostic: { node: ESTree.Node; messageId: string; data?: Record<string, string> }): void },
|
|
167
|
-
node: ESTree.Function,
|
|
168
|
-
idName: string | null,
|
|
169
|
-
): void {
|
|
170
|
-
if (idName !== null && idName.endsWith("Option")) return;
|
|
171
|
-
const annotation = node.returnType?.typeAnnotation;
|
|
172
|
-
if (annotation === undefined) return;
|
|
173
|
-
const found = optionInReturnType(annotation);
|
|
174
|
-
if (found !== null) {
|
|
175
|
-
context.report({ node: node.returnType ?? node, messageId: "optionReturn", data: { found } });
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
/** Search a type node for an embedded Option reference (tuples, arrays, members).
|
|
181
|
-
*
|
|
182
|
-
* @param {ESTree.TSType} type - The type node to search.
|
|
183
|
-
* @returns {string | null} The first embedded Option spelling, or null when none.
|
|
184
|
-
*/
|
|
185
|
-
function findOptionType(type: ESTree.TSType): string | null {
|
|
186
|
-
const direct = optionTypeReference(type);
|
|
187
|
-
if (direct !== null) return direct;
|
|
188
|
-
switch (type.type) {
|
|
189
|
-
case "TSTupleType": {
|
|
190
|
-
const render = (element: ESTree.TSTupleElement): string | null => {
|
|
191
|
-
if ("elementType" in element) return render(element.elementType);
|
|
192
|
-
if (element.type === "TSOptionalType" || element.type === "TSRestType") {
|
|
193
|
-
return findOptionType(element.typeAnnotation);
|
|
194
|
-
}
|
|
195
|
-
return findOptionType(element);
|
|
196
|
-
};
|
|
197
|
-
for (const element of type.elementTypes) {
|
|
198
|
-
const found = render(element);
|
|
199
|
-
if (found !== null) return found;
|
|
200
|
-
}
|
|
201
|
-
return null;
|
|
202
|
-
}
|
|
203
|
-
case "TSTypeOperator":
|
|
204
|
-
return findOptionType(type.typeAnnotation);
|
|
205
|
-
case "TSArrayType":
|
|
206
|
-
return findOptionType(type.elementType);
|
|
207
|
-
case "TSTypeLiteral":
|
|
208
|
-
for (const member of type.members) {
|
|
209
|
-
if (member.type !== "TSPropertySignature") continue;
|
|
210
|
-
const annotation = member.typeAnnotation?.typeAnnotation;
|
|
211
|
-
if (annotation === undefined || annotation === null) continue;
|
|
212
|
-
const found = findOptionType(annotation);
|
|
213
|
-
if (found !== null) return found;
|
|
214
|
-
}
|
|
215
|
-
return null;
|
|
216
|
-
default:
|
|
217
|
-
return null;
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
/** A return-type annotation hiding an Option anywhere: unions, tuples, arrays, members.
|
|
222
|
-
*
|
|
223
|
-
* @param {ESTree.TSType} annotation - The return-type annotation to search.
|
|
224
|
-
* @returns {string | null} The Option spelling found, or null when none.
|
|
225
|
-
*/
|
|
226
|
-
function optionInReturnType(annotation: ESTree.TSType): string | null {
|
|
227
|
-
let current = annotation;
|
|
228
|
-
while (current.type === "TSParenthesizedType") current = current.typeAnnotation;
|
|
229
|
-
if (current.type === "TSUnionType") {
|
|
230
|
-
for (const member of current.types) {
|
|
231
|
-
const found = optionTypeReference(member);
|
|
232
|
-
if (found !== null) return found;
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
return findOptionType(current);
|
|
236
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
-
import { ancestorsOf } from "../../shared/ancestors.ts";
|
|
5
|
-
|
|
6
|
-
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
7
|
-
|
|
8
|
-
type FunctionWithReturnType = ESTree.ArrowFunctionExpression | ESTree.Function;
|
|
9
|
-
|
|
10
|
-
function isNullableKeyword(type: ESTree.TSType): boolean {
|
|
11
|
-
return type.type === "TSNullKeyword" || type.type === "TSUndefinedKeyword";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Returns whether a union pairs a nullable member with at least one real
|
|
16
|
-
* value type (T | null/undefined, not void | undefined).
|
|
17
|
-
*
|
|
18
|
-
* @param {ESTree.TSUnionType} union - The union annotation to inspect.
|
|
19
|
-
* @returns {boolean} True when the union mixes null/undefined with value types.
|
|
20
|
-
*/
|
|
21
|
-
function nullableWithValue(union: ESTree.TSUnionType): boolean {
|
|
22
|
-
let nullable = false;
|
|
23
|
-
let value = false;
|
|
24
|
-
for (const member of union.types) {
|
|
25
|
-
if (isNullableKeyword(member)) {
|
|
26
|
-
nullable = true;
|
|
27
|
-
} else if (member.type !== "TSVoidKeyword" && member.type !== "TSNeverKeyword") {
|
|
28
|
-
value = true;
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
return nullable && value;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function asUnion(type: ESTree.TSType): ESTree.TSUnionType | null {
|
|
35
|
-
if (type.type === "TSParenthesizedType") return asUnion(type.typeAnnotation);
|
|
36
|
-
return type.type === "TSUnionType" ? type : null;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/** Guarded operations must surface absence as Option, not null/undefined unions or crashes. */
|
|
40
|
-
export const guardedOpMustReturnOptionRule = defineRule({
|
|
41
|
-
meta: {
|
|
42
|
-
type: "problem",
|
|
43
|
-
docs: {
|
|
44
|
-
description:
|
|
45
|
-
"Disallow returning T | null/undefined from guarded operations and getOrThrow escapes; express absence as Option<T> so callers compose with Option.zip/flatMap/filter.",
|
|
46
|
-
},
|
|
47
|
-
messages: {
|
|
48
|
-
nullableReturn:
|
|
49
|
-
"This function returns `{{type}}`, conflating absence with a value. Return Option<{{value}}> and express absence as Option.none().",
|
|
50
|
-
optionEscape:
|
|
51
|
-
"`getOrThrow` turns absence into a runtime crash. Keep the Option in the type and handle None, or parse to a refined value so the empty branch can't exist.",
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
createOnce(context) {
|
|
55
|
-
|
|
56
|
-
const checkReturnType = (node: FunctionWithReturnType) => {
|
|
57
|
-
const annotation = node.returnType;
|
|
58
|
-
if (annotation === null || annotation === undefined) return;
|
|
59
|
-
const type = annotation.typeAnnotation;
|
|
60
|
-
const union = asUnion(type);
|
|
61
|
-
if (union === null || !nullableWithValue(union)) return;
|
|
62
|
-
const value = union.types
|
|
63
|
-
.filter((member) => !isNullableKeyword(member) && member.type !== "TSVoidKeyword")
|
|
64
|
-
.map((member) => context.sourceCode.getText(member))
|
|
65
|
-
.join(" | ");
|
|
66
|
-
context.report({
|
|
67
|
-
node: type,
|
|
68
|
-
messageId: "nullableReturn",
|
|
69
|
-
data: { type: context.sourceCode.getText(type), value },
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
return {
|
|
74
|
-
before() {
|
|
75
|
-
if (TEST_FILE.test(context.filename.replaceAll("\\", "/"))) return false;
|
|
76
|
-
},
|
|
77
|
-
ArrowFunctionExpression: checkReturnType,
|
|
78
|
-
FunctionDeclaration: checkReturnType,
|
|
79
|
-
FunctionExpression: checkReturnType,
|
|
80
|
-
MemberExpression(node) {
|
|
81
|
-
if (
|
|
82
|
-
!node.computed &&
|
|
83
|
-
node.property.type === "Identifier" &&
|
|
84
|
-
node.property.name === "getOrThrow"
|
|
85
|
-
) {
|
|
86
|
-
context.report({ node, messageId: "optionEscape" });
|
|
87
|
-
}
|
|
88
|
-
},
|
|
89
|
-
Identifier(node) {
|
|
90
|
-
if (node.name !== "getOrThrow") return;
|
|
91
|
-
const parent = (ancestorsOf(context.sourceCode, node)).at(-1);
|
|
92
|
-
if (parent?.type === "MemberExpression" && parent.property === node) return;
|
|
93
|
-
context.report({ node, messageId: "optionEscape" });
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
},
|
|
97
|
-
});
|