@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
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { classifyWideningTarget, createTypeEnvironment, isKnownEvidenceExpression, } from "../shared/dictionary-types.js";
|
|
3
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
4
|
+
function unwrapExpression(expression) {
|
|
5
|
+
let current = expression;
|
|
6
|
+
while (current.type === "ParenthesizedExpression" ||
|
|
7
|
+
current.type === "TSAsExpression" ||
|
|
8
|
+
current.type === "TSSatisfiesExpression" ||
|
|
9
|
+
current.type === "TSTypeAssertion" ||
|
|
10
|
+
current.type === "TSNonNullExpression") {
|
|
11
|
+
current = current.expression;
|
|
12
|
+
}
|
|
13
|
+
return current;
|
|
14
|
+
}
|
|
15
|
+
function resolveVariable(sourceCode, identifier) {
|
|
16
|
+
let scope = sourceCode.getScope(identifier);
|
|
17
|
+
while (scope !== null) {
|
|
18
|
+
const variable = scope.set.get(identifier.name);
|
|
19
|
+
if (variable !== undefined)
|
|
20
|
+
return variable;
|
|
21
|
+
scope = scope.upper;
|
|
22
|
+
}
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
function variableDeclarator(variable) {
|
|
26
|
+
if (variable.defs.length !== 1)
|
|
27
|
+
return null;
|
|
28
|
+
const [definition] = variable.defs;
|
|
29
|
+
return definition?.type === "Variable" && definition.node.type === "VariableDeclarator"
|
|
30
|
+
? definition.node
|
|
31
|
+
: null;
|
|
32
|
+
}
|
|
33
|
+
function sourceKeyName(sourceCode, key) {
|
|
34
|
+
if (key.type === "Identifier" || key.type === "PrivateIdentifier")
|
|
35
|
+
return key.name;
|
|
36
|
+
if (key.type === "Literal")
|
|
37
|
+
return String(key.value);
|
|
38
|
+
return sourceCode.getText(key);
|
|
39
|
+
}
|
|
40
|
+
function functionName(sourceCode, owner) {
|
|
41
|
+
if (owner === null)
|
|
42
|
+
return "anonymous function";
|
|
43
|
+
if (owner.id !== null)
|
|
44
|
+
return owner.id.name;
|
|
45
|
+
const parent = ancestorsOf(sourceCode, owner).at(-1);
|
|
46
|
+
if (parent?.type === "VariableDeclarator" && parent.id.type === "Identifier")
|
|
47
|
+
return parent.id.name;
|
|
48
|
+
if (parent?.type === "MethodDefinition")
|
|
49
|
+
return sourceKeyName(sourceCode, parent.key);
|
|
50
|
+
return "anonymous function";
|
|
51
|
+
}
|
|
52
|
+
function hasParentAssertion(sourceCode, node) {
|
|
53
|
+
const parent = ancestorsOf(sourceCode, node).at(-1);
|
|
54
|
+
return parent?.type === "TSAsExpression" || parent?.type === "TSTypeAssertion";
|
|
55
|
+
}
|
|
56
|
+
/** Detect sound syntactic cases where a known value is explicitly widened and loses evidence. */
|
|
57
|
+
export const noKnownValueWideningRule = defineRule({
|
|
58
|
+
meta: {
|
|
59
|
+
type: "problem",
|
|
60
|
+
docs: {
|
|
61
|
+
description: "Disallow syntactically established values from flowing into explicitly broad or anonymous target types that discard useful evidence.",
|
|
62
|
+
},
|
|
63
|
+
messages: {
|
|
64
|
+
widening: "The explicit {{target}} type on {{subject}} discards known type evidence. Keep inference, validate with `satisfies`, or use a named owner contract.",
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
createOnce(context) {
|
|
68
|
+
let environment = null;
|
|
69
|
+
const reportFlow = (expression, destination, subject) => {
|
|
70
|
+
if (destination === null)
|
|
71
|
+
return;
|
|
72
|
+
const unwrapped = unwrapExpression(expression);
|
|
73
|
+
const accumulator = destination.kind === "open dictionary" || destination.kind === "generic container";
|
|
74
|
+
if (accumulator &&
|
|
75
|
+
unwrapped.type === "ObjectExpression" &&
|
|
76
|
+
unwrapped.properties.length === 0) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const visit = (expression, visitedVariables) => {
|
|
80
|
+
if (isKnownEvidenceExpression(expression))
|
|
81
|
+
return true;
|
|
82
|
+
const unwrapped = unwrapExpression(expression);
|
|
83
|
+
if (unwrapped.type !== "Identifier")
|
|
84
|
+
return false;
|
|
85
|
+
const variable = resolveVariable(context.sourceCode, unwrapped);
|
|
86
|
+
if (variable === null || visitedVariables.has(variable))
|
|
87
|
+
return false;
|
|
88
|
+
const declarator = variableDeclarator(variable);
|
|
89
|
+
if (declarator === null || declarator.init === null)
|
|
90
|
+
return false;
|
|
91
|
+
const declaration = ancestorsOf(context.sourceCode, declarator).at(-1);
|
|
92
|
+
const stableConst = declaration?.type === "VariableDeclaration" &&
|
|
93
|
+
declaration.kind === "const" &&
|
|
94
|
+
variable.references.every((reference) => reference.init || !reference.isWrite());
|
|
95
|
+
if (!stableConst)
|
|
96
|
+
return false;
|
|
97
|
+
visitedVariables.add(variable);
|
|
98
|
+
return visit(declarator.init, visitedVariables);
|
|
99
|
+
};
|
|
100
|
+
if (!visit(expression, new Set()))
|
|
101
|
+
return;
|
|
102
|
+
context.report({
|
|
103
|
+
node: expression,
|
|
104
|
+
messageId: "widening",
|
|
105
|
+
data: { subject, target: destination.kind },
|
|
106
|
+
});
|
|
107
|
+
};
|
|
108
|
+
const targetFromAnnotation = (annotation) => environment === null || annotation === null || annotation === undefined
|
|
109
|
+
? null
|
|
110
|
+
: classifyWideningTarget(annotation.typeAnnotation, environment);
|
|
111
|
+
return {
|
|
112
|
+
Program(node) {
|
|
113
|
+
environment = createTypeEnvironment(node);
|
|
114
|
+
},
|
|
115
|
+
VariableDeclarator(node) {
|
|
116
|
+
if (node.init === null || node.id.type !== "Identifier")
|
|
117
|
+
return;
|
|
118
|
+
reportFlow(node.init, targetFromAnnotation(node.id.typeAnnotation), `binding \`${node.id.name}\``);
|
|
119
|
+
},
|
|
120
|
+
PropertyDefinition(node) {
|
|
121
|
+
if (node.value === null)
|
|
122
|
+
return;
|
|
123
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
124
|
+
},
|
|
125
|
+
AccessorProperty(node) {
|
|
126
|
+
if (node.value === null)
|
|
127
|
+
return;
|
|
128
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
129
|
+
},
|
|
130
|
+
AssignmentExpression(node) {
|
|
131
|
+
if (node.operator !== "=" || node.left.type !== "Identifier")
|
|
132
|
+
return;
|
|
133
|
+
const variable = resolveVariable(context.sourceCode, node.left);
|
|
134
|
+
if (variable === null)
|
|
135
|
+
return;
|
|
136
|
+
const declarator = variableDeclarator(variable);
|
|
137
|
+
if (declarator === null || declarator.id.type !== "Identifier")
|
|
138
|
+
return;
|
|
139
|
+
reportFlow(node.right, targetFromAnnotation(declarator.id.typeAnnotation), `binding \`${declarator.id.name}\``);
|
|
140
|
+
},
|
|
141
|
+
ReturnStatement(node) {
|
|
142
|
+
if (node.argument === null)
|
|
143
|
+
return;
|
|
144
|
+
let owner = null;
|
|
145
|
+
const ancestors = ancestorsOf(context.sourceCode, node);
|
|
146
|
+
for (let index = ancestors.length - 1; index >= 0; index--) {
|
|
147
|
+
const current = ancestors[index];
|
|
148
|
+
if (current.type === "Program")
|
|
149
|
+
break;
|
|
150
|
+
if (current.type === "ArrowFunctionExpression" ||
|
|
151
|
+
current.type === "FunctionDeclaration" ||
|
|
152
|
+
current.type === "FunctionExpression") {
|
|
153
|
+
owner = current;
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
reportFlow(node.argument, targetFromAnnotation(owner?.returnType), `return value of \`${functionName(context.sourceCode, owner)}\``);
|
|
158
|
+
},
|
|
159
|
+
ArrowFunctionExpression(node) {
|
|
160
|
+
if (node.body.type === "BlockStatement")
|
|
161
|
+
return;
|
|
162
|
+
reportFlow(node.body, targetFromAnnotation(node.returnType), `return value of \`${functionName(context.sourceCode, node)}\``);
|
|
163
|
+
},
|
|
164
|
+
TSAsExpression(node) {
|
|
165
|
+
if (environment === null || hasParentAssertion(context.sourceCode, node))
|
|
166
|
+
return;
|
|
167
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
168
|
+
},
|
|
169
|
+
TSTypeAssertion(node) {
|
|
170
|
+
if (environment === null || hasParentAssertion(context.sourceCode, node))
|
|
171
|
+
return;
|
|
172
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
173
|
+
},
|
|
174
|
+
};
|
|
175
|
+
},
|
|
176
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
3
|
+
const moduleMockMethods = new Set(["doMock", "mock", "unstable_mockModule"]);
|
|
4
|
+
/**
|
|
5
|
+
* Whether a callee invokes a test framework's module mocking API
|
|
6
|
+
* (`vi.doMock`, `jest.mock`, `vi.unstable_mockModule`), either through the
|
|
7
|
+
* global or through a binding imported from vitest / @jest/globals.
|
|
8
|
+
*
|
|
9
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
10
|
+
* @param {ESTree.Expression} callee - The visited call expression's callee.
|
|
11
|
+
* @returns {boolean} True when the call mocks a module.
|
|
12
|
+
*/
|
|
13
|
+
function moduleMockCall(sourceCode, callee) {
|
|
14
|
+
if (callee.type !== "MemberExpression")
|
|
15
|
+
return false;
|
|
16
|
+
if (callee.object.type !== "Identifier")
|
|
17
|
+
return false;
|
|
18
|
+
const identifier = callee.object;
|
|
19
|
+
let resolved = null;
|
|
20
|
+
let scope = sourceCode.getScope(identifier);
|
|
21
|
+
while (scope !== null) {
|
|
22
|
+
const variable = scope.set.get(identifier.name);
|
|
23
|
+
if (variable !== undefined) {
|
|
24
|
+
resolved = variable;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
scope = scope.upper;
|
|
28
|
+
}
|
|
29
|
+
const isTestFrameworkName = identifier.name === "vi" || identifier.name === "jest";
|
|
30
|
+
let boundToFramework;
|
|
31
|
+
if (resolved === null || resolved.defs.length === 0) {
|
|
32
|
+
boundToFramework =
|
|
33
|
+
(isTestFrameworkName && sourceCode.isGlobalReference(identifier)) || isTestFrameworkName;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
boundToFramework = resolved.defs.some((definition) => {
|
|
37
|
+
if (definition.type !== "ImportBinding")
|
|
38
|
+
return false;
|
|
39
|
+
const importDeclaration = ancestorsOf(sourceCode, definition.node).at(-1);
|
|
40
|
+
if (importDeclaration?.type !== "ImportDeclaration")
|
|
41
|
+
return false;
|
|
42
|
+
const source = importDeclaration.source.value;
|
|
43
|
+
const specifier = definition.node;
|
|
44
|
+
const importedName = specifier.type === "ImportSpecifier"
|
|
45
|
+
? specifier.imported.type === "Identifier"
|
|
46
|
+
? specifier.imported.name
|
|
47
|
+
: specifier.imported.value
|
|
48
|
+
: null;
|
|
49
|
+
return ((source === "vitest" && importedName === "vi") ||
|
|
50
|
+
(source === "@jest/globals" && importedName === "jest"));
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
if (!boundToFramework)
|
|
54
|
+
return false;
|
|
55
|
+
const property = callee.property;
|
|
56
|
+
const method = callee.computed
|
|
57
|
+
? property.type === "Literal" &&
|
|
58
|
+
(property.value === "doMock" ||
|
|
59
|
+
property.value === "mock" ||
|
|
60
|
+
property.value === "unstable_mockModule")
|
|
61
|
+
? property.value
|
|
62
|
+
: null
|
|
63
|
+
: property.type === "Identifier"
|
|
64
|
+
? property.name
|
|
65
|
+
: null;
|
|
66
|
+
return method !== null && moduleMockMethods.has(method);
|
|
67
|
+
}
|
|
68
|
+
/** Ban test framework module mocking in favor of real dependency seams. */
|
|
69
|
+
export const noModuleMockingRule = defineRule({
|
|
70
|
+
meta: {
|
|
71
|
+
type: "problem",
|
|
72
|
+
docs: {
|
|
73
|
+
description: "Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces.",
|
|
74
|
+
},
|
|
75
|
+
messages: {
|
|
76
|
+
moduleMock: "Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation.",
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
createOnce(context) {
|
|
80
|
+
return {
|
|
81
|
+
CallExpression(node) {
|
|
82
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
83
|
+
return;
|
|
84
|
+
if (moduleMockCall(context.sourceCode, node.callee)) {
|
|
85
|
+
context.report({ node, messageId: "moduleMock" });
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
},
|
|
90
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { readField } from "../shared/structural.js";
|
|
3
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
4
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
5
|
+
const DEFAULT_MAX_PARAMS = 1;
|
|
6
|
+
/** Conventional HTTP method handler names, exempt inside framework route files. */
|
|
7
|
+
const HTTP_METHOD_NAMES = new Set([
|
|
8
|
+
"DELETE",
|
|
9
|
+
"GET",
|
|
10
|
+
"HEAD",
|
|
11
|
+
"OPTIONS",
|
|
12
|
+
"PATCH",
|
|
13
|
+
"POST",
|
|
14
|
+
"PUT",
|
|
15
|
+
]);
|
|
16
|
+
const DEFAULT_ROUTE_BASENAMES = new Set(["route.ts", "route.tsx"]);
|
|
17
|
+
function parentOf(sourceCode, node) {
|
|
18
|
+
return ancestorsOf(sourceCode, node)[0] ?? null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Functions the module owns: declarations and named bindings, not callbacks.
|
|
22
|
+
*
|
|
23
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
24
|
+
* @param {ESTree.Node} node - The visited function-like node.
|
|
25
|
+
* @returns {boolean} True when the module owns the function's arity.
|
|
26
|
+
*/
|
|
27
|
+
function isOwnedFunction(sourceCode, node) {
|
|
28
|
+
if (node.type === "FunctionDeclaration")
|
|
29
|
+
return true;
|
|
30
|
+
const parent = parentOf(sourceCode, node);
|
|
31
|
+
if (parent === null)
|
|
32
|
+
return false;
|
|
33
|
+
const parentType = parent.type;
|
|
34
|
+
if (parentType === "CallExpression" ||
|
|
35
|
+
parentType === "NewExpression" ||
|
|
36
|
+
parentType === "JSXExpressionContainer") {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
return (parentType === "VariableDeclarator" ||
|
|
40
|
+
parentType === "AssignmentExpression" ||
|
|
41
|
+
parentType === "ExportDefaultDeclaration");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Exported HTTP-method-named handlers in framework route files own their arity.
|
|
45
|
+
*
|
|
46
|
+
* @param {{ options: ResolvedOptions; baseName: string; sourceCode: SourceCode; node: ESTree.Node }} payload - Exemption inputs.
|
|
47
|
+
* @returns {boolean} True when the handler is an exempt route handler.
|
|
48
|
+
*/
|
|
49
|
+
function isExemptRouteHandler(payload) {
|
|
50
|
+
if (payload.node.type !== "FunctionDeclaration")
|
|
51
|
+
return false;
|
|
52
|
+
const name = payload.node.id?.name;
|
|
53
|
+
if (name === undefined || !payload.options.methodNames.has(name))
|
|
54
|
+
return false;
|
|
55
|
+
if (!payload.options.routeBasenames.has(payload.baseName))
|
|
56
|
+
return false;
|
|
57
|
+
const parent = parentOf(payload.sourceCode, payload.node);
|
|
58
|
+
return parent !== null && parent.type === "ExportNamedDeclaration";
|
|
59
|
+
}
|
|
60
|
+
/** Primitive annotations transpose silently; structural ones fail the compiler. */
|
|
61
|
+
const PRIMITIVE_TYPE_TEXT = /^(?:string|number|bigint|boolean|unknown|any|null|undefined|void)$/u;
|
|
62
|
+
/**
|
|
63
|
+
* Whether every positional parameter carries a distinct non-primitive type:
|
|
64
|
+
* transposing such arguments is a compile error, so ordering needs no guard.
|
|
65
|
+
*
|
|
66
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
67
|
+
* @param {ReadonlyArray<ESTree.ParamPattern>} params - The parameter list to inspect.
|
|
68
|
+
* @returns {boolean} True when transposing any pair fails the compiler.
|
|
69
|
+
*/
|
|
70
|
+
function transpositionSafe(sourceCode, params) {
|
|
71
|
+
const texts = [];
|
|
72
|
+
for (const param of params) {
|
|
73
|
+
if (param.type !== "Identifier")
|
|
74
|
+
return false;
|
|
75
|
+
const annotation = param.typeAnnotation;
|
|
76
|
+
if (annotation === null || annotation === undefined)
|
|
77
|
+
return false;
|
|
78
|
+
const text = sourceCode.getText(annotation.typeAnnotation).replaceAll(" ", "");
|
|
79
|
+
if (text === "" || PRIMITIVE_TYPE_TEXT.test(text))
|
|
80
|
+
return false;
|
|
81
|
+
texts.push(text);
|
|
82
|
+
}
|
|
83
|
+
return new Set(texts).size === texts.length;
|
|
84
|
+
}
|
|
85
|
+
/** Owned functions taking several positional inputs accept transposed arguments silently; require one payload object. */
|
|
86
|
+
export const noMultipleFunctionParamsRule = defineRule({
|
|
87
|
+
meta: {
|
|
88
|
+
type: "problem",
|
|
89
|
+
docs: {
|
|
90
|
+
description: "Disallow owned functions with more than one positional parameter; collapse multiple inputs into a single named payload object so transposed arguments fail the compiler.",
|
|
91
|
+
},
|
|
92
|
+
messages: {
|
|
93
|
+
multipleParams: "`{{name}}` takes {{count}} positional parameters. Accept a single named payload object instead; transposed primitive arguments compile silently.",
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
createOnce(context) {
|
|
97
|
+
const rawOptions = readField(context, "options");
|
|
98
|
+
const options = {
|
|
99
|
+
methodNames: new Set(rawOptions?.[0]?.exemptFunctionNames ?? HTTP_METHOD_NAMES),
|
|
100
|
+
routeBasenames: new Set(rawOptions?.[0]?.exemptRouteBasenames ?? DEFAULT_ROUTE_BASENAMES),
|
|
101
|
+
};
|
|
102
|
+
let fileBase = "";
|
|
103
|
+
const check = (node) => {
|
|
104
|
+
if (node.type !== "FunctionDeclaration" &&
|
|
105
|
+
node.type !== "FunctionExpression" &&
|
|
106
|
+
node.type !== "ArrowFunctionExpression")
|
|
107
|
+
return;
|
|
108
|
+
const fn = node;
|
|
109
|
+
if (fn.params.length <= DEFAULT_MAX_PARAMS)
|
|
110
|
+
return;
|
|
111
|
+
if (!isOwnedFunction(context.sourceCode, node))
|
|
112
|
+
return;
|
|
113
|
+
if (transpositionSafe(context.sourceCode, fn.params))
|
|
114
|
+
return;
|
|
115
|
+
if (isExemptRouteHandler({
|
|
116
|
+
options,
|
|
117
|
+
baseName: fileBase,
|
|
118
|
+
sourceCode: context.sourceCode,
|
|
119
|
+
node,
|
|
120
|
+
}))
|
|
121
|
+
return;
|
|
122
|
+
let name = "(anonymous)";
|
|
123
|
+
if (node.id !== null && node.id !== undefined) {
|
|
124
|
+
name = node.id.name;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const parent = parentOf(context.sourceCode, node);
|
|
128
|
+
if (parent?.type === "VariableDeclarator" && parent.id.type === "Identifier") {
|
|
129
|
+
name = parent.id.name;
|
|
130
|
+
}
|
|
131
|
+
else if (parent?.type === "AssignmentExpression" &&
|
|
132
|
+
parent.left.type === "Identifier") {
|
|
133
|
+
name = parent.left.name;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
context.report({
|
|
137
|
+
node,
|
|
138
|
+
messageId: "multipleParams",
|
|
139
|
+
data: { name, count: String(fn.params.length) },
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
return {
|
|
143
|
+
before() {
|
|
144
|
+
const normalized = context.filename.replaceAll("\\", "/");
|
|
145
|
+
const segments = normalized.split("/");
|
|
146
|
+
fileBase = segments[segments.length - 1] ?? normalized;
|
|
147
|
+
if (TEST_FILE.test(normalized))
|
|
148
|
+
return false;
|
|
149
|
+
},
|
|
150
|
+
FunctionDeclaration: check,
|
|
151
|
+
FunctionExpression: check,
|
|
152
|
+
ArrowFunctionExpression: check,
|
|
153
|
+
};
|
|
154
|
+
},
|
|
155
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A function that reads or writes module state instead of taking it as a
|
|
3
|
+
* parameter is dishonest: its result depends on outside context, so it is not
|
|
4
|
+
* locally testable. Inject the value at the call site (the document's seed-the
|
|
5
|
+
* PRNG-once, pass-it-in pattern) so the dependency is explicit in the signature.
|
|
6
|
+
*/
|
|
7
|
+
export declare const noMutableEnvironmentCaptureRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
3
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
4
|
+
/**
|
|
5
|
+
* Whether the identifier sits inside a type annotation rather than a value position.
|
|
6
|
+
*
|
|
7
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
8
|
+
* @param {ESTree.Node} identifier - The reference identifier to test.
|
|
9
|
+
* @returns {boolean} True when an ancestor is a TypeScript type node.
|
|
10
|
+
*/
|
|
11
|
+
function isTypePosition(sourceCode, identifier) {
|
|
12
|
+
for (const ancestor of ancestorsOf(sourceCode, identifier)) {
|
|
13
|
+
if (ancestor.type.startsWith("TS"))
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* The name a function is called by at its definition site.
|
|
20
|
+
*
|
|
21
|
+
* @param {ESTree.Node} node - The function-like node.
|
|
22
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
23
|
+
* @returns {string} The function's name, or "(anonymous)".
|
|
24
|
+
*/
|
|
25
|
+
function functionName(node, sourceCode) {
|
|
26
|
+
if (node.type === "FunctionDeclaration" && node.id !== null)
|
|
27
|
+
return node.id.name;
|
|
28
|
+
const parent = ancestorsOf(sourceCode, node)[0];
|
|
29
|
+
if (parent !== undefined) {
|
|
30
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier")
|
|
31
|
+
return parent.id.name;
|
|
32
|
+
if (parent.type === "AssignmentExpression" && parent.left.type === "Identifier")
|
|
33
|
+
return parent.left.name;
|
|
34
|
+
if (parent.type === "MethodDefinition" && parent.key.type === "Identifier")
|
|
35
|
+
return parent.key.name;
|
|
36
|
+
if (parent.type === "Property" && parent.key.type === "Identifier")
|
|
37
|
+
return parent.key.name;
|
|
38
|
+
if (parent.type === "ExportDefaultDeclaration")
|
|
39
|
+
return "default";
|
|
40
|
+
}
|
|
41
|
+
return "(anonymous)";
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Collect module-scope let/var bindings, separating real state from
|
|
45
|
+
* function-valued helpers (which are legitimate module-private utilities).
|
|
46
|
+
*
|
|
47
|
+
* @param {ESTree.Program} program - The file's root node.
|
|
48
|
+
* @returns {ModuleState} Mutable state names and helper names.
|
|
49
|
+
*/
|
|
50
|
+
function collectModuleState(program) {
|
|
51
|
+
const state = new Set();
|
|
52
|
+
const helpers = new Set();
|
|
53
|
+
for (const statement of program.body) {
|
|
54
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
55
|
+
if (declaration === null || declaration === undefined)
|
|
56
|
+
continue;
|
|
57
|
+
if (declaration.type !== "VariableDeclaration")
|
|
58
|
+
continue;
|
|
59
|
+
if (declaration.kind !== "let" && declaration.kind !== "var")
|
|
60
|
+
continue;
|
|
61
|
+
for (const declarator of declaration.declarations) {
|
|
62
|
+
if (declarator.id.type !== "Identifier")
|
|
63
|
+
continue;
|
|
64
|
+
const init = declarator.init;
|
|
65
|
+
const isHelper = init !== null &&
|
|
66
|
+
init !== undefined &&
|
|
67
|
+
(init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression");
|
|
68
|
+
(isHelper ? helpers : state).add(declarator.id.name);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { state, helpers };
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* A function that reads or writes module state instead of taking it as a
|
|
75
|
+
* parameter is dishonest: its result depends on outside context, so it is not
|
|
76
|
+
* locally testable. Inject the value at the call site (the document's seed-the
|
|
77
|
+
* PRNG-once, pass-it-in pattern) so the dependency is explicit in the signature.
|
|
78
|
+
*/
|
|
79
|
+
export const noMutableEnvironmentCaptureRule = defineRule({
|
|
80
|
+
meta: {
|
|
81
|
+
type: "problem",
|
|
82
|
+
docs: {
|
|
83
|
+
description: "Disallow functions that read or write module-scope mutable state (let/var) instead of receiving it as a parameter; hidden module state makes a function dishonest and not locally testable.",
|
|
84
|
+
},
|
|
85
|
+
messages: {
|
|
86
|
+
mutableModuleState: "Function `{{name}}` {{kind}} module-scope mutable state `{{binding}}` rather than receiving it as a parameter. Hidden module state makes the function dishonest: its result depends on outside context, so it is not locally testable. Inject `{{binding}}` at the call site or thread it through the signature so the dependency is explicit.",
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
createOnce(context) {
|
|
90
|
+
let moduleState = { state: new Set(), helpers: new Set() };
|
|
91
|
+
const check = (node) => {
|
|
92
|
+
if (node.type !== "FunctionDeclaration" &&
|
|
93
|
+
node.type !== "FunctionExpression" &&
|
|
94
|
+
node.type !== "ArrowFunctionExpression") {
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
const scope = context.sourceCode.getScope(node);
|
|
98
|
+
const { state, helpers } = moduleState;
|
|
99
|
+
for (const reference of scope.references) {
|
|
100
|
+
const resolved = reference.resolved;
|
|
101
|
+
if (resolved === null || resolved === undefined)
|
|
102
|
+
continue;
|
|
103
|
+
const name = resolved.name;
|
|
104
|
+
if (helpers.has(name))
|
|
105
|
+
continue;
|
|
106
|
+
if (!state.has(name))
|
|
107
|
+
continue;
|
|
108
|
+
if (isTypePosition(context.sourceCode, reference.identifier))
|
|
109
|
+
continue;
|
|
110
|
+
context.report({
|
|
111
|
+
node: reference.identifier,
|
|
112
|
+
messageId: "mutableModuleState",
|
|
113
|
+
data: {
|
|
114
|
+
name: functionName(node, context.sourceCode),
|
|
115
|
+
kind: reference.isWrite() ? "writes" : "reads",
|
|
116
|
+
binding: name,
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
return {
|
|
122
|
+
before() {
|
|
123
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
124
|
+
return false;
|
|
125
|
+
},
|
|
126
|
+
Program(node) {
|
|
127
|
+
moduleState = collectModuleState(node);
|
|
128
|
+
},
|
|
129
|
+
FunctionDeclaration: check,
|
|
130
|
+
FunctionExpression: check,
|
|
131
|
+
ArrowFunctionExpression: check,
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical form: identical inputs must produce identical outputs. Time and
|
|
3
|
+
* randomness are boundary inputs: capture them once at the edge and pass
|
|
4
|
+
* them in, so cores stay reproducible and their serialisations stable.
|
|
5
|
+
*/
|
|
6
|
+
export declare const noNondeterministicCoreRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
const NONDETERMINISTIC_MEMBERS = new Set(["getRandomValues", "random", "randomUUID", "now"]);
|
|
3
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$|\/test\//u;
|
|
4
|
+
function memberChain(node) {
|
|
5
|
+
if (node.type === "MemberExpression" && !node.computed) {
|
|
6
|
+
return [...memberChain(node.object), node.property.type === "Identifier" ? node.property.name : ""];
|
|
7
|
+
}
|
|
8
|
+
return [node.type === "Identifier" ? node.name : "?"];
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Canonical form: identical inputs must produce identical outputs. Time and
|
|
12
|
+
* randomness are boundary inputs: capture them once at the edge and pass
|
|
13
|
+
* them in, so cores stay reproducible and their serialisations stable.
|
|
14
|
+
*/
|
|
15
|
+
export const noNondeterministicCoreRule = defineRule({
|
|
16
|
+
meta: {
|
|
17
|
+
type: "problem",
|
|
18
|
+
docs: {
|
|
19
|
+
description: "Disallow nondeterministic sources (Math.random, Date.now, new Date(), crypto randomness) outside tests; inject them as boundary inputs so cores stay canonical.",
|
|
20
|
+
},
|
|
21
|
+
messages: {
|
|
22
|
+
nondeterministicSource: "`{{source}}` makes this code's output depend on when/where it runs, breaking canonical form (identical inputs, identical outputs). Capture it at the boundary and pass it in as a parameter.",
|
|
23
|
+
unseededDate: "`new Date()` captures wall-clock time here, making output non-reproducible. Take the instant as a parameter (or use a fixed epoch for pure conversions).",
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
createOnce(context) {
|
|
27
|
+
return {
|
|
28
|
+
before() {
|
|
29
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
30
|
+
return false;
|
|
31
|
+
},
|
|
32
|
+
MemberExpression(node) {
|
|
33
|
+
const property = node.property;
|
|
34
|
+
if (node.computed || property.type !== "Identifier" || !NONDETERMINISTIC_MEMBERS.has(property.name)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const chain = memberChain(node.object);
|
|
38
|
+
const root = chain[0] ?? "";
|
|
39
|
+
if ((root === "Math" && property.name === "random") || (root === "crypto" && property.name !== "now")) {
|
|
40
|
+
context.report({
|
|
41
|
+
node,
|
|
42
|
+
messageId: "nondeterministicSource",
|
|
43
|
+
data: { source: `${root}.${property.name}` },
|
|
44
|
+
});
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
if (property.name === "now" &&
|
|
48
|
+
((root === "Date" && chain.length === 1) || (root === "performance" && chain.length === 1))) {
|
|
49
|
+
context.report({
|
|
50
|
+
node,
|
|
51
|
+
messageId: "nondeterministicSource",
|
|
52
|
+
data: { source: `${root}.now` },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
NewExpression(node) {
|
|
57
|
+
if (node.callee.type === "Identifier" && node.callee.name === "Date" && node.arguments.length === 0) {
|
|
58
|
+
context.report({ node, messageId: "unseededDate" });
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
},
|
|
63
|
+
});
|