@autotelic/plumb 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/effect/index.d.ts +3 -0
- package/dist/effect/index.js +56 -0
- package/dist/effect/rules/guarded-op-must-return-effect.d.ts +6 -0
- package/dist/effect/rules/guarded-op-must-return-effect.js +235 -0
- package/dist/effect/rules/guarded-op-must-return-option.d.ts +2 -0
- package/dist/effect/rules/guarded-op-must-return-option.js +88 -0
- package/dist/effect/rules/no-cascading-layer-provide.d.ts +6 -0
- package/dist/effect/rules/no-cascading-layer-provide.js +73 -0
- package/dist/effect/rules/no-decode-unknown-option.d.ts +7 -0
- package/dist/effect/rules/no-decode-unknown-option.js +34 -0
- package/dist/effect/rules/no-direct-browser-storage.d.ts +2 -0
- package/dist/effect/rules/no-direct-browser-storage.js +88 -0
- package/dist/effect/rules/no-direct-fetch.d.ts +2 -0
- package/dist/effect/rules/no-direct-fetch.js +37 -0
- package/dist/effect/rules/no-manual-field-guards.d.ts +6 -0
- package/dist/effect/rules/no-manual-field-guards.js +129 -0
- package/dist/effect/rules/no-nested-layer-provide.d.ts +2 -0
- package/dist/effect/rules/no-nested-layer-provide.js +50 -0
- package/dist/effect/rules/no-schema-class-modeling.d.ts +2 -0
- package/dist/effect/rules/no-schema-class-modeling.js +52 -0
- package/dist/effect/rules/no-service-constructor-imports.d.ts +2 -0
- package/dist/effect/rules/no-service-constructor-imports.js +42 -0
- package/dist/effect/rules/no-silent-error-swallow.d.ts +2 -0
- package/dist/effect/rules/no-silent-error-swallow.js +95 -0
- package/dist/effect/rules/no-static-effect-service-forwarders.d.ts +6 -0
- package/dist/effect/rules/no-static-effect-service-forwarders.js +222 -0
- package/dist/effect/rules/no-try-catch.d.ts +2 -0
- package/dist/effect/rules/no-try-catch.js +30 -0
- package/dist/effect/rules/no-unguarded-json-parse.d.ts +5 -0
- package/dist/effect/rules/no-unguarded-json-parse.js +200 -0
- package/dist/effect/rules/option-core-needs-effect-public.d.ts +5 -0
- package/dist/effect/rules/option-core-needs-effect-public.js +76 -0
- package/dist/effect/rules/prefer-die-for-precondition-defects.d.ts +5 -0
- package/dist/effect/rules/prefer-die-for-precondition-defects.js +112 -0
- package/dist/effect/rules/prefer-effect-gen-for-guard-ladders.d.ts +5 -0
- package/dist/effect/rules/prefer-effect-gen-for-guard-ladders.js +72 -0
- package/dist/effect/rules/prefer-effect-match.d.ts +2 -0
- package/dist/effect/rules/prefer-effect-match.js +74 -0
- package/dist/effect/rules/prefer-effect-predicate-over-boolean-function.d.ts +2 -0
- package/dist/effect/rules/prefer-effect-predicate-over-boolean-function.js +75 -0
- package/dist/effect/rules/prefer-named-guard-predicate.d.ts +2 -0
- package/dist/effect/rules/prefer-named-guard-predicate.js +93 -0
- package/dist/effect/rules/prefer-option-pipeline.d.ts +2 -0
- package/dist/effect/rules/prefer-option-pipeline.js +87 -0
- package/dist/effect/rules/prefer-ordering-match.d.ts +2 -0
- package/dist/effect/rules/prefer-ordering-match.js +135 -0
- package/dist/effect/rules/prefer-tagged-enum.d.ts +2 -0
- package/dist/effect/rules/prefer-tagged-enum.js +66 -0
- package/dist/effect/rules/require-schema-type-derivation.d.ts +7 -0
- package/dist/effect/rules/require-schema-type-derivation.js +80 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +104 -0
- package/dist/meta/index.d.ts +7 -0
- package/{src/meta/index.ts → dist/meta/index.js} +13 -16
- package/dist/meta/rules/no-disable-directives.d.ts +6 -0
- package/dist/meta/rules/no-disable-directives.js +35 -0
- package/dist/meta/rules/no-manual-ancestor-walks.d.ts +2 -0
- package/dist/meta/rules/no-manual-ancestor-walks.js +26 -0
- package/dist/meta/rules/prefer-before-file-scope.d.ts +2 -0
- package/dist/meta/rules/prefer-before-file-scope.js +57 -0
- package/dist/meta/rules/require-create-once.d.ts +2 -0
- package/dist/meta/rules/require-create-once.js +51 -0
- package/dist/meta/rules/require-rule-tester.d.ts +2 -0
- package/dist/meta/rules/require-rule-tester.js +50 -0
- package/dist/rules/no-anonymous-wide-tuples.d.ts +7 -0
- package/dist/rules/no-anonymous-wide-tuples.js +34 -0
- package/dist/rules/no-barrel-export-star.d.ts +2 -0
- package/dist/rules/no-barrel-export-star.js +26 -0
- package/dist/rules/no-boolean-field-signals.d.ts +2 -0
- package/dist/rules/no-boolean-field-signals.js +279 -0
- package/dist/rules/no-builtin-throws.d.ts +6 -0
- package/dist/rules/no-builtin-throws.js +74 -0
- package/dist/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/rules/no-chained-type-assertions.js +54 -0
- package/dist/rules/no-conditional-empty-object-spread.d.ts +2 -0
- package/dist/rules/no-conditional-empty-object-spread.js +36 -0
- package/dist/rules/no-duplicated-literal-union.d.ts +6 -0
- package/dist/rules/no-duplicated-literal-union.js +62 -0
- package/dist/rules/no-exported-mutable-state.d.ts +7 -0
- package/dist/rules/no-exported-mutable-state.js +104 -0
- package/dist/rules/no-generic-export-names.d.ts +2 -0
- package/dist/rules/no-generic-export-names.js +141 -0
- package/dist/rules/no-impossible-branch-throw.d.ts +2 -0
- package/dist/rules/no-impossible-branch-throw.js +51 -0
- package/dist/rules/no-known-value-widening.d.ts +2 -0
- package/dist/rules/no-known-value-widening.js +176 -0
- package/dist/rules/no-module-mocking.d.ts +2 -0
- package/dist/rules/no-module-mocking.js +90 -0
- package/dist/rules/no-multiple-function-params.d.ts +2 -0
- package/dist/rules/no-multiple-function-params.js +155 -0
- package/dist/rules/no-mutable-environment-capture.d.ts +7 -0
- package/dist/rules/no-mutable-environment-capture.js +134 -0
- package/dist/rules/no-nondeterministic-core.d.ts +6 -0
- package/dist/rules/no-nondeterministic-core.js +63 -0
- package/dist/rules/no-object-parameters.d.ts +2 -0
- package/dist/rules/no-object-parameters.js +95 -0
- package/dist/rules/no-optional-function-parameters.d.ts +7 -0
- package/dist/rules/no-optional-function-parameters.js +44 -0
- package/dist/rules/no-product-of-state-booleans.d.ts +2 -0
- package/dist/rules/no-product-of-state-booleans.js +79 -0
- package/dist/rules/no-redundant-derived-field.d.ts +6 -0
- package/dist/rules/no-redundant-derived-field.js +194 -0
- package/dist/rules/no-reflect-apply.d.ts +2 -0
- package/dist/rules/no-reflect-apply.js +29 -0
- package/dist/rules/no-reflect-get.d.ts +2 -0
- package/dist/rules/no-reflect-get.js +29 -0
- package/dist/rules/no-reinterpret-cast.d.ts +2 -0
- package/dist/rules/no-reinterpret-cast.js +68 -0
- package/dist/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/rules/no-runtime-typeof.js +54 -0
- package/dist/rules/no-sentinel-comparison-union.d.ts +2 -0
- package/dist/rules/no-sentinel-comparison-union.js +139 -0
- package/dist/rules/no-shape-in-symbol-names.d.ts +2 -0
- package/dist/rules/no-shape-in-symbol-names.js +39 -0
- package/dist/rules/no-single-use-private-functions.d.ts +17 -0
- package/dist/rules/no-single-use-private-functions.js +419 -0
- package/dist/rules/no-stacked-jsdoc-blocks.d.ts +2 -0
- package/dist/rules/no-stacked-jsdoc-blocks.js +50 -0
- package/dist/rules/no-stray-inline-comments.d.ts +2 -0
- package/dist/rules/no-stray-inline-comments.js +74 -0
- package/dist/rules/no-swappable-primitive-params.d.ts +2 -0
- package/dist/rules/no-swappable-primitive-params.js +96 -0
- package/dist/rules/no-tag-ladder-assertions.d.ts +6 -0
- package/dist/rules/no-tag-ladder-assertions.js +63 -0
- package/dist/rules/no-transposed-field-reads.d.ts +8 -0
- package/dist/rules/no-transposed-field-reads.js +101 -0
- package/dist/rules/no-unit-return-validators.d.ts +2 -0
- package/dist/rules/no-unit-return-validators.js +64 -0
- package/dist/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/rules/no-unknown-parameters.js +82 -0
- package/dist/rules/no-unknown-returns.d.ts +2 -0
- package/dist/rules/no-unknown-returns.js +86 -0
- package/dist/rules/no-unknown-type-aliases.d.ts +2 -0
- package/dist/rules/no-unknown-type-aliases.js +64 -0
- package/dist/rules/no-unsafe-dictionary-type.d.ts +2 -0
- package/dist/rules/no-unsafe-dictionary-type.js +116 -0
- package/dist/rules/no-vague-test-filenames.d.ts +2 -0
- package/dist/rules/no-vague-test-filenames.js +61 -0
- package/dist/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/rules/no-widen-then-assert.js +268 -0
- package/dist/rules/prefer-payload-brand.d.ts +2 -0
- package/dist/rules/prefer-payload-brand.js +57 -0
- package/dist/rules/prefer-property-tests.d.ts +5 -0
- package/dist/rules/prefer-property-tests.js +39 -0
- package/dist/rules/require-canonical-stringify-for-identity.d.ts +8 -0
- package/dist/rules/require-canonical-stringify-for-identity.js +72 -0
- package/dist/rules/require-deprecated-tag-for-legacy-comments.d.ts +2 -0
- package/dist/rules/require-deprecated-tag-for-legacy-comments.js +34 -0
- package/dist/rules/require-exhaustive-tag-switch.d.ts +7 -0
- package/dist/rules/require-exhaustive-tag-switch.js +34 -0
- package/dist/rules/require-fc-block-predicate.d.ts +2 -0
- package/dist/rules/require-fc-block-predicate.js +37 -0
- package/dist/rules/require-jsdoc-on-exported.d.ts +2 -0
- package/dist/rules/require-jsdoc-on-exported.js +66 -0
- package/dist/rules/require-published-order.d.ts +2 -0
- package/dist/rules/require-published-order.js +102 -0
- package/dist/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
- package/dist/rules/require-safety-comment-for-type-assertion.js +53 -0
- package/dist/rules/require-sort-comparator.d.ts +7 -0
- package/dist/rules/require-sort-comparator.js +34 -0
- package/dist/rules/test-utils.d.ts +35 -0
- package/dist/rules/test-utils.js +53 -0
- package/{src/shared/ancestors.ts → dist/shared/ancestors.d.ts} +1 -4
- package/dist/shared/ancestors.js +19 -0
- package/dist/shared/child-nodes.d.ts +12 -0
- package/dist/shared/child-nodes.js +26 -0
- package/dist/shared/dictionary-types.d.ts +55 -0
- package/dist/shared/dictionary-types.js +413 -0
- package/{src/shared/is-node.ts → dist/shared/is-node.d.ts} +2 -6
- package/dist/shared/is-node.js +10 -0
- package/dist/shared/lexical-type-parameters.d.ts +12 -0
- package/dist/shared/lexical-type-parameters.js +50 -0
- package/dist/shared/reflect-method.d.ts +15 -0
- package/dist/shared/reflect-method.js +32 -0
- package/{src/shared/rule-options.ts → dist/shared/rule-options.d.ts} +2 -7
- package/dist/shared/rule-options.js +12 -0
- package/{src/shared/structural.ts → dist/shared/structural.d.ts} +17 -44
- package/dist/shared/structural.js +40 -0
- package/{src/testing/index.ts → dist/testing/index.d.ts} +0 -1
- package/dist/testing/index.js +1 -0
- package/dist/testing/testable-rule.d.ts +38 -0
- package/dist/testing/testable-rule.js +70 -0
- package/package.json +7 -6
- package/src/effect/index.ts +0 -59
- package/src/effect/rules/guarded-op-must-return-effect.ts +0 -236
- package/src/effect/rules/guarded-op-must-return-option.ts +0 -97
- package/src/effect/rules/no-cascading-layer-provide.ts +0 -80
- package/src/effect/rules/no-decode-unknown-option.ts +0 -41
- package/src/effect/rules/no-direct-browser-storage.ts +0 -91
- package/src/effect/rules/no-direct-fetch.ts +0 -44
- package/src/effect/rules/no-manual-field-guards.ts +0 -144
- package/src/effect/rules/no-nested-layer-provide.ts +0 -51
- package/src/effect/rules/no-schema-class-modeling.ts +0 -59
- package/src/effect/rules/no-service-constructor-imports.ts +0 -50
- package/src/effect/rules/no-silent-error-swallow.ts +0 -99
- package/src/effect/rules/no-static-effect-service-forwarders.ts +0 -227
- package/src/effect/rules/no-try-catch.ts +0 -32
- package/src/effect/rules/no-unguarded-json-parse.ts +0 -211
- package/src/effect/rules/option-core-needs-effect-public.ts +0 -81
- package/src/effect/rules/prefer-die-for-precondition-defects.ts +0 -118
- package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +0 -76
- package/src/effect/rules/prefer-effect-match.ts +0 -78
- package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +0 -79
- package/src/effect/rules/prefer-named-guard-predicate.ts +0 -105
- package/src/effect/rules/prefer-option-pipeline.ts +0 -95
- package/src/effect/rules/prefer-ordering-match.ts +0 -152
- package/src/effect/rules/prefer-tagged-enum.ts +0 -68
- package/src/effect/rules/require-schema-type-derivation.ts +0 -83
- package/src/index.ts +0 -106
- package/src/meta/rules/no-disable-directives.ts +0 -38
- package/src/meta/rules/no-manual-ancestor-walks.ts +0 -27
- package/src/meta/rules/prefer-before-file-scope.ts +0 -57
- package/src/meta/rules/require-create-once.ts +0 -51
- package/src/meta/rules/require-rule-tester.ts +0 -57
- package/src/rules/no-anonymous-wide-tuples.ts +0 -38
- package/src/rules/no-barrel-export-star.ts +0 -30
- package/src/rules/no-boolean-field-signals.ts +0 -305
- package/src/rules/no-builtin-throws.ts +0 -83
- package/src/rules/no-chained-type-assertions.ts +0 -65
- package/src/rules/no-conditional-empty-object-spread.ts +0 -42
- package/src/rules/no-duplicated-literal-union.ts +0 -66
- package/src/rules/no-exported-mutable-state.ts +0 -111
- package/src/rules/no-generic-export-names.ts +0 -149
- package/src/rules/no-impossible-branch-throw.ts +0 -55
- package/src/rules/no-known-value-widening.ts +0 -225
- package/src/rules/no-module-mocking.ts +0 -95
- package/src/rules/no-multiple-function-params.ts +0 -192
- package/src/rules/no-nondeterministic-core.ts +0 -73
- package/src/rules/no-object-parameters.ts +0 -127
- package/src/rules/no-optional-function-parameters.ts +0 -49
- package/src/rules/no-product-of-state-booleans.ts +0 -80
- package/src/rules/no-redundant-derived-field.ts +0 -192
- package/src/rules/no-reflect-apply.ts +0 -32
- package/src/rules/no-reflect-get.ts +0 -32
- package/src/rules/no-reinterpret-cast.ts +0 -78
- package/src/rules/no-runtime-typeof.ts +0 -59
- package/src/rules/no-sentinel-comparison-union.ts +0 -148
- package/src/rules/no-shape-in-symbol-names.ts +0 -45
- package/src/rules/no-single-use-private-functions.ts +0 -329
- package/src/rules/no-stacked-jsdoc-blocks.ts +0 -65
- package/src/rules/no-stray-inline-comments.ts +0 -79
- package/src/rules/no-swappable-primitive-params.ts +0 -109
- package/src/rules/no-tag-ladder-assertions.ts +0 -71
- package/src/rules/no-transposed-field-reads.ts +0 -135
- package/src/rules/no-unit-return-validators.ts +0 -65
- package/src/rules/no-unknown-parameters.ts +0 -97
- package/src/rules/no-unknown-returns.ts +0 -120
- package/src/rules/no-unknown-type-aliases.ts +0 -70
- package/src/rules/no-unsafe-dictionary-type.ts +0 -132
- package/src/rules/no-vague-test-filenames.ts +0 -67
- package/src/rules/no-widen-then-assert.ts +0 -345
- package/src/rules/prefer-payload-brand.ts +0 -67
- package/src/rules/prefer-property-tests.ts +0 -43
- package/src/rules/require-canonical-stringify-for-identity.ts +0 -80
- package/src/rules/require-deprecated-tag-for-legacy-comments.ts +0 -45
- package/src/rules/require-exhaustive-tag-switch.ts +0 -42
- package/src/rules/require-fc-block-predicate.ts +0 -42
- package/src/rules/require-jsdoc-on-exported.ts +0 -74
- package/src/rules/require-published-order.ts +0 -109
- package/src/rules/require-safety-comment-for-type-assertion.ts +0 -61
- package/src/rules/require-sort-comparator.ts +0 -41
- package/src/rules/test-utils.ts +0 -68
- package/src/shared/child-nodes.ts +0 -27
- package/src/shared/dictionary-types.ts +0 -524
- package/src/shared/lexical-type-parameters.ts +0 -62
- package/src/shared/reflect-method.ts +0 -41
- package/src/testing/testable-rule.test.ts +0 -68
- package/src/testing/testable-rule.ts +0 -86
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
3
|
+
import { lexicalTypeParameterNames } from "../shared/lexical-type-parameters.js";
|
|
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
|
+
/** Ban the broad object type on function inputs, including local aliases to object. */
|
|
17
|
+
export const noObjectParametersRule = defineRule({
|
|
18
|
+
meta: {
|
|
19
|
+
type: "problem",
|
|
20
|
+
docs: {
|
|
21
|
+
description: "Disallow object function parameters; inputs must use an owner-provided type and be parsed at their boundary.",
|
|
22
|
+
},
|
|
23
|
+
messages: {
|
|
24
|
+
objectParameter: "Parameter `{{parameter}}` uses the broad `object` type. Accept a named owner type; parse external input at its boundary before calling this function.",
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
createOnce(context) {
|
|
28
|
+
const aliases = new Map();
|
|
29
|
+
const resolvesToObject = (type, shadowedAliases, visited = new Set()) => {
|
|
30
|
+
if (type.type === "TSObjectKeyword")
|
|
31
|
+
return true;
|
|
32
|
+
if (type.type === "TSParenthesizedType")
|
|
33
|
+
return resolvesToObject(type.typeAnnotation, shadowedAliases, visited);
|
|
34
|
+
if (type.type === "TSUnionType") {
|
|
35
|
+
return type.types.some((member) => resolvesToObject(member, shadowedAliases, visited));
|
|
36
|
+
}
|
|
37
|
+
if (type.type !== "TSTypeReference" ||
|
|
38
|
+
type.typeName.type !== "Identifier" ||
|
|
39
|
+
(type.typeArguments !== null &&
|
|
40
|
+
type.typeArguments !== undefined &&
|
|
41
|
+
type.typeArguments.params.length > 0) ||
|
|
42
|
+
visited.has(type.typeName.name) ||
|
|
43
|
+
shadowedAliases.has(type.typeName.name)) {
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
const alias = aliases.get(type.typeName.name);
|
|
47
|
+
if (alias === undefined)
|
|
48
|
+
return false;
|
|
49
|
+
const nextVisited = new Set(visited);
|
|
50
|
+
nextVisited.add(type.typeName.name);
|
|
51
|
+
return resolvesToObject(alias, shadowedAliases, nextVisited);
|
|
52
|
+
};
|
|
53
|
+
const checkParameters = (node) => {
|
|
54
|
+
const shadowedAliases = lexicalTypeParameterNames(node, ancestorsOf(context.sourceCode, node), context.sourceCode.visitorKeys);
|
|
55
|
+
for (const parameter of node.params) {
|
|
56
|
+
const annotation = parameterAnnotation(parameter);
|
|
57
|
+
if (annotation === null || annotation === undefined)
|
|
58
|
+
continue;
|
|
59
|
+
if (!resolvesToObject(annotation.typeAnnotation, shadowedAliases))
|
|
60
|
+
continue;
|
|
61
|
+
context.report({
|
|
62
|
+
node: annotation.typeAnnotation,
|
|
63
|
+
messageId: "objectParameter",
|
|
64
|
+
data: {
|
|
65
|
+
parameter: parameter.type === "Identifier"
|
|
66
|
+
? parameter.name
|
|
67
|
+
: context.sourceCode.getText(parameter).replace(/\s*:\s*object\s*$/u, ""),
|
|
68
|
+
},
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
return {
|
|
73
|
+
Program(node) {
|
|
74
|
+
aliases.clear();
|
|
75
|
+
for (const statement of node.body) {
|
|
76
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
77
|
+
if (declaration?.type === "TSTypeAliasDeclaration" &&
|
|
78
|
+
(declaration.typeParameters === null || declaration.typeParameters === undefined)) {
|
|
79
|
+
aliases.set(declaration.id.name, declaration.typeAnnotation);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
ArrowFunctionExpression: checkParameters,
|
|
84
|
+
FunctionDeclaration: checkParameters,
|
|
85
|
+
FunctionExpression: checkParameters,
|
|
86
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
87
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
88
|
+
TSConstructorType: checkParameters,
|
|
89
|
+
TSDeclareFunction: checkParameters,
|
|
90
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
91
|
+
TSFunctionType: checkParameters,
|
|
92
|
+
TSMethodSignature: checkParameters,
|
|
93
|
+
};
|
|
94
|
+
},
|
|
95
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Optional parameters hide undefined from the signature; require the union spelled out.
|
|
3
|
+
*
|
|
4
|
+
* Optionality is a grammar-level flag present on every concrete ParamPattern
|
|
5
|
+
* shape, read after unwrapping TypeScript parameter-property wrappers.
|
|
6
|
+
*/
|
|
7
|
+
export declare const noOptionalFunctionParametersRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
const TEST_FILE = /.(?:test|spec).[cm]?[jt]sx?$/u;
|
|
3
|
+
/**
|
|
4
|
+
* Optional parameters hide undefined from the signature; require the union spelled out.
|
|
5
|
+
*
|
|
6
|
+
* Optionality is a grammar-level flag present on every concrete ParamPattern
|
|
7
|
+
* shape, read after unwrapping TypeScript parameter-property wrappers.
|
|
8
|
+
*/
|
|
9
|
+
export const noOptionalFunctionParametersRule = defineRule({
|
|
10
|
+
meta: {
|
|
11
|
+
type: "problem",
|
|
12
|
+
docs: {
|
|
13
|
+
description: "Disallow optional function parameters (`param?:`); require an explicit union with undefined so callers see the possibility in the type.",
|
|
14
|
+
},
|
|
15
|
+
messages: {
|
|
16
|
+
optionalParameter: "Optional parameters hide `undefined` from callers. Annotate the parameter as `T | undefined` so the possibility is spelled out in the type.",
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
createOnce(context) {
|
|
20
|
+
const check = (node) => {
|
|
21
|
+
if (node.type !== "FunctionDeclaration" &&
|
|
22
|
+
node.type !== "FunctionExpression" &&
|
|
23
|
+
node.type !== "ArrowFunctionExpression")
|
|
24
|
+
return;
|
|
25
|
+
for (const parameter of node.params) {
|
|
26
|
+
let pattern = parameter;
|
|
27
|
+
if (pattern.type === "TSParameterProperty")
|
|
28
|
+
pattern = pattern.parameter;
|
|
29
|
+
if (pattern.optional !== true)
|
|
30
|
+
continue;
|
|
31
|
+
context.report({ node: parameter, messageId: "optionalParameter" });
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
before() {
|
|
36
|
+
if (TEST_FILE.test(context.filename.replaceAll("\\", "/")))
|
|
37
|
+
return false;
|
|
38
|
+
},
|
|
39
|
+
FunctionDeclaration: check,
|
|
40
|
+
FunctionExpression: check,
|
|
41
|
+
ArrowFunctionExpression: check,
|
|
42
|
+
};
|
|
43
|
+
},
|
|
44
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { readField } from "../shared/structural.js";
|
|
3
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
4
|
+
function lastTypeNameSegment(typeName) {
|
|
5
|
+
if (typeName.type === "Identifier")
|
|
6
|
+
return typeName.name;
|
|
7
|
+
if (typeName.type === "TSQualifiedName")
|
|
8
|
+
return lastTypeNameSegment(typeName.right);
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Returns the enclosing TSPropertySignature when the literal is a variant
|
|
13
|
+
* payload of a Data.TaggedEnum (i.e. `{ Variant: Payload }` inside the enum's
|
|
14
|
+
* type argument), otherwise null.
|
|
15
|
+
*
|
|
16
|
+
* @param {ESTree.TSTypeLiteral} node - The visited type literal (candidate payload).
|
|
17
|
+
* @param {ReadonlyArray<ESTree.Node>} ancestors - Nearest-first ancestor chain of `node`.
|
|
18
|
+
* @returns {ESTree.TSPropertySignature | null} The variant property signature, or null.
|
|
19
|
+
*/
|
|
20
|
+
function variantPayloadSignature(node, ancestors) {
|
|
21
|
+
const at = (depth) => ancestors[ancestors.length - 1 - depth];
|
|
22
|
+
const annotation = at(0);
|
|
23
|
+
if (annotation?.type !== "TSTypeAnnotation")
|
|
24
|
+
return null;
|
|
25
|
+
const signature = at(1);
|
|
26
|
+
if (signature?.type !== "TSPropertySignature")
|
|
27
|
+
return null;
|
|
28
|
+
const outer = at(2);
|
|
29
|
+
if (outer?.type !== "TSTypeLiteral")
|
|
30
|
+
return null;
|
|
31
|
+
const args = at(3);
|
|
32
|
+
if (args === undefined)
|
|
33
|
+
return null;
|
|
34
|
+
const reference = at(4);
|
|
35
|
+
if (reference?.type !== "TSTypeReference")
|
|
36
|
+
return null;
|
|
37
|
+
const name = lastTypeNameSegment(reference.typeName);
|
|
38
|
+
return name !== null && /TaggedEnum$/u.test(name) ? signature : null;
|
|
39
|
+
}
|
|
40
|
+
/** A product of booleans inside a variant tends to admit an illegal combination. */
|
|
41
|
+
export const noProductOfStateBooleansRule = defineRule({
|
|
42
|
+
meta: {
|
|
43
|
+
type: "problem",
|
|
44
|
+
docs: {
|
|
45
|
+
description: "Disallow tagged-enum variant payloads with two or more boolean state flags; replace them with a nested tag union so impossible combinations can't be represented.",
|
|
46
|
+
},
|
|
47
|
+
messages: {
|
|
48
|
+
productOfStateBooleans: "Variant payload `{{name}}` carries {{count}} boolean state flags. Replace them with a nested tag union (e.g. Sign) so combinations like {zero, negative} can't be represented.",
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
createOnce(context) {
|
|
52
|
+
return {
|
|
53
|
+
TSTypeLiteral(node) {
|
|
54
|
+
const signature = variantPayloadSignature(node, ancestorsOf(context.sourceCode, node));
|
|
55
|
+
if (signature === null)
|
|
56
|
+
return;
|
|
57
|
+
let count = 0;
|
|
58
|
+
for (const member of node.members) {
|
|
59
|
+
if (member.type !== "TSPropertySignature")
|
|
60
|
+
continue;
|
|
61
|
+
const annotation = member.typeAnnotation;
|
|
62
|
+
if (annotation === null || annotation === undefined)
|
|
63
|
+
continue;
|
|
64
|
+
if (annotation.typeAnnotation.type === "TSBooleanKeyword")
|
|
65
|
+
count += 1;
|
|
66
|
+
}
|
|
67
|
+
if (count < 2)
|
|
68
|
+
return;
|
|
69
|
+
const key = signature.key;
|
|
70
|
+
const name = key.type === "Identifier" ? key.name : "variant";
|
|
71
|
+
context.report({
|
|
72
|
+
node,
|
|
73
|
+
messageId: "productOfStateBooleans",
|
|
74
|
+
data: { name, count: String(count) },
|
|
75
|
+
});
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
},
|
|
79
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One fact, one place: a type that stores data *and* a field derivable from
|
|
3
|
+
* that data keeps two sources of truth that drift apart independently. Counts
|
|
4
|
+
* belong to read sites; ordering belongs in the structure itself.
|
|
5
|
+
*/
|
|
6
|
+
export declare const noRedundantDerivedFieldRule: import("@oxlint/plugins").Rule;
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { isRecordObject, isString } from "../shared/structural.js";
|
|
3
|
+
const COUNT_SUFFIX = /(?:Count|Length|Size)$/u;
|
|
4
|
+
const COUNT_PREFIX = /^(?:count|num|numberOf|total)(?=[A-Z])/u;
|
|
5
|
+
const KEYED_COLLECTION_NAMES = new Set(["Array", "Map", "Record", "ReadonlyArray", "ReadonlyMap", "ReadonlySet", "Set"]);
|
|
6
|
+
function lastTypeNameSegment(typeName) {
|
|
7
|
+
if (typeName.type === "Identifier")
|
|
8
|
+
return typeName.name;
|
|
9
|
+
if (typeName.type === "TSQualifiedName")
|
|
10
|
+
return lastTypeNameSegment(typeName.right);
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
function propertyName(member) {
|
|
14
|
+
if (member.type !== "TSPropertySignature")
|
|
15
|
+
return null;
|
|
16
|
+
const key = member.key;
|
|
17
|
+
return key.type === "Identifier" ? key.name : null;
|
|
18
|
+
}
|
|
19
|
+
function propertyAnnotation(member) {
|
|
20
|
+
if (member.type !== "TSPropertySignature")
|
|
21
|
+
return null;
|
|
22
|
+
return member.typeAnnotation?.typeAnnotation ?? null;
|
|
23
|
+
}
|
|
24
|
+
/** Lowercase, strip separators, and naive-singularise so `seatOrder`/`seats`, `entryIndex`/`entries` stems can be compared.
|
|
25
|
+
*
|
|
26
|
+
* @param {string} name - The field or property name to fold.
|
|
27
|
+
* @returns {string} The comparison stem.
|
|
28
|
+
*/
|
|
29
|
+
function normaliseName(name) {
|
|
30
|
+
const folded = name.replaceAll(/[^a-zA-Z0-9]/gu, "").toLowerCase();
|
|
31
|
+
if (folded.endsWith("ies") && folded.length > 4)
|
|
32
|
+
return `${folded.slice(0, -3)}y`;
|
|
33
|
+
return folded.endsWith("s") && folded.length > 3 ? folded.slice(0, -1) : folded;
|
|
34
|
+
}
|
|
35
|
+
/** Extract the `x` from `xCount`, `countX`, `numberOfX`, … otherwise null.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} name - The candidate count-field name.
|
|
38
|
+
* @returns {string | null} The counted stem, or null when the name isn't a count pattern.
|
|
39
|
+
*/
|
|
40
|
+
function countStem(name) {
|
|
41
|
+
const suffixMatch = name.match(COUNT_SUFFIX);
|
|
42
|
+
if (suffixMatch !== null && suffixMatch.index !== null)
|
|
43
|
+
return name.slice(0, suffixMatch.index);
|
|
44
|
+
const prefixMatch = name.match(COUNT_PREFIX);
|
|
45
|
+
if (prefixMatch !== null) {
|
|
46
|
+
const stem = name.slice(prefixMatch[0].length);
|
|
47
|
+
return stem.charAt(0).toLowerCase() + stem.slice(1);
|
|
48
|
+
}
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
/** Whether the annotation is any array/map/set/record shape.
|
|
52
|
+
*
|
|
53
|
+
* @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
|
|
54
|
+
* @returns {boolean} True when the annotation denotes a keyed collection.
|
|
55
|
+
*/
|
|
56
|
+
function isCollectionLike(annotation) {
|
|
57
|
+
if (annotation === null || annotation === undefined)
|
|
58
|
+
return false;
|
|
59
|
+
if (annotation.type === "TSTypeReference") {
|
|
60
|
+
const name = lastTypeNameSegment(annotation.typeName);
|
|
61
|
+
return name !== null && KEYED_COLLECTION_NAMES.has(name);
|
|
62
|
+
}
|
|
63
|
+
return annotation.type === "TSTypeOperator" && annotation.operator === "readonly"
|
|
64
|
+
? isCollectionLike(annotation.typeAnnotation)
|
|
65
|
+
: annotation.type === "TSArrayType";
|
|
66
|
+
}
|
|
67
|
+
/** Whether the annotation is an array (or readonly array reference) whose element is a primitive keyword.
|
|
68
|
+
*
|
|
69
|
+
* @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
|
|
70
|
+
* @returns {"string" | "number" | null} The primitive keyword of the element type, or null.
|
|
71
|
+
*/
|
|
72
|
+
function arrayOfPrimitiveKeys(annotation) {
|
|
73
|
+
if (annotation === null || annotation === undefined)
|
|
74
|
+
return null;
|
|
75
|
+
let elementType;
|
|
76
|
+
if (annotation.type === "TSArrayType") {
|
|
77
|
+
elementType = annotation.elementType;
|
|
78
|
+
}
|
|
79
|
+
else if (annotation.type === "TSTypeOperator" &&
|
|
80
|
+
annotation.operator === "readonly" &&
|
|
81
|
+
annotation.typeAnnotation.type === "TSArrayType") {
|
|
82
|
+
elementType = annotation.typeAnnotation.elementType;
|
|
83
|
+
}
|
|
84
|
+
else if (annotation.type === "TSTypeReference" &&
|
|
85
|
+
annotation.typeArguments != null &&
|
|
86
|
+
annotation.typeArguments.params.length === 1) {
|
|
87
|
+
const name = lastTypeNameSegment(annotation.typeName);
|
|
88
|
+
if (name === "Array" || name === "ReadonlyArray")
|
|
89
|
+
elementType = annotation.typeArguments.params[0];
|
|
90
|
+
}
|
|
91
|
+
if (elementType === undefined)
|
|
92
|
+
return null;
|
|
93
|
+
if (elementType.type === "TSStringKeyword")
|
|
94
|
+
return "string";
|
|
95
|
+
if (elementType.type === "TSNumberKeyword")
|
|
96
|
+
return "number";
|
|
97
|
+
return null;
|
|
98
|
+
}
|
|
99
|
+
/** First type argument's primitive keyword for a keyed collection like `Record<string, T>` / `Map<string, T>`.
|
|
100
|
+
*
|
|
101
|
+
* @param {ESTree.TSType | null | undefined} annotation - The annotation to inspect.
|
|
102
|
+
* @returns {"string" | "number" | null} The key keyword, or null when not a keyed reference.
|
|
103
|
+
*/
|
|
104
|
+
function keyKeywordOf(annotation) {
|
|
105
|
+
if (annotation?.type !== "TSTypeReference")
|
|
106
|
+
return null;
|
|
107
|
+
const first = annotation.typeArguments?.params[0] ?? null;
|
|
108
|
+
if (first === null || first === undefined)
|
|
109
|
+
return null;
|
|
110
|
+
if (first.type === "TSStringKeyword")
|
|
111
|
+
return "string";
|
|
112
|
+
if (first.type === "TSNumberKeyword")
|
|
113
|
+
return "number";
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* One fact, one place: a type that stores data *and* a field derivable from
|
|
118
|
+
* that data keeps two sources of truth that drift apart independently. Counts
|
|
119
|
+
* belong to read sites; ordering belongs in the structure itself.
|
|
120
|
+
*/
|
|
121
|
+
export const noRedundantDerivedFieldRule = defineRule({
|
|
122
|
+
meta: {
|
|
123
|
+
type: "suggestion",
|
|
124
|
+
docs: {
|
|
125
|
+
description: "Disallow fields that duplicate information derivable from a sibling field on the same type (derived counts, parallel key/order arrays); compute or encode them instead.",
|
|
126
|
+
},
|
|
127
|
+
messages: {
|
|
128
|
+
redundantCount: "`{{name}}` restates what is derivable from `{{sibling}}`, giving the type two sources of truth that can drift. Drop it and compute the count at read time.",
|
|
129
|
+
parallelKeyArray: "`{{name}}` mirrors the keys of `{{sibling}}` as a second source of truth that can drift. Encode the canonical form (e.g. ordering) in the collection itself instead of a parallel array.",
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
createOnce(context) {
|
|
133
|
+
function checkMembers(members) {
|
|
134
|
+
for (const member of members) {
|
|
135
|
+
const name = propertyName(member);
|
|
136
|
+
if (name === null)
|
|
137
|
+
continue;
|
|
138
|
+
const annotation = propertyAnnotation(member);
|
|
139
|
+
const stem = countStem(name);
|
|
140
|
+
if (stem !== null && annotation?.type === "TSNumberKeyword") {
|
|
141
|
+
const stemNorm = normaliseName(stem);
|
|
142
|
+
const sibling = members.find((other) => {
|
|
143
|
+
if (other === member)
|
|
144
|
+
return false;
|
|
145
|
+
const otherName = propertyName(other);
|
|
146
|
+
return otherName !== null && normaliseName(otherName) === stemNorm && isCollectionLike(propertyAnnotation(other));
|
|
147
|
+
});
|
|
148
|
+
if (sibling !== undefined) {
|
|
149
|
+
context.report({
|
|
150
|
+
node: member,
|
|
151
|
+
messageId: "redundantCount",
|
|
152
|
+
data: { name, sibling: propertyName(sibling) ?? "" },
|
|
153
|
+
});
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
const elementKey = arrayOfPrimitiveKeys(annotation);
|
|
158
|
+
if (elementKey === null)
|
|
159
|
+
continue;
|
|
160
|
+
const nameNorm = normaliseName(name);
|
|
161
|
+
const sibling = members.find((other) => {
|
|
162
|
+
if (other === member)
|
|
163
|
+
return false;
|
|
164
|
+
const otherName = propertyName(other);
|
|
165
|
+
if (otherName === null)
|
|
166
|
+
return false;
|
|
167
|
+
if (keyKeywordOf(propertyAnnotation(other)) !== elementKey)
|
|
168
|
+
return false;
|
|
169
|
+
const otherNorm = normaliseName(otherName);
|
|
170
|
+
return otherNorm.includes(nameNorm) || nameNorm.includes(otherNorm);
|
|
171
|
+
});
|
|
172
|
+
if (sibling !== undefined) {
|
|
173
|
+
context.report({
|
|
174
|
+
node: member,
|
|
175
|
+
messageId: "parallelKeyArray",
|
|
176
|
+
data: { name, sibling: propertyName(sibling) ?? "" },
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
function memberList(bodyNode) {
|
|
182
|
+
return [...bodyNode.body];
|
|
183
|
+
}
|
|
184
|
+
return {
|
|
185
|
+
TSInterfaceDeclaration(node) {
|
|
186
|
+
checkMembers(memberList(node.body));
|
|
187
|
+
},
|
|
188
|
+
TSTypeAliasDeclaration(node) {
|
|
189
|
+
if (node.typeAnnotation.type === "TSTypeLiteral")
|
|
190
|
+
checkMembers(node.typeAnnotation.members);
|
|
191
|
+
},
|
|
192
|
+
};
|
|
193
|
+
},
|
|
194
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { isGlobalReflectMethodCall } from "../shared/reflect-method.js";
|
|
3
|
+
/** Ban Reflect.apply, which bypasses ordinary typed function calls. */
|
|
4
|
+
export const noReflectApplyRule = defineRule({
|
|
5
|
+
meta: {
|
|
6
|
+
type: "problem",
|
|
7
|
+
docs: {
|
|
8
|
+
description: "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface.",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
reflectApply: "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
createOnce(context) {
|
|
15
|
+
return {
|
|
16
|
+
CallExpression(node) {
|
|
17
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
18
|
+
return;
|
|
19
|
+
if (isGlobalReflectMethodCall({
|
|
20
|
+
sourceCode: context.sourceCode,
|
|
21
|
+
callee: node.callee,
|
|
22
|
+
methodName: "apply",
|
|
23
|
+
})) {
|
|
24
|
+
context.report({ node, messageId: "reflectApply" });
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { isGlobalReflectMethodCall } from "../shared/reflect-method.js";
|
|
3
|
+
/** Ban Reflect.get, which bypasses ordinary property access and useful type evidence. */
|
|
4
|
+
export const noReflectGetRule = defineRule({
|
|
5
|
+
meta: {
|
|
6
|
+
type: "problem",
|
|
7
|
+
docs: {
|
|
8
|
+
description: "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type.",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
reflectGet: "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
createOnce(context) {
|
|
15
|
+
return {
|
|
16
|
+
CallExpression(node) {
|
|
17
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
18
|
+
return;
|
|
19
|
+
if (isGlobalReflectMethodCall({
|
|
20
|
+
sourceCode: context.sourceCode,
|
|
21
|
+
callee: node.callee,
|
|
22
|
+
methodName: "get",
|
|
23
|
+
})) {
|
|
24
|
+
context.report({ node, messageId: "reflectGet" });
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
},
|
|
29
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
/** Whether a function-like body consists solely of returning an as-cast.
|
|
3
|
+
*
|
|
4
|
+
* @param {ESTree.Node | null | undefined} body - The function body to inspect.
|
|
5
|
+
* @returns {boolean} True when the body only returns an as-cast expression.
|
|
6
|
+
*/
|
|
7
|
+
function isSoleReturnOfAssertion(body) {
|
|
8
|
+
if (body === null || body === undefined)
|
|
9
|
+
return false;
|
|
10
|
+
if (body.type === "BlockStatement") {
|
|
11
|
+
if (body.body.length !== 1)
|
|
12
|
+
return false;
|
|
13
|
+
const only = body.body[0];
|
|
14
|
+
return only?.type === "ReturnStatement" && only.argument?.type === "TSAsExpression";
|
|
15
|
+
}
|
|
16
|
+
return body.type === "TSAsExpression";
|
|
17
|
+
}
|
|
18
|
+
/** Whether the assertion node has an adjacent `// SAFETY:` justification.
|
|
19
|
+
*
|
|
20
|
+
* @param {{ getCommentsBefore(n: ESTree.Node): Array<CommentLike> }} sourceCode - The rule's source-code accessor.
|
|
21
|
+
* @param {ESTree.Node} node - The assertion node.
|
|
22
|
+
* @returns {boolean} True when a SAFETY comment precedes the assertion.
|
|
23
|
+
*/
|
|
24
|
+
function hasAdjacentSafetyComment(sourceCode, node) {
|
|
25
|
+
return sourceCode.getCommentsBefore(node).some((comment) => /\bSAFETY\s*:/u.test(comment.value));
|
|
26
|
+
}
|
|
27
|
+
/** Type assertions erase evidence; `as const` is exempt and `// SAFETY:` documents rare verified invariants. */
|
|
28
|
+
export const noReinterpretCastRule = defineRule({
|
|
29
|
+
meta: {
|
|
30
|
+
type: "problem",
|
|
31
|
+
docs: {
|
|
32
|
+
description: "Disallow type reinterpretation via `as`; absence must be modeled by the type system, with SAFETY-commented exceptions.",
|
|
33
|
+
},
|
|
34
|
+
messages: {
|
|
35
|
+
noReinterpret: "Type assertion discards type evidence. Narrow with discriminants/`in` checks or a type predicate instead.",
|
|
36
|
+
noLaundering: "`{{name}}` exists only to perform a type assertion: a laundering wrapper that hides the cast from review and tooling.",
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
createOnce(context) {
|
|
40
|
+
return {
|
|
41
|
+
TSAsExpression(node) {
|
|
42
|
+
const annotation = node.typeAnnotation;
|
|
43
|
+
if (annotation.type === "TSTypeReference" &&
|
|
44
|
+
annotation.typeName.type === "Identifier" &&
|
|
45
|
+
annotation.typeName.name === "const")
|
|
46
|
+
return;
|
|
47
|
+
if (hasAdjacentSafetyComment(context.sourceCode, node))
|
|
48
|
+
return;
|
|
49
|
+
context.report({ node, messageId: "noReinterpret" });
|
|
50
|
+
},
|
|
51
|
+
TSTypeAssertion(node) {
|
|
52
|
+
if (hasAdjacentSafetyComment(context.sourceCode, node))
|
|
53
|
+
return;
|
|
54
|
+
context.report({ node, messageId: "noReinterpret" });
|
|
55
|
+
},
|
|
56
|
+
FunctionDeclaration(node) {
|
|
57
|
+
if (node.body !== null && isSoleReturnOfAssertion(node.body)) {
|
|
58
|
+
context.report({ node, messageId: "noLaundering", data: { name: node.id?.name ?? "<anonymous>" } });
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
ArrowFunctionExpression(node) {
|
|
62
|
+
if (node.body.type === "TSAsExpression") {
|
|
63
|
+
context.report({ node, messageId: "noLaundering", data: { name: "<arrow>" } });
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { readField } from "../shared/structural.js";
|
|
3
|
+
import { ancestorsOf } from "../shared/ancestors.js";
|
|
4
|
+
import { firstOptionRecord } from "../shared/rule-options.js";
|
|
5
|
+
/** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
|
|
6
|
+
export const noRuntimeTypeofRule = defineRule({
|
|
7
|
+
meta: {
|
|
8
|
+
type: "problem",
|
|
9
|
+
docs: {
|
|
10
|
+
description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.",
|
|
11
|
+
},
|
|
12
|
+
messages: {
|
|
13
|
+
runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
|
|
14
|
+
},
|
|
15
|
+
schema: [
|
|
16
|
+
{
|
|
17
|
+
type: "object",
|
|
18
|
+
properties: {
|
|
19
|
+
allowInTypeGuards: { type: "boolean" },
|
|
20
|
+
},
|
|
21
|
+
additionalProperties: false,
|
|
22
|
+
},
|
|
23
|
+
],
|
|
24
|
+
defaultOptions: [{ allowInTypeGuards: false }],
|
|
25
|
+
},
|
|
26
|
+
createOnce(context) {
|
|
27
|
+
return {
|
|
28
|
+
UnaryExpression(node) {
|
|
29
|
+
if (node.operator !== "typeof")
|
|
30
|
+
return;
|
|
31
|
+
const option = firstOptionRecord(context.options);
|
|
32
|
+
const allowInTypeGuards = option.allowInTypeGuards === true;
|
|
33
|
+
let insideTypeGuard = false;
|
|
34
|
+
if (allowInTypeGuards) {
|
|
35
|
+
const ancestors = ancestorsOf(context.sourceCode, node);
|
|
36
|
+
for (let index = ancestors.length - 1; index >= 0; index--) {
|
|
37
|
+
const current = ancestors[index];
|
|
38
|
+
if (current.type === "Program")
|
|
39
|
+
break;
|
|
40
|
+
if (current.type === "ArrowFunctionExpression" ||
|
|
41
|
+
current.type === "FunctionDeclaration" ||
|
|
42
|
+
current.type === "FunctionExpression") {
|
|
43
|
+
insideTypeGuard =
|
|
44
|
+
current.returnType?.typeAnnotation.type === "TSTypePredicate";
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (!insideTypeGuard)
|
|
50
|
+
context.report({ node, messageId: "runtimeTypeof" });
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
},
|
|
54
|
+
});
|