@rc312/oxlint-rules 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +43 -2
- package/dist/anti-slop/effect/index.d.ts +3 -0
- package/dist/anti-slop/effect/index.js +18 -0
- package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.js +39 -0
- package/dist/anti-slop/effect/rules/no-manual-tag-comparison.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-tag-comparison.js +32 -0
- package/dist/anti-slop/effect/rules/no-manual-tagged-construction.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-tagged-construction.js +27 -0
- package/dist/anti-slop/effect/rules/no-service-constructor-imports.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-service-constructor-imports.js +44 -0
- package/dist/anti-slop/effect/rules/prefer-effect-match.d.ts +1 -0
- package/dist/anti-slop/effect/rules/prefer-effect-match.js +48 -0
- package/dist/anti-slop/effect/shared/tagged-values.d.ts +8 -0
- package/dist/anti-slop/effect/shared/tagged-values.js +68 -0
- package/dist/anti-slop/index.d.ts +3 -0
- package/dist/anti-slop/index.js +44 -0
- package/dist/anti-slop/rules/no-array-filter-map.d.ts +2 -0
- package/dist/anti-slop/rules/no-array-filter-map.js +30 -0
- package/dist/anti-slop/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/anti-slop/rules/no-chained-type-assertions.js +60 -0
- package/dist/anti-slop/rules/no-conditional-empty-object-spread.d.ts +2 -0
- package/dist/anti-slop/rules/no-conditional-empty-object-spread.js +40 -0
- package/dist/anti-slop/rules/no-known-value-widening.d.ts +2 -0
- package/dist/anti-slop/rules/no-known-value-widening.js +300 -0
- package/dist/anti-slop/rules/no-module-mocking.d.ts +2 -0
- package/dist/anti-slop/rules/no-module-mocking.js +69 -0
- package/dist/anti-slop/rules/no-object-parameters.d.ts +2 -0
- package/dist/anti-slop/rules/no-object-parameters.js +56 -0
- package/dist/anti-slop/rules/no-reduce-accumulator-copy.d.ts +2 -0
- package/dist/anti-slop/rules/no-reduce-accumulator-copy.js +99 -0
- package/dist/anti-slop/rules/no-reflect-apply.d.ts +2 -0
- package/dist/anti-slop/rules/no-reflect-apply.js +25 -0
- package/dist/anti-slop/rules/no-reflect-get.d.ts +2 -0
- package/dist/anti-slop/rules/no-reflect-get.js +25 -0
- package/dist/anti-slop/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/anti-slop/rules/no-runtime-typeof.js +64 -0
- package/dist/anti-slop/rules/no-shape-in-symbol-names.d.ts +2 -0
- package/dist/anti-slop/rules/no-shape-in-symbol-names.js +40 -0
- package/dist/anti-slop/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-parameters.js +51 -0
- package/dist/anti-slop/rules/no-unknown-returns.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-returns.js +58 -0
- package/dist/anti-slop/rules/no-unknown-type-aliases.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-type-aliases.js +40 -0
- package/dist/anti-slop/rules/no-unsafe-dictionary-type.d.ts +2 -0
- package/dist/anti-slop/rules/no-unsafe-dictionary-type.js +134 -0
- package/dist/anti-slop/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/anti-slop/rules/no-widen-then-assert.js +270 -0
- package/dist/anti-slop/rules/require-readable-spacing.d.ts +3 -0
- package/dist/anti-slop/rules/require-readable-spacing.js +65 -0
- package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
- package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.js +100 -0
- package/dist/anti-slop/shared/array-method.d.ts +12 -0
- package/dist/anti-slop/shared/array-method.js +84 -0
- package/dist/anti-slop/shared/dictionary-types.d.ts +20 -0
- package/dist/anti-slop/shared/dictionary-types.js +377 -0
- package/dist/anti-slop/shared/function-parameters.d.ts +8 -0
- package/dist/anti-slop/shared/function-parameters.js +40 -0
- package/dist/anti-slop/shared/lexical-type-parameters.d.ts +5 -0
- package/dist/anti-slop/shared/lexical-type-parameters.js +47 -0
- package/dist/anti-slop/shared/reflect-method.d.ts +3 -0
- package/dist/anti-slop/shared/reflect-method.js +20 -0
- package/dist/anti-slop/shared/scope.d.ts +3 -0
- package/dist/anti-slop/shared/scope.js +11 -0
- package/dist/anti-slop/shared/type-alias-resolution.d.ts +23 -0
- package/dist/anti-slop/shared/type-alias-resolution.js +162 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.d.ts +27 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.js +28 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.d.ts +37 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.js +703 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-options.d.ts +16 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-options.js +2 -0
- package/dist/configs/base.d.ts +28 -0
- package/dist/configs/base.js +27 -0
- package/dist/configs/effect-starter.d.ts +10 -0
- package/dist/configs/effect-starter.js +10 -0
- package/dist/configs/effect.d.ts +14 -0
- package/dist/configs/effect.js +16 -0
- package/dist/effect-starter.d.ts +3 -0
- package/dist/effect-starter.js +40 -0
- package/package.json +46 -4
- package/src/anti-slop/LICENSE +21 -0
- package/src/anti-slop/UPSTREAM.md +14 -0
- package/src/anti-slop/vendor/eslint-stylistic/LICENSE +22 -0
- package/src/anti-slop/vendor/eslint-stylistic/UPSTREAM.md +28 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
const FORBIDDEN_SYMBOL_NAME = "shape";
|
|
3
|
+
function containsForbiddenSymbolName(name) {
|
|
4
|
+
return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
|
|
5
|
+
}
|
|
6
|
+
/** Return whether an identifier names a statically accessed member owned by another value. */
|
|
7
|
+
function isBorrowedMemberName(node) {
|
|
8
|
+
const parent = node.parent;
|
|
9
|
+
if (parent === null || parent.type !== "MemberExpression")
|
|
10
|
+
return false;
|
|
11
|
+
return parent.property === node && parent.computed === false;
|
|
12
|
+
}
|
|
13
|
+
/** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
|
|
14
|
+
export const noForbiddenTermInSymbolNamesRule = defineRule({
|
|
15
|
+
meta: {
|
|
16
|
+
type: "problem",
|
|
17
|
+
docs: {
|
|
18
|
+
description: 'Disallow the case-insensitive substring "shape" in JavaScript, TypeScript, private, and JSX symbol names.',
|
|
19
|
+
},
|
|
20
|
+
messages: {
|
|
21
|
+
forbiddenSymbolName: 'Rename symbol "{{name}}" for its domain role; "shape" describes structure rather than ownership.',
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
createOnce(context) {
|
|
25
|
+
const reportForbiddenSymbolName = (node) => {
|
|
26
|
+
if (!containsForbiddenSymbolName(node.name) || isBorrowedMemberName(node))
|
|
27
|
+
return;
|
|
28
|
+
context.report({
|
|
29
|
+
node,
|
|
30
|
+
messageId: "forbiddenSymbolName",
|
|
31
|
+
data: { name: node.name },
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
Identifier: reportForbiddenSymbolName,
|
|
36
|
+
PrivateIdentifier: reportForbiddenSymbolName,
|
|
37
|
+
JSXIdentifier: reportForbiddenSymbolName,
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
});
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { containsUnknownType, functionParameterBindingName, functionParameterTypeAnnotation, } from "../shared/function-parameters.js";
|
|
3
|
+
function isTypePredicateSubject(owner, parameterName) {
|
|
4
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
5
|
+
return (predicate?.type === "TSTypePredicate" &&
|
|
6
|
+
predicate.parameterName.type === "Identifier" &&
|
|
7
|
+
predicate.parameterName.name === parameterName);
|
|
8
|
+
}
|
|
9
|
+
/** Disallow unknown inputs except explicitly named error-cause enrichment. */
|
|
10
|
+
export const noUnknownParametersRule = defineRule({
|
|
11
|
+
meta: {
|
|
12
|
+
type: "problem",
|
|
13
|
+
docs: {
|
|
14
|
+
description: "Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead.",
|
|
15
|
+
},
|
|
16
|
+
messages: {
|
|
17
|
+
unknownParameter: "Parameter `{{parameter}}` leaves input unparsed. Accept a named domain type; run the expected schema or parser at the I/O boundary before calling this function.",
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
createOnce(context) {
|
|
21
|
+
const checkParameters = (node) => {
|
|
22
|
+
for (const parameter of node.params) {
|
|
23
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
24
|
+
if (annotation === null || annotation === undefined)
|
|
25
|
+
continue;
|
|
26
|
+
if (!containsUnknownType(annotation.typeAnnotation))
|
|
27
|
+
continue;
|
|
28
|
+
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
29
|
+
if (name === "cause" || isTypePredicateSubject(node, name))
|
|
30
|
+
continue;
|
|
31
|
+
context.report({
|
|
32
|
+
node: annotation.typeAnnotation,
|
|
33
|
+
messageId: "unknownParameter",
|
|
34
|
+
data: { parameter: name },
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
return {
|
|
39
|
+
ArrowFunctionExpression: checkParameters,
|
|
40
|
+
FunctionDeclaration: checkParameters,
|
|
41
|
+
FunctionExpression: checkParameters,
|
|
42
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
43
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
44
|
+
TSConstructorType: checkParameters,
|
|
45
|
+
TSDeclareFunction: checkParameters,
|
|
46
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
47
|
+
TSFunctionType: checkParameters,
|
|
48
|
+
TSMethodSignature: checkParameters,
|
|
49
|
+
};
|
|
50
|
+
},
|
|
51
|
+
});
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { createTypeAliasEnvironment, resolvedTypeMatches, } from "../shared/type-alias-resolution.js";
|
|
3
|
+
/** Ban function contracts that return unknown instead of a parsed domain type. */
|
|
4
|
+
export const noUnknownReturnsRule = defineRule({
|
|
5
|
+
meta: {
|
|
6
|
+
type: "problem",
|
|
7
|
+
docs: {
|
|
8
|
+
description: "Disallow functions whose explicit return contract is unknown or Promise<unknown>.",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
unknownReturn: "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
createOnce(context) {
|
|
15
|
+
let environment = null;
|
|
16
|
+
const resolvesToUnknown = (type) => environment !== null &&
|
|
17
|
+
resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
18
|
+
if (resolved.type === "TSUnknownKeyword")
|
|
19
|
+
return true;
|
|
20
|
+
if (resolved.type === "TSParenthesizedType") {
|
|
21
|
+
return matches(resolved.typeAnnotation);
|
|
22
|
+
}
|
|
23
|
+
if (resolved.type === "TSUnionType")
|
|
24
|
+
return resolved.types.some(matches);
|
|
25
|
+
if (resolved.type !== "TSTypeReference" ||
|
|
26
|
+
resolved.typeName.type !== "Identifier" ||
|
|
27
|
+
(resolved.typeName.name !== "Promise" &&
|
|
28
|
+
resolved.typeName.name !== "PromiseLike")) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const value = resolved.typeArguments?.params[0];
|
|
32
|
+
return value !== undefined && matches(value);
|
|
33
|
+
});
|
|
34
|
+
const checkReturnType = (node) => {
|
|
35
|
+
const annotation = node.returnType;
|
|
36
|
+
if (annotation === null || annotation === undefined)
|
|
37
|
+
return;
|
|
38
|
+
if (!resolvesToUnknown(annotation.typeAnnotation))
|
|
39
|
+
return;
|
|
40
|
+
context.report({ node: annotation.typeAnnotation, messageId: "unknownReturn" });
|
|
41
|
+
};
|
|
42
|
+
return {
|
|
43
|
+
Program(node) {
|
|
44
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
45
|
+
},
|
|
46
|
+
ArrowFunctionExpression: checkReturnType,
|
|
47
|
+
FunctionDeclaration: checkReturnType,
|
|
48
|
+
FunctionExpression: checkReturnType,
|
|
49
|
+
TSCallSignatureDeclaration: checkReturnType,
|
|
50
|
+
TSConstructSignatureDeclaration: checkReturnType,
|
|
51
|
+
TSConstructorType: checkReturnType,
|
|
52
|
+
TSDeclareFunction: checkReturnType,
|
|
53
|
+
TSEmptyBodyFunctionExpression: checkReturnType,
|
|
54
|
+
TSFunctionType: checkReturnType,
|
|
55
|
+
TSMethodSignature: checkReturnType,
|
|
56
|
+
};
|
|
57
|
+
},
|
|
58
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { createTypeAliasEnvironment, resolvedTypeMatches, } from "../shared/type-alias-resolution.js";
|
|
3
|
+
/** Ban named aliases that merely conceal TypeScript's unknown top type. */
|
|
4
|
+
export const noUnknownTypeAliasesRule = defineRule({
|
|
5
|
+
meta: {
|
|
6
|
+
type: "problem",
|
|
7
|
+
docs: {
|
|
8
|
+
description: "Disallow type aliases whose resolved type is unknown; unknown must remain visible at an allowed boundary.",
|
|
9
|
+
},
|
|
10
|
+
messages: {
|
|
11
|
+
unknownAlias: "Type alias `{{alias}}` hides `unknown`. Keep `unknown` explicit at the parsing boundary or on an allowed `cause` field; otherwise use the parsed owner type.",
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
createOnce(context) {
|
|
15
|
+
let environment = null;
|
|
16
|
+
const resolvesToUnknown = (type) => environment !== null &&
|
|
17
|
+
resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
18
|
+
if (resolved.type === "TSUnknownKeyword")
|
|
19
|
+
return true;
|
|
20
|
+
if (resolved.type === "TSParenthesizedType") {
|
|
21
|
+
return matches(resolved.typeAnnotation);
|
|
22
|
+
}
|
|
23
|
+
return resolved.type === "TSUnionType" && resolved.types.some(matches);
|
|
24
|
+
});
|
|
25
|
+
return {
|
|
26
|
+
Program(node) {
|
|
27
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
28
|
+
},
|
|
29
|
+
TSTypeAliasDeclaration(node) {
|
|
30
|
+
if (!resolvesToUnknown(node.typeAnnotation))
|
|
31
|
+
return;
|
|
32
|
+
context.report({
|
|
33
|
+
node: node.id,
|
|
34
|
+
messageId: "unknownAlias",
|
|
35
|
+
data: { alias: node.id.name },
|
|
36
|
+
});
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import { classifyUnsafeDictionary, classifyUnsafeDictionaryValue, createTypeEnvironment, } from "../shared/dictionary-types.js";
|
|
3
|
+
import { visibleTypeAlias } from "../shared/type-alias-resolution.js";
|
|
4
|
+
const typeNodeKinds = new Set([
|
|
5
|
+
"JSDocNonNullableType",
|
|
6
|
+
"JSDocNullableType",
|
|
7
|
+
"JSDocUnknownType",
|
|
8
|
+
"TSAnyKeyword",
|
|
9
|
+
"TSArrayType",
|
|
10
|
+
"TSBigIntKeyword",
|
|
11
|
+
"TSBooleanKeyword",
|
|
12
|
+
"TSConditionalType",
|
|
13
|
+
"TSConstructorType",
|
|
14
|
+
"TSFunctionType",
|
|
15
|
+
"TSImportType",
|
|
16
|
+
"TSIndexedAccessType",
|
|
17
|
+
"TSInferType",
|
|
18
|
+
"TSIntersectionType",
|
|
19
|
+
"TSIntrinsicKeyword",
|
|
20
|
+
"TSLiteralType",
|
|
21
|
+
"TSMappedType",
|
|
22
|
+
"TSNamedTupleMember",
|
|
23
|
+
"TSNeverKeyword",
|
|
24
|
+
"TSNullKeyword",
|
|
25
|
+
"TSNumberKeyword",
|
|
26
|
+
"TSObjectKeyword",
|
|
27
|
+
"TSParenthesizedType",
|
|
28
|
+
"TSStringKeyword",
|
|
29
|
+
"TSSymbolKeyword",
|
|
30
|
+
"TSTemplateLiteralType",
|
|
31
|
+
"TSThisType",
|
|
32
|
+
"TSTupleType",
|
|
33
|
+
"TSTypeLiteral",
|
|
34
|
+
"TSTypeOperator",
|
|
35
|
+
"TSTypePredicate",
|
|
36
|
+
"TSTypeQuery",
|
|
37
|
+
"TSTypeReference",
|
|
38
|
+
"TSUndefinedKeyword",
|
|
39
|
+
"TSUnionType",
|
|
40
|
+
"TSUnknownKeyword",
|
|
41
|
+
"TSVoidKeyword",
|
|
42
|
+
]);
|
|
43
|
+
function isTypeNode(node) {
|
|
44
|
+
return typeNodeKinds.has(node.type);
|
|
45
|
+
}
|
|
46
|
+
function typeReferenceName(type) {
|
|
47
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
48
|
+
}
|
|
49
|
+
function isInsideTypeAliasDeclaration(node) {
|
|
50
|
+
let current = node.parent;
|
|
51
|
+
while (current !== null && current.type !== "Program") {
|
|
52
|
+
if (current.type === "TSTypeAliasDeclaration")
|
|
53
|
+
return true;
|
|
54
|
+
current = current.parent;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function isPlainAliasConsumerUse(node, environment) {
|
|
59
|
+
if (node.type !== "TSTypeReference" || node.typeArguments?.params.length)
|
|
60
|
+
return false;
|
|
61
|
+
const name = typeReferenceName(node);
|
|
62
|
+
return (name !== null &&
|
|
63
|
+
visibleTypeAlias(name, node, environment.typeAliases) !== null &&
|
|
64
|
+
!isInsideTypeAliasDeclaration(node));
|
|
65
|
+
}
|
|
66
|
+
function isInsideTypeParameterConstraint(node) {
|
|
67
|
+
let child = node;
|
|
68
|
+
let parent = child.parent;
|
|
69
|
+
while (parent !== null && parent.type !== "Program") {
|
|
70
|
+
if (parent.type === "TSTypeParameter" && parent.constraint === child)
|
|
71
|
+
return true;
|
|
72
|
+
child = parent;
|
|
73
|
+
parent = child.parent;
|
|
74
|
+
}
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
function shouldReportType(node, environment) {
|
|
78
|
+
if (isInsideTypeParameterConstraint(node))
|
|
79
|
+
return false;
|
|
80
|
+
if (isPlainAliasConsumerUse(node, environment))
|
|
81
|
+
return false;
|
|
82
|
+
if (classifyUnsafeDictionary(node, environment) === null)
|
|
83
|
+
return false;
|
|
84
|
+
let current = node.parent;
|
|
85
|
+
while (current !== null && current.type !== "Program") {
|
|
86
|
+
if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null)
|
|
87
|
+
return false;
|
|
88
|
+
current = current.parent;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
/** Disallow object-dictionary contracts whose direct value type is an unsafe escape hatch. */
|
|
93
|
+
export const noUnsafeDictionaryTypeRule = defineRule({
|
|
94
|
+
meta: {
|
|
95
|
+
type: "problem",
|
|
96
|
+
docs: {
|
|
97
|
+
description: "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches.",
|
|
98
|
+
},
|
|
99
|
+
messages: {
|
|
100
|
+
unsafeDictionary: "This dictionary's {{value}} value type gives callers no concrete value contract. Use an owner/schema-derived value type; parse external payloads before insertion.",
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
createOnce(context) {
|
|
104
|
+
let environment = null;
|
|
105
|
+
const report = (node, value) => {
|
|
106
|
+
context.report({ node, messageId: "unsafeDictionary", data: { value } });
|
|
107
|
+
};
|
|
108
|
+
const reportIfUnsafe = (node) => {
|
|
109
|
+
if (environment === null || !shouldReportType(node, environment))
|
|
110
|
+
return;
|
|
111
|
+
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
112
|
+
if (unsafe === null)
|
|
113
|
+
return;
|
|
114
|
+
report(node, unsafe.unsafeValue);
|
|
115
|
+
};
|
|
116
|
+
return {
|
|
117
|
+
Program(node) {
|
|
118
|
+
environment = createTypeEnvironment(node, context.sourceCode.visitorKeys);
|
|
119
|
+
},
|
|
120
|
+
TSTypeReference: reportIfUnsafe,
|
|
121
|
+
TSTypeLiteral: reportIfUnsafe,
|
|
122
|
+
TSMappedType: reportIfUnsafe,
|
|
123
|
+
TSIndexSignature(node) {
|
|
124
|
+
if (environment === null ||
|
|
125
|
+
node.typeAnnotation === null ||
|
|
126
|
+
node.parent.type === "TSTypeLiteral")
|
|
127
|
+
return;
|
|
128
|
+
const unsafe = classifyUnsafeDictionaryValue(node.typeAnnotation.typeAnnotation, environment);
|
|
129
|
+
if (unsafe !== null)
|
|
130
|
+
report(node, unsafe.unsafeValue);
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
},
|
|
134
|
+
});
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
const functionBoundaryTypes = new Set([
|
|
3
|
+
"ArrowFunctionExpression",
|
|
4
|
+
"FunctionDeclaration",
|
|
5
|
+
"FunctionExpression",
|
|
6
|
+
"TSDeclareFunction",
|
|
7
|
+
"TSEmptyBodyFunctionExpression",
|
|
8
|
+
]);
|
|
9
|
+
function unwrapExpressionParentheses(expression) {
|
|
10
|
+
let current = expression;
|
|
11
|
+
while (current.type === "ParenthesizedExpression")
|
|
12
|
+
current = current.expression;
|
|
13
|
+
return current;
|
|
14
|
+
}
|
|
15
|
+
function unwrapTypeParentheses(type) {
|
|
16
|
+
let current = type;
|
|
17
|
+
while (current.type === "TSParenthesizedType")
|
|
18
|
+
current = current.typeAnnotation;
|
|
19
|
+
return current;
|
|
20
|
+
}
|
|
21
|
+
function typeReferenceName(type) {
|
|
22
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
23
|
+
}
|
|
24
|
+
function isUnknownOrAnyType(type) {
|
|
25
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
26
|
+
return unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword";
|
|
27
|
+
}
|
|
28
|
+
function isBroadRecordKeyType(type) {
|
|
29
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
30
|
+
if (unwrapped.type === "TSStringKeyword" ||
|
|
31
|
+
unwrapped.type === "TSNumberKeyword" ||
|
|
32
|
+
unwrapped.type === "TSSymbolKeyword") {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (unwrapped.type === "TSUnionType")
|
|
36
|
+
return unwrapped.types.every(isBroadRecordKeyType);
|
|
37
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName(unwrapped) === "PropertyKey";
|
|
38
|
+
}
|
|
39
|
+
function isBroadRecordType(type) {
|
|
40
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
41
|
+
if (unwrapped.type === "TSTypeReference") {
|
|
42
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
43
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
44
|
+
return inner !== undefined && isBroadRecordType(inner);
|
|
45
|
+
}
|
|
46
|
+
if (typeReferenceName(unwrapped) !== "Record")
|
|
47
|
+
return false;
|
|
48
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
49
|
+
return (parameters.length === 2 &&
|
|
50
|
+
parameters[0] !== undefined &&
|
|
51
|
+
parameters[1] !== undefined &&
|
|
52
|
+
isBroadRecordKeyType(parameters[0]) &&
|
|
53
|
+
isUnknownOrAnyType(parameters[1]));
|
|
54
|
+
}
|
|
55
|
+
if (unwrapped.type !== "TSTypeLiteral" || unwrapped.members.length !== 1)
|
|
56
|
+
return false;
|
|
57
|
+
const [member] = unwrapped.members;
|
|
58
|
+
const [parameter] = member?.type === "TSIndexSignature" ? member.parameters : [];
|
|
59
|
+
return (member?.type === "TSIndexSignature" &&
|
|
60
|
+
member.parameters.length === 1 &&
|
|
61
|
+
parameter !== undefined &&
|
|
62
|
+
isBroadRecordKeyType(parameter.typeAnnotation.typeAnnotation) &&
|
|
63
|
+
isUnknownOrAnyType(member.typeAnnotation.typeAnnotation));
|
|
64
|
+
}
|
|
65
|
+
function broadTypeKind(type) {
|
|
66
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
67
|
+
if (unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword")
|
|
68
|
+
return "top";
|
|
69
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
70
|
+
return "object";
|
|
71
|
+
return isBroadRecordType(unwrapped) ? "record" : null;
|
|
72
|
+
}
|
|
73
|
+
function assertedExpression(node) {
|
|
74
|
+
return unwrapExpressionParentheses(node.expression);
|
|
75
|
+
}
|
|
76
|
+
function assertionFromExpression(expression) {
|
|
77
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
78
|
+
return unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion"
|
|
79
|
+
? unwrapped
|
|
80
|
+
: null;
|
|
81
|
+
}
|
|
82
|
+
function normalizedTypeText(sourceText, type) {
|
|
83
|
+
return sourceText.slice(type.start, type.end).replaceAll(/\s+/gu, "");
|
|
84
|
+
}
|
|
85
|
+
function typesHaveSameSyntax(sourceText, left, right) {
|
|
86
|
+
return (left !== null &&
|
|
87
|
+
normalizedTypeText(sourceText, unwrapTypeParentheses(left)) ===
|
|
88
|
+
normalizedTypeText(sourceText, unwrapTypeParentheses(right)));
|
|
89
|
+
}
|
|
90
|
+
function isDefinitelyObjectType(type) {
|
|
91
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
92
|
+
switch (unwrapped.type) {
|
|
93
|
+
case "TSArrayType":
|
|
94
|
+
case "TSConstructorType":
|
|
95
|
+
case "TSFunctionType":
|
|
96
|
+
case "TSMappedType":
|
|
97
|
+
case "TSObjectKeyword":
|
|
98
|
+
case "TSTupleType":
|
|
99
|
+
return true;
|
|
100
|
+
case "TSTypeLiteral":
|
|
101
|
+
return unwrapped.members.length > 0;
|
|
102
|
+
case "TSIntersectionType":
|
|
103
|
+
return unwrapped.types.every(isDefinitelyObjectType);
|
|
104
|
+
case "TSTypeOperator":
|
|
105
|
+
return unwrapped.operator === "readonly" && isDefinitelyObjectType(unwrapped.typeAnnotation);
|
|
106
|
+
default:
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
function isDefinitelyNarrowerRecordType(type) {
|
|
111
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
112
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
113
|
+
return unwrapped.members.some((member) => member.type !== "TSIndexSignature");
|
|
114
|
+
}
|
|
115
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
116
|
+
return false;
|
|
117
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
118
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
119
|
+
return inner !== undefined && isDefinitelyNarrowerRecordType(inner);
|
|
120
|
+
}
|
|
121
|
+
if (typeReferenceName(unwrapped) !== "Record")
|
|
122
|
+
return false;
|
|
123
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
124
|
+
return (parameters.length === 2 && parameters[1] !== undefined && !isUnknownOrAnyType(parameters[1]));
|
|
125
|
+
}
|
|
126
|
+
function functionBoundary(node) {
|
|
127
|
+
let current = node.parent;
|
|
128
|
+
while (current !== null && current.type !== "Program") {
|
|
129
|
+
if (functionBoundaryTypes.has(current.type))
|
|
130
|
+
return current;
|
|
131
|
+
current = current.parent;
|
|
132
|
+
}
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
function resolvedVariableForIdentifier(scopes, identifier) {
|
|
136
|
+
for (const scope of scopes) {
|
|
137
|
+
const reference = scope.references.find((candidate) => candidate.identifier.start === identifier.start &&
|
|
138
|
+
candidate.identifier.end === identifier.end);
|
|
139
|
+
if (reference !== undefined)
|
|
140
|
+
return reference.resolved;
|
|
141
|
+
}
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
function variableDeclarator(variable) {
|
|
145
|
+
for (const definition of variable.defs) {
|
|
146
|
+
if (definition.type === "Variable" && definition.node.type === "VariableDeclarator") {
|
|
147
|
+
return definition.node;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
function knownValueEvidence(expression, scopes, boundary, visitedVariables) {
|
|
153
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
154
|
+
if (unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion") {
|
|
155
|
+
if (broadTypeKind(unwrapped.typeAnnotation) !== null)
|
|
156
|
+
return null;
|
|
157
|
+
return { type: unwrapped.typeAnnotation };
|
|
158
|
+
}
|
|
159
|
+
if (unwrapped.type === "Literal" || unwrapped.type === "TemplateLiteral") {
|
|
160
|
+
return { type: null };
|
|
161
|
+
}
|
|
162
|
+
if (unwrapped.type === "ArrayExpression" ||
|
|
163
|
+
unwrapped.type === "ArrowFunctionExpression" ||
|
|
164
|
+
unwrapped.type === "ClassExpression" ||
|
|
165
|
+
unwrapped.type === "FunctionExpression" ||
|
|
166
|
+
unwrapped.type === "NewExpression" ||
|
|
167
|
+
unwrapped.type === "ObjectExpression") {
|
|
168
|
+
return { type: null };
|
|
169
|
+
}
|
|
170
|
+
if (unwrapped.type !== "Identifier")
|
|
171
|
+
return null;
|
|
172
|
+
const variable = resolvedVariableForIdentifier(scopes, unwrapped);
|
|
173
|
+
if (variable === null || visitedVariables.has(variable))
|
|
174
|
+
return null;
|
|
175
|
+
const annotatedIdentifier = variable.identifiers.find((identifier) => identifier.typeAnnotation !== null && identifier.typeAnnotation !== undefined);
|
|
176
|
+
const annotation = annotatedIdentifier?.typeAnnotation?.typeAnnotation;
|
|
177
|
+
if (annotation !== undefined && annotatedIdentifier !== undefined) {
|
|
178
|
+
if (functionBoundary(annotatedIdentifier) !== boundary || broadTypeKind(annotation) !== null) {
|
|
179
|
+
return null;
|
|
180
|
+
}
|
|
181
|
+
return { type: annotation };
|
|
182
|
+
}
|
|
183
|
+
const declarator = variableDeclarator(variable);
|
|
184
|
+
if (declarator === null ||
|
|
185
|
+
declarator.parent.type !== "VariableDeclaration" ||
|
|
186
|
+
declarator.parent.kind !== "const" ||
|
|
187
|
+
declarator.init === null ||
|
|
188
|
+
variable.references.some((reference) => reference.isWrite() && !reference.init) ||
|
|
189
|
+
functionBoundary(declarator) !== boundary) {
|
|
190
|
+
return null;
|
|
191
|
+
}
|
|
192
|
+
return knownValueEvidence(declarator.init, scopes, boundary, new Set([...visitedVariables, variable]));
|
|
193
|
+
}
|
|
194
|
+
function widenedBinding(variable, scopes) {
|
|
195
|
+
const declarator = variableDeclarator(variable);
|
|
196
|
+
if (declarator === null ||
|
|
197
|
+
declarator.parent.type !== "VariableDeclaration" ||
|
|
198
|
+
declarator.parent.kind !== "const" ||
|
|
199
|
+
declarator.id.type !== "Identifier" ||
|
|
200
|
+
declarator.init === null ||
|
|
201
|
+
variable.references.some((reference) => reference.isWrite() && !reference.init)) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
const boundary = functionBoundary(declarator);
|
|
205
|
+
const declaredType = declarator.id.typeAnnotation?.typeAnnotation;
|
|
206
|
+
const initializerAssertion = assertionFromExpression(declarator.init);
|
|
207
|
+
const initializerBroadKind = initializerAssertion === null ? null : broadTypeKind(initializerAssertion.typeAnnotation);
|
|
208
|
+
const declaredBroadKind = declaredType === undefined ? null : broadTypeKind(declaredType);
|
|
209
|
+
const broadKind = declaredBroadKind ?? initializerBroadKind;
|
|
210
|
+
if (broadKind === null)
|
|
211
|
+
return null;
|
|
212
|
+
const originalExpression = initializerAssertion !== null && initializerBroadKind !== null
|
|
213
|
+
? assertedExpression(initializerAssertion)
|
|
214
|
+
: declarator.init;
|
|
215
|
+
const evidence = knownValueEvidence(originalExpression, scopes, boundary, new Set([variable]));
|
|
216
|
+
return evidence === null ? null : { broadKind, evidence, declaredAt: declarator.end, boundary };
|
|
217
|
+
}
|
|
218
|
+
function assertionIsNarrower(sourceText, broadKind, evidence, assertedType) {
|
|
219
|
+
if (broadTypeKind(assertedType) !== null)
|
|
220
|
+
return false;
|
|
221
|
+
if (broadKind === "top")
|
|
222
|
+
return true;
|
|
223
|
+
if (typesHaveSameSyntax(sourceText, evidence.type, assertedType))
|
|
224
|
+
return true;
|
|
225
|
+
if (broadKind === "object")
|
|
226
|
+
return isDefinitelyObjectType(assertedType);
|
|
227
|
+
return isDefinitelyNarrowerRecordType(assertedType);
|
|
228
|
+
}
|
|
229
|
+
/** Detect immutable local bindings that erase a known type and are later asserted back to a narrower type. */
|
|
230
|
+
export const noWidenThenAssertRule = defineRule({
|
|
231
|
+
meta: {
|
|
232
|
+
type: "problem",
|
|
233
|
+
docs: {
|
|
234
|
+
description: "Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type.",
|
|
235
|
+
},
|
|
236
|
+
messages: {
|
|
237
|
+
widenThenAssert: 'Binding "{{name}}" discards type evidence and later recreates it with an assertion. Keep the precise type from initialization through use; parse boundary input once.',
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
createOnce(context) {
|
|
241
|
+
let scopes = [];
|
|
242
|
+
const checkAssertion = (node) => {
|
|
243
|
+
const expression = assertedExpression(node);
|
|
244
|
+
if (expression.type !== "Identifier")
|
|
245
|
+
return;
|
|
246
|
+
const variable = resolvedVariableForIdentifier(scopes, expression);
|
|
247
|
+
if (variable === null)
|
|
248
|
+
return;
|
|
249
|
+
const widened = widenedBinding(variable, scopes);
|
|
250
|
+
if (widened === null ||
|
|
251
|
+
node.start <= widened.declaredAt ||
|
|
252
|
+
functionBoundary(node) !== widened.boundary ||
|
|
253
|
+
!assertionIsNarrower(context.sourceCode.text, widened.broadKind, widened.evidence, node.typeAnnotation)) {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
context.report({
|
|
257
|
+
node,
|
|
258
|
+
messageId: "widenThenAssert",
|
|
259
|
+
data: { name: expression.name },
|
|
260
|
+
});
|
|
261
|
+
};
|
|
262
|
+
return {
|
|
263
|
+
Program() {
|
|
264
|
+
scopes = context.sourceCode.scopeManager.scopes;
|
|
265
|
+
},
|
|
266
|
+
TSAsExpression: checkAssertion,
|
|
267
|
+
TSTypeAssertion: checkAssertion,
|
|
268
|
+
};
|
|
269
|
+
},
|
|
270
|
+
});
|