@danieljvdm/dev-kit 0.17.0 → 0.18.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/README.md +18 -7
- package/package.json +20 -9
- package/scripts/sync-anti-slop-runtime.mjs +19 -0
- package/skills/dev-kit/SKILL.md +4 -5
- package/src/bin/dev-kit.ts +11 -7
- package/src/catalog-manager.ts +23 -18
- package/src/catalog.ts +8 -5
- package/src/cli-ui.ts +2 -2
- package/src/effect-tsgo.ts +10 -6
- package/src/gitignore.ts +6 -3
- package/src/manifest.ts +10 -2
- package/src/oxlint-plugin-anti-slop/LICENSE +21 -0
- package/src/oxlint-plugin-anti-slop/index.ts +43 -0
- package/src/oxlint-plugin-anti-slop/rules/no-chained-type-assertions.ts +79 -0
- package/src/oxlint-plugin-anti-slop/rules/no-conditional-empty-object-spread.ts +51 -0
- package/src/oxlint-plugin-anti-slop/rules/no-known-value-widening.ts +267 -0
- package/src/oxlint-plugin-anti-slop/rules/no-module-mocking.ts +105 -0
- package/src/oxlint-plugin-anti-slop/rules/no-object-parameters.ts +141 -0
- package/src/oxlint-plugin-anti-slop/rules/no-reflect-apply.ts +28 -0
- package/src/oxlint-plugin-anti-slop/rules/no-reflect-get.ts +28 -0
- package/src/oxlint-plugin-anti-slop/rules/no-runtime-typeof.ts +25 -0
- package/src/oxlint-plugin-anti-slop/rules/no-shape-in-symbol-names.ts +38 -0
- package/src/oxlint-plugin-anti-slop/rules/no-unknown-parameters.ts +86 -0
- package/src/oxlint-plugin-anti-slop/rules/no-unknown-returns.ts +125 -0
- package/src/oxlint-plugin-anti-slop/rules/no-unknown-type-aliases.ts +73 -0
- package/src/oxlint-plugin-anti-slop/rules/no-unsafe-dictionary-type.ts +139 -0
- package/src/oxlint-plugin-anti-slop/rules/no-widen-then-assert.ts +396 -0
- package/src/oxlint-plugin-anti-slop/rules/require-safety-comment-for-type-assertion.ts +61 -0
- package/src/oxlint-plugin-anti-slop/runtime.d.ts +22 -0
- package/src/oxlint-plugin-anti-slop/runtime.js +1209 -0
- package/src/oxlint-plugin-anti-slop/shared/dictionary-types.ts +555 -0
- package/src/oxlint-plugin-anti-slop/shared/reflect-method.ts +40 -0
- package/src/oxlint-plugin-effect.d.ts +4 -2
- package/src/oxlint.js +19 -0
- package/src/oxlint.ts +22 -5
- package/src/package-skill-source.ts +17 -25
- package/src/path-digest.ts +2 -1
- package/src/project-package.ts +14 -12
- package/src/skill-manager.ts +25 -13
- package/src/sync.ts +65 -49
- package/src/vendor.ts +35 -16
|
@@ -0,0 +1,1209 @@
|
|
|
1
|
+
// Bundled from the vendored anti-slop source at commit 9b80d9a5c317d3af94d88a577bdbde4d9a45f7be.
|
|
2
|
+
import { definePlugin, defineRule } from "@oxlint/plugins";
|
|
3
|
+
function isTypeAssertionExpression(node) {
|
|
4
|
+
return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
|
|
5
|
+
}
|
|
6
|
+
function unwrapParenthesizedExpression(expression) {
|
|
7
|
+
let current = expression;
|
|
8
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
9
|
+
return current;
|
|
10
|
+
}
|
|
11
|
+
function isConstAssertion$1(node) {
|
|
12
|
+
const { typeAnnotation } = node;
|
|
13
|
+
return typeAnnotation.type === "TSTypeReference" && typeAnnotation.typeName.type === "Identifier" && typeAnnotation.typeName.name === "const";
|
|
14
|
+
}
|
|
15
|
+
function isOutermostAssertionInChain(node) {
|
|
16
|
+
let current = node;
|
|
17
|
+
let parent = node.parent;
|
|
18
|
+
while (parent.type === "ParenthesizedExpression" && parent.expression === current) {
|
|
19
|
+
current = parent;
|
|
20
|
+
parent = parent.parent;
|
|
21
|
+
}
|
|
22
|
+
return !isTypeAssertionExpression(parent) || parent.expression !== current;
|
|
23
|
+
}
|
|
24
|
+
function isForbiddenAssertionChain(node) {
|
|
25
|
+
let assertionCount = 0;
|
|
26
|
+
let hasNonConstAssertion = false;
|
|
27
|
+
let current = node;
|
|
28
|
+
while (isTypeAssertionExpression(current)) {
|
|
29
|
+
assertionCount += 1;
|
|
30
|
+
hasNonConstAssertion ||= !isConstAssertion$1(current);
|
|
31
|
+
current = unwrapParenthesizedExpression(current.expression);
|
|
32
|
+
}
|
|
33
|
+
return assertionCount > 1 && hasNonConstAssertion;
|
|
34
|
+
}
|
|
35
|
+
/** Disallow nested TypeScript type assertions, while permitting chains made only of const assertions. */
|
|
36
|
+
const noChainedTypeAssertionsRule = defineRule({
|
|
37
|
+
meta: {
|
|
38
|
+
type: "problem",
|
|
39
|
+
docs: { description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains." },
|
|
40
|
+
messages: { chained: "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it." }
|
|
41
|
+
},
|
|
42
|
+
create(context) {
|
|
43
|
+
const checkTypeAssertion = (node) => {
|
|
44
|
+
if (!isOutermostAssertionInChain(node) || !isForbiddenAssertionChain(node)) return;
|
|
45
|
+
context.report({
|
|
46
|
+
node,
|
|
47
|
+
messageId: "chained"
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
TSAsExpression: checkTypeAssertion,
|
|
52
|
+
TSTypeAssertion: checkTypeAssertion
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
function unwrapParentheses(node) {
|
|
57
|
+
let current = node;
|
|
58
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
59
|
+
return current;
|
|
60
|
+
}
|
|
61
|
+
function isEmptyObjectExpression$1(node) {
|
|
62
|
+
return node.type === "ObjectExpression" && node.properties.length === 0;
|
|
63
|
+
}
|
|
64
|
+
function isConditionalEmptyObjectSpread(node) {
|
|
65
|
+
const conditional = unwrapParentheses(node);
|
|
66
|
+
return conditional.type === "ConditionalExpression" && (isEmptyObjectExpression$1(conditional.consequent) || isEmptyObjectExpression$1(conditional.alternate));
|
|
67
|
+
}
|
|
68
|
+
/** Ban conditional empty-object spreads without changing their omission semantics. */
|
|
69
|
+
const noConditionalEmptyObjectSpreadRule = defineRule({
|
|
70
|
+
meta: {
|
|
71
|
+
type: "suggestion",
|
|
72
|
+
docs: { description: "Disallow object spreads that conditionally spread an empty object to omit fields." },
|
|
73
|
+
messages: { avoid: "This conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present." }
|
|
74
|
+
},
|
|
75
|
+
create(context) {
|
|
76
|
+
return { SpreadElement(node) {
|
|
77
|
+
if (node.parent.type !== "ObjectExpression") return;
|
|
78
|
+
if (isConditionalEmptyObjectSpread(node.argument)) context.report({
|
|
79
|
+
node,
|
|
80
|
+
messageId: "avoid"
|
|
81
|
+
});
|
|
82
|
+
} };
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
const BUILT_INS = /* @__PURE__ */ new Set([
|
|
86
|
+
"Record",
|
|
87
|
+
"Readonly",
|
|
88
|
+
"Partial",
|
|
89
|
+
"Required",
|
|
90
|
+
"Pick",
|
|
91
|
+
"Omit",
|
|
92
|
+
"PropertyKey",
|
|
93
|
+
"NonNullable"
|
|
94
|
+
]);
|
|
95
|
+
const TRANSPARENT_WRAPPERS = /* @__PURE__ */ new Set([
|
|
96
|
+
"Readonly",
|
|
97
|
+
"Partial",
|
|
98
|
+
"Required",
|
|
99
|
+
"NonNullable"
|
|
100
|
+
]);
|
|
101
|
+
function declaredStatement(statement) {
|
|
102
|
+
return statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? statement.declaration ?? null : statement;
|
|
103
|
+
}
|
|
104
|
+
function createTypeEnvironment(program) {
|
|
105
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
106
|
+
const interfaces = /* @__PURE__ */ new Map();
|
|
107
|
+
const shadowedBuiltIns = /* @__PURE__ */ new Set();
|
|
108
|
+
for (const statement of program.body) {
|
|
109
|
+
const declaration = declaredStatement(statement);
|
|
110
|
+
if (declaration?.type === "ImportDeclaration") {
|
|
111
|
+
for (const specifier of declaration.specifiers) if (BUILT_INS.has(specifier.local.name)) shadowedBuiltIns.add(specifier.local.name);
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
if (declaration?.type === "TSTypeAliasDeclaration") {
|
|
115
|
+
if (aliases.get(declaration.id.name) === void 0) aliases.set(declaration.id.name, declaration);
|
|
116
|
+
else shadowedBuiltIns.add(declaration.id.name);
|
|
117
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (declaration?.type === "TSInterfaceDeclaration") {
|
|
121
|
+
const declarations = interfaces.get(declaration.id.name) ?? [];
|
|
122
|
+
declarations.push(declaration);
|
|
123
|
+
interfaces.set(declaration.id.name, declarations);
|
|
124
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (declaration?.type === "TSEnumDeclaration") {
|
|
128
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
if ((declaration?.type === "ClassDeclaration" || declaration?.type === "FunctionDeclaration") && declaration.id !== null) {
|
|
132
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
aliases,
|
|
137
|
+
interfaces,
|
|
138
|
+
shadowedBuiltIns
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function typeReferenceName$2(type) {
|
|
142
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
143
|
+
}
|
|
144
|
+
function isBuiltIn(name, environment) {
|
|
145
|
+
return BUILT_INS.has(name) && !environment.shadowedBuiltIns.has(name);
|
|
146
|
+
}
|
|
147
|
+
function isUnappliedReferenceTo(type, name) {
|
|
148
|
+
const unwrapped = unwrapTransparentType(type);
|
|
149
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName$2(unwrapped) === name && (unwrapped.typeArguments === null || unwrapped.typeArguments === void 0 || unwrapped.typeArguments.params.length === 0);
|
|
150
|
+
}
|
|
151
|
+
function unwrapTransparentType(type) {
|
|
152
|
+
let current = type;
|
|
153
|
+
while (current.type === "TSParenthesizedType" || current.type === "TSTypeOperator" && current.operator === "readonly") current = current.typeAnnotation;
|
|
154
|
+
return current;
|
|
155
|
+
}
|
|
156
|
+
function isNeverType(type) {
|
|
157
|
+
return unwrapTransparentType(type).type === "TSNeverKeyword";
|
|
158
|
+
}
|
|
159
|
+
function isEffectivelyEmptyMember(member) {
|
|
160
|
+
return member.type === "TSPropertySignature" && member.optional === true && member.typeAnnotation !== null && member.typeAnnotation !== void 0 && isNeverType(member.typeAnnotation.typeAnnotation);
|
|
161
|
+
}
|
|
162
|
+
function isEffectivelyEmptyTypeLiteral(type) {
|
|
163
|
+
return type.members.length === 0 || type.members.every(isEffectivelyEmptyMember);
|
|
164
|
+
}
|
|
165
|
+
function isEffectivelyEmptyInterface(declarations) {
|
|
166
|
+
if (declarations.length !== 1) return false;
|
|
167
|
+
const [type] = declarations;
|
|
168
|
+
return type !== void 0 && type.extends.length === 0 && (type.body.body.length === 0 || type.body.body.every(isEffectivelyEmptyMember));
|
|
169
|
+
}
|
|
170
|
+
function resolvedSubstitutionArgument(type, base, resolving = /* @__PURE__ */ new Set()) {
|
|
171
|
+
const unwrapped = unwrapTransparentType(type);
|
|
172
|
+
if (unwrapped.type !== "TSTypeReference") return type;
|
|
173
|
+
const name = typeReferenceName$2(unwrapped);
|
|
174
|
+
if (name === null || resolving.has(name)) return type;
|
|
175
|
+
const substitution = base.get(name);
|
|
176
|
+
if (substitution === void 0) return type;
|
|
177
|
+
const nextResolving = new Set(resolving);
|
|
178
|
+
nextResolving.add(name);
|
|
179
|
+
return resolvedSubstitutionArgument(substitution, base, nextResolving);
|
|
180
|
+
}
|
|
181
|
+
function aliasSubstitution(alias, type, base) {
|
|
182
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
183
|
+
const arguments_ = type.typeArguments?.params ?? [];
|
|
184
|
+
const next = new Map(base);
|
|
185
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
186
|
+
const argument = arguments_[index] ?? parameter.default;
|
|
187
|
+
if (argument === null || argument === void 0) return null;
|
|
188
|
+
next.set(parameter.name.name, resolvedSubstitutionArgument(argument, next));
|
|
189
|
+
}
|
|
190
|
+
return next;
|
|
191
|
+
}
|
|
192
|
+
function unsafeDirectValue(type, environment, substitutions, resolvingAliases) {
|
|
193
|
+
const unwrapped = unwrapTransparentType(type);
|
|
194
|
+
if (unwrapped.type === "TSUnknownKeyword") return "unknown";
|
|
195
|
+
if (unwrapped.type === "TSAnyKeyword") return "any";
|
|
196
|
+
if (unwrapped.type === "TSObjectKeyword") return "object";
|
|
197
|
+
if (unwrapped.type === "TSTypeLiteral" && isEffectivelyEmptyTypeLiteral(unwrapped)) return "empty-object";
|
|
198
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.some((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases) !== null) ? "union" : null;
|
|
199
|
+
if (unwrapped.type === "TSIntersectionType") {
|
|
200
|
+
const unsafeMembers = unwrapped.types.map((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases));
|
|
201
|
+
if (unsafeMembers.includes("any")) return "any";
|
|
202
|
+
return unsafeMembers.length > 0 && unsafeMembers.every((member) => member !== null) ? unsafeMembers[0] ?? null : null;
|
|
203
|
+
}
|
|
204
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
205
|
+
const name = typeReferenceName$2(unwrapped);
|
|
206
|
+
if (name === null) return null;
|
|
207
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, environment)) {
|
|
208
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
209
|
+
return wrapped === void 0 ? null : unsafeDirectValue(wrapped, environment, substitutions, resolvingAliases);
|
|
210
|
+
}
|
|
211
|
+
const substitution = substitutions.get(name);
|
|
212
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? null : unsafeDirectValue(substitution, environment, substitutions, resolvingAliases);
|
|
213
|
+
const interfaceDeclarations = environment.interfaces.get(name);
|
|
214
|
+
if (interfaceDeclarations !== void 0) return isEffectivelyEmptyInterface(interfaceDeclarations) ? "empty-object" : null;
|
|
215
|
+
const alias = environment.aliases.get(name);
|
|
216
|
+
if (alias === void 0 || resolvingAliases.has(name)) return null;
|
|
217
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
218
|
+
if (nextSubstitutions === null) return null;
|
|
219
|
+
const nextResolving = new Set(resolvingAliases);
|
|
220
|
+
nextResolving.add(name);
|
|
221
|
+
return unsafeDirectValue(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
222
|
+
}
|
|
223
|
+
function dictionaryValueTypes(type, environment, substitutions, resolvingAliases) {
|
|
224
|
+
const unwrapped = unwrapTransparentType(type);
|
|
225
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.flatMap((member) => member.type === "TSIndexSignature" && member.typeAnnotation !== null ? [{
|
|
226
|
+
type: member.typeAnnotation.typeAnnotation,
|
|
227
|
+
substitutions
|
|
228
|
+
}] : []);
|
|
229
|
+
if (unwrapped.type === "TSMappedType") return unwrapped.typeAnnotation === null ? [] : [{
|
|
230
|
+
type: unwrapped.typeAnnotation,
|
|
231
|
+
substitutions
|
|
232
|
+
}];
|
|
233
|
+
if (unwrapped.type !== "TSTypeReference") return [];
|
|
234
|
+
const name = typeReferenceName$2(unwrapped);
|
|
235
|
+
if (name === null) return [];
|
|
236
|
+
const substitution = substitutions.get(name);
|
|
237
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? [] : dictionaryValueTypes(substitution, environment, substitutions, resolvingAliases);
|
|
238
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, environment)) {
|
|
239
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
240
|
+
return wrapped === void 0 ? [] : dictionaryValueTypes(wrapped, environment, substitutions, resolvingAliases);
|
|
241
|
+
}
|
|
242
|
+
if (name === "Record" && isBuiltIn(name, environment)) {
|
|
243
|
+
const value = unwrapped.typeArguments?.params[1] ?? null;
|
|
244
|
+
return value === null ? [] : [{
|
|
245
|
+
type: value,
|
|
246
|
+
substitutions
|
|
247
|
+
}];
|
|
248
|
+
}
|
|
249
|
+
if ((name === "Pick" || name === "Omit") && isBuiltIn(name, environment)) {
|
|
250
|
+
const source = unwrapped.typeArguments?.params[0];
|
|
251
|
+
return source === void 0 ? [] : dictionaryValueTypes(source, environment, substitutions, resolvingAliases);
|
|
252
|
+
}
|
|
253
|
+
const alias = environment.aliases.get(name);
|
|
254
|
+
if (alias === void 0 || resolvingAliases.has(name)) return [];
|
|
255
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
256
|
+
if (nextSubstitutions === null) return [];
|
|
257
|
+
const nextResolving = new Set(resolvingAliases);
|
|
258
|
+
nextResolving.add(name);
|
|
259
|
+
return dictionaryValueTypes(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
260
|
+
}
|
|
261
|
+
function classifyUnsafeDictionaryValue(valueType, environment) {
|
|
262
|
+
const unsafeValue = unsafeDirectValue(valueType, environment, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set());
|
|
263
|
+
return unsafeValue === null ? null : {
|
|
264
|
+
kind: "unsafe-dictionary",
|
|
265
|
+
unsafeValue
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function classifyUnsafeDictionary(type, environment) {
|
|
269
|
+
for (const valueType of dictionaryValueTypes(type, environment, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set())) {
|
|
270
|
+
const unsafeValue = unsafeDirectValue(valueType.type, environment, valueType.substitutions, /* @__PURE__ */ new Set());
|
|
271
|
+
if (unsafeValue !== null) return {
|
|
272
|
+
kind: "unsafe-dictionary",
|
|
273
|
+
unsafeValue
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
return null;
|
|
277
|
+
}
|
|
278
|
+
function resolvesToDictionary(type, environment, substitutions, resolvingAliases) {
|
|
279
|
+
return dictionaryValueTypes(type, environment, substitutions, resolvingAliases).length > 0;
|
|
280
|
+
}
|
|
281
|
+
function classifyWideningTarget(type, environment) {
|
|
282
|
+
const unwrapped = unwrapTransparentType(type);
|
|
283
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
284
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
285
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : unwrapped.members.length > 0 ? { kind: "anonymous object" } : null;
|
|
286
|
+
if (unwrapped.type === "TSMappedType") return { kind: "open dictionary" };
|
|
287
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
288
|
+
const name = typeReferenceName$2(unwrapped);
|
|
289
|
+
if (name === null) return null;
|
|
290
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, environment)) {
|
|
291
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
292
|
+
return wrapped === void 0 ? null : classifyWideningTarget(wrapped, environment);
|
|
293
|
+
}
|
|
294
|
+
if (name === "Record" && isBuiltIn(name, environment)) return { kind: "open dictionary" };
|
|
295
|
+
const alias = environment.aliases.get(name);
|
|
296
|
+
if (alias === void 0) return null;
|
|
297
|
+
if ((alias.typeParameters?.params.length ?? 0) > 0) {
|
|
298
|
+
const substitutions = aliasSubstitution(alias, unwrapped, /* @__PURE__ */ new Map());
|
|
299
|
+
return substitutions !== null && resolvesToDictionary(alias.typeAnnotation, environment, substitutions, /* @__PURE__ */ new Set([name])) ? { kind: "generic container" } : null;
|
|
300
|
+
}
|
|
301
|
+
const substitutions = aliasSubstitution(alias, unwrapped, /* @__PURE__ */ new Map());
|
|
302
|
+
if (substitutions === null) return null;
|
|
303
|
+
return classifyAliasBroadTarget(alias.typeAnnotation, environment, substitutions, /* @__PURE__ */ new Set([name]));
|
|
304
|
+
}
|
|
305
|
+
function isBroadMappedKey(type, environment, substitutions) {
|
|
306
|
+
const unwrapped = unwrapTransparentType(type);
|
|
307
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") return true;
|
|
308
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.every((member) => isBroadMappedKey(member, environment, substitutions));
|
|
309
|
+
if (unwrapped.type !== "TSTypeReference") return false;
|
|
310
|
+
const name = typeReferenceName$2(unwrapped);
|
|
311
|
+
if (name === null) return false;
|
|
312
|
+
const substitution = substitutions.get(name);
|
|
313
|
+
if (substitution !== void 0 && !isUnappliedReferenceTo(substitution, name)) return isBroadMappedKey(substitution, environment, substitutions);
|
|
314
|
+
return name === "PropertyKey" && isBuiltIn(name, environment);
|
|
315
|
+
}
|
|
316
|
+
function classifyAliasBroadTarget(type, environment, substitutions, resolvingAliases) {
|
|
317
|
+
const unwrapped = unwrapTransparentType(type);
|
|
318
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
319
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
320
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : null;
|
|
321
|
+
if (unwrapped.type === "TSMappedType") return isBroadMappedKey(unwrapped.constraint, environment, substitutions) ? { kind: "open dictionary" } : null;
|
|
322
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
323
|
+
const name = typeReferenceName$2(unwrapped);
|
|
324
|
+
if (name === null) return null;
|
|
325
|
+
const substitution = substitutions.get(name);
|
|
326
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? null : classifyAliasBroadTarget(substitution, environment, substitutions, resolvingAliases);
|
|
327
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, environment)) {
|
|
328
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
329
|
+
return wrapped === void 0 ? null : classifyAliasBroadTarget(wrapped, environment, substitutions, resolvingAliases);
|
|
330
|
+
}
|
|
331
|
+
if (name === "Record" && isBuiltIn(name, environment)) return { kind: "open dictionary" };
|
|
332
|
+
const alias = environment.aliases.get(name);
|
|
333
|
+
if (alias === void 0 || resolvingAliases.has(name)) return null;
|
|
334
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
335
|
+
if (nextSubstitutions === null) return null;
|
|
336
|
+
const nextResolving = new Set(resolvingAliases);
|
|
337
|
+
nextResolving.add(name);
|
|
338
|
+
return classifyAliasBroadTarget(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
339
|
+
}
|
|
340
|
+
function isKnownEvidenceExpression(expression) {
|
|
341
|
+
let current = expression;
|
|
342
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression") current = current.expression;
|
|
343
|
+
if (current.type === "ObjectExpression") return true;
|
|
344
|
+
return current.type === "ArrayExpression" || current.type === "ArrowFunctionExpression" || current.type === "ClassExpression" || current.type === "FunctionExpression" || current.type === "NewExpression" || current.type === "Literal" || current.type === "TemplateLiteral" || current.type === "UnaryExpression";
|
|
345
|
+
}
|
|
346
|
+
function unwrapExpression(expression) {
|
|
347
|
+
let current = expression;
|
|
348
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression") current = current.expression;
|
|
349
|
+
return current;
|
|
350
|
+
}
|
|
351
|
+
function resolveVariable$2(sourceCode, identifier) {
|
|
352
|
+
let scope = sourceCode.getScope(identifier);
|
|
353
|
+
while (scope !== null) {
|
|
354
|
+
const variable = scope.set.get(identifier.name);
|
|
355
|
+
if (variable !== void 0) return variable;
|
|
356
|
+
scope = scope.upper;
|
|
357
|
+
}
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
function variableDeclarator$1(variable) {
|
|
361
|
+
if (variable.defs.length !== 1) return null;
|
|
362
|
+
const [definition] = variable.defs;
|
|
363
|
+
return definition?.type === "Variable" && definition.node.type === "VariableDeclarator" ? definition.node : null;
|
|
364
|
+
}
|
|
365
|
+
function isStableConstVariable(variable, declarator) {
|
|
366
|
+
return declarator.parent.type === "VariableDeclaration" && declarator.parent.kind === "const" && variable.references.every((reference) => reference.init || !reference.isWrite());
|
|
367
|
+
}
|
|
368
|
+
function hasKnownEvidence(sourceCode, expression, visitedVariables = /* @__PURE__ */ new Set()) {
|
|
369
|
+
if (isKnownEvidenceExpression(expression)) return true;
|
|
370
|
+
const unwrapped = unwrapExpression(expression);
|
|
371
|
+
if (unwrapped.type !== "Identifier") return false;
|
|
372
|
+
const variable = resolveVariable$2(sourceCode, unwrapped);
|
|
373
|
+
if (variable === null || visitedVariables.has(variable)) return false;
|
|
374
|
+
const declarator = variableDeclarator$1(variable);
|
|
375
|
+
if (declarator === null || declarator.init === null || !isStableConstVariable(variable, declarator)) return false;
|
|
376
|
+
visitedVariables.add(variable);
|
|
377
|
+
return hasKnownEvidence(sourceCode, declarator.init, visitedVariables);
|
|
378
|
+
}
|
|
379
|
+
function annotationTarget(annotation, environment) {
|
|
380
|
+
return annotation === null || annotation === void 0 ? null : classifyWideningTarget(annotation.typeAnnotation, environment);
|
|
381
|
+
}
|
|
382
|
+
function enclosingFunction(node) {
|
|
383
|
+
let current = node.parent;
|
|
384
|
+
while (current !== null && current.type !== "Program") {
|
|
385
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionDeclaration" || current.type === "FunctionExpression") return current;
|
|
386
|
+
current = current.parent;
|
|
387
|
+
}
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
function sourceKeyName(sourceCode, key) {
|
|
391
|
+
if (key.type === "Identifier" || key.type === "PrivateIdentifier") return key.name;
|
|
392
|
+
if (key.type === "Literal") return String(key.value);
|
|
393
|
+
return sourceCode.getText(key);
|
|
394
|
+
}
|
|
395
|
+
function functionName(sourceCode, owner) {
|
|
396
|
+
if (owner === null) return "anonymous function";
|
|
397
|
+
if (owner.id !== null) return owner.id.name;
|
|
398
|
+
const parent = owner.parent;
|
|
399
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier") return parent.id.name;
|
|
400
|
+
if (parent.type === "MethodDefinition") return sourceKeyName(sourceCode, parent.key);
|
|
401
|
+
return "anonymous function";
|
|
402
|
+
}
|
|
403
|
+
function isEmptyObjectExpression(expression) {
|
|
404
|
+
const unwrapped = unwrapExpression(expression);
|
|
405
|
+
return unwrapped.type === "ObjectExpression" && unwrapped.properties.length === 0;
|
|
406
|
+
}
|
|
407
|
+
function isDictionaryAccumulatorTarget(destination) {
|
|
408
|
+
return destination.kind === "open dictionary" || destination.kind === "generic container";
|
|
409
|
+
}
|
|
410
|
+
function hasParentAssertion(node) {
|
|
411
|
+
return node.parent?.type === "TSAsExpression" || node.parent?.type === "TSTypeAssertion";
|
|
412
|
+
}
|
|
413
|
+
/** Detect sound syntactic cases where a known value is explicitly widened and loses evidence. */
|
|
414
|
+
const noKnownValueWideningRule = defineRule({
|
|
415
|
+
meta: {
|
|
416
|
+
type: "problem",
|
|
417
|
+
docs: { description: "Disallow syntactically established values from flowing into explicitly broad or anonymous target types that discard useful evidence." },
|
|
418
|
+
messages: { widening: "The explicit {{target}} type on {{subject}} discards known type evidence. Keep inference, validate with `satisfies`, or use a named owner contract." }
|
|
419
|
+
},
|
|
420
|
+
createOnce(context) {
|
|
421
|
+
let environment = null;
|
|
422
|
+
const reportFlow = (expression, destination, subject) => {
|
|
423
|
+
if (destination === null) return;
|
|
424
|
+
if (isDictionaryAccumulatorTarget(destination) && isEmptyObjectExpression(expression)) return;
|
|
425
|
+
if (!hasKnownEvidence(context.sourceCode, expression)) return;
|
|
426
|
+
context.report({
|
|
427
|
+
node: expression,
|
|
428
|
+
messageId: "widening",
|
|
429
|
+
data: {
|
|
430
|
+
subject,
|
|
431
|
+
target: destination.kind
|
|
432
|
+
}
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
const targetFromAnnotation = (annotation) => environment === null ? null : annotationTarget(annotation, environment);
|
|
436
|
+
return {
|
|
437
|
+
Program(node) {
|
|
438
|
+
environment = createTypeEnvironment(node);
|
|
439
|
+
},
|
|
440
|
+
VariableDeclarator(node) {
|
|
441
|
+
if (node.init === null || node.id.type !== "Identifier") return;
|
|
442
|
+
reportFlow(node.init, targetFromAnnotation(node.id.typeAnnotation), `binding \`${node.id.name}\``);
|
|
443
|
+
},
|
|
444
|
+
PropertyDefinition(node) {
|
|
445
|
+
if (node.value === null) return;
|
|
446
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
447
|
+
},
|
|
448
|
+
AccessorProperty(node) {
|
|
449
|
+
if (node.value === null) return;
|
|
450
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
451
|
+
},
|
|
452
|
+
AssignmentExpression(node) {
|
|
453
|
+
if (node.operator !== "=" || node.left.type !== "Identifier") return;
|
|
454
|
+
const variable = resolveVariable$2(context.sourceCode, node.left);
|
|
455
|
+
if (variable === null) return;
|
|
456
|
+
const declarator = variableDeclarator$1(variable);
|
|
457
|
+
if (declarator === null || declarator.id.type !== "Identifier") return;
|
|
458
|
+
reportFlow(node.right, targetFromAnnotation(declarator.id.typeAnnotation), `binding \`${declarator.id.name}\``);
|
|
459
|
+
},
|
|
460
|
+
ReturnStatement(node) {
|
|
461
|
+
if (node.argument === null) return;
|
|
462
|
+
const owner = enclosingFunction(node);
|
|
463
|
+
reportFlow(node.argument, targetFromAnnotation(owner?.returnType), `return value of \`${functionName(context.sourceCode, owner)}\``);
|
|
464
|
+
},
|
|
465
|
+
ArrowFunctionExpression(node) {
|
|
466
|
+
if (node.body.type === "BlockStatement") return;
|
|
467
|
+
reportFlow(node.body, targetFromAnnotation(node.returnType), `return value of \`${functionName(context.sourceCode, node)}\``);
|
|
468
|
+
},
|
|
469
|
+
TSAsExpression(node) {
|
|
470
|
+
if (environment === null || hasParentAssertion(node)) return;
|
|
471
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
472
|
+
},
|
|
473
|
+
TSTypeAssertion(node) {
|
|
474
|
+
if (environment === null || hasParentAssertion(node)) return;
|
|
475
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
});
|
|
480
|
+
const moduleMockMethods = /* @__PURE__ */ new Set([
|
|
481
|
+
"doMock",
|
|
482
|
+
"mock",
|
|
483
|
+
"unstable_mockModule"
|
|
484
|
+
]);
|
|
485
|
+
function resolveVariable$1(sourceCode, identifier) {
|
|
486
|
+
let scope = sourceCode.getScope(identifier);
|
|
487
|
+
while (scope !== null) {
|
|
488
|
+
const variable = scope.set.get(identifier.name);
|
|
489
|
+
if (variable !== void 0) return variable;
|
|
490
|
+
scope = scope.upper;
|
|
491
|
+
}
|
|
492
|
+
return null;
|
|
493
|
+
}
|
|
494
|
+
function importedName(node) {
|
|
495
|
+
if (node.type !== "ImportSpecifier") return null;
|
|
496
|
+
return node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
497
|
+
}
|
|
498
|
+
function isTestFrameworkObject(sourceCode, expression) {
|
|
499
|
+
if (expression.type !== "Identifier") return false;
|
|
500
|
+
if ((expression.name === "vi" || expression.name === "jest") && sourceCode.isGlobalReference(expression)) return true;
|
|
501
|
+
const variable = resolveVariable$1(sourceCode, expression);
|
|
502
|
+
if (variable === null || variable.defs.length === 0) return expression.name === "vi" || expression.name === "jest";
|
|
503
|
+
return variable.defs.some((definition) => {
|
|
504
|
+
if (definition.type !== "ImportBinding" || definition.parent?.type !== "ImportDeclaration") return false;
|
|
505
|
+
const source = definition.parent.source.value;
|
|
506
|
+
const name = importedName(definition.node);
|
|
507
|
+
return source === "vitest" && name === "vi" || source === "@jest/globals" && name === "jest";
|
|
508
|
+
});
|
|
509
|
+
}
|
|
510
|
+
function moduleMockCall(sourceCode, callee) {
|
|
511
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
512
|
+
if (!isTestFrameworkObject(sourceCode, callee.object)) return false;
|
|
513
|
+
const property = callee.property;
|
|
514
|
+
const method = callee.computed ? property.type === "Literal" && (property.value === "doMock" || property.value === "mock" || property.value === "unstable_mockModule") ? property.value : null : property.type === "Identifier" ? property.name : null;
|
|
515
|
+
return method !== null && moduleMockMethods.has(method);
|
|
516
|
+
}
|
|
517
|
+
/** Ban test framework module mocking in favor of real dependency seams. */
|
|
518
|
+
const noModuleMockingRule = defineRule({
|
|
519
|
+
meta: {
|
|
520
|
+
type: "problem",
|
|
521
|
+
docs: { description: "Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces." },
|
|
522
|
+
messages: { moduleMock: "Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation." }
|
|
523
|
+
},
|
|
524
|
+
create(context) {
|
|
525
|
+
return { CallExpression(node) {
|
|
526
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
527
|
+
if (moduleMockCall(context.sourceCode, node.callee)) context.report({
|
|
528
|
+
node,
|
|
529
|
+
messageId: "moduleMock"
|
|
530
|
+
});
|
|
531
|
+
} };
|
|
532
|
+
}
|
|
533
|
+
});
|
|
534
|
+
function parameterAnnotation$1(parameter) {
|
|
535
|
+
if (parameter.type === "TSParameterProperty") return parameterAnnotation$1(parameter.parameter);
|
|
536
|
+
if (parameter.type === "RestElement") return parameter.typeAnnotation ?? parameterAnnotation$1(parameter.argument);
|
|
537
|
+
if (parameter.type === "AssignmentPattern") return parameter.typeAnnotation ?? parameter.left.typeAnnotation;
|
|
538
|
+
return parameter.typeAnnotation;
|
|
539
|
+
}
|
|
540
|
+
function parameterName$1(parameter, sourceCode) {
|
|
541
|
+
return parameter.type === "Identifier" ? parameter.name : sourceCode.getText(parameter).replace(/\s*:\s*object\s*$/u, "");
|
|
542
|
+
}
|
|
543
|
+
function lexicalTypeParameterNames$1(node) {
|
|
544
|
+
const names = /* @__PURE__ */ new Set();
|
|
545
|
+
let current = node;
|
|
546
|
+
while (current !== null && current.type !== "Program") {
|
|
547
|
+
if ("typeParameters" in current) for (const parameter of current.typeParameters?.params ?? []) names.add(parameter.name.name);
|
|
548
|
+
if (current.type === "TSMappedType") names.add(current.key.name);
|
|
549
|
+
if (current.type === "TSInferType") names.add(current.typeParameter.name.name);
|
|
550
|
+
current = current.parent;
|
|
551
|
+
}
|
|
552
|
+
return names;
|
|
553
|
+
}
|
|
554
|
+
/** Ban the broad object type on function inputs, including local aliases to object. */
|
|
555
|
+
const noObjectParametersRule = defineRule({
|
|
556
|
+
meta: {
|
|
557
|
+
type: "problem",
|
|
558
|
+
docs: { description: "Disallow object function parameters; inputs must use an owner-provided type and be parsed at their boundary." },
|
|
559
|
+
messages: { objectParameter: "Parameter `{{parameter}}` uses the broad `object` type. Accept a named owner type; parse external input at its boundary before calling this function." }
|
|
560
|
+
},
|
|
561
|
+
create(context) {
|
|
562
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
563
|
+
const resolvesToObject = (type, shadowedAliases, visited = /* @__PURE__ */ new Set()) => {
|
|
564
|
+
if (type.type === "TSObjectKeyword") return true;
|
|
565
|
+
if (type.type === "TSParenthesizedType") return resolvesToObject(type.typeAnnotation, shadowedAliases, visited);
|
|
566
|
+
if (type.type === "TSUnionType") return type.types.some((member) => resolvesToObject(member, shadowedAliases, visited));
|
|
567
|
+
if (type.type !== "TSTypeReference" || type.typeName.type !== "Identifier" || type.typeArguments !== null && type.typeArguments !== void 0 && type.typeArguments.params.length > 0 || visited.has(type.typeName.name) || shadowedAliases.has(type.typeName.name)) return false;
|
|
568
|
+
const alias = aliases.get(type.typeName.name);
|
|
569
|
+
if (alias === void 0) return false;
|
|
570
|
+
const nextVisited = new Set(visited);
|
|
571
|
+
nextVisited.add(type.typeName.name);
|
|
572
|
+
return resolvesToObject(alias, shadowedAliases, nextVisited);
|
|
573
|
+
};
|
|
574
|
+
const checkParameters = (node) => {
|
|
575
|
+
const shadowedAliases = lexicalTypeParameterNames$1(node);
|
|
576
|
+
for (const parameter of node.params) {
|
|
577
|
+
const annotation = parameterAnnotation$1(parameter);
|
|
578
|
+
if (annotation === null || annotation === void 0) continue;
|
|
579
|
+
if (!resolvesToObject(annotation.typeAnnotation, shadowedAliases)) continue;
|
|
580
|
+
context.report({
|
|
581
|
+
node: annotation.typeAnnotation,
|
|
582
|
+
messageId: "objectParameter",
|
|
583
|
+
data: { parameter: parameterName$1(parameter, context.sourceCode) }
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
};
|
|
587
|
+
return {
|
|
588
|
+
Program(node) {
|
|
589
|
+
for (const statement of node.body) {
|
|
590
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
591
|
+
if (declaration?.type === "TSTypeAliasDeclaration" && (declaration.typeParameters === null || declaration.typeParameters === void 0)) aliases.set(declaration.id.name, declaration.typeAnnotation);
|
|
592
|
+
}
|
|
593
|
+
},
|
|
594
|
+
ArrowFunctionExpression: checkParameters,
|
|
595
|
+
FunctionDeclaration: checkParameters,
|
|
596
|
+
FunctionExpression: checkParameters,
|
|
597
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
598
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
599
|
+
TSConstructorType: checkParameters,
|
|
600
|
+
TSDeclareFunction: checkParameters,
|
|
601
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
602
|
+
TSFunctionType: checkParameters,
|
|
603
|
+
TSMethodSignature: checkParameters
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
function resolveVariable(sourceCode, identifier) {
|
|
608
|
+
let scope = sourceCode.getScope(identifier);
|
|
609
|
+
while (scope !== null) {
|
|
610
|
+
const variable = scope.set.get(identifier.name);
|
|
611
|
+
if (variable !== void 0) return variable;
|
|
612
|
+
scope = scope.upper;
|
|
613
|
+
}
|
|
614
|
+
return null;
|
|
615
|
+
}
|
|
616
|
+
function isGlobalReflect(sourceCode, expression) {
|
|
617
|
+
if (expression.type !== "Identifier" || expression.name !== "Reflect") return false;
|
|
618
|
+
if (sourceCode.isGlobalReference(expression)) return true;
|
|
619
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
620
|
+
return variable === null || variable.defs.length === 0;
|
|
621
|
+
}
|
|
622
|
+
/** Reports whether a call target names one method on the global Reflect object. */
|
|
623
|
+
function isGlobalReflectMethodCall(sourceCode, callee, methodName) {
|
|
624
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
625
|
+
if (!isGlobalReflect(sourceCode, callee.object)) return false;
|
|
626
|
+
const property = callee.property;
|
|
627
|
+
return callee.computed ? property.type === "Literal" && property.value === methodName : property.type === "Identifier" && property.name === methodName;
|
|
628
|
+
}
|
|
629
|
+
/** Ban Reflect.apply, which bypasses ordinary typed function calls. */
|
|
630
|
+
const noReflectApplyRule = defineRule({
|
|
631
|
+
meta: {
|
|
632
|
+
type: "problem",
|
|
633
|
+
docs: { description: "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface." },
|
|
634
|
+
messages: { reflectApply: "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface." }
|
|
635
|
+
},
|
|
636
|
+
create(context) {
|
|
637
|
+
return { CallExpression(node) {
|
|
638
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
639
|
+
if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "apply")) context.report({
|
|
640
|
+
node,
|
|
641
|
+
messageId: "reflectApply"
|
|
642
|
+
});
|
|
643
|
+
} };
|
|
644
|
+
}
|
|
645
|
+
});
|
|
646
|
+
/** Ban Reflect.get, which bypasses ordinary property access and useful type evidence. */
|
|
647
|
+
const noReflectGetRule = defineRule({
|
|
648
|
+
meta: {
|
|
649
|
+
type: "problem",
|
|
650
|
+
docs: { description: "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type." },
|
|
651
|
+
messages: { reflectGet: "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it." }
|
|
652
|
+
},
|
|
653
|
+
create(context) {
|
|
654
|
+
return { CallExpression(node) {
|
|
655
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
656
|
+
if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "get")) context.report({
|
|
657
|
+
node,
|
|
658
|
+
messageId: "reflectGet"
|
|
659
|
+
});
|
|
660
|
+
} };
|
|
661
|
+
}
|
|
662
|
+
});
|
|
663
|
+
/** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
|
|
664
|
+
const noRuntimeTypeofRule = defineRule({
|
|
665
|
+
meta: {
|
|
666
|
+
type: "problem",
|
|
667
|
+
docs: { description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary." },
|
|
668
|
+
messages: { runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value." }
|
|
669
|
+
},
|
|
670
|
+
create(context) {
|
|
671
|
+
return { UnaryExpression(node) {
|
|
672
|
+
if (node.operator === "typeof") context.report({
|
|
673
|
+
node,
|
|
674
|
+
messageId: "runtimeTypeof"
|
|
675
|
+
});
|
|
676
|
+
} };
|
|
677
|
+
}
|
|
678
|
+
});
|
|
679
|
+
const FORBIDDEN_SYMBOL_NAME = "shape";
|
|
680
|
+
function containsForbiddenSymbolName(name) {
|
|
681
|
+
return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
|
|
682
|
+
}
|
|
683
|
+
/** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
|
|
684
|
+
const noForbiddenTermInSymbolNamesRule = defineRule({
|
|
685
|
+
meta: {
|
|
686
|
+
type: "problem",
|
|
687
|
+
docs: { description: "Disallow the case-insensitive substring \"shape\" in JavaScript, TypeScript, private, and JSX symbol names." },
|
|
688
|
+
messages: { forbiddenSymbolName: "Rename symbol \"{{name}}\" for its domain role; \"shape\" describes structure rather than ownership." }
|
|
689
|
+
},
|
|
690
|
+
create(context) {
|
|
691
|
+
const reportForbiddenSymbolName = (node) => {
|
|
692
|
+
if (!containsForbiddenSymbolName(node.name)) return;
|
|
693
|
+
context.report({
|
|
694
|
+
node,
|
|
695
|
+
messageId: "forbiddenSymbolName",
|
|
696
|
+
data: { name: node.name }
|
|
697
|
+
});
|
|
698
|
+
};
|
|
699
|
+
return {
|
|
700
|
+
Identifier: reportForbiddenSymbolName,
|
|
701
|
+
PrivateIdentifier: reportForbiddenSymbolName,
|
|
702
|
+
JSXIdentifier: reportForbiddenSymbolName
|
|
703
|
+
};
|
|
704
|
+
}
|
|
705
|
+
});
|
|
706
|
+
function parameterAnnotation(parameter) {
|
|
707
|
+
if (parameter.type === "TSParameterProperty") return parameterAnnotation(parameter.parameter);
|
|
708
|
+
if (parameter.type === "RestElement") return parameter.typeAnnotation ?? parameterAnnotation(parameter.argument);
|
|
709
|
+
if (parameter.type === "AssignmentPattern") return parameter.typeAnnotation ?? parameter.left.typeAnnotation;
|
|
710
|
+
return parameter.typeAnnotation;
|
|
711
|
+
}
|
|
712
|
+
function parameterName(parameter, sourceText) {
|
|
713
|
+
if (parameter.type === "TSParameterProperty") return parameterName(parameter.parameter, sourceText);
|
|
714
|
+
if (parameter.type === "AssignmentPattern") return parameterName(parameter.left, sourceText);
|
|
715
|
+
if (parameter.type === "RestElement") return parameterName(parameter.argument, sourceText);
|
|
716
|
+
return parameter.type === "Identifier" ? parameter.name : sourceText.replace(/\s*:\s*unknown\s*$/u, "");
|
|
717
|
+
}
|
|
718
|
+
/** Disallow unknown inputs except explicitly named error-cause enrichment. */
|
|
719
|
+
const noUnknownParametersRule = defineRule({
|
|
720
|
+
meta: {
|
|
721
|
+
type: "problem",
|
|
722
|
+
docs: { description: "Disallow explicitly unknown function parameters except `cause`; decode unknown input at its I/O boundary instead." },
|
|
723
|
+
messages: { 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." }
|
|
724
|
+
},
|
|
725
|
+
create(context) {
|
|
726
|
+
const checkParameters = (node) => {
|
|
727
|
+
for (const parameter of node.params) {
|
|
728
|
+
const annotation = parameterAnnotation(parameter);
|
|
729
|
+
if (annotation?.typeAnnotation.type !== "TSUnknownKeyword") continue;
|
|
730
|
+
const name = parameterName(parameter, context.sourceCode.getText(parameter));
|
|
731
|
+
if (name === "cause") continue;
|
|
732
|
+
context.report({
|
|
733
|
+
node: annotation.typeAnnotation,
|
|
734
|
+
messageId: "unknownParameter",
|
|
735
|
+
data: { parameter: name }
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
};
|
|
739
|
+
return {
|
|
740
|
+
ArrowFunctionExpression: checkParameters,
|
|
741
|
+
FunctionDeclaration: checkParameters,
|
|
742
|
+
FunctionExpression: checkParameters,
|
|
743
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
744
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
745
|
+
TSConstructorType: checkParameters,
|
|
746
|
+
TSDeclareFunction: checkParameters,
|
|
747
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
748
|
+
TSFunctionType: checkParameters,
|
|
749
|
+
TSMethodSignature: checkParameters
|
|
750
|
+
};
|
|
751
|
+
}
|
|
752
|
+
});
|
|
753
|
+
function referencedAliasName$1(type) {
|
|
754
|
+
if (type.type === "TSParenthesizedType") return referencedAliasName$1(type.typeAnnotation);
|
|
755
|
+
if (type.type !== "TSTypeReference" || type.typeName.type !== "Identifier") return null;
|
|
756
|
+
return type.typeArguments === null || type.typeArguments === void 0 || type.typeArguments.params.length === 0 ? type.typeName.name : null;
|
|
757
|
+
}
|
|
758
|
+
function lexicalTypeParameterNames(node) {
|
|
759
|
+
const names = /* @__PURE__ */ new Set();
|
|
760
|
+
let current = node;
|
|
761
|
+
while (current !== null && current.type !== "Program") {
|
|
762
|
+
if ("typeParameters" in current) for (const parameter of current.typeParameters?.params ?? []) names.add(parameter.name.name);
|
|
763
|
+
current = current.parent;
|
|
764
|
+
}
|
|
765
|
+
return names;
|
|
766
|
+
}
|
|
767
|
+
/** Ban function contracts that return unknown instead of a parsed domain type. */
|
|
768
|
+
const noUnknownReturnsRule = defineRule({
|
|
769
|
+
meta: {
|
|
770
|
+
type: "problem",
|
|
771
|
+
docs: { description: "Disallow functions whose explicit return contract is unknown or Promise<unknown>." },
|
|
772
|
+
messages: { unknownReturn: "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type." }
|
|
773
|
+
},
|
|
774
|
+
create(context) {
|
|
775
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
776
|
+
const resolvesToUnknown = (type, shadowedAliases, visited = /* @__PURE__ */ new Set()) => {
|
|
777
|
+
if (type.type === "TSUnknownKeyword") return true;
|
|
778
|
+
if (type.type === "TSParenthesizedType") return resolvesToUnknown(type.typeAnnotation, shadowedAliases, visited);
|
|
779
|
+
if (type.type === "TSUnionType") return type.types.some((member) => resolvesToUnknown(member, shadowedAliases, visited));
|
|
780
|
+
if (type.type === "TSTypeReference" && type.typeName.type === "Identifier" && (type.typeName.name === "Promise" || type.typeName.name === "PromiseLike")) {
|
|
781
|
+
const value = type.typeArguments?.params[0];
|
|
782
|
+
return value !== void 0 && resolvesToUnknown(value, shadowedAliases, visited);
|
|
783
|
+
}
|
|
784
|
+
const name = referencedAliasName$1(type);
|
|
785
|
+
if (name === null || visited.has(name) || shadowedAliases.has(name)) return false;
|
|
786
|
+
const alias = aliases.get(name);
|
|
787
|
+
if (alias === void 0 || alias.typeParameters !== null && alias.typeParameters !== void 0) return false;
|
|
788
|
+
const nextVisited = new Set(visited);
|
|
789
|
+
nextVisited.add(name);
|
|
790
|
+
return resolvesToUnknown(alias.typeAnnotation, shadowedAliases, nextVisited);
|
|
791
|
+
};
|
|
792
|
+
const checkReturnType = (node) => {
|
|
793
|
+
const annotation = node.returnType;
|
|
794
|
+
if (annotation === null || annotation === void 0) return;
|
|
795
|
+
if (!resolvesToUnknown(annotation.typeAnnotation, lexicalTypeParameterNames(node))) return;
|
|
796
|
+
context.report({
|
|
797
|
+
node: annotation.typeAnnotation,
|
|
798
|
+
messageId: "unknownReturn"
|
|
799
|
+
});
|
|
800
|
+
};
|
|
801
|
+
return {
|
|
802
|
+
Program(node) {
|
|
803
|
+
for (const statement of node.body) {
|
|
804
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
805
|
+
if (declaration?.type === "TSTypeAliasDeclaration") aliases.set(declaration.id.name, declaration);
|
|
806
|
+
}
|
|
807
|
+
},
|
|
808
|
+
ArrowFunctionExpression: checkReturnType,
|
|
809
|
+
FunctionDeclaration: checkReturnType,
|
|
810
|
+
FunctionExpression: checkReturnType,
|
|
811
|
+
TSCallSignatureDeclaration: checkReturnType,
|
|
812
|
+
TSConstructSignatureDeclaration: checkReturnType,
|
|
813
|
+
TSConstructorType: checkReturnType,
|
|
814
|
+
TSDeclareFunction: checkReturnType,
|
|
815
|
+
TSEmptyBodyFunctionExpression: checkReturnType,
|
|
816
|
+
TSFunctionType: checkReturnType,
|
|
817
|
+
TSMethodSignature: checkReturnType
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
});
|
|
821
|
+
function referencedAliasName(type) {
|
|
822
|
+
if (type.type === "TSParenthesizedType") return referencedAliasName(type.typeAnnotation);
|
|
823
|
+
if (type.type !== "TSTypeReference" || type.typeName.type !== "Identifier") return null;
|
|
824
|
+
return type.typeArguments === null || type.typeArguments === void 0 || type.typeArguments.params.length === 0 ? type.typeName.name : null;
|
|
825
|
+
}
|
|
826
|
+
/** Ban named aliases that merely conceal TypeScript's unknown top type. */
|
|
827
|
+
const noUnknownTypeAliasesRule = defineRule({
|
|
828
|
+
meta: {
|
|
829
|
+
type: "problem",
|
|
830
|
+
docs: { description: "Disallow type aliases whose resolved type is unknown; unknown must remain visible at an allowed boundary." },
|
|
831
|
+
messages: { 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." }
|
|
832
|
+
},
|
|
833
|
+
create(context) {
|
|
834
|
+
const aliases = /* @__PURE__ */ new Map();
|
|
835
|
+
const resolvesToUnknown = (type, visited = /* @__PURE__ */ new Set()) => {
|
|
836
|
+
if (type.type === "TSUnknownKeyword") return true;
|
|
837
|
+
if (type.type === "TSParenthesizedType") return resolvesToUnknown(type.typeAnnotation, visited);
|
|
838
|
+
const name = referencedAliasName(type);
|
|
839
|
+
if (name === null || visited.has(name)) return false;
|
|
840
|
+
const alias = aliases.get(name);
|
|
841
|
+
if (alias === void 0 || alias.typeParameters !== null && alias.typeParameters !== void 0) return false;
|
|
842
|
+
const nextVisited = new Set(visited);
|
|
843
|
+
nextVisited.add(name);
|
|
844
|
+
return resolvesToUnknown(alias.typeAnnotation, nextVisited);
|
|
845
|
+
};
|
|
846
|
+
return { Program(node) {
|
|
847
|
+
for (const statement of node.body) {
|
|
848
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
849
|
+
if (declaration?.type === "TSTypeAliasDeclaration") aliases.set(declaration.id.name, declaration);
|
|
850
|
+
}
|
|
851
|
+
for (const alias of aliases.values()) {
|
|
852
|
+
if (!resolvesToUnknown(alias.typeAnnotation, /* @__PURE__ */ new Set([alias.id.name]))) continue;
|
|
853
|
+
context.report({
|
|
854
|
+
node: alias.id,
|
|
855
|
+
messageId: "unknownAlias",
|
|
856
|
+
data: { alias: alias.id.name }
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
} };
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
const typeNodeKinds = /* @__PURE__ */ new Set([
|
|
863
|
+
"JSDocNonNullableType",
|
|
864
|
+
"JSDocNullableType",
|
|
865
|
+
"JSDocUnknownType",
|
|
866
|
+
"TSAnyKeyword",
|
|
867
|
+
"TSArrayType",
|
|
868
|
+
"TSBigIntKeyword",
|
|
869
|
+
"TSBooleanKeyword",
|
|
870
|
+
"TSConditionalType",
|
|
871
|
+
"TSConstructorType",
|
|
872
|
+
"TSFunctionType",
|
|
873
|
+
"TSImportType",
|
|
874
|
+
"TSIndexedAccessType",
|
|
875
|
+
"TSInferType",
|
|
876
|
+
"TSIntersectionType",
|
|
877
|
+
"TSIntrinsicKeyword",
|
|
878
|
+
"TSLiteralType",
|
|
879
|
+
"TSMappedType",
|
|
880
|
+
"TSNamedTupleMember",
|
|
881
|
+
"TSNeverKeyword",
|
|
882
|
+
"TSNullKeyword",
|
|
883
|
+
"TSNumberKeyword",
|
|
884
|
+
"TSObjectKeyword",
|
|
885
|
+
"TSParenthesizedType",
|
|
886
|
+
"TSStringKeyword",
|
|
887
|
+
"TSSymbolKeyword",
|
|
888
|
+
"TSTemplateLiteralType",
|
|
889
|
+
"TSThisType",
|
|
890
|
+
"TSTupleType",
|
|
891
|
+
"TSTypeLiteral",
|
|
892
|
+
"TSTypeOperator",
|
|
893
|
+
"TSTypePredicate",
|
|
894
|
+
"TSTypeQuery",
|
|
895
|
+
"TSTypeReference",
|
|
896
|
+
"TSUndefinedKeyword",
|
|
897
|
+
"TSUnionType",
|
|
898
|
+
"TSUnknownKeyword",
|
|
899
|
+
"TSVoidKeyword"
|
|
900
|
+
]);
|
|
901
|
+
function isTypeNode(node) {
|
|
902
|
+
return typeNodeKinds.has(node.type);
|
|
903
|
+
}
|
|
904
|
+
function typeReferenceName$1(type) {
|
|
905
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
906
|
+
}
|
|
907
|
+
function isInsideTypeAliasDeclaration(node) {
|
|
908
|
+
let current = node.parent;
|
|
909
|
+
while (current !== null && current.type !== "Program") {
|
|
910
|
+
if (current.type === "TSTypeAliasDeclaration") return true;
|
|
911
|
+
current = current.parent;
|
|
912
|
+
}
|
|
913
|
+
return false;
|
|
914
|
+
}
|
|
915
|
+
function isPlainAliasConsumerUse(node, environment) {
|
|
916
|
+
if (node.type !== "TSTypeReference" || node.typeArguments?.params.length) return false;
|
|
917
|
+
const name = typeReferenceName$1(node);
|
|
918
|
+
return name !== null && environment.aliases.has(name) && !isInsideTypeAliasDeclaration(node);
|
|
919
|
+
}
|
|
920
|
+
function shouldReportType(node, environment) {
|
|
921
|
+
if (isPlainAliasConsumerUse(node, environment)) return false;
|
|
922
|
+
if (classifyUnsafeDictionary(node, environment) === null) return false;
|
|
923
|
+
let current = node.parent;
|
|
924
|
+
while (current !== null && current.type !== "Program") {
|
|
925
|
+
if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null) return false;
|
|
926
|
+
current = current.parent;
|
|
927
|
+
}
|
|
928
|
+
return true;
|
|
929
|
+
}
|
|
930
|
+
/** Disallow object-dictionary contracts whose direct value type is an unsafe escape hatch. */
|
|
931
|
+
const noUnsafeDictionaryTypeRule = defineRule({
|
|
932
|
+
meta: {
|
|
933
|
+
type: "problem",
|
|
934
|
+
docs: { description: "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches." },
|
|
935
|
+
messages: { 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." }
|
|
936
|
+
},
|
|
937
|
+
createOnce(context) {
|
|
938
|
+
let environment = null;
|
|
939
|
+
const report = (node, value) => {
|
|
940
|
+
context.report({
|
|
941
|
+
node,
|
|
942
|
+
messageId: "unsafeDictionary",
|
|
943
|
+
data: { value }
|
|
944
|
+
});
|
|
945
|
+
};
|
|
946
|
+
const reportIfUnsafe = (node) => {
|
|
947
|
+
if (environment === null || !shouldReportType(node, environment)) return;
|
|
948
|
+
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
949
|
+
if (unsafe === null) return;
|
|
950
|
+
report(node, unsafe.unsafeValue);
|
|
951
|
+
};
|
|
952
|
+
return {
|
|
953
|
+
Program(node) {
|
|
954
|
+
environment = createTypeEnvironment(node);
|
|
955
|
+
},
|
|
956
|
+
TSTypeReference: reportIfUnsafe,
|
|
957
|
+
TSTypeLiteral: reportIfUnsafe,
|
|
958
|
+
TSMappedType: reportIfUnsafe,
|
|
959
|
+
TSIndexSignature(node) {
|
|
960
|
+
if (environment === null || node.typeAnnotation === null || node.parent.type === "TSTypeLiteral") return;
|
|
961
|
+
const unsafe = classifyUnsafeDictionaryValue(node.typeAnnotation.typeAnnotation, environment);
|
|
962
|
+
if (unsafe !== null) report(node, unsafe.unsafeValue);
|
|
963
|
+
}
|
|
964
|
+
};
|
|
965
|
+
}
|
|
966
|
+
});
|
|
967
|
+
const functionBoundaryTypes = /* @__PURE__ */ new Set([
|
|
968
|
+
"ArrowFunctionExpression",
|
|
969
|
+
"FunctionDeclaration",
|
|
970
|
+
"FunctionExpression",
|
|
971
|
+
"TSDeclareFunction",
|
|
972
|
+
"TSEmptyBodyFunctionExpression"
|
|
973
|
+
]);
|
|
974
|
+
function unwrapExpressionParentheses(expression) {
|
|
975
|
+
let current = expression;
|
|
976
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
977
|
+
return current;
|
|
978
|
+
}
|
|
979
|
+
function unwrapTypeParentheses(type) {
|
|
980
|
+
let current = type;
|
|
981
|
+
while (current.type === "TSParenthesizedType") current = current.typeAnnotation;
|
|
982
|
+
return current;
|
|
983
|
+
}
|
|
984
|
+
function typeReferenceName(type) {
|
|
985
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
986
|
+
}
|
|
987
|
+
function isUnknownOrAnyType(type) {
|
|
988
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
989
|
+
return unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword";
|
|
990
|
+
}
|
|
991
|
+
function isBroadRecordKeyType(type) {
|
|
992
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
993
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") return true;
|
|
994
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.every(isBroadRecordKeyType);
|
|
995
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName(unwrapped) === "PropertyKey";
|
|
996
|
+
}
|
|
997
|
+
function isBroadRecordType(type) {
|
|
998
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
999
|
+
if (unwrapped.type === "TSTypeReference") {
|
|
1000
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
1001
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
1002
|
+
return inner !== void 0 && isBroadRecordType(inner);
|
|
1003
|
+
}
|
|
1004
|
+
if (typeReferenceName(unwrapped) !== "Record") return false;
|
|
1005
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
1006
|
+
return parameters.length === 2 && parameters[0] !== void 0 && parameters[1] !== void 0 && isBroadRecordKeyType(parameters[0]) && isUnknownOrAnyType(parameters[1]);
|
|
1007
|
+
}
|
|
1008
|
+
if (unwrapped.type !== "TSTypeLiteral" || unwrapped.members.length !== 1) return false;
|
|
1009
|
+
const [member] = unwrapped.members;
|
|
1010
|
+
const [parameter] = member?.type === "TSIndexSignature" ? member.parameters : [];
|
|
1011
|
+
return member?.type === "TSIndexSignature" && member.parameters.length === 1 && parameter !== void 0 && isBroadRecordKeyType(parameter.typeAnnotation.typeAnnotation) && isUnknownOrAnyType(member.typeAnnotation.typeAnnotation);
|
|
1012
|
+
}
|
|
1013
|
+
function broadTypeKind(type) {
|
|
1014
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1015
|
+
if (unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword") return "top";
|
|
1016
|
+
if (unwrapped.type === "TSObjectKeyword") return "object";
|
|
1017
|
+
return isBroadRecordType(unwrapped) ? "record" : null;
|
|
1018
|
+
}
|
|
1019
|
+
function assertedExpression(node) {
|
|
1020
|
+
return unwrapExpressionParentheses(node.expression);
|
|
1021
|
+
}
|
|
1022
|
+
function assertionFromExpression(expression) {
|
|
1023
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
1024
|
+
return unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion" ? unwrapped : null;
|
|
1025
|
+
}
|
|
1026
|
+
function normalizedTypeText(sourceText, type) {
|
|
1027
|
+
return sourceText.slice(type.start, type.end).replaceAll(/\s+/gu, "");
|
|
1028
|
+
}
|
|
1029
|
+
function typesHaveSameSyntax(sourceText, left, right) {
|
|
1030
|
+
return left !== null && normalizedTypeText(sourceText, unwrapTypeParentheses(left)) === normalizedTypeText(sourceText, unwrapTypeParentheses(right));
|
|
1031
|
+
}
|
|
1032
|
+
function isDefinitelyObjectType(type) {
|
|
1033
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1034
|
+
switch (unwrapped.type) {
|
|
1035
|
+
case "TSArrayType":
|
|
1036
|
+
case "TSConstructorType":
|
|
1037
|
+
case "TSFunctionType":
|
|
1038
|
+
case "TSMappedType":
|
|
1039
|
+
case "TSObjectKeyword":
|
|
1040
|
+
case "TSTupleType": return true;
|
|
1041
|
+
case "TSTypeLiteral": return unwrapped.members.length > 0;
|
|
1042
|
+
case "TSIntersectionType": return unwrapped.types.every(isDefinitelyObjectType);
|
|
1043
|
+
case "TSTypeOperator": return unwrapped.operator === "readonly" && isDefinitelyObjectType(unwrapped.typeAnnotation);
|
|
1044
|
+
default: return false;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
function isDefinitelyNarrowerRecordType(type) {
|
|
1048
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1049
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type !== "TSIndexSignature");
|
|
1050
|
+
if (unwrapped.type !== "TSTypeReference") return false;
|
|
1051
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
1052
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
1053
|
+
return inner !== void 0 && isDefinitelyNarrowerRecordType(inner);
|
|
1054
|
+
}
|
|
1055
|
+
if (typeReferenceName(unwrapped) !== "Record") return false;
|
|
1056
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
1057
|
+
return parameters.length === 2 && parameters[1] !== void 0 && !isUnknownOrAnyType(parameters[1]);
|
|
1058
|
+
}
|
|
1059
|
+
function functionBoundary(node) {
|
|
1060
|
+
let current = node.parent;
|
|
1061
|
+
while (current !== null && current.type !== "Program") {
|
|
1062
|
+
if (functionBoundaryTypes.has(current.type)) return current;
|
|
1063
|
+
current = current.parent;
|
|
1064
|
+
}
|
|
1065
|
+
return null;
|
|
1066
|
+
}
|
|
1067
|
+
function resolvedVariableForIdentifier(scopes, identifier) {
|
|
1068
|
+
for (const scope of scopes) {
|
|
1069
|
+
const reference = scope.references.find((candidate) => candidate.identifier.start === identifier.start && candidate.identifier.end === identifier.end);
|
|
1070
|
+
if (reference !== void 0) return reference.resolved;
|
|
1071
|
+
}
|
|
1072
|
+
return null;
|
|
1073
|
+
}
|
|
1074
|
+
function variableDeclarator(variable) {
|
|
1075
|
+
for (const definition of variable.defs) if (definition.type === "Variable" && definition.node.type === "VariableDeclarator") return definition.node;
|
|
1076
|
+
return null;
|
|
1077
|
+
}
|
|
1078
|
+
function knownValueEvidence(expression, scopes, boundary, visitedVariables) {
|
|
1079
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
1080
|
+
if (unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion") {
|
|
1081
|
+
if (broadTypeKind(unwrapped.typeAnnotation) !== null) return null;
|
|
1082
|
+
return { type: unwrapped.typeAnnotation };
|
|
1083
|
+
}
|
|
1084
|
+
if (unwrapped.type === "Literal" || unwrapped.type === "TemplateLiteral") return { type: null };
|
|
1085
|
+
if (unwrapped.type === "ArrayExpression" || unwrapped.type === "ArrowFunctionExpression" || unwrapped.type === "ClassExpression" || unwrapped.type === "FunctionExpression" || unwrapped.type === "NewExpression" || unwrapped.type === "ObjectExpression") return { type: null };
|
|
1086
|
+
if (unwrapped.type !== "Identifier") return null;
|
|
1087
|
+
const variable = resolvedVariableForIdentifier(scopes, unwrapped);
|
|
1088
|
+
if (variable === null || visitedVariables.has(variable)) return null;
|
|
1089
|
+
const annotatedIdentifier = variable.identifiers.find((identifier) => identifier.typeAnnotation !== null && identifier.typeAnnotation !== void 0);
|
|
1090
|
+
const annotation = annotatedIdentifier?.typeAnnotation?.typeAnnotation;
|
|
1091
|
+
if (annotation !== void 0 && annotatedIdentifier !== void 0) {
|
|
1092
|
+
if (functionBoundary(annotatedIdentifier) !== boundary || broadTypeKind(annotation) !== null) return null;
|
|
1093
|
+
return { type: annotation };
|
|
1094
|
+
}
|
|
1095
|
+
const declarator = variableDeclarator(variable);
|
|
1096
|
+
if (declarator === null || declarator.parent.type !== "VariableDeclaration" || declarator.parent.kind !== "const" || declarator.init === null || variable.references.some((reference) => reference.isWrite() && !reference.init) || functionBoundary(declarator) !== boundary) return null;
|
|
1097
|
+
return knownValueEvidence(declarator.init, scopes, boundary, /* @__PURE__ */ new Set([...visitedVariables, variable]));
|
|
1098
|
+
}
|
|
1099
|
+
function widenedBinding(variable, scopes) {
|
|
1100
|
+
const declarator = variableDeclarator(variable);
|
|
1101
|
+
if (declarator === null || declarator.parent.type !== "VariableDeclaration" || declarator.parent.kind !== "const" || declarator.id.type !== "Identifier" || declarator.init === null || variable.references.some((reference) => reference.isWrite() && !reference.init)) return null;
|
|
1102
|
+
const boundary = functionBoundary(declarator);
|
|
1103
|
+
const declaredType = declarator.id.typeAnnotation?.typeAnnotation;
|
|
1104
|
+
const initializerAssertion = assertionFromExpression(declarator.init);
|
|
1105
|
+
const initializerBroadKind = initializerAssertion === null ? null : broadTypeKind(initializerAssertion.typeAnnotation);
|
|
1106
|
+
const broadKind = (declaredType === void 0 ? null : broadTypeKind(declaredType)) ?? initializerBroadKind;
|
|
1107
|
+
if (broadKind === null) return null;
|
|
1108
|
+
const evidence = knownValueEvidence(initializerAssertion !== null && initializerBroadKind !== null ? assertedExpression(initializerAssertion) : declarator.init, scopes, boundary, /* @__PURE__ */ new Set([variable]));
|
|
1109
|
+
return evidence === null ? null : {
|
|
1110
|
+
broadKind,
|
|
1111
|
+
evidence,
|
|
1112
|
+
declaredAt: declarator.end,
|
|
1113
|
+
boundary
|
|
1114
|
+
};
|
|
1115
|
+
}
|
|
1116
|
+
function assertionIsNarrower(sourceText, broadKind, evidence, assertedType) {
|
|
1117
|
+
if (broadTypeKind(assertedType) !== null) return false;
|
|
1118
|
+
if (broadKind === "top") return true;
|
|
1119
|
+
if (typesHaveSameSyntax(sourceText, evidence.type, assertedType)) return true;
|
|
1120
|
+
if (broadKind === "object") return isDefinitelyObjectType(assertedType);
|
|
1121
|
+
return isDefinitelyNarrowerRecordType(assertedType);
|
|
1122
|
+
}
|
|
1123
|
+
/** Detect immutable local bindings that erase a known type and are later asserted back to a narrower type. */
|
|
1124
|
+
const noWidenThenAssertRule = defineRule({
|
|
1125
|
+
meta: {
|
|
1126
|
+
type: "problem",
|
|
1127
|
+
docs: { description: "Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type." },
|
|
1128
|
+
messages: { 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." }
|
|
1129
|
+
},
|
|
1130
|
+
create(context) {
|
|
1131
|
+
const scopes = context.sourceCode.scopeManager.scopes;
|
|
1132
|
+
const checkAssertion = (node) => {
|
|
1133
|
+
const expression = assertedExpression(node);
|
|
1134
|
+
if (expression.type !== "Identifier") return;
|
|
1135
|
+
const variable = resolvedVariableForIdentifier(scopes, expression);
|
|
1136
|
+
if (variable === null) return;
|
|
1137
|
+
const widened = widenedBinding(variable, scopes);
|
|
1138
|
+
if (widened === null || node.start <= widened.declaredAt || functionBoundary(node) !== widened.boundary || !assertionIsNarrower(context.sourceCode.text, widened.broadKind, widened.evidence, node.typeAnnotation)) return;
|
|
1139
|
+
context.report({
|
|
1140
|
+
node,
|
|
1141
|
+
messageId: "widenThenAssert",
|
|
1142
|
+
data: { name: expression.name }
|
|
1143
|
+
});
|
|
1144
|
+
};
|
|
1145
|
+
return {
|
|
1146
|
+
TSAsExpression: checkAssertion,
|
|
1147
|
+
TSTypeAssertion: checkAssertion
|
|
1148
|
+
};
|
|
1149
|
+
}
|
|
1150
|
+
});
|
|
1151
|
+
const commentOwnerKinds = /* @__PURE__ */ new Set([
|
|
1152
|
+
"ExpressionStatement",
|
|
1153
|
+
"PropertyDefinition",
|
|
1154
|
+
"ReturnStatement",
|
|
1155
|
+
"ThrowStatement",
|
|
1156
|
+
"VariableDeclaration"
|
|
1157
|
+
]);
|
|
1158
|
+
function isConstAssertion(node) {
|
|
1159
|
+
return node.typeAnnotation.type === "TSTypeReference" && node.typeAnnotation.typeName.type === "Identifier" && node.typeAnnotation.typeName.name === "const";
|
|
1160
|
+
}
|
|
1161
|
+
function hasSafetyComment(sourceCode, node) {
|
|
1162
|
+
let current = node;
|
|
1163
|
+
while (true) {
|
|
1164
|
+
if (sourceCode.getCommentsBefore(current).some((comment) => comment.end <= node.start && /\bSAFETY\s*:/u.test(comment.value))) return true;
|
|
1165
|
+
if (commentOwnerKinds.has(current.type) || current.parent.type === "Program") return false;
|
|
1166
|
+
current = current.parent;
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
/** Generic Oxlint rules that reject low-evidence and low-signal implementation patterns. */
|
|
1170
|
+
const antiSlopPlugin = definePlugin({
|
|
1171
|
+
meta: { name: "anti-slop" },
|
|
1172
|
+
rules: {
|
|
1173
|
+
"no-chained-type-assertions": noChainedTypeAssertionsRule,
|
|
1174
|
+
"no-conditional-empty-object-spread": noConditionalEmptyObjectSpreadRule,
|
|
1175
|
+
"no-known-value-widening": noKnownValueWideningRule,
|
|
1176
|
+
"no-module-mocking": noModuleMockingRule,
|
|
1177
|
+
"no-object-parameters": noObjectParametersRule,
|
|
1178
|
+
"no-reflect-apply": noReflectApplyRule,
|
|
1179
|
+
"no-reflect-get": noReflectGetRule,
|
|
1180
|
+
"no-runtime-typeof": noRuntimeTypeofRule,
|
|
1181
|
+
"no-unsafe-dictionary-type": noUnsafeDictionaryTypeRule,
|
|
1182
|
+
"no-shape-in-symbol-names": noForbiddenTermInSymbolNamesRule,
|
|
1183
|
+
"no-unknown-parameters": noUnknownParametersRule,
|
|
1184
|
+
"no-unknown-returns": noUnknownReturnsRule,
|
|
1185
|
+
"no-unknown-type-aliases": noUnknownTypeAliasesRule,
|
|
1186
|
+
"no-widen-then-assert": noWidenThenAssertRule,
|
|
1187
|
+
"require-safety-comment-for-type-assertion": defineRule({
|
|
1188
|
+
meta: {
|
|
1189
|
+
type: "problem",
|
|
1190
|
+
docs: { description: "Require a nearby SAFETY comment for every TypeScript type assertion except const assertions." },
|
|
1191
|
+
messages: { missingSafetyComment: "This type assertion has no `SAFETY:` justification. State the checked invariant immediately before the assertion or its containing statement." }
|
|
1192
|
+
},
|
|
1193
|
+
create(context) {
|
|
1194
|
+
const checkAssertion = (node) => {
|
|
1195
|
+
if (isConstAssertion(node) || hasSafetyComment(context.sourceCode, node)) return;
|
|
1196
|
+
context.report({
|
|
1197
|
+
node,
|
|
1198
|
+
messageId: "missingSafetyComment"
|
|
1199
|
+
});
|
|
1200
|
+
};
|
|
1201
|
+
return {
|
|
1202
|
+
TSAsExpression: checkAssertion,
|
|
1203
|
+
TSTypeAssertion: checkAssertion
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
})
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
export { antiSlopPlugin as default };
|