@autotelic/plumb 0.5.2 → 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/index.js +4 -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.d.ts +13 -0
- package/dist/effect/rules/no-nullish-default-on-partial-input.js +109 -0
- package/dist/effect/rules/no-raw-error-forwarding.d.ts +16 -0
- package/dist/effect/rules/no-raw-error-forwarding.js +108 -0
- 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 +10 -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.d.ts +7 -0
- package/dist/rules/no-sql-string-interpolation.js +62 -0
- 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,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a React hook used at `hookName` inside a function named `parentName`
|
|
3
|
+
* violates the encapsulation rule. Provider components (XProvider) are the
|
|
4
|
+
* stateful root of a context family and legitimately use hooks inline, so they
|
|
5
|
+
* are exempt.
|
|
6
|
+
*
|
|
7
|
+
* @param {{ hookName: string; parentName: string }} input - The usage facts.
|
|
8
|
+
* @returns {boolean} True when the usage should be reported.
|
|
9
|
+
*/
|
|
10
|
+
export declare function isEncapsulationViolation(input: {
|
|
11
|
+
hookName: string;
|
|
12
|
+
parentName: string;
|
|
13
|
+
}): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Do not call React's hooks directly inside a component. Abstract the
|
|
16
|
+
* functionality into a custom hook (`useXxx`) and call that instead - the
|
|
17
|
+
* "useEncapsulation" pattern. A component that reaches for `useState`/`useEffect`
|
|
18
|
+
* inline hides its state and effects; a custom hook names them and can be tested
|
|
19
|
+
* and composed in isolation. Provider components (XProvider) are exempt: they are
|
|
20
|
+
* the stateful root of a context family and must use hooks inline.
|
|
21
|
+
*/
|
|
22
|
+
export declare const useEncapsulationRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../../shared/ancestors.js";
|
|
3
|
+
import { readField } from "../../shared/structural.js";
|
|
4
|
+
import { isProviderName } from "../role.js";
|
|
5
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
6
|
+
/** Pattern identifying custom hooks (names starting with `use`). */
|
|
7
|
+
const HOOK_PATTERN = /^use/u;
|
|
8
|
+
/** React's built-in hooks that must not be used directly inside components. */
|
|
9
|
+
const REACT_HOOKS = new Set([
|
|
10
|
+
"useCallback",
|
|
11
|
+
"useContext",
|
|
12
|
+
"useDebugValue",
|
|
13
|
+
"useDeferredValue",
|
|
14
|
+
"useEffect",
|
|
15
|
+
"useId",
|
|
16
|
+
"useImperativeHandle",
|
|
17
|
+
"useInsertionEffect",
|
|
18
|
+
"useLayoutEffect",
|
|
19
|
+
"useMemo",
|
|
20
|
+
"useReducer",
|
|
21
|
+
"useRef",
|
|
22
|
+
"useState",
|
|
23
|
+
"useSyncExternalStore",
|
|
24
|
+
"useTransition",
|
|
25
|
+
]);
|
|
26
|
+
/**
|
|
27
|
+
* Whether a React hook used at `hookName` inside a function named `parentName`
|
|
28
|
+
* violates the encapsulation rule. Provider components (XProvider) are the
|
|
29
|
+
* stateful root of a context family and legitimately use hooks inline, so they
|
|
30
|
+
* are exempt.
|
|
31
|
+
*
|
|
32
|
+
* @param {{ hookName: string; parentName: string }} input - The usage facts.
|
|
33
|
+
* @returns {boolean} True when the usage should be reported.
|
|
34
|
+
*/
|
|
35
|
+
export function isEncapsulationViolation(input) {
|
|
36
|
+
return REACT_HOOKS.has(input.hookName) && !HOOK_PATTERN.test(input.parentName) && !isProviderName(input.parentName);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Do not call React's hooks directly inside a component. Abstract the
|
|
40
|
+
* functionality into a custom hook (`useXxx`) and call that instead - the
|
|
41
|
+
* "useEncapsulation" pattern. A component that reaches for `useState`/`useEffect`
|
|
42
|
+
* inline hides its state and effects; a custom hook names them and can be tested
|
|
43
|
+
* and composed in isolation. Provider components (XProvider) are exempt: they are
|
|
44
|
+
* the stateful root of a context family and must use hooks inline.
|
|
45
|
+
*/
|
|
46
|
+
export const useEncapsulationRule = defineRule({
|
|
47
|
+
meta: {
|
|
48
|
+
type: "problem",
|
|
49
|
+
docs: {
|
|
50
|
+
description: "Disallow using React's hooks directly inside a component; abstract them into a custom hook (useXxx) and call that instead (useEncapsulation).",
|
|
51
|
+
},
|
|
52
|
+
messages: {
|
|
53
|
+
noDirectHooks: "Do not use React hook `{{hook}}` directly inside component `{{parent}}`. Abstract the functionality into a custom hook (useXxx) and call that instead.",
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
createOnce(context) {
|
|
57
|
+
const raw = readField(context, "options");
|
|
58
|
+
const options = raw?.[0] ?? {};
|
|
59
|
+
const allowed = new Set(options.allow ?? []);
|
|
60
|
+
const blocked = new Set(options.block ?? []);
|
|
61
|
+
const hooksToCheck = new Set([...REACT_HOOKS, ...blocked].filter((h) => !allowed.has(h)));
|
|
62
|
+
function nearestFunctionName(node) {
|
|
63
|
+
const ancestors = ancestorsOf(context.sourceCode, node);
|
|
64
|
+
for (let i = ancestors.length - 1; i >= 0; i--) {
|
|
65
|
+
const a = ancestors[i];
|
|
66
|
+
if (a === undefined)
|
|
67
|
+
continue;
|
|
68
|
+
if (a.type === "FunctionDeclaration" && a.id !== null && a.id !== undefined)
|
|
69
|
+
return a.id.name;
|
|
70
|
+
if (a.type === "FunctionExpression" && a.id !== null && a.id !== undefined)
|
|
71
|
+
return a.id.name;
|
|
72
|
+
if (a.type === "VariableDeclarator" && a.id.type === "Identifier") {
|
|
73
|
+
const init = a.init;
|
|
74
|
+
if (init !== null &&
|
|
75
|
+
init !== undefined &&
|
|
76
|
+
(init.type === "ArrowFunctionExpression" || init.type === "FunctionExpression"))
|
|
77
|
+
return a.id.name;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
before() {
|
|
84
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
85
|
+
return false;
|
|
86
|
+
},
|
|
87
|
+
Identifier(node) {
|
|
88
|
+
if (!hooksToCheck.has(node.name))
|
|
89
|
+
return;
|
|
90
|
+
const parent = nearestFunctionName(node);
|
|
91
|
+
if (parent !== null && !HOOK_PATTERN.test(parent) && !isProviderName(parent)) {
|
|
92
|
+
context.report({ node, messageId: "noDirectHooks", data: { hook: node.name, parent } });
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dense representations are sliceable on any axis because their parts are
|
|
3
|
+
* named. Past two elements, positional tuples hide which position carries
|
|
4
|
+
* which fact: swap-prone, unreadable at call sites, and impossible to
|
|
5
|
+
* slice meaningfully. Give wide tuples a named shape.
|
|
6
|
+
*/
|
|
7
|
+
export declare const noAnonymousWideTuplesRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { readField } from "../shared/structural.js";
|
|
3
|
+
/**
|
|
4
|
+
* Dense representations are sliceable on any axis because their parts are
|
|
5
|
+
* named. Past two elements, positional tuples hide which position carries
|
|
6
|
+
* which fact: swap-prone, unreadable at call sites, and impossible to
|
|
7
|
+
* slice meaningfully. Give wide tuples a named shape.
|
|
8
|
+
*/
|
|
9
|
+
export const noAnonymousWideTuplesRule = defineRule({
|
|
10
|
+
meta: {
|
|
11
|
+
type: "suggestion",
|
|
12
|
+
docs: {
|
|
13
|
+
description: "Disallow anonymous tuple types of three or more elements; use a named object so every field's role is explicit.",
|
|
14
|
+
},
|
|
15
|
+
messages: {
|
|
16
|
+
wideTuple: "This {{count}}-element tuple leaves {{count}} unnamed positions whose meaning lives only in the reader's head. Declare a named object (or tagged union) instead so fields are self-describing.",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
createOnce(context) {
|
|
20
|
+
return {
|
|
21
|
+
TSTupleType(node) {
|
|
22
|
+
const elements = readField(node, "elementTypes") ?? [];
|
|
23
|
+
const count = elements.length;
|
|
24
|
+
if (count < 3)
|
|
25
|
+
return;
|
|
26
|
+
context.report({
|
|
27
|
+
node,
|
|
28
|
+
messageId: "wideTuple",
|
|
29
|
+
data: { count: String(count) },
|
|
30
|
+
});
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
/** Barrel `export *` erases the names it forwards, forcing search-driven readers back inside the package. */
|
|
3
|
+
export const noBarrelExportStarRule = defineRule({
|
|
4
|
+
meta: {
|
|
5
|
+
type: "problem",
|
|
6
|
+
docs: {
|
|
7
|
+
description: "Disallow `export * from ...` re-exports because the star erases forwarded names; an agent navigating by text search cannot resolve which module owns a symbol and must re-grep inside the package.",
|
|
8
|
+
},
|
|
9
|
+
messages: {
|
|
10
|
+
erasesForwardedNames: "`export * from '{{source}}'` erases the names it forwards; readers resolving a symbol through this barrel must grep inside the package instead. Re-export the explicit names.",
|
|
11
|
+
},
|
|
12
|
+
},
|
|
13
|
+
createOnce(context) {
|
|
14
|
+
return {
|
|
15
|
+
ExportAllDeclaration(node) {
|
|
16
|
+
if (node.exported !== null && node.exported !== undefined)
|
|
17
|
+
return;
|
|
18
|
+
context.report({
|
|
19
|
+
node,
|
|
20
|
+
messageId: "erasesForwardedNames",
|
|
21
|
+
data: { source: String(node.source.value) },
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
});
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
const COMPARISON_OPERATORS = new Set(["===", "!==", "==", "!=", "<", ">", "<=", ">="]);
|
|
3
|
+
const CONSTRUCTOR_NAME = /^(?:make[A-Z]|make$|fromRaw|unsafeMake)/u;
|
|
4
|
+
function parameterAnnotation(parameter) {
|
|
5
|
+
if (parameter.type === "TSParameterProperty") {
|
|
6
|
+
return parameterAnnotation(parameter.parameter);
|
|
7
|
+
}
|
|
8
|
+
if (parameter.type === "RestElement") {
|
|
9
|
+
return parameter.typeAnnotation ?? parameterAnnotation(parameter.argument);
|
|
10
|
+
}
|
|
11
|
+
if (parameter.type === "AssignmentPattern") {
|
|
12
|
+
return parameter.typeAnnotation ?? parameter.left.typeAnnotation;
|
|
13
|
+
}
|
|
14
|
+
return parameter.typeAnnotation;
|
|
15
|
+
}
|
|
16
|
+
function referencedTypeName(type) {
|
|
17
|
+
if (type.type === "TSParenthesizedType")
|
|
18
|
+
return referencedTypeName(type.typeAnnotation);
|
|
19
|
+
if (type.type !== "TSTypeReference" || type.typeName.type !== "Identifier")
|
|
20
|
+
return null;
|
|
21
|
+
return type.typeName.name;
|
|
22
|
+
}
|
|
23
|
+
function isLiteralExpression(expression) {
|
|
24
|
+
if (expression.type === "Literal")
|
|
25
|
+
return true;
|
|
26
|
+
return (expression.type === "UnaryExpression" &&
|
|
27
|
+
expression.operator === "-" &&
|
|
28
|
+
expression.argument.type === "Literal");
|
|
29
|
+
}
|
|
30
|
+
function isParamField(payload) {
|
|
31
|
+
const { expression, paramName } = payload;
|
|
32
|
+
if (expression.type === "PrivateIdentifier")
|
|
33
|
+
return false;
|
|
34
|
+
if (expression.type !== "MemberExpression" || expression.object.type !== "Identifier") {
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
if (expression.object.name !== paramName)
|
|
38
|
+
return false;
|
|
39
|
+
if (expression.computed)
|
|
40
|
+
return expression.property.type === "Literal";
|
|
41
|
+
return expression.property.type === "Identifier" || expression.property.type === "PrivateIdentifier";
|
|
42
|
+
}
|
|
43
|
+
function isFieldSignalBody(payload) {
|
|
44
|
+
const { expression, paramName } = payload;
|
|
45
|
+
if (expression.type === "LogicalExpression") {
|
|
46
|
+
return (isFieldSignalBody({ expression: expression.left, paramName }) &&
|
|
47
|
+
isFieldSignalBody({ expression: expression.right, paramName }));
|
|
48
|
+
}
|
|
49
|
+
if (expression.type === "UnaryExpression" && expression.operator === "!") {
|
|
50
|
+
return isFieldSignalBody({ expression: expression.argument, paramName });
|
|
51
|
+
}
|
|
52
|
+
if (expression.type === "ParenthesizedExpression") {
|
|
53
|
+
return isFieldSignalBody({ expression: expression.expression, paramName });
|
|
54
|
+
}
|
|
55
|
+
if (expression.type === "BinaryExpression" && COMPARISON_OPERATORS.has(expression.operator)) {
|
|
56
|
+
const fieldOnLeft = isParamField({ expression: expression.left, paramName });
|
|
57
|
+
const literalOnRight = isLiteralExpression(expression.right);
|
|
58
|
+
const fieldOnRight = isParamField({ expression: expression.right, paramName });
|
|
59
|
+
const literalOnLeft = isLiteralExpression(expression.left);
|
|
60
|
+
return (fieldOnLeft && literalOnRight) || (fieldOnRight && literalOnLeft);
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Build a predicate-analysis candidate when the function reads as a boolean
|
|
66
|
+
* classification derived solely from comparing one typed parameter's fields.
|
|
67
|
+
*
|
|
68
|
+
* @param {PredicateFunction} node - The exported function node under analysis.
|
|
69
|
+
* @returns {PredicateCandidate | null} The candidate, or null when not a field-signal predicate.
|
|
70
|
+
*/
|
|
71
|
+
function candidateFrom(node) {
|
|
72
|
+
if (node.returnType === null || node.returnType === undefined)
|
|
73
|
+
return null;
|
|
74
|
+
const returnType = node.returnType.typeAnnotation;
|
|
75
|
+
if (returnType.type !== "TSBooleanKeyword")
|
|
76
|
+
return null;
|
|
77
|
+
if (node.params.length !== 1)
|
|
78
|
+
return null;
|
|
79
|
+
const firstParam = node.params[0];
|
|
80
|
+
if (firstParam === undefined)
|
|
81
|
+
return null;
|
|
82
|
+
const annotation = parameterAnnotation(firstParam);
|
|
83
|
+
if (annotation === null || annotation === undefined)
|
|
84
|
+
return null;
|
|
85
|
+
const typeName = referencedTypeName(annotation.typeAnnotation);
|
|
86
|
+
if (typeName === null)
|
|
87
|
+
return null;
|
|
88
|
+
let body = null;
|
|
89
|
+
if (node.body !== null && node.body !== undefined) {
|
|
90
|
+
if (node.body.type === "BlockStatement") {
|
|
91
|
+
const only = node.body.body.length === 1 ? node.body.body[0] : undefined;
|
|
92
|
+
if (only !== undefined && only.type === "ReturnStatement" && only.argument !== null) {
|
|
93
|
+
body = only.argument;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
body = node.body;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
if (body === null)
|
|
101
|
+
return null;
|
|
102
|
+
if (!isFieldSignalBody({
|
|
103
|
+
expression: body,
|
|
104
|
+
paramName: firstParam.type === "Identifier" ? firstParam.name : "",
|
|
105
|
+
})) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
return { node, typeName };
|
|
109
|
+
}
|
|
110
|
+
const AUXILIARIES = new Set(["is", "has", "can", "should", "was", "will", "did", "be", "the"]);
|
|
111
|
+
/** Split a camelCase/separator name into lowercase tokens, dropping auxiliary prefixes like `is`/`has`.
|
|
112
|
+
*
|
|
113
|
+
* @param {string} name - The name to tokenise.
|
|
114
|
+
* @returns {string[]} Content-bearing lowercase tokens.
|
|
115
|
+
*/
|
|
116
|
+
function contentTokens(name) {
|
|
117
|
+
return name
|
|
118
|
+
.replaceAll(/([a-z0-9])([A-Z])/gu, "$1 $2")
|
|
119
|
+
.split(/[^a-zA-Z0-9]+/u)
|
|
120
|
+
.map((token) => token.toLowerCase())
|
|
121
|
+
.filter((token) => token.length > 0 && !AUXILIARIES.has(token));
|
|
122
|
+
}
|
|
123
|
+
/** Accept both `{ members }` and flattened `{ body }` shapes for an interface/type-literal body.
|
|
124
|
+
*
|
|
125
|
+
* @param {ESTree.TSInterfaceBody | ESTree.TSTypeLiteral} bodyNode - The declared body node.
|
|
126
|
+
* @returns {ESTree.TSSignature[]} The signature list.
|
|
127
|
+
*/
|
|
128
|
+
function memberList(bodyNode) {
|
|
129
|
+
const members = "body" in bodyNode ? bodyNode.body : bodyNode.members;
|
|
130
|
+
return [...members];
|
|
131
|
+
}
|
|
132
|
+
/** Ban predicate families that re-derive classification, and un-guarded construction. */
|
|
133
|
+
export const noBooleanFieldSignalsRule = defineRule({
|
|
134
|
+
meta: {
|
|
135
|
+
type: "problem",
|
|
136
|
+
docs: {
|
|
137
|
+
description: "Disallow families of exported predicates over one type that re-test the same fields, constructors that produce un-guarded values, and boolean state products on declared types; classify (parse) once.",
|
|
138
|
+
},
|
|
139
|
+
messages: {
|
|
140
|
+
reportPredicate: "Predicates over `{{type}}` each re-derive classification from the same field comparisons. Classify the value once into a named union (or comparable structure) and derive the predicates from it.",
|
|
141
|
+
booleanFieldSignals: "Predicates over `{{type}}` each re-derive classification from the same field comparisons. Classify the value once into a named union (or comparable structure) and derive the predicates from it.",
|
|
142
|
+
unguardedConstructor: "`{{type}}` is constructed directly by `make` yet inspected by a predicate family. Make the constructor a parser (normalise or return Option) so invalid states can't be produced.",
|
|
143
|
+
booleanStateProduct: "`{{name}}` carries {{count}} boolean {{kind}} admitting invalid combinations. Classify once into a named union over that state so impossible combinations can't be represented, and derive any predicates from it.",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
createOnce(context) {
|
|
147
|
+
const pending = new Map();
|
|
148
|
+
const reported = new Set();
|
|
149
|
+
const constructors = new Map();
|
|
150
|
+
const reportPredicate = (candidate) => {
|
|
151
|
+
context.report({
|
|
152
|
+
node: candidate.node,
|
|
153
|
+
messageId: "booleanFieldSignals",
|
|
154
|
+
data: { type: candidate.typeName },
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
const flushConstructors = (typeName) => {
|
|
158
|
+
const list = constructors.get(typeName);
|
|
159
|
+
if (list === undefined)
|
|
160
|
+
return;
|
|
161
|
+
for (const node of list) {
|
|
162
|
+
context.report({ node, messageId: "unguardedConstructor", data: { type: typeName } });
|
|
163
|
+
}
|
|
164
|
+
constructors.delete(typeName);
|
|
165
|
+
};
|
|
166
|
+
const recordPredicate = (candidate) => {
|
|
167
|
+
if (candidate === null)
|
|
168
|
+
return;
|
|
169
|
+
if (reported.has(candidate.typeName)) {
|
|
170
|
+
reportPredicate(candidate);
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const first = pending.get(candidate.typeName);
|
|
174
|
+
if (first !== undefined) {
|
|
175
|
+
reportPredicate(first);
|
|
176
|
+
reportPredicate(candidate);
|
|
177
|
+
reported.add(candidate.typeName);
|
|
178
|
+
pending.delete(candidate.typeName);
|
|
179
|
+
flushConstructors(candidate.typeName);
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
pending.set(candidate.typeName, candidate);
|
|
183
|
+
};
|
|
184
|
+
const recordConstructor = (typeName, node) => {
|
|
185
|
+
if (typeName === null)
|
|
186
|
+
return;
|
|
187
|
+
if (reported.has(typeName)) {
|
|
188
|
+
context.report({ node, messageId: "unguardedConstructor", data: { type: typeName } });
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const list = constructors.get(typeName) ?? [];
|
|
192
|
+
list.push(node);
|
|
193
|
+
constructors.set(typeName, list);
|
|
194
|
+
};
|
|
195
|
+
const checkDeclaredBody = (reportNode, members, typeName) => {
|
|
196
|
+
const booleanNames = members.flatMap((member) => {
|
|
197
|
+
if (member.type !== "TSPropertySignature")
|
|
198
|
+
return [];
|
|
199
|
+
if (member.typeAnnotation?.typeAnnotation.type !== "TSBooleanKeyword")
|
|
200
|
+
return [];
|
|
201
|
+
return member.key.type === "Identifier" ? [member.key.name] : [];
|
|
202
|
+
});
|
|
203
|
+
if (booleanNames.length >= 3) {
|
|
204
|
+
context.report({
|
|
205
|
+
node: reportNode,
|
|
206
|
+
messageId: "booleanStateProduct",
|
|
207
|
+
data: { name: typeName, count: String(booleanNames.length), kind: "flags" },
|
|
208
|
+
});
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
for (let i = 0; i < booleanNames.length; i += 1) {
|
|
212
|
+
for (let j = i + 1; j < booleanNames.length; j += 1) {
|
|
213
|
+
const first = contentTokens(booleanNames[i] ?? "");
|
|
214
|
+
const second = contentTokens(booleanNames[j] ?? "");
|
|
215
|
+
const shared = first.find((token) => second.includes(token));
|
|
216
|
+
if (shared === undefined)
|
|
217
|
+
continue;
|
|
218
|
+
context.report({
|
|
219
|
+
node: reportNode,
|
|
220
|
+
messageId: "booleanStateProduct",
|
|
221
|
+
data: { name: `${booleanNames[i]}/${booleanNames[j]}`, count: "2", kind: `flags over \`${shared}\`` },
|
|
222
|
+
});
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
const handle = (name, fn, rawNode) => {
|
|
228
|
+
const firstParameter = fn.params[0];
|
|
229
|
+
const boundParameter = firstParameter === undefined
|
|
230
|
+
? null
|
|
231
|
+
: firstParameter.type === "Identifier"
|
|
232
|
+
? firstParameter.name
|
|
233
|
+
: firstParameter.type === "AssignmentPattern" && firstParameter.left.type === "Identifier"
|
|
234
|
+
? firstParameter.left.name
|
|
235
|
+
: null;
|
|
236
|
+
if (boundParameter !== null)
|
|
237
|
+
recordPredicate(candidateFrom(fn));
|
|
238
|
+
const declaredReturn = fn.returnType === null || fn.returnType === undefined
|
|
239
|
+
? null
|
|
240
|
+
: referencedTypeName(fn.returnType.typeAnnotation);
|
|
241
|
+
const ctorReturn = CONSTRUCTOR_NAME.test(name) ? declaredReturn : null;
|
|
242
|
+
if (ctorReturn !== null &&
|
|
243
|
+
(fn.type === "FunctionDeclaration" || fn.type === "FunctionExpression") &&
|
|
244
|
+
fn.id !== null) {
|
|
245
|
+
recordConstructor(ctorReturn, fn);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
return {
|
|
249
|
+
ExportNamedDeclaration(node) {
|
|
250
|
+
const declaration = node.declaration;
|
|
251
|
+
if (declaration === null)
|
|
252
|
+
return;
|
|
253
|
+
if (declaration.type === "FunctionDeclaration") {
|
|
254
|
+
if (declaration.id !== null)
|
|
255
|
+
handle(declaration.id.name, declaration, declaration);
|
|
256
|
+
return;
|
|
257
|
+
}
|
|
258
|
+
if (declaration.type !== "VariableDeclaration")
|
|
259
|
+
return;
|
|
260
|
+
for (const declarator of declaration.declarations) {
|
|
261
|
+
const init = declarator.init;
|
|
262
|
+
if (init === null || init === undefined || init.type !== "ArrowFunctionExpression")
|
|
263
|
+
continue;
|
|
264
|
+
if (declarator.id.type !== "Identifier")
|
|
265
|
+
continue;
|
|
266
|
+
handle(declarator.id.name, init, declarator);
|
|
267
|
+
}
|
|
268
|
+
},
|
|
269
|
+
TSInterfaceDeclaration(node) {
|
|
270
|
+
checkDeclaredBody(node, memberList(node.body), node.id.type === "Identifier" ? node.id.name : "anonymous interface");
|
|
271
|
+
},
|
|
272
|
+
TSTypeAliasDeclaration(node) {
|
|
273
|
+
if (node.typeAnnotation.type !== "TSTypeLiteral")
|
|
274
|
+
return;
|
|
275
|
+
checkDeclaredBody(node, node.typeAnnotation.members, node.id.name);
|
|
276
|
+
},
|
|
277
|
+
};
|
|
278
|
+
},
|
|
279
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flag throwing generic built-in errors at call sites that should instead throw
|
|
3
|
+
* a project-defined tagged error carrying its evidence structurally (e.g.
|
|
4
|
+
* Data.TaggedError with operation/value/domain fields plus a rendered message).
|
|
5
|
+
*/
|
|
6
|
+
export declare const noBuiltinThrowsRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { firstOptionRecord } from "../shared/rule-options.js";
|
|
3
|
+
import { isRecordObject, isString } from "../shared/structural.js";
|
|
4
|
+
const TEST_FILE = /\.(?:test|spec)\.[cm]?[jt]sx?$/u;
|
|
5
|
+
/** Generic built-in errors: untyped, prose-only, and invisible to tag-based handling. */
|
|
6
|
+
const BUILTIN_ERRORS = new Set([
|
|
7
|
+
"Error",
|
|
8
|
+
"EvalError",
|
|
9
|
+
"RangeError",
|
|
10
|
+
"ReferenceError",
|
|
11
|
+
"SyntaxError",
|
|
12
|
+
"TypeError",
|
|
13
|
+
"URIError",
|
|
14
|
+
"AggregateError",
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* Unwrap parenthesized expressions to the underlying expression.
|
|
18
|
+
*
|
|
19
|
+
* @param {ESTree.Expression} expression - The expression to unwrap.
|
|
20
|
+
* @returns {ESTree.Expression} The first non-parenthesized inner expression.
|
|
21
|
+
*/
|
|
22
|
+
function unwrapParentheses(expression) {
|
|
23
|
+
let current = expression;
|
|
24
|
+
while (current.type === "ParenthesizedExpression")
|
|
25
|
+
current = current.expression;
|
|
26
|
+
return current;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Flag throwing generic built-in errors at call sites that should instead throw
|
|
30
|
+
* a project-defined tagged error carrying its evidence structurally (e.g.
|
|
31
|
+
* Data.TaggedError with operation/value/domain fields plus a rendered message).
|
|
32
|
+
*/
|
|
33
|
+
export const noBuiltinThrowsRule = defineRule({
|
|
34
|
+
meta: {
|
|
35
|
+
type: "problem",
|
|
36
|
+
docs: {
|
|
37
|
+
description: "Disallow throwing built-in Error constructors; require project-defined tagged error classes so defects carry structured evidence and can be matched by tag.",
|
|
38
|
+
},
|
|
39
|
+
messages: {
|
|
40
|
+
noBuiltinThrows: "Throwing the built-in `{{builtin}}` loses structure: consumers get prose only. Throw a project-defined tagged error (e.g. Data.TaggedError) whose fields carry the operation, offending value, and domain, and render this context as its message.",
|
|
41
|
+
},
|
|
42
|
+
schema: [
|
|
43
|
+
{
|
|
44
|
+
type: "object",
|
|
45
|
+
properties: {
|
|
46
|
+
allow: { type: "array", items: { type: "string" } },
|
|
47
|
+
},
|
|
48
|
+
additionalProperties: false,
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
defaultOptions: [{ allow: [] }],
|
|
52
|
+
},
|
|
53
|
+
createOnce(context) {
|
|
54
|
+
const option = firstOptionRecord(context.options);
|
|
55
|
+
const allowed = new Set(Array.isArray(option.allow) ? option.allow.filter(isString) : []);
|
|
56
|
+
return {
|
|
57
|
+
before() {
|
|
58
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
59
|
+
return false;
|
|
60
|
+
},
|
|
61
|
+
ThrowStatement(node) {
|
|
62
|
+
const argument = node.argument === null || node.argument === undefined ? undefined : unwrapParentheses(node.argument);
|
|
63
|
+
if (argument?.type !== "NewExpression")
|
|
64
|
+
return;
|
|
65
|
+
if (argument.callee.type !== "Identifier")
|
|
66
|
+
return;
|
|
67
|
+
const builtin = argument.callee.name;
|
|
68
|
+
if (!BUILTIN_ERRORS.has(builtin) || allowed.has(builtin))
|
|
69
|
+
return;
|
|
70
|
+
context.report({ node, messageId: "noBuiltinThrows", data: { builtin } });
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
},
|
|
74
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
3
|
+
/** Disallow nested TypeScript type assertions, while permitting chains made only of const assertions. */
|
|
4
|
+
export const noChainedTypeAssertionsRule = defineRule({
|
|
5
|
+
meta: {
|
|
6
|
+
type: "problem",
|
|
7
|
+
docs: {
|
|
8
|
+
description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains.",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
chained: "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
createOnce(context) {
|
|
15
|
+
const checkTypeAssertion = (node) => {
|
|
16
|
+
const ancestors = ancestorsOf(context.sourceCode, node);
|
|
17
|
+
let current = node;
|
|
18
|
+
let index = ancestors.length - 1;
|
|
19
|
+
while (index >= 0) {
|
|
20
|
+
const parent = ancestors[index];
|
|
21
|
+
if (parent.type !== "ParenthesizedExpression" || parent.expression !== current)
|
|
22
|
+
break;
|
|
23
|
+
current = parent;
|
|
24
|
+
index -= 1;
|
|
25
|
+
}
|
|
26
|
+
const outermost = ancestors[index];
|
|
27
|
+
if (outermost !== undefined &&
|
|
28
|
+
(outermost.type === "TSAsExpression" || outermost.type === "TSTypeAssertion") &&
|
|
29
|
+
outermost.expression === current)
|
|
30
|
+
return;
|
|
31
|
+
let assertionCount = 0;
|
|
32
|
+
let hasNonConstAssertion = false;
|
|
33
|
+
current = node;
|
|
34
|
+
while (current.type === "TSAsExpression" || current.type === "TSTypeAssertion") {
|
|
35
|
+
assertionCount += 1;
|
|
36
|
+
const { typeAnnotation } = current;
|
|
37
|
+
hasNonConstAssertion ||= !(typeAnnotation.type === "TSTypeReference" &&
|
|
38
|
+
typeAnnotation.typeName.type === "Identifier" &&
|
|
39
|
+
typeAnnotation.typeName.name === "const");
|
|
40
|
+
let next = current.expression;
|
|
41
|
+
while (next.type === "ParenthesizedExpression")
|
|
42
|
+
next = next.expression;
|
|
43
|
+
current = next;
|
|
44
|
+
}
|
|
45
|
+
if (assertionCount > 1 && hasNonConstAssertion) {
|
|
46
|
+
context.report({ node, messageId: "chained" });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
return {
|
|
50
|
+
TSAsExpression: checkTypeAssertion,
|
|
51
|
+
TSTypeAssertion: checkTypeAssertion,
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
3
|
+
function isEmptyObjectExpression(node) {
|
|
4
|
+
return node.type === "ObjectExpression" && node.properties.length === 0;
|
|
5
|
+
}
|
|
6
|
+
/** Ban conditional empty-object spreads without changing their omission semantics. */
|
|
7
|
+
export const noConditionalEmptyObjectSpreadRule = defineRule({
|
|
8
|
+
meta: {
|
|
9
|
+
type: "suggestion",
|
|
10
|
+
docs: {
|
|
11
|
+
description: "Disallow object spreads that conditionally spread an empty object to omit fields.",
|
|
12
|
+
},
|
|
13
|
+
messages: {
|
|
14
|
+
avoid: "This conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present.",
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
createOnce(context) {
|
|
18
|
+
return {
|
|
19
|
+
SpreadElement(node) {
|
|
20
|
+
const parent = ancestorsOf(context.sourceCode, node).at(-1);
|
|
21
|
+
if (parent?.type !== "ObjectExpression")
|
|
22
|
+
return;
|
|
23
|
+
let conditional = node.argument;
|
|
24
|
+
while (conditional.type === "ParenthesizedExpression") {
|
|
25
|
+
conditional = conditional.expression;
|
|
26
|
+
}
|
|
27
|
+
const emptyObjectOmission = conditional.type === "ConditionalExpression" &&
|
|
28
|
+
(isEmptyObjectExpression(conditional.consequent) ||
|
|
29
|
+
isEmptyObjectExpression(conditional.alternate));
|
|
30
|
+
if (emptyObjectOmission) {
|
|
31
|
+
context.report({ node, messageId: "avoid" });
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
},
|
|
36
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One fact, one place: a set of variants declared twice drifts the moment
|
|
3
|
+
* someone adds a member to one copy. Declare each variant set once and
|
|
4
|
+
* derive every use from it.
|
|
5
|
+
*/
|
|
6
|
+
export declare const noDuplicatedLiteralUnionRule: import("@oxlint/plugins").Rule;
|