oxlint-plugin-samdotci 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +53 -2
- package/dist/index.d.ts +12 -0
- package/dist/index.js +25 -0
- package/dist/production-file.d.ts +1 -0
- package/dist/production-file.js +7 -0
- package/dist/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/rules/no-chained-type-assertions.js +55 -0
- package/dist/rules/no-discarded-map-error.d.ts +2 -0
- package/dist/rules/no-discarded-map-error.js +152 -0
- package/dist/rules/no-known-value-widening.d.ts +3 -0
- package/dist/rules/no-known-value-widening.js +306 -0
- package/dist/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/rules/no-runtime-typeof.js +48 -0
- package/dist/rules/no-unannotated-any.d.ts +2 -0
- package/dist/rules/no-unannotated-any.js +25 -0
- package/dist/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/rules/no-unknown-parameters.js +57 -0
- package/dist/rules/no-unsafe-dictionary-type.d.ts +3 -0
- package/dist/rules/no-unsafe-dictionary-type.js +122 -0
- package/dist/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/rules/no-widen-then-assert.js +274 -0
- package/dist/shared/dictionary-types.d.ts +19 -0
- package/dist/shared/dictionary-types.js +338 -0
- package/dist/shared/function-parameters.d.ts +8 -0
- package/dist/shared/function-parameters.js +40 -0
- package/dist/shared/lexical-type-parameters.d.ts +5 -0
- package/dist/shared/lexical-type-parameters.js +44 -0
- package/dist/shared/scope.d.ts +3 -0
- package/dist/shared/scope.js +6 -0
- package/dist/shared/type-alias-resolution.d.ts +20 -0
- package/dist/shared/type-alias-resolution.js +114 -0
- package/package.json +62 -4
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Diagnostic, Rule, RuleContext, Visitor } from "effect-oxlint";
|
|
3
|
+
function isRuntimeFunction(node) {
|
|
4
|
+
return (node.type === "ArrowFunctionExpression" ||
|
|
5
|
+
node.type === "FunctionDeclaration" ||
|
|
6
|
+
node.type === "FunctionExpression");
|
|
7
|
+
}
|
|
8
|
+
function isInsideTypeGuard(node) {
|
|
9
|
+
let current = node.parent;
|
|
10
|
+
while (current !== null && current.type !== "Program") {
|
|
11
|
+
if (isRuntimeFunction(current)) {
|
|
12
|
+
return current.returnType?.typeAnnotation.type === "TSTypePredicate";
|
|
13
|
+
}
|
|
14
|
+
current = current.parent;
|
|
15
|
+
}
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
/** Return whether typeof safely probes for the existence of a possibly absent binding. */
|
|
19
|
+
function isExistenceProbe(node) {
|
|
20
|
+
const parent = node.parent;
|
|
21
|
+
if (parent.type !== "BinaryExpression")
|
|
22
|
+
return false;
|
|
23
|
+
if (!["===", "!==", "==", "!="].includes(parent.operator))
|
|
24
|
+
return false;
|
|
25
|
+
const other = parent.left === node ? parent.right : parent.left;
|
|
26
|
+
return other.type === "Literal" && other.value === "undefined";
|
|
27
|
+
}
|
|
28
|
+
const optionsSchema = Schema.Struct({ allowInTypeGuards: Schema.optionalKey(Schema.Boolean) });
|
|
29
|
+
export default Rule.define({
|
|
30
|
+
name: "no-runtime-typeof",
|
|
31
|
+
meta: Rule.meta({
|
|
32
|
+
type: "problem",
|
|
33
|
+
description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.",
|
|
34
|
+
messages: {
|
|
35
|
+
runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
|
|
36
|
+
},
|
|
37
|
+
schema: [Schema.toJsonSchemaDocument(optionsSchema, { onExcessProperty: "error" }).schema],
|
|
38
|
+
}),
|
|
39
|
+
options: Schema.UndefinedOr(optionsSchema),
|
|
40
|
+
create: function* (options) {
|
|
41
|
+
const ctx = yield* RuleContext;
|
|
42
|
+
return Visitor.on("UnaryExpression", (node) => node.operator === "typeof" &&
|
|
43
|
+
!isExistenceProbe(node) &&
|
|
44
|
+
(!options?.allowInTypeGuards || !isInsideTypeGuard(node))
|
|
45
|
+
? ctx.report(Diagnostic.fromId({ node, messageId: "runtimeTypeof" }))
|
|
46
|
+
: Effect.void);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Diagnostic, Rule, RuleContext, SourceCode, Visitor } from "effect-oxlint";
|
|
3
|
+
import { isProductionFile } from "../production-file.js";
|
|
4
|
+
export default Rule.define({
|
|
5
|
+
name: "no-unannotated-any",
|
|
6
|
+
meta: Rule.meta({
|
|
7
|
+
type: "problem",
|
|
8
|
+
description: "Require a reason for explicit `any` in production code.",
|
|
9
|
+
}),
|
|
10
|
+
create: function* () {
|
|
11
|
+
const ctx = yield* RuleContext;
|
|
12
|
+
if (!isProductionFile(ctx.cwd, ctx.filename))
|
|
13
|
+
return {};
|
|
14
|
+
const comments = yield* SourceCode.getAllComments();
|
|
15
|
+
const intentionalLines = new Set(comments
|
|
16
|
+
.filter((comment) => comment.type === "Line" && /^\s*intentional:\s*\S/.test(comment.value))
|
|
17
|
+
.map((comment) => comment.loc.start.line));
|
|
18
|
+
return Visitor.on("TSAnyKeyword", (node) => intentionalLines.has(node.loc.start.line)
|
|
19
|
+
? Effect.void
|
|
20
|
+
: ctx.report(Diagnostic.make({
|
|
21
|
+
node,
|
|
22
|
+
message: "Production module uses `any` without an inline `// intentional: <reason>` comment.",
|
|
23
|
+
})));
|
|
24
|
+
},
|
|
25
|
+
});
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import { Diagnostic, Rule, RuleContext, Visitor } from "effect-oxlint";
|
|
3
|
+
import { containsUnknownType, functionParameterBindingName, functionParameterTypeAnnotation, } from "../shared/function-parameters.js";
|
|
4
|
+
const parameterOwners = [
|
|
5
|
+
"ArrowFunctionExpression",
|
|
6
|
+
"FunctionDeclaration",
|
|
7
|
+
"FunctionExpression",
|
|
8
|
+
"TSCallSignatureDeclaration",
|
|
9
|
+
"TSConstructSignatureDeclaration",
|
|
10
|
+
"TSConstructorType",
|
|
11
|
+
"TSDeclareFunction",
|
|
12
|
+
"TSEmptyBodyFunctionExpression",
|
|
13
|
+
"TSFunctionType",
|
|
14
|
+
"TSMethodSignature",
|
|
15
|
+
];
|
|
16
|
+
const optionsSchema = Schema.Struct({ allowInTypeGuards: Schema.optionalKey(Schema.Boolean) });
|
|
17
|
+
function isTypePredicateSubject(owner, parameterName) {
|
|
18
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
19
|
+
return (predicate?.type === "TSTypePredicate" &&
|
|
20
|
+
predicate.parameterName.type === "Identifier" &&
|
|
21
|
+
predicate.parameterName.name === parameterName);
|
|
22
|
+
}
|
|
23
|
+
export default Rule.define({
|
|
24
|
+
name: "no-unknown-parameters",
|
|
25
|
+
meta: Rule.meta({
|
|
26
|
+
type: "problem",
|
|
27
|
+
description: "Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead.",
|
|
28
|
+
messages: {
|
|
29
|
+
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.",
|
|
30
|
+
},
|
|
31
|
+
schema: [Schema.toJsonSchemaDocument(optionsSchema, { onExcessProperty: "error" }).schema],
|
|
32
|
+
}),
|
|
33
|
+
options: Schema.UndefinedOr(optionsSchema),
|
|
34
|
+
create: function* (options) {
|
|
35
|
+
const ctx = yield* RuleContext;
|
|
36
|
+
const checkParameters = Effect.fnUntraced(function* (node) {
|
|
37
|
+
if (options?.allowInTypeGuards && node.returnType?.typeAnnotation.type === "TSTypePredicate")
|
|
38
|
+
return;
|
|
39
|
+
for (const parameter of node.params) {
|
|
40
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
41
|
+
if (annotation === null || annotation === undefined)
|
|
42
|
+
continue;
|
|
43
|
+
if (!containsUnknownType(annotation.typeAnnotation))
|
|
44
|
+
continue;
|
|
45
|
+
const name = functionParameterBindingName(parameter, ctx.sourceCode);
|
|
46
|
+
if (name === "cause" || isTypePredicateSubject(node, name))
|
|
47
|
+
continue;
|
|
48
|
+
yield* ctx.report(Diagnostic.fromId({
|
|
49
|
+
node: annotation.typeAnnotation,
|
|
50
|
+
messageId: "unknownParameter",
|
|
51
|
+
data: { parameter: name },
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
return Visitor.merge(...parameterOwners.map((type) => Visitor.on(type, checkParameters)));
|
|
56
|
+
},
|
|
57
|
+
});
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Diagnostic, Rule, RuleContext, Visitor } from "effect-oxlint";
|
|
3
|
+
import { classifyUnsafeDictionary, classifyUnsafeDictionaryValue, createTypeEnvironment, } from "../shared/dictionary-types.js";
|
|
4
|
+
import { visibleTypeAlias } from "../shared/type-alias-resolution.js";
|
|
5
|
+
const typeNodeKinds = new Set([
|
|
6
|
+
"JSDocNonNullableType",
|
|
7
|
+
"JSDocNullableType",
|
|
8
|
+
"JSDocUnknownType",
|
|
9
|
+
"TSAnyKeyword",
|
|
10
|
+
"TSArrayType",
|
|
11
|
+
"TSBigIntKeyword",
|
|
12
|
+
"TSBooleanKeyword",
|
|
13
|
+
"TSConditionalType",
|
|
14
|
+
"TSConstructorType",
|
|
15
|
+
"TSFunctionType",
|
|
16
|
+
"TSImportType",
|
|
17
|
+
"TSIndexedAccessType",
|
|
18
|
+
"TSInferType",
|
|
19
|
+
"TSIntersectionType",
|
|
20
|
+
"TSIntrinsicKeyword",
|
|
21
|
+
"TSLiteralType",
|
|
22
|
+
"TSMappedType",
|
|
23
|
+
"TSNamedTupleMember",
|
|
24
|
+
"TSNeverKeyword",
|
|
25
|
+
"TSNullKeyword",
|
|
26
|
+
"TSNumberKeyword",
|
|
27
|
+
"TSObjectKeyword",
|
|
28
|
+
"TSParenthesizedType",
|
|
29
|
+
"TSStringKeyword",
|
|
30
|
+
"TSSymbolKeyword",
|
|
31
|
+
"TSTemplateLiteralType",
|
|
32
|
+
"TSThisType",
|
|
33
|
+
"TSTupleType",
|
|
34
|
+
"TSTypeLiteral",
|
|
35
|
+
"TSTypeOperator",
|
|
36
|
+
"TSTypePredicate",
|
|
37
|
+
"TSTypeQuery",
|
|
38
|
+
"TSTypeReference",
|
|
39
|
+
"TSUndefinedKeyword",
|
|
40
|
+
"TSUnionType",
|
|
41
|
+
"TSUnknownKeyword",
|
|
42
|
+
"TSVoidKeyword",
|
|
43
|
+
]);
|
|
44
|
+
function isTypeNode(node) {
|
|
45
|
+
return typeNodeKinds.has(node.type);
|
|
46
|
+
}
|
|
47
|
+
function typeReferenceName(type) {
|
|
48
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
49
|
+
}
|
|
50
|
+
function isInsideTypeAliasDeclaration(node) {
|
|
51
|
+
let current = node.parent;
|
|
52
|
+
while (current !== null && current.type !== "Program") {
|
|
53
|
+
if (current.type === "TSTypeAliasDeclaration")
|
|
54
|
+
return true;
|
|
55
|
+
current = current.parent;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
function isPlainAliasConsumerUse(node, environment) {
|
|
60
|
+
if (node.type !== "TSTypeReference" || node.typeArguments?.params.length)
|
|
61
|
+
return false;
|
|
62
|
+
const name = typeReferenceName(node);
|
|
63
|
+
return (name !== null &&
|
|
64
|
+
visibleTypeAlias(name, node, environment.typeAliases) !== null &&
|
|
65
|
+
!isInsideTypeAliasDeclaration(node));
|
|
66
|
+
}
|
|
67
|
+
function isInsideTypeParameterConstraint(node) {
|
|
68
|
+
let child = node;
|
|
69
|
+
let parent = child.parent;
|
|
70
|
+
while (parent !== null && parent.type !== "Program") {
|
|
71
|
+
if (parent.type === "TSTypeParameter" && parent.constraint === child)
|
|
72
|
+
return true;
|
|
73
|
+
child = parent;
|
|
74
|
+
parent = child.parent;
|
|
75
|
+
}
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
function shouldReportType(node, environment) {
|
|
79
|
+
if (isInsideTypeParameterConstraint(node))
|
|
80
|
+
return false;
|
|
81
|
+
if (isPlainAliasConsumerUse(node, environment))
|
|
82
|
+
return false;
|
|
83
|
+
if (classifyUnsafeDictionary(node, environment) === null)
|
|
84
|
+
return false;
|
|
85
|
+
let current = node.parent;
|
|
86
|
+
while (current !== null && current.type !== "Program") {
|
|
87
|
+
if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null)
|
|
88
|
+
return false;
|
|
89
|
+
current = current.parent;
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
/** Disallow object-dictionary contracts whose direct value type is an unsafe escape hatch. */
|
|
94
|
+
export default Rule.define({
|
|
95
|
+
name: "no-unsafe-dictionary-type",
|
|
96
|
+
meta: Rule.meta({
|
|
97
|
+
type: "problem",
|
|
98
|
+
description: "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches.",
|
|
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
|
+
create: function* () {
|
|
104
|
+
const ctx = yield* RuleContext;
|
|
105
|
+
const { ast, visitorKeys } = ctx.sourceCode;
|
|
106
|
+
const environment = createTypeEnvironment(ast, visitorKeys);
|
|
107
|
+
const report = (node, value) => ctx.report(Diagnostic.fromId({ node, messageId: "unsafeDictionary", data: { value } }));
|
|
108
|
+
const reportIfUnsafe = (node) => {
|
|
109
|
+
if (!shouldReportType(node, environment))
|
|
110
|
+
return Effect.void;
|
|
111
|
+
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
112
|
+
return unsafe === null ? Effect.void : report(node, unsafe.unsafeValue);
|
|
113
|
+
};
|
|
114
|
+
return Visitor.merge(Visitor.on("TSTypeReference", reportIfUnsafe), Visitor.on("TSTypeLiteral", reportIfUnsafe), Visitor.on("TSMappedType", reportIfUnsafe), Visitor.on("TSIndexSignature", (node) => {
|
|
115
|
+
if (node.typeAnnotation === null || node.parent.type === "TSTypeLiteral") {
|
|
116
|
+
return Effect.void;
|
|
117
|
+
}
|
|
118
|
+
const unsafe = classifyUnsafeDictionaryValue(node.typeAnnotation.typeAnnotation, environment);
|
|
119
|
+
return unsafe === null ? Effect.void : report(node, unsafe.unsafeValue);
|
|
120
|
+
}));
|
|
121
|
+
},
|
|
122
|
+
});
|
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { Diagnostic, Rule, RuleContext, Visitor } from "effect-oxlint";
|
|
3
|
+
const functionBoundaryTypes = new Set([
|
|
4
|
+
"ArrowFunctionExpression",
|
|
5
|
+
"FunctionDeclaration",
|
|
6
|
+
"FunctionExpression",
|
|
7
|
+
"TSDeclareFunction",
|
|
8
|
+
"TSEmptyBodyFunctionExpression",
|
|
9
|
+
]);
|
|
10
|
+
function unwrapExpressionParentheses(expression) {
|
|
11
|
+
let current = expression;
|
|
12
|
+
while (current.type === "ParenthesizedExpression")
|
|
13
|
+
current = current.expression;
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
function unwrapTypeParentheses(type) {
|
|
17
|
+
let current = type;
|
|
18
|
+
while (current.type === "TSParenthesizedType")
|
|
19
|
+
current = current.typeAnnotation;
|
|
20
|
+
return current;
|
|
21
|
+
}
|
|
22
|
+
function typeReferenceName(type) {
|
|
23
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
24
|
+
}
|
|
25
|
+
function isUnknownOrAnyType(type) {
|
|
26
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
27
|
+
return unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword";
|
|
28
|
+
}
|
|
29
|
+
function isBroadRecordKeyType(type) {
|
|
30
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
31
|
+
if (unwrapped.type === "TSStringKeyword" ||
|
|
32
|
+
unwrapped.type === "TSNumberKeyword" ||
|
|
33
|
+
unwrapped.type === "TSSymbolKeyword") {
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
if (unwrapped.type === "TSUnionType")
|
|
37
|
+
return unwrapped.types.every(isBroadRecordKeyType);
|
|
38
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName(unwrapped) === "PropertyKey";
|
|
39
|
+
}
|
|
40
|
+
function unwrapRecordType(type) {
|
|
41
|
+
let current = unwrapTypeParentheses(type);
|
|
42
|
+
while (current.type === "TSTypeReference" && typeReferenceName(current) === "Readonly") {
|
|
43
|
+
const inner = current.typeArguments?.params[0];
|
|
44
|
+
if (inner === undefined)
|
|
45
|
+
return null;
|
|
46
|
+
current = unwrapTypeParentheses(inner);
|
|
47
|
+
}
|
|
48
|
+
return current;
|
|
49
|
+
}
|
|
50
|
+
function recordTypeArguments(reference) {
|
|
51
|
+
if (typeReferenceName(reference) !== "Record")
|
|
52
|
+
return null;
|
|
53
|
+
const args = reference.typeArguments?.params;
|
|
54
|
+
if (args?.length !== 2)
|
|
55
|
+
return null;
|
|
56
|
+
const [key, value] = args;
|
|
57
|
+
return key === undefined || value === undefined ? null : [key, value];
|
|
58
|
+
}
|
|
59
|
+
function isBroadIndexSignature(type) {
|
|
60
|
+
if (type.members.length !== 1)
|
|
61
|
+
return false;
|
|
62
|
+
const [member] = type.members;
|
|
63
|
+
if (member?.type !== "TSIndexSignature" || member.parameters.length !== 1)
|
|
64
|
+
return false;
|
|
65
|
+
const [parameter] = member.parameters;
|
|
66
|
+
return (parameter !== undefined &&
|
|
67
|
+
isBroadRecordKeyType(parameter.typeAnnotation.typeAnnotation) &&
|
|
68
|
+
isUnknownOrAnyType(member.typeAnnotation.typeAnnotation));
|
|
69
|
+
}
|
|
70
|
+
function isBroadRecordType(type) {
|
|
71
|
+
const unwrapped = unwrapRecordType(type);
|
|
72
|
+
if (unwrapped?.type === "TSTypeLiteral")
|
|
73
|
+
return isBroadIndexSignature(unwrapped);
|
|
74
|
+
if (unwrapped?.type !== "TSTypeReference")
|
|
75
|
+
return false;
|
|
76
|
+
const args = recordTypeArguments(unwrapped);
|
|
77
|
+
return args !== null && isBroadRecordKeyType(args[0]) && isUnknownOrAnyType(args[1]);
|
|
78
|
+
}
|
|
79
|
+
function broadTypeKind(type) {
|
|
80
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
81
|
+
if (unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword")
|
|
82
|
+
return "top";
|
|
83
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
84
|
+
return "object";
|
|
85
|
+
return isBroadRecordType(unwrapped) ? "record" : null;
|
|
86
|
+
}
|
|
87
|
+
function assertedExpression(node) {
|
|
88
|
+
return unwrapExpressionParentheses(node.expression);
|
|
89
|
+
}
|
|
90
|
+
function assertionFromExpression(expression) {
|
|
91
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
92
|
+
return unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion"
|
|
93
|
+
? unwrapped
|
|
94
|
+
: null;
|
|
95
|
+
}
|
|
96
|
+
function normalizedTypeText(sourceText, type) {
|
|
97
|
+
return sourceText.slice(type.start, type.end).replaceAll(/\s+/gu, "");
|
|
98
|
+
}
|
|
99
|
+
function typesHaveSameSyntax(sourceText, left, right) {
|
|
100
|
+
return (left !== null &&
|
|
101
|
+
normalizedTypeText(sourceText, unwrapTypeParentheses(left)) ===
|
|
102
|
+
normalizedTypeText(sourceText, unwrapTypeParentheses(right)));
|
|
103
|
+
}
|
|
104
|
+
function isDefinitelyObjectType(type) {
|
|
105
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
106
|
+
switch (unwrapped.type) {
|
|
107
|
+
case "TSArrayType":
|
|
108
|
+
case "TSConstructorType":
|
|
109
|
+
case "TSFunctionType":
|
|
110
|
+
case "TSMappedType":
|
|
111
|
+
case "TSObjectKeyword":
|
|
112
|
+
case "TSTupleType":
|
|
113
|
+
return true;
|
|
114
|
+
case "TSTypeLiteral":
|
|
115
|
+
return unwrapped.members.length > 0;
|
|
116
|
+
case "TSIntersectionType":
|
|
117
|
+
return unwrapped.types.every(isDefinitelyObjectType);
|
|
118
|
+
case "TSTypeOperator":
|
|
119
|
+
return unwrapped.operator === "readonly" && isDefinitelyObjectType(unwrapped.typeAnnotation);
|
|
120
|
+
default:
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function isDefinitelyNarrowerRecordType(type) {
|
|
125
|
+
const unwrapped = unwrapRecordType(type);
|
|
126
|
+
if (unwrapped?.type === "TSTypeLiteral") {
|
|
127
|
+
return unwrapped.members.some((member) => member.type !== "TSIndexSignature");
|
|
128
|
+
}
|
|
129
|
+
if (unwrapped?.type !== "TSTypeReference")
|
|
130
|
+
return false;
|
|
131
|
+
const args = recordTypeArguments(unwrapped);
|
|
132
|
+
return args !== null && !isUnknownOrAnyType(args[1]);
|
|
133
|
+
}
|
|
134
|
+
function functionBoundary(node) {
|
|
135
|
+
let current = node.parent;
|
|
136
|
+
while (current !== null && current.type !== "Program") {
|
|
137
|
+
if (functionBoundaryTypes.has(current.type))
|
|
138
|
+
return current;
|
|
139
|
+
current = current.parent;
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
function resolvedVariableForIdentifier(scopes, identifier) {
|
|
144
|
+
for (const scope of scopes) {
|
|
145
|
+
const reference = scope.references.find((candidate) => candidate.identifier.start === identifier.start &&
|
|
146
|
+
candidate.identifier.end === identifier.end);
|
|
147
|
+
if (reference !== undefined)
|
|
148
|
+
return reference.resolved;
|
|
149
|
+
}
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
function variableDeclarator(variable) {
|
|
153
|
+
for (const definition of variable.defs) {
|
|
154
|
+
if (definition.type === "Variable" && definition.node.type === "VariableDeclarator") {
|
|
155
|
+
return definition.node;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
function stableConstBinding(variable) {
|
|
161
|
+
const declarator = variableDeclarator(variable);
|
|
162
|
+
if (declarator === null ||
|
|
163
|
+
declarator.parent.type !== "VariableDeclaration" ||
|
|
164
|
+
declarator.parent.kind !== "const" ||
|
|
165
|
+
declarator.init === null ||
|
|
166
|
+
variable.references.some((reference) => reference.isWrite() && !reference.init)) {
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
return { declarator, initializer: declarator.init };
|
|
170
|
+
}
|
|
171
|
+
function knownValueEvidence(expression, scopes, boundary, visitedVariables) {
|
|
172
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
173
|
+
switch (unwrapped.type) {
|
|
174
|
+
case "TSAsExpression":
|
|
175
|
+
case "TSTypeAssertion":
|
|
176
|
+
return broadTypeKind(unwrapped.typeAnnotation) === null
|
|
177
|
+
? { type: unwrapped.typeAnnotation }
|
|
178
|
+
: null;
|
|
179
|
+
case "Literal":
|
|
180
|
+
case "TemplateLiteral":
|
|
181
|
+
case "ArrayExpression":
|
|
182
|
+
case "ArrowFunctionExpression":
|
|
183
|
+
case "ClassExpression":
|
|
184
|
+
case "FunctionExpression":
|
|
185
|
+
case "NewExpression":
|
|
186
|
+
case "ObjectExpression":
|
|
187
|
+
return { type: null };
|
|
188
|
+
case "Identifier":
|
|
189
|
+
return knownIdentifierEvidence(unwrapped, scopes, boundary, visitedVariables);
|
|
190
|
+
default:
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
function knownIdentifierEvidence(identifier, scopes, boundary, visitedVariables) {
|
|
195
|
+
const variable = resolvedVariableForIdentifier(scopes, identifier);
|
|
196
|
+
if (variable === null || visitedVariables.has(variable))
|
|
197
|
+
return null;
|
|
198
|
+
const annotatedIdentifier = variable.identifiers.find((candidate) => candidate.typeAnnotation !== null && candidate.typeAnnotation !== undefined);
|
|
199
|
+
const annotation = annotatedIdentifier?.typeAnnotation?.typeAnnotation;
|
|
200
|
+
if (annotation !== undefined && annotatedIdentifier !== undefined) {
|
|
201
|
+
if (functionBoundary(annotatedIdentifier) !== boundary || broadTypeKind(annotation) !== null) {
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
return { type: annotation };
|
|
205
|
+
}
|
|
206
|
+
const binding = stableConstBinding(variable);
|
|
207
|
+
if (binding === null || functionBoundary(binding.declarator) !== boundary) {
|
|
208
|
+
return null;
|
|
209
|
+
}
|
|
210
|
+
return knownValueEvidence(binding.initializer, scopes, boundary, new Set([...visitedVariables, variable]));
|
|
211
|
+
}
|
|
212
|
+
function widenedBinding(variable, scopes) {
|
|
213
|
+
const binding = stableConstBinding(variable);
|
|
214
|
+
if (binding === null)
|
|
215
|
+
return null;
|
|
216
|
+
const { declarator, initializer } = binding;
|
|
217
|
+
if (declarator.id.type !== "Identifier")
|
|
218
|
+
return null;
|
|
219
|
+
const boundary = functionBoundary(declarator);
|
|
220
|
+
const declaredType = declarator.id.typeAnnotation?.typeAnnotation;
|
|
221
|
+
const initializerAssertion = assertionFromExpression(initializer);
|
|
222
|
+
const initializerBroadKind = initializerAssertion === null ? null : broadTypeKind(initializerAssertion.typeAnnotation);
|
|
223
|
+
const declaredBroadKind = declaredType === undefined ? null : broadTypeKind(declaredType);
|
|
224
|
+
const broadKind = declaredBroadKind ?? initializerBroadKind;
|
|
225
|
+
if (broadKind === null)
|
|
226
|
+
return null;
|
|
227
|
+
const originalExpression = initializerAssertion !== null && initializerBroadKind !== null
|
|
228
|
+
? assertedExpression(initializerAssertion)
|
|
229
|
+
: initializer;
|
|
230
|
+
const evidence = knownValueEvidence(originalExpression, scopes, boundary, new Set([variable]));
|
|
231
|
+
return evidence === null ? null : { broadKind, evidence, declaredAt: declarator.end, boundary };
|
|
232
|
+
}
|
|
233
|
+
function assertionIsNarrower(sourceText, broadKind, evidence, assertedType) {
|
|
234
|
+
if (broadTypeKind(assertedType) !== null)
|
|
235
|
+
return false;
|
|
236
|
+
if (broadKind === "top")
|
|
237
|
+
return true;
|
|
238
|
+
if (typesHaveSameSyntax(sourceText, evidence.type, assertedType))
|
|
239
|
+
return true;
|
|
240
|
+
if (broadKind === "object")
|
|
241
|
+
return isDefinitelyObjectType(assertedType);
|
|
242
|
+
return isDefinitelyNarrowerRecordType(assertedType);
|
|
243
|
+
}
|
|
244
|
+
export default Rule.define({
|
|
245
|
+
name: "no-widen-then-assert",
|
|
246
|
+
meta: Rule.meta({
|
|
247
|
+
type: "problem",
|
|
248
|
+
description: "Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type.",
|
|
249
|
+
messages: {
|
|
250
|
+
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.',
|
|
251
|
+
},
|
|
252
|
+
}),
|
|
253
|
+
create: function* () {
|
|
254
|
+
const ctx = yield* RuleContext;
|
|
255
|
+
const scopes = ctx.sourceCode.scopeManager.scopes;
|
|
256
|
+
const checkAssertion = (node) => {
|
|
257
|
+
const expression = assertedExpression(node);
|
|
258
|
+
if (expression.type !== "Identifier")
|
|
259
|
+
return Effect.void;
|
|
260
|
+
const variable = resolvedVariableForIdentifier(scopes, expression);
|
|
261
|
+
if (variable === null)
|
|
262
|
+
return Effect.void;
|
|
263
|
+
const widened = widenedBinding(variable, scopes);
|
|
264
|
+
if (widened === null ||
|
|
265
|
+
node.start <= widened.declaredAt ||
|
|
266
|
+
functionBoundary(node) !== widened.boundary ||
|
|
267
|
+
!assertionIsNarrower(ctx.sourceCode.text, widened.broadKind, widened.evidence, node.typeAnnotation)) {
|
|
268
|
+
return Effect.void;
|
|
269
|
+
}
|
|
270
|
+
return ctx.report(Diagnostic.fromId({ node, messageId: "widenThenAssert", data: { name: expression.name } }));
|
|
271
|
+
};
|
|
272
|
+
return Visitor.merge(Visitor.on("TSAsExpression", checkAssertion), Visitor.on("TSTypeAssertion", checkAssertion));
|
|
273
|
+
},
|
|
274
|
+
});
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ESTree } from "effect-oxlint";
|
|
2
|
+
import { type TypeAliasEnvironment as LexicalTypeAliasEnvironment } from "./type-alias-resolution.ts";
|
|
3
|
+
export type UnsafeDictionary = {
|
|
4
|
+
readonly kind: "unsafe-dictionary";
|
|
5
|
+
readonly unsafeValue: "any" | "empty-object" | "object" | "union" | "unknown";
|
|
6
|
+
};
|
|
7
|
+
export type WideningTargetKind = "anonymous object" | "generic container" | "object" | "open dictionary" | "unknown";
|
|
8
|
+
export type WideningTarget = {
|
|
9
|
+
readonly kind: WideningTargetKind;
|
|
10
|
+
};
|
|
11
|
+
export type TypeEnvironment = {
|
|
12
|
+
readonly interfaces: ReadonlyMap<string, readonly ESTree.TSInterfaceDeclaration[]>;
|
|
13
|
+
readonly typeAliases: LexicalTypeAliasEnvironment;
|
|
14
|
+
};
|
|
15
|
+
export declare function createTypeEnvironment(program: ESTree.Program, visitorKeys: Readonly<Record<string, readonly string[]>>): TypeEnvironment;
|
|
16
|
+
export declare function classifyUnsafeDictionaryValue(valueType: ESTree.TSType, environment: TypeEnvironment): UnsafeDictionary | null;
|
|
17
|
+
export declare function classifyUnsafeDictionary(type: ESTree.TSType, environment: TypeEnvironment): UnsafeDictionary | null;
|
|
18
|
+
export declare function classifyWideningTarget(type: ESTree.TSType, environment: TypeEnvironment): WideningTarget | null;
|
|
19
|
+
export declare function isKnownEvidenceExpression(expression: ESTree.Expression): boolean;
|