@autotelic/plumb 0.6.0 → 0.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +81 -8
- package/dist/configs/recommended-rules.d.ts +36 -0
- package/dist/configs/recommended-rules.js +57 -0
- package/dist/effect/rules/guarded-op-must-return-effect.js +1 -1
- package/dist/effect/rules/no-cascading-layer-provide.js +1 -1
- package/dist/effect/rules/no-decode-unknown-option.js +1 -1
- package/dist/effect/rules/no-direct-browser-storage.js +3 -3
- package/dist/effect/rules/no-direct-fetch.js +1 -1
- package/dist/effect/rules/no-manual-field-guards.js +1 -1
- package/dist/effect/rules/no-nested-layer-provide.js +1 -1
- package/dist/effect/rules/no-nullish-default-on-partial-input.js +2 -2
- package/dist/effect/rules/no-raw-error-forwarding.d.ts +4 -0
- package/dist/effect/rules/no-raw-error-forwarding.js +7 -2
- package/dist/effect/rules/no-silent-error-swallow.js +1 -1
- package/dist/effect/rules/no-static-effect-service-forwarders.js +5 -4
- package/dist/effect/rules/no-try-catch.js +1 -1
- package/dist/effect/rules/no-unguarded-json-parse.js +1 -1
- package/dist/effect/rules/option-core-needs-effect-public.js +1 -1
- package/dist/effect/rules/prefer-die-for-precondition-defects.js +1 -1
- package/dist/effect/rules/prefer-effect-gen-for-guard-ladders.js +1 -1
- package/dist/effect/rules/prefer-effect-match.js +2 -2
- package/dist/effect/rules/prefer-effect-predicate-over-boolean-function.js +1 -1
- package/dist/effect/rules/prefer-named-guard-predicate.js +1 -1
- package/dist/effect/rules/prefer-option-pipeline.js +13 -12
- package/dist/effect/rules/prefer-ordering-match.js +1 -1
- package/dist/effect/rules/prefer-tagged-enum.js +1 -1
- package/dist/effect/rules/require-schema-type-derivation.d.ts +8 -2
- package/dist/effect/rules/require-schema-type-derivation.js +14 -4
- package/dist/index.js +8 -0
- package/dist/react/rules/require-dot-notation-exports.js +1 -1
- package/dist/react/rules/require-guarded-context-hook.js +1 -1
- package/dist/react/rules/require-tripartite-context.js +1 -1
- package/dist/react/rules/use-encapsulation.js +1 -1
- package/dist/rules/no-adapter-in-values.d.ts +12 -0
- package/dist/rules/no-adapter-in-values.js +110 -0
- package/dist/rules/no-builtin-throws.js +1 -1
- package/dist/rules/no-duplicated-literal-union.d.ts +7 -0
- package/dist/rules/no-duplicated-literal-union.js +5 -1
- package/dist/rules/no-generic-export-names.js +1 -1
- package/dist/rules/no-multiple-function-params.js +26 -12
- package/dist/rules/no-mutable-environment-capture.js +2 -2
- package/dist/rules/no-nondeterministic-core.js +1 -1
- package/dist/rules/no-nullable-value-field.d.ts +16 -0
- package/dist/rules/no-nullable-value-field.js +117 -0
- package/dist/rules/no-optional-function-parameters.js +1 -1
- package/dist/rules/no-row-coercion.d.ts +13 -0
- package/dist/rules/no-row-coercion.js +135 -0
- package/dist/rules/no-single-use-private-functions.js +1 -1
- package/dist/rules/no-sql-string-interpolation.js +2 -2
- package/dist/rules/no-stray-inline-comments.js +1 -1
- package/dist/rules/no-string-instant.d.ts +15 -0
- package/dist/rules/no-string-instant.js +108 -0
- package/dist/rules/no-swappable-primitive-params.js +1 -1
- package/dist/rules/no-tag-ladder-assertions.js +1 -1
- package/dist/rules/no-vague-test-filenames.js +1 -1
- package/dist/rules/prefer-property-tests.d.ts +5 -0
- package/dist/rules/prefer-property-tests.js +7 -0
- package/dist/rules/require-jsdoc-on-exported.js +1 -1
- package/dist/shared/ancestors.d.ts +1 -1
- package/dist/shared/ancestors.js +1 -1
- package/dist/shared/values-path.d.ts +12 -0
- package/dist/shared/values-path.js +56 -0
- package/dist/src/configs/recommended-rules.d.ts +20 -0
- package/dist/src/configs/recommended-rules.js +22 -0
- package/dist/src/effect/index.d.ts +3 -0
- package/dist/src/effect/index.js +60 -0
- package/dist/src/effect/rules/guarded-op-must-return-effect.d.ts +6 -0
- package/dist/src/effect/rules/guarded-op-must-return-effect.js +235 -0
- package/dist/src/effect/rules/guarded-op-must-return-option.d.ts +2 -0
- package/dist/src/effect/rules/guarded-op-must-return-option.js +88 -0
- package/dist/src/effect/rules/no-cascading-layer-provide.d.ts +6 -0
- package/dist/src/effect/rules/no-cascading-layer-provide.js +73 -0
- package/dist/src/effect/rules/no-decode-unknown-option.d.ts +7 -0
- package/dist/src/effect/rules/no-decode-unknown-option.js +34 -0
- package/dist/src/effect/rules/no-direct-browser-storage.d.ts +2 -0
- package/dist/src/effect/rules/no-direct-browser-storage.js +88 -0
- package/dist/src/effect/rules/no-direct-fetch.d.ts +2 -0
- package/dist/src/effect/rules/no-direct-fetch.js +37 -0
- package/dist/src/effect/rules/no-manual-field-guards.d.ts +6 -0
- package/dist/src/effect/rules/no-manual-field-guards.js +129 -0
- package/dist/src/effect/rules/no-nested-layer-provide.d.ts +2 -0
- package/dist/src/effect/rules/no-nested-layer-provide.js +50 -0
- package/dist/src/effect/rules/no-nullish-default-on-partial-input.d.ts +13 -0
- package/dist/src/effect/rules/no-nullish-default-on-partial-input.js +109 -0
- package/dist/src/effect/rules/no-raw-error-forwarding.d.ts +12 -0
- package/dist/src/effect/rules/no-raw-error-forwarding.js +103 -0
- package/dist/src/effect/rules/no-schema-class-modeling.d.ts +2 -0
- package/dist/src/effect/rules/no-schema-class-modeling.js +52 -0
- package/dist/src/effect/rules/no-service-constructor-imports.d.ts +2 -0
- package/dist/src/effect/rules/no-service-constructor-imports.js +42 -0
- package/dist/src/effect/rules/no-silent-error-swallow.d.ts +2 -0
- package/dist/src/effect/rules/no-silent-error-swallow.js +95 -0
- package/dist/src/effect/rules/no-static-effect-service-forwarders.d.ts +6 -0
- package/dist/src/effect/rules/no-static-effect-service-forwarders.js +222 -0
- package/dist/src/effect/rules/no-try-catch.d.ts +2 -0
- package/dist/src/effect/rules/no-try-catch.js +30 -0
- package/dist/src/effect/rules/no-unguarded-json-parse.d.ts +5 -0
- package/dist/src/effect/rules/no-unguarded-json-parse.js +200 -0
- package/dist/src/effect/rules/option-core-needs-effect-public.d.ts +5 -0
- package/dist/src/effect/rules/option-core-needs-effect-public.js +76 -0
- package/dist/src/effect/rules/prefer-die-for-precondition-defects.d.ts +5 -0
- package/dist/src/effect/rules/prefer-die-for-precondition-defects.js +112 -0
- package/dist/src/effect/rules/prefer-effect-gen-for-guard-ladders.d.ts +5 -0
- package/dist/src/effect/rules/prefer-effect-gen-for-guard-ladders.js +72 -0
- package/dist/src/effect/rules/prefer-effect-match.d.ts +2 -0
- package/dist/src/effect/rules/prefer-effect-match.js +74 -0
- package/dist/src/effect/rules/prefer-effect-predicate-over-boolean-function.d.ts +2 -0
- package/dist/src/effect/rules/prefer-effect-predicate-over-boolean-function.js +75 -0
- package/dist/src/effect/rules/prefer-named-guard-predicate.d.ts +2 -0
- package/dist/src/effect/rules/prefer-named-guard-predicate.js +93 -0
- package/dist/src/effect/rules/prefer-option-pipeline.d.ts +2 -0
- package/dist/src/effect/rules/prefer-option-pipeline.js +87 -0
- package/dist/src/effect/rules/prefer-ordering-match.d.ts +2 -0
- package/dist/src/effect/rules/prefer-ordering-match.js +135 -0
- package/dist/src/effect/rules/prefer-tagged-enum.d.ts +2 -0
- package/dist/src/effect/rules/prefer-tagged-enum.js +66 -0
- package/dist/src/effect/rules/require-schema-type-derivation.d.ts +7 -0
- package/dist/src/effect/rules/require-schema-type-derivation.js +80 -0
- package/dist/src/index.d.ts +3 -0
- package/dist/src/index.js +106 -0
- package/dist/src/meta/index.d.ts +7 -0
- package/dist/src/meta/index.js +22 -0
- package/dist/src/meta/rules/no-disable-directives.d.ts +6 -0
- package/dist/src/meta/rules/no-disable-directives.js +35 -0
- package/dist/src/meta/rules/no-manual-ancestor-walks.d.ts +2 -0
- package/dist/src/meta/rules/no-manual-ancestor-walks.js +26 -0
- package/dist/src/meta/rules/prefer-before-file-scope.d.ts +2 -0
- package/dist/src/meta/rules/prefer-before-file-scope.js +57 -0
- package/dist/src/meta/rules/require-create-once.d.ts +2 -0
- package/dist/src/meta/rules/require-create-once.js +51 -0
- package/dist/src/meta/rules/require-rule-tester.d.ts +2 -0
- package/dist/src/meta/rules/require-rule-tester.js +50 -0
- package/dist/src/react/index.d.ts +3 -0
- package/dist/src/react/index.js +16 -0
- package/dist/src/react/role.d.ts +23 -0
- package/dist/src/react/role.js +30 -0
- package/dist/src/react/rules/require-dot-notation-exports.d.ts +27 -0
- package/dist/src/react/rules/require-dot-notation-exports.js +144 -0
- package/dist/src/react/rules/require-guarded-context-hook.d.ts +19 -0
- package/dist/src/react/rules/require-guarded-context-hook.js +167 -0
- package/dist/src/react/rules/require-tripartite-context.d.ts +17 -0
- package/dist/src/react/rules/require-tripartite-context.js +160 -0
- package/dist/src/react/rules/use-encapsulation.d.ts +22 -0
- package/dist/src/react/rules/use-encapsulation.js +97 -0
- package/dist/src/rules/no-anonymous-wide-tuples.d.ts +7 -0
- package/dist/src/rules/no-anonymous-wide-tuples.js +34 -0
- package/dist/src/rules/no-barrel-export-star.d.ts +2 -0
- package/dist/src/rules/no-barrel-export-star.js +26 -0
- package/dist/src/rules/no-boolean-field-signals.d.ts +2 -0
- package/dist/src/rules/no-boolean-field-signals.js +279 -0
- package/dist/src/rules/no-builtin-throws.d.ts +6 -0
- package/dist/src/rules/no-builtin-throws.js +74 -0
- package/dist/src/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/src/rules/no-chained-type-assertions.js +54 -0
- package/dist/src/rules/no-conditional-empty-object-spread.d.ts +2 -0
- package/dist/src/rules/no-conditional-empty-object-spread.js +36 -0
- package/dist/src/rules/no-duplicated-literal-union.d.ts +6 -0
- package/dist/src/rules/no-duplicated-literal-union.js +66 -0
- package/dist/src/rules/no-exported-mutable-state.d.ts +7 -0
- package/dist/src/rules/no-exported-mutable-state.js +104 -0
- package/dist/src/rules/no-generic-export-names.d.ts +2 -0
- package/dist/src/rules/no-generic-export-names.js +141 -0
- package/dist/src/rules/no-impossible-branch-throw.d.ts +2 -0
- package/dist/src/rules/no-impossible-branch-throw.js +51 -0
- package/dist/src/rules/no-known-value-widening.d.ts +2 -0
- package/dist/src/rules/no-known-value-widening.js +176 -0
- package/dist/src/rules/no-module-mocking.d.ts +2 -0
- package/dist/src/rules/no-module-mocking.js +90 -0
- package/dist/src/rules/no-multiple-function-params.d.ts +2 -0
- package/dist/src/rules/no-multiple-function-params.js +155 -0
- package/dist/src/rules/no-mutable-environment-capture.d.ts +7 -0
- package/dist/src/rules/no-mutable-environment-capture.js +134 -0
- package/dist/src/rules/no-nondeterministic-core.d.ts +6 -0
- package/dist/src/rules/no-nondeterministic-core.js +63 -0
- package/dist/src/rules/no-object-parameters.d.ts +2 -0
- package/dist/src/rules/no-object-parameters.js +95 -0
- package/dist/src/rules/no-optional-function-parameters.d.ts +7 -0
- package/dist/src/rules/no-optional-function-parameters.js +44 -0
- package/dist/src/rules/no-product-of-state-booleans.d.ts +2 -0
- package/dist/src/rules/no-product-of-state-booleans.js +79 -0
- package/dist/src/rules/no-redundant-derived-field.d.ts +6 -0
- package/dist/src/rules/no-redundant-derived-field.js +194 -0
- package/dist/src/rules/no-reflect-apply.d.ts +2 -0
- package/dist/src/rules/no-reflect-apply.js +29 -0
- package/dist/src/rules/no-reflect-get.d.ts +2 -0
- package/dist/src/rules/no-reflect-get.js +29 -0
- package/dist/src/rules/no-reinterpret-cast.d.ts +2 -0
- package/dist/src/rules/no-reinterpret-cast.js +68 -0
- package/dist/src/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/src/rules/no-runtime-typeof.js +54 -0
- package/dist/src/rules/no-sentinel-comparison-union.d.ts +2 -0
- package/dist/src/rules/no-sentinel-comparison-union.js +139 -0
- package/dist/src/rules/no-shape-in-symbol-names.d.ts +2 -0
- package/dist/src/rules/no-shape-in-symbol-names.js +39 -0
- package/dist/src/rules/no-single-use-private-functions.d.ts +17 -0
- package/dist/src/rules/no-single-use-private-functions.js +419 -0
- package/dist/src/rules/no-sql-string-interpolation.d.ts +7 -0
- package/dist/src/rules/no-sql-string-interpolation.js +62 -0
- package/dist/src/rules/no-stacked-jsdoc-blocks.d.ts +2 -0
- package/dist/src/rules/no-stacked-jsdoc-blocks.js +50 -0
- package/dist/src/rules/no-stray-inline-comments.d.ts +2 -0
- package/dist/src/rules/no-stray-inline-comments.js +74 -0
- package/dist/src/rules/no-swappable-primitive-params.d.ts +2 -0
- package/dist/src/rules/no-swappable-primitive-params.js +96 -0
- package/dist/src/rules/no-tag-ladder-assertions.d.ts +6 -0
- package/dist/src/rules/no-tag-ladder-assertions.js +63 -0
- package/dist/src/rules/no-transposed-field-reads.d.ts +8 -0
- package/dist/src/rules/no-transposed-field-reads.js +101 -0
- package/dist/src/rules/no-unit-return-validators.d.ts +2 -0
- package/dist/src/rules/no-unit-return-validators.js +64 -0
- package/dist/src/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/src/rules/no-unknown-parameters.js +82 -0
- package/dist/src/rules/no-unknown-returns.d.ts +2 -0
- package/dist/src/rules/no-unknown-returns.js +86 -0
- package/dist/src/rules/no-unknown-type-aliases.d.ts +2 -0
- package/dist/src/rules/no-unknown-type-aliases.js +64 -0
- package/dist/src/rules/no-unsafe-dictionary-type.d.ts +2 -0
- package/dist/src/rules/no-unsafe-dictionary-type.js +116 -0
- package/dist/src/rules/no-vague-test-filenames.d.ts +2 -0
- package/dist/src/rules/no-vague-test-filenames.js +61 -0
- package/dist/src/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/src/rules/no-widen-then-assert.js +268 -0
- package/dist/src/rules/prefer-payload-brand.d.ts +2 -0
- package/dist/src/rules/prefer-payload-brand.js +57 -0
- package/dist/src/rules/prefer-property-tests.d.ts +5 -0
- package/dist/src/rules/prefer-property-tests.js +39 -0
- package/dist/src/rules/require-canonical-stringify-for-identity.d.ts +8 -0
- package/dist/src/rules/require-canonical-stringify-for-identity.js +72 -0
- package/dist/src/rules/require-deprecated-tag-for-legacy-comments.d.ts +2 -0
- package/dist/src/rules/require-deprecated-tag-for-legacy-comments.js +34 -0
- package/dist/src/rules/require-exhaustive-tag-switch.d.ts +7 -0
- package/dist/src/rules/require-exhaustive-tag-switch.js +34 -0
- package/dist/src/rules/require-fc-block-predicate.d.ts +2 -0
- package/dist/src/rules/require-fc-block-predicate.js +37 -0
- package/dist/src/rules/require-jsdoc-on-exported.d.ts +2 -0
- package/dist/src/rules/require-jsdoc-on-exported.js +66 -0
- package/dist/src/rules/require-published-order.d.ts +2 -0
- package/dist/src/rules/require-published-order.js +102 -0
- package/dist/src/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
- package/dist/src/rules/require-safety-comment-for-type-assertion.js +53 -0
- package/dist/src/rules/require-sort-comparator.d.ts +7 -0
- package/dist/src/rules/require-sort-comparator.js +34 -0
- package/dist/src/rules/test-utils.d.ts +35 -0
- package/dist/src/rules/test-utils.js +53 -0
- package/dist/src/shared/ancestors.d.ts +18 -0
- package/dist/src/shared/ancestors.js +19 -0
- package/dist/src/shared/child-nodes.d.ts +12 -0
- package/dist/src/shared/child-nodes.js +26 -0
- package/dist/src/shared/dictionary-types.d.ts +55 -0
- package/dist/src/shared/dictionary-types.js +413 -0
- package/dist/src/shared/is-node.d.ts +9 -0
- package/dist/src/shared/is-node.js +10 -0
- package/dist/src/shared/lexical-type-parameters.d.ts +12 -0
- package/dist/src/shared/lexical-type-parameters.js +50 -0
- package/dist/src/shared/reflect-method.d.ts +15 -0
- package/dist/src/shared/reflect-method.js +32 -0
- package/dist/src/shared/rule-options.d.ts +18 -0
- package/dist/src/shared/rule-options.js +12 -0
- package/dist/src/shared/structural.d.ts +54 -0
- package/dist/src/shared/structural.js +40 -0
- package/dist/src/testing/index.d.ts +2 -0
- package/dist/src/testing/index.js +1 -0
- package/dist/src/testing/testable-rule.d.ts +39 -0
- package/dist/src/testing/testable-rule.js +84 -0
- package/dist/src/ui/index.d.ts +12 -0
- package/dist/src/ui/index.js +18 -0
- package/dist/src/ui/manifest.d.ts +34 -0
- package/dist/src/ui/manifest.js +75 -0
- package/dist/src/ui/rules/no-deprecated-parts.d.ts +3 -0
- package/dist/src/ui/rules/no-deprecated-parts.js +131 -0
- package/dist/testing/testable-rule.d.ts +6 -5
- package/dist/testing/testable-rule.js +20 -6
- package/dist/ui/index.d.ts +12 -0
- package/dist/ui/index.js +18 -0
- package/dist/ui/manifest.d.ts +49 -0
- package/dist/ui/manifest.js +90 -0
- package/dist/ui/rules/no-deprecated-parts.d.ts +3 -0
- package/dist/ui/rules/no-deprecated-parts.js +172 -0
- package/package.json +20 -11
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
/** Single words whose bare grep hits span unrelated subsystems (the `create` problem). */
|
|
3
|
+
const GENERIC_TERMS = new Set([
|
|
4
|
+
"create",
|
|
5
|
+
"init",
|
|
6
|
+
"initialize",
|
|
7
|
+
"get",
|
|
8
|
+
"set",
|
|
9
|
+
"validate",
|
|
10
|
+
"process",
|
|
11
|
+
"handle",
|
|
12
|
+
"run",
|
|
13
|
+
"make",
|
|
14
|
+
"build",
|
|
15
|
+
"execute",
|
|
16
|
+
"update",
|
|
17
|
+
"parse",
|
|
18
|
+
"format",
|
|
19
|
+
"convert",
|
|
20
|
+
"transform",
|
|
21
|
+
"start",
|
|
22
|
+
"stop",
|
|
23
|
+
"load",
|
|
24
|
+
"save",
|
|
25
|
+
"check",
|
|
26
|
+
"do",
|
|
27
|
+
"main",
|
|
28
|
+
"util",
|
|
29
|
+
"utils",
|
|
30
|
+
"helper",
|
|
31
|
+
"helpers",
|
|
32
|
+
"misc",
|
|
33
|
+
"common",
|
|
34
|
+
"data",
|
|
35
|
+
"manager",
|
|
36
|
+
"service",
|
|
37
|
+
"wrapper",
|
|
38
|
+
"temp",
|
|
39
|
+
"tmp",
|
|
40
|
+
]);
|
|
41
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
42
|
+
/** Split an identifier into its words across camelCase and separators.
|
|
43
|
+
*
|
|
44
|
+
* @param {string} name - Identifier or filename stem to split.
|
|
45
|
+
* @returns {Array<string>} Non-empty words of the name.
|
|
46
|
+
*/
|
|
47
|
+
function splitWords(name) {
|
|
48
|
+
return name
|
|
49
|
+
.replace(/([a-z0-9])([A-Z])/gu, "$1 $2")
|
|
50
|
+
.split(/[^A-Za-z0-9]+/u)
|
|
51
|
+
.filter((word) => word.length > 0);
|
|
52
|
+
}
|
|
53
|
+
/** Module segment acting as the qualification prefix at qualified call sites.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} filename - Lint-context filename of the module.
|
|
56
|
+
* @returns {Array<string>} Words of the module's qualifying segment.
|
|
57
|
+
*/
|
|
58
|
+
function qualifierWords(filename) {
|
|
59
|
+
const segments = filename.replaceAll("\\", "/").split("/");
|
|
60
|
+
const base = segments[segments.length - 1] ?? "";
|
|
61
|
+
let stem = base.replace(/\.[cm]?[jt]sx?$/u, "");
|
|
62
|
+
if (stem === "index" || stem === "mod") {
|
|
63
|
+
stem = segments[segments.length - 2] ?? stem;
|
|
64
|
+
}
|
|
65
|
+
return splitWords(stem);
|
|
66
|
+
}
|
|
67
|
+
/** True when every word of the name is generic, so qualification adds nothing either.
|
|
68
|
+
*
|
|
69
|
+
* @param {Array<string>} words - Words of the exported symbol's name.
|
|
70
|
+
* @returns {boolean} True when no word carries domain meaning.
|
|
71
|
+
*/
|
|
72
|
+
function isAllGeneric(words) {
|
|
73
|
+
return (words.length > 0 &&
|
|
74
|
+
words.every((word) => GENERIC_TERMS.has(word.toLowerCase())));
|
|
75
|
+
}
|
|
76
|
+
/** Exported symbols must resolve by grep in one hop; qualification by the module counts. */
|
|
77
|
+
export const noGenericExportNamesRule = defineRule({
|
|
78
|
+
meta: {
|
|
79
|
+
type: "problem",
|
|
80
|
+
docs: {
|
|
81
|
+
description: "Disallow exported symbol names that stay generic even after module qualification (e.g. utils.ts exporting `create`); a distinctive qualified token such as `Currency.make` greps to exactly the right definition and call sites.",
|
|
82
|
+
},
|
|
83
|
+
messages: {
|
|
84
|
+
genericExportName: "Exported symbol `{{qualified}}` stays generic, so searching it lands on hundreds of unrelated hits. Add a domain word to the name (e.g. `create` -> `createStripeClient`) or move it into a domain-named module so callers read `{{domain}}.{{name}}`.",
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
createOnce(context) {
|
|
88
|
+
let moduleWords = [];
|
|
89
|
+
let moduleSuppliesDomain = false;
|
|
90
|
+
return {
|
|
91
|
+
before() {
|
|
92
|
+
const filePath = context.filename.replaceAll("\\", "/");
|
|
93
|
+
if (TEST_FILE.test(filePath))
|
|
94
|
+
return false;
|
|
95
|
+
moduleWords = qualifierWords(filePath);
|
|
96
|
+
moduleSuppliesDomain = moduleWords.some((word) => !GENERIC_TERMS.has(word.toLowerCase()));
|
|
97
|
+
},
|
|
98
|
+
Program(node) {
|
|
99
|
+
for (const statement of node.body) {
|
|
100
|
+
const declaration = statement.type === "ExportNamedDeclaration" ||
|
|
101
|
+
statement.type === "ExportDefaultDeclaration"
|
|
102
|
+
? (statement.declaration ?? null)
|
|
103
|
+
: statement;
|
|
104
|
+
if (declaration === null)
|
|
105
|
+
continue;
|
|
106
|
+
const identifiers = [];
|
|
107
|
+
if (declaration.type === "FunctionDeclaration" ||
|
|
108
|
+
declaration.type === "ClassDeclaration" ||
|
|
109
|
+
declaration.type === "TSInterfaceDeclaration" ||
|
|
110
|
+
declaration.type === "TSTypeAliasDeclaration" ||
|
|
111
|
+
declaration.type === "TSEnumDeclaration") {
|
|
112
|
+
if (declaration.id !== null)
|
|
113
|
+
identifiers.push(declaration.id);
|
|
114
|
+
}
|
|
115
|
+
else if (declaration.type === "VariableDeclaration") {
|
|
116
|
+
for (const declarator of declaration.declarations) {
|
|
117
|
+
if (declarator.id.type === "Identifier")
|
|
118
|
+
identifiers.push(declarator.id);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
for (const id of identifiers) {
|
|
122
|
+
const nameWords = splitWords(id.name);
|
|
123
|
+
if (!isAllGeneric(nameWords))
|
|
124
|
+
continue;
|
|
125
|
+
if (moduleSuppliesDomain)
|
|
126
|
+
continue;
|
|
127
|
+
context.report({
|
|
128
|
+
node: id,
|
|
129
|
+
messageId: "genericExportName",
|
|
130
|
+
data: {
|
|
131
|
+
name: id.name,
|
|
132
|
+
qualified: moduleWords.concat(nameWords).join("."),
|
|
133
|
+
domain: moduleWords.join(""),
|
|
134
|
+
},
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
},
|
|
139
|
+
};
|
|
140
|
+
},
|
|
141
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { isRecordObject, isString } from "../shared/structural.js";
|
|
3
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
4
|
+
const IMPOSSIBLE_MESSAGE = /(?:never\s+happen|impossible|unreachable|invariant|cannot\s+happen|can'?t\s+happen|unexpected\s+state|not\s+possible)/iu;
|
|
5
|
+
/** Extract the prose message of a thrown error literal or constructor.
|
|
6
|
+
*
|
|
7
|
+
* @param {ESTree.ThrowStatement} node - The throw statement to inspect.
|
|
8
|
+
* @returns {string | null} The thrown message, or null when not a string literal.
|
|
9
|
+
*/
|
|
10
|
+
function throwMessage(node) {
|
|
11
|
+
const argument = node.argument;
|
|
12
|
+
if (argument === null || argument === undefined)
|
|
13
|
+
return null;
|
|
14
|
+
if (argument.type === "Literal" && isString(argument.value))
|
|
15
|
+
return argument.value;
|
|
16
|
+
if (argument.type === "NewExpression") {
|
|
17
|
+
const first = argument.arguments[0];
|
|
18
|
+
if (first !== undefined && first.type === "Literal" && isString(first.value)) {
|
|
19
|
+
return first.value;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
/** Reject "should never happen" throws; encode the case out of the type instead. */
|
|
25
|
+
export const noImpossibleBranchThrowRule = defineRule({
|
|
26
|
+
meta: {
|
|
27
|
+
type: "problem",
|
|
28
|
+
docs: {
|
|
29
|
+
description: "Disallow hand-asserted impossible branches (throws about never-happens/invariants); encode the case out of the type so the compiler rejects it.",
|
|
30
|
+
},
|
|
31
|
+
messages: {
|
|
32
|
+
impossibleThrow: "This branch is asserted impossible by hand (\"{{message}}\"). If it truly cannot occur, parse/encode it out of the type (Option, parsed/refined type) so the compiler rejects it; otherwise return a structured failure.",
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
createOnce(context) {
|
|
36
|
+
return {
|
|
37
|
+
before() {
|
|
38
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
39
|
+
return false;
|
|
40
|
+
},
|
|
41
|
+
ThrowStatement(node) {
|
|
42
|
+
const message = throwMessage(node);
|
|
43
|
+
if (message === null)
|
|
44
|
+
return;
|
|
45
|
+
if (!IMPOSSIBLE_MESSAGE.test(message))
|
|
46
|
+
return;
|
|
47
|
+
context.report({ node, messageId: "impossibleThrow", data: { message } });
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
@@ -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;
|