@timmo001/oxlint-rules 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 +201 -0
- package/README.md +102 -0
- package/THIRD_PARTY_NOTICES.md +10 -0
- package/dist/cli.js +78 -0
- package/dist/configs/effect.js +52 -0
- package/dist/configs/recommended.js +35 -0
- package/dist/effect/index.js +106 -0
- package/dist/upstream/anti-slop.js +1838 -0
- package/dist/upstream/effect.js +59 -0
- package/package.json +61 -0
- package/skills/add-oxlint-rule/SKILL.md +31 -0
- package/skills/install-timmo-oxlint-rules/SKILL.md +45 -0
- package/skills/install-timmo-oxlint-rules/scripts/copy.mjs +30 -0
- package/src/cli.ts +35 -0
- package/src/configs/effect.ts +20 -0
- package/src/configs/recommended.ts +29 -0
- package/src/effect/index.ts +12 -0
- package/src/effect/rules/no-try-catch-in-effect-generators.ts +140 -0
- package/src/install/copy.ts +77 -0
- package/src/jsr/effect-config.ts +7 -0
- package/src/jsr/effect.ts +7 -0
- package/src/jsr/recommended.ts +7 -0
- package/src/jsr/upstream-anti-slop.ts +7 -0
- package/src/jsr/upstream-effect.ts +7 -0
- package/src/upstream/anti-slop.ts +1 -0
- package/src/upstream/effect.ts +1 -0
- package/vendor/anti-slop/LICENSE +21 -0
- package/vendor/anti-slop/README.md +299 -0
- package/vendor/anti-slop/src/effect/index.ts +13 -0
- package/vendor/anti-slop/src/effect/rules/no-service-constructor-imports.ts +52 -0
- package/vendor/anti-slop/src/index.ts +41 -0
- package/vendor/anti-slop/src/rules/no-chained-type-assertions.ts +77 -0
- package/vendor/anti-slop/src/rules/no-conditional-empty-object-spread.ts +49 -0
- package/vendor/anti-slop/src/rules/no-known-value-widening.ts +427 -0
- package/vendor/anti-slop/src/rules/no-module-mocking.ts +91 -0
- package/vendor/anti-slop/src/rules/no-object-parameters.ts +83 -0
- package/vendor/anti-slop/src/rules/no-reflect-apply.ts +28 -0
- package/vendor/anti-slop/src/rules/no-reflect-get.ts +28 -0
- package/vendor/anti-slop/src/rules/no-runtime-typeof.ts +77 -0
- package/vendor/anti-slop/src/rules/no-shape-in-symbol-names.ts +46 -0
- package/vendor/anti-slop/src/rules/no-unknown-parameters.ts +69 -0
- package/vendor/anti-slop/src/rules/no-unknown-returns.ts +82 -0
- package/vendor/anti-slop/src/rules/no-unknown-type-aliases.ts +54 -0
- package/vendor/anti-slop/src/rules/no-unsafe-dictionary-type.ts +154 -0
- package/vendor/anti-slop/src/rules/no-widen-then-assert.ts +366 -0
- package/vendor/anti-slop/src/rules/require-safety-comment-for-type-assertion.ts +131 -0
- package/vendor/anti-slop/src/shared/dictionary-types.ts +515 -0
- package/vendor/anti-slop/src/shared/function-parameters.ts +49 -0
- package/vendor/anti-slop/src/shared/lexical-type-parameters.ts +61 -0
- package/vendor/anti-slop/src/shared/reflect-method.ts +35 -0
- package/vendor/anti-slop/src/shared/type-alias-resolution.ts +250 -0
|
@@ -0,0 +1,1838 @@
|
|
|
1
|
+
// vendor/anti-slop/src/index.ts
|
|
2
|
+
import { eslintCompatPlugin } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
// vendor/anti-slop/src/rules/no-chained-type-assertions.ts
|
|
5
|
+
import { defineRule } from "@oxlint/plugins";
|
|
6
|
+
function isTypeAssertionExpression(node) {
|
|
7
|
+
return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
|
|
8
|
+
}
|
|
9
|
+
function unwrapParenthesizedExpression(expression) {
|
|
10
|
+
let current = expression;
|
|
11
|
+
while (current.type === "ParenthesizedExpression") {
|
|
12
|
+
current = current.expression;
|
|
13
|
+
}
|
|
14
|
+
return current;
|
|
15
|
+
}
|
|
16
|
+
function isConstAssertion(node) {
|
|
17
|
+
const { typeAnnotation } = node;
|
|
18
|
+
return typeAnnotation.type === "TSTypeReference" && typeAnnotation.typeName.type === "Identifier" && typeAnnotation.typeName.name === "const";
|
|
19
|
+
}
|
|
20
|
+
function isOutermostAssertionInChain(node) {
|
|
21
|
+
let current = node;
|
|
22
|
+
let parent = node.parent;
|
|
23
|
+
while (parent.type === "ParenthesizedExpression" && parent.expression === current) {
|
|
24
|
+
current = parent;
|
|
25
|
+
parent = parent.parent;
|
|
26
|
+
}
|
|
27
|
+
return !isTypeAssertionExpression(parent) || parent.expression !== current;
|
|
28
|
+
}
|
|
29
|
+
function isForbiddenAssertionChain(node) {
|
|
30
|
+
let assertionCount = 0;
|
|
31
|
+
let hasNonConstAssertion = false;
|
|
32
|
+
let current = node;
|
|
33
|
+
while (isTypeAssertionExpression(current)) {
|
|
34
|
+
assertionCount += 1;
|
|
35
|
+
hasNonConstAssertion ||= !isConstAssertion(current);
|
|
36
|
+
current = unwrapParenthesizedExpression(current.expression);
|
|
37
|
+
}
|
|
38
|
+
return assertionCount > 1 && hasNonConstAssertion;
|
|
39
|
+
}
|
|
40
|
+
var noChainedTypeAssertionsRule = defineRule({
|
|
41
|
+
meta: {
|
|
42
|
+
type: "problem",
|
|
43
|
+
docs: {
|
|
44
|
+
description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains."
|
|
45
|
+
},
|
|
46
|
+
messages: {
|
|
47
|
+
chained: "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it."
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
createOnce(context) {
|
|
51
|
+
const checkTypeAssertion = (node) => {
|
|
52
|
+
if (!isOutermostAssertionInChain(node) || !isForbiddenAssertionChain(node))
|
|
53
|
+
return;
|
|
54
|
+
context.report({ node, messageId: "chained" });
|
|
55
|
+
};
|
|
56
|
+
return {
|
|
57
|
+
TSAsExpression: checkTypeAssertion,
|
|
58
|
+
TSTypeAssertion: checkTypeAssertion
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// vendor/anti-slop/src/rules/no-conditional-empty-object-spread.ts
|
|
64
|
+
import { defineRule as defineRule2 } from "@oxlint/plugins";
|
|
65
|
+
function unwrapParentheses(node) {
|
|
66
|
+
let current = node;
|
|
67
|
+
while (current.type === "ParenthesizedExpression") {
|
|
68
|
+
current = current.expression;
|
|
69
|
+
}
|
|
70
|
+
return current;
|
|
71
|
+
}
|
|
72
|
+
function isEmptyObjectExpression(node) {
|
|
73
|
+
return node.type === "ObjectExpression" && node.properties.length === 0;
|
|
74
|
+
}
|
|
75
|
+
function isConditionalEmptyObjectSpread(node) {
|
|
76
|
+
const conditional = unwrapParentheses(node);
|
|
77
|
+
return conditional.type === "ConditionalExpression" && (isEmptyObjectExpression(conditional.consequent) || isEmptyObjectExpression(conditional.alternate));
|
|
78
|
+
}
|
|
79
|
+
var noConditionalEmptyObjectSpreadRule = defineRule2({
|
|
80
|
+
meta: {
|
|
81
|
+
type: "suggestion",
|
|
82
|
+
docs: {
|
|
83
|
+
description: "Disallow object spreads that conditionally spread an empty object to omit fields."
|
|
84
|
+
},
|
|
85
|
+
messages: {
|
|
86
|
+
avoid: "This conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present."
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
createOnce(context) {
|
|
90
|
+
return {
|
|
91
|
+
SpreadElement(node) {
|
|
92
|
+
if (node.parent.type !== "ObjectExpression")
|
|
93
|
+
return;
|
|
94
|
+
if (isConditionalEmptyObjectSpread(node.argument)) {
|
|
95
|
+
context.report({ node, messageId: "avoid" });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// vendor/anti-slop/src/rules/no-known-value-widening.ts
|
|
103
|
+
import { defineRule as defineRule3 } from "@oxlint/plugins";
|
|
104
|
+
|
|
105
|
+
// vendor/anti-slop/src/shared/lexical-type-parameters.ts
|
|
106
|
+
function isNode(value) {
|
|
107
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
108
|
+
}
|
|
109
|
+
function collectInferTypeParameterNames(node, visitorKeys, names) {
|
|
110
|
+
if (node.type === "TSInferType")
|
|
111
|
+
names.add(node.typeParameter.name.name);
|
|
112
|
+
const record = node;
|
|
113
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
114
|
+
const value = record[key];
|
|
115
|
+
if (isNode(value)) {
|
|
116
|
+
collectInferTypeParameterNames(value, visitorKeys, names);
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (!Array.isArray(value))
|
|
120
|
+
continue;
|
|
121
|
+
for (const child of value) {
|
|
122
|
+
if (isNode(child))
|
|
123
|
+
collectInferTypeParameterNames(child, visitorKeys, names);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function lexicalTypeParameterNames(node, visitorKeys) {
|
|
128
|
+
const names = new Set;
|
|
129
|
+
let descendant = node;
|
|
130
|
+
let current = node;
|
|
131
|
+
while (current !== null && current.type !== "Program") {
|
|
132
|
+
if ("typeParameters" in current) {
|
|
133
|
+
for (const parameter of current.typeParameters?.params ?? []) {
|
|
134
|
+
names.add(parameter.name.name);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
if (current.type === "TSMappedType" && (descendant === current.nameType || descendant === current.typeAnnotation)) {
|
|
138
|
+
names.add(current.key.name);
|
|
139
|
+
}
|
|
140
|
+
if (current.type === "TSConditionalType" && descendant === current.trueType) {
|
|
141
|
+
collectInferTypeParameterNames(current.extendsType, visitorKeys, names);
|
|
142
|
+
}
|
|
143
|
+
descendant = current;
|
|
144
|
+
current = current.parent;
|
|
145
|
+
}
|
|
146
|
+
return names;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// vendor/anti-slop/src/shared/type-alias-resolution.ts
|
|
150
|
+
var environmentsByProgram = new WeakMap;
|
|
151
|
+
function isNode2(value) {
|
|
152
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
153
|
+
}
|
|
154
|
+
function enclosingTypeScope(node) {
|
|
155
|
+
let current = node.parent;
|
|
156
|
+
while (current !== null) {
|
|
157
|
+
if (current.type === "Program" || current.type === "BlockStatement" || current.type === "TSModuleBlock" || current.type === "StaticBlock" || current.type === "SwitchStatement") {
|
|
158
|
+
return current;
|
|
159
|
+
}
|
|
160
|
+
current = current.parent;
|
|
161
|
+
}
|
|
162
|
+
return node;
|
|
163
|
+
}
|
|
164
|
+
function declaredTypeBinding(node) {
|
|
165
|
+
if (node.type === "TSTypeAliasDeclaration") {
|
|
166
|
+
return { alias: node, name: node.id.name };
|
|
167
|
+
}
|
|
168
|
+
if (node.type === "TSInterfaceDeclaration" || node.type === "TSEnumDeclaration" || node.type === "ClassDeclaration" || node.type === "ClassExpression") {
|
|
169
|
+
return node.id === null ? null : { alias: null, name: node.id.name };
|
|
170
|
+
}
|
|
171
|
+
if (node.type === "ImportSpecifier" || node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") {
|
|
172
|
+
return { alias: null, name: node.local.name };
|
|
173
|
+
}
|
|
174
|
+
return null;
|
|
175
|
+
}
|
|
176
|
+
function collectTypeBindings(node, visitorKeys, bindingsByName, aliases) {
|
|
177
|
+
const declared = declaredTypeBinding(node);
|
|
178
|
+
if (declared !== null) {
|
|
179
|
+
const bindings = bindingsByName.get(declared.name) ?? [];
|
|
180
|
+
bindings.push({ ...declared, scope: enclosingTypeScope(node) });
|
|
181
|
+
bindingsByName.set(declared.name, bindings);
|
|
182
|
+
if (declared.alias !== null)
|
|
183
|
+
aliases.push(declared.alias);
|
|
184
|
+
}
|
|
185
|
+
const fields = node;
|
|
186
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
187
|
+
const value = fields[key];
|
|
188
|
+
if (isNode2(value)) {
|
|
189
|
+
collectTypeBindings(value, visitorKeys, bindingsByName, aliases);
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (!Array.isArray(value))
|
|
193
|
+
continue;
|
|
194
|
+
for (const child of value) {
|
|
195
|
+
if (isNode2(child)) {
|
|
196
|
+
collectTypeBindings(child, visitorKeys, bindingsByName, aliases);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
function createTypeAliasEnvironment(program, visitorKeys) {
|
|
202
|
+
const cached = environmentsByProgram.get(program);
|
|
203
|
+
if (cached !== undefined)
|
|
204
|
+
return cached;
|
|
205
|
+
const bindingsByName = new Map;
|
|
206
|
+
const aliases = [];
|
|
207
|
+
collectTypeBindings(program, visitorKeys, bindingsByName, aliases);
|
|
208
|
+
const environment = { aliases, bindingsByName, visitorKeys };
|
|
209
|
+
environmentsByProgram.set(program, environment);
|
|
210
|
+
return environment;
|
|
211
|
+
}
|
|
212
|
+
function ancestorDistance(ancestor, node) {
|
|
213
|
+
let current = node;
|
|
214
|
+
let distance = 0;
|
|
215
|
+
while (current !== null) {
|
|
216
|
+
if (current === ancestor)
|
|
217
|
+
return distance;
|
|
218
|
+
current = current.parent;
|
|
219
|
+
distance += 1;
|
|
220
|
+
}
|
|
221
|
+
return null;
|
|
222
|
+
}
|
|
223
|
+
function nearestTypeBindings(name, use, environment) {
|
|
224
|
+
const candidates = environment.bindingsByName.get(name) ?? [];
|
|
225
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
226
|
+
let nearest = [];
|
|
227
|
+
for (const candidate of candidates) {
|
|
228
|
+
const distance = ancestorDistance(candidate.scope, use);
|
|
229
|
+
if (distance === null || distance > nearestDistance)
|
|
230
|
+
continue;
|
|
231
|
+
if (distance === nearestDistance) {
|
|
232
|
+
nearest.push(candidate);
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
nearestDistance = distance;
|
|
236
|
+
nearest = [candidate];
|
|
237
|
+
}
|
|
238
|
+
return nearest;
|
|
239
|
+
}
|
|
240
|
+
function visibleTypeAlias(name, use, environment) {
|
|
241
|
+
if (lexicalTypeParameterNames(use, environment.visitorKeys).has(name))
|
|
242
|
+
return null;
|
|
243
|
+
const bindings = nearestTypeBindings(name, use, environment);
|
|
244
|
+
return bindings.length === 1 ? bindings[0]?.alias ?? null : null;
|
|
245
|
+
}
|
|
246
|
+
function hasVisibleTypeBinding(name, use, environment) {
|
|
247
|
+
return lexicalTypeParameterNames(use, environment.visitorKeys).has(name) || nearestTypeBindings(name, use, environment).length > 0;
|
|
248
|
+
}
|
|
249
|
+
function typeReferenceName(type) {
|
|
250
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
251
|
+
}
|
|
252
|
+
function aliasSubstitutions(alias, reference, base) {
|
|
253
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
254
|
+
const arguments_ = reference.typeArguments?.params ?? [];
|
|
255
|
+
const next = new Map(base);
|
|
256
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
257
|
+
const explicitArgument = arguments_[index];
|
|
258
|
+
const argument = explicitArgument ?? parameter.default;
|
|
259
|
+
if (argument === null || argument === undefined)
|
|
260
|
+
return null;
|
|
261
|
+
const argumentSubstitutions = explicitArgument === undefined ? next : base;
|
|
262
|
+
next.set(parameter.name.name, {
|
|
263
|
+
type: argument,
|
|
264
|
+
substitutions: new Map(argumentSubstitutions)
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
return next;
|
|
268
|
+
}
|
|
269
|
+
function resolvedTypeMatches(type, environment, matcher) {
|
|
270
|
+
const evaluate = (current, substitutions, resolvingAliases) => {
|
|
271
|
+
if (current.type === "TSTypeReference") {
|
|
272
|
+
const name = typeReferenceName(current);
|
|
273
|
+
if (name !== null) {
|
|
274
|
+
const substitution = substitutions.get(name);
|
|
275
|
+
if (substitution !== undefined && !current.typeArguments?.params.length) {
|
|
276
|
+
return evaluate(substitution.type, substitution.substitutions, resolvingAliases);
|
|
277
|
+
}
|
|
278
|
+
const alias = visibleTypeAlias(name, current, environment);
|
|
279
|
+
if (alias !== null && !resolvingAliases.has(alias)) {
|
|
280
|
+
const nextSubstitutions = aliasSubstitutions(alias, current, substitutions);
|
|
281
|
+
if (nextSubstitutions !== null) {
|
|
282
|
+
const nextResolving = new Set(resolvingAliases);
|
|
283
|
+
nextResolving.add(alias);
|
|
284
|
+
return evaluate(alias.typeAnnotation, nextSubstitutions, nextResolving);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
return matcher(current, (child) => evaluate(child, substitutions, resolvingAliases));
|
|
290
|
+
};
|
|
291
|
+
return evaluate(type, new Map, new Set);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// vendor/anti-slop/src/shared/dictionary-types.ts
|
|
295
|
+
var BUILT_INS = new Set([
|
|
296
|
+
"Record",
|
|
297
|
+
"Readonly",
|
|
298
|
+
"Partial",
|
|
299
|
+
"Required",
|
|
300
|
+
"Pick",
|
|
301
|
+
"Omit",
|
|
302
|
+
"PropertyKey",
|
|
303
|
+
"NonNullable"
|
|
304
|
+
]);
|
|
305
|
+
var TRANSPARENT_WRAPPERS = new Set(["Readonly", "Partial", "Required", "NonNullable"]);
|
|
306
|
+
function declaredStatement(statement) {
|
|
307
|
+
return statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? statement.declaration ?? null : statement;
|
|
308
|
+
}
|
|
309
|
+
function createTypeEnvironment(program, visitorKeys) {
|
|
310
|
+
const interfaces = new Map;
|
|
311
|
+
for (const statement of program.body) {
|
|
312
|
+
const declaration = declaredStatement(statement);
|
|
313
|
+
if (declaration?.type !== "TSInterfaceDeclaration")
|
|
314
|
+
continue;
|
|
315
|
+
const declarations = interfaces.get(declaration.id.name) ?? [];
|
|
316
|
+
declarations.push(declaration);
|
|
317
|
+
interfaces.set(declaration.id.name, declarations);
|
|
318
|
+
}
|
|
319
|
+
return {
|
|
320
|
+
interfaces,
|
|
321
|
+
typeAliases: createTypeAliasEnvironment(program, visitorKeys)
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
function typeReferenceName2(type) {
|
|
325
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
326
|
+
}
|
|
327
|
+
function isBuiltIn(name, use, environment) {
|
|
328
|
+
return BUILT_INS.has(name) && !hasVisibleTypeBinding(name, use, environment.typeAliases);
|
|
329
|
+
}
|
|
330
|
+
function isUnappliedReferenceTo(type, name) {
|
|
331
|
+
const unwrapped = unwrapTransparentType(type);
|
|
332
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName2(unwrapped) === name && (unwrapped.typeArguments === null || unwrapped.typeArguments === undefined || unwrapped.typeArguments.params.length === 0);
|
|
333
|
+
}
|
|
334
|
+
function unwrapTransparentType(type) {
|
|
335
|
+
let current = type;
|
|
336
|
+
while (current.type === "TSParenthesizedType" || current.type === "TSTypeOperator" && current.operator === "readonly") {
|
|
337
|
+
current = current.typeAnnotation;
|
|
338
|
+
}
|
|
339
|
+
return current;
|
|
340
|
+
}
|
|
341
|
+
function isNeverType(type) {
|
|
342
|
+
return unwrapTransparentType(type).type === "TSNeverKeyword";
|
|
343
|
+
}
|
|
344
|
+
function isEffectivelyEmptyMember(member) {
|
|
345
|
+
return member.type === "TSPropertySignature" && member.optional === true && member.typeAnnotation !== null && member.typeAnnotation !== undefined && isNeverType(member.typeAnnotation.typeAnnotation);
|
|
346
|
+
}
|
|
347
|
+
function isEffectivelyEmptyTypeLiteral(type) {
|
|
348
|
+
return type.members.length === 0 || type.members.every(isEffectivelyEmptyMember);
|
|
349
|
+
}
|
|
350
|
+
function isEffectivelyEmptyInterface(declarations) {
|
|
351
|
+
if (declarations.length !== 1)
|
|
352
|
+
return false;
|
|
353
|
+
const [type] = declarations;
|
|
354
|
+
return type !== undefined && type.extends.length === 0 && (type.body.body.length === 0 || type.body.body.every(isEffectivelyEmptyMember));
|
|
355
|
+
}
|
|
356
|
+
function resolvedSubstitutionArgument(type, base, resolving = new Set) {
|
|
357
|
+
const unwrapped = unwrapTransparentType(type);
|
|
358
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
359
|
+
return type;
|
|
360
|
+
const name = typeReferenceName2(unwrapped);
|
|
361
|
+
if (name === null || resolving.has(name))
|
|
362
|
+
return type;
|
|
363
|
+
const substitution = base.get(name);
|
|
364
|
+
if (substitution === undefined)
|
|
365
|
+
return type;
|
|
366
|
+
const nextResolving = new Set(resolving);
|
|
367
|
+
nextResolving.add(name);
|
|
368
|
+
return resolvedSubstitutionArgument(substitution, base, nextResolving);
|
|
369
|
+
}
|
|
370
|
+
function aliasSubstitution(alias, type, base) {
|
|
371
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
372
|
+
const arguments_ = type.typeArguments?.params ?? [];
|
|
373
|
+
const next = new Map(base);
|
|
374
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
375
|
+
const argument = arguments_[index] ?? parameter.default;
|
|
376
|
+
if (argument === null || argument === undefined)
|
|
377
|
+
return null;
|
|
378
|
+
next.set(parameter.name.name, resolvedSubstitutionArgument(argument, next));
|
|
379
|
+
}
|
|
380
|
+
return next;
|
|
381
|
+
}
|
|
382
|
+
function unsafeDirectValue(type, environment, substitutions, resolvingAliases) {
|
|
383
|
+
const unwrapped = unwrapTransparentType(type);
|
|
384
|
+
if (unwrapped.type === "TSUnknownKeyword")
|
|
385
|
+
return "unknown";
|
|
386
|
+
if (unwrapped.type === "TSAnyKeyword")
|
|
387
|
+
return "any";
|
|
388
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
389
|
+
return "object";
|
|
390
|
+
if (unwrapped.type === "TSTypeLiteral" && isEffectivelyEmptyTypeLiteral(unwrapped))
|
|
391
|
+
return "empty-object";
|
|
392
|
+
if (unwrapped.type === "TSUnionType") {
|
|
393
|
+
return unwrapped.types.some((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases) !== null) ? "union" : null;
|
|
394
|
+
}
|
|
395
|
+
if (unwrapped.type === "TSIntersectionType") {
|
|
396
|
+
const unsafeMembers = unwrapped.types.map((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases));
|
|
397
|
+
if (unsafeMembers.includes("any"))
|
|
398
|
+
return "any";
|
|
399
|
+
return unsafeMembers.length > 0 && unsafeMembers.every((member) => member !== null) ? unsafeMembers[0] : null;
|
|
400
|
+
}
|
|
401
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
402
|
+
return null;
|
|
403
|
+
const name = typeReferenceName2(unwrapped);
|
|
404
|
+
if (name === null)
|
|
405
|
+
return null;
|
|
406
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
407
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
408
|
+
return wrapped === undefined ? null : unsafeDirectValue(wrapped, environment, substitutions, resolvingAliases);
|
|
409
|
+
}
|
|
410
|
+
const substitution = substitutions.get(name);
|
|
411
|
+
if (substitution !== undefined) {
|
|
412
|
+
return isUnappliedReferenceTo(substitution, name) ? null : unsafeDirectValue(substitution, environment, substitutions, resolvingAliases);
|
|
413
|
+
}
|
|
414
|
+
const interfaceDeclarations = environment.interfaces.get(name);
|
|
415
|
+
if (interfaceDeclarations !== undefined) {
|
|
416
|
+
return isEffectivelyEmptyInterface(interfaceDeclarations) ? "empty-object" : null;
|
|
417
|
+
}
|
|
418
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
419
|
+
if (alias === null || resolvingAliases.has(name))
|
|
420
|
+
return null;
|
|
421
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
422
|
+
if (nextSubstitutions === null)
|
|
423
|
+
return null;
|
|
424
|
+
const nextResolving = new Set(resolvingAliases);
|
|
425
|
+
nextResolving.add(name);
|
|
426
|
+
return unsafeDirectValue(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
427
|
+
}
|
|
428
|
+
function dictionaryValueTypes(type, environment, substitutions, resolvingAliases) {
|
|
429
|
+
const unwrapped = unwrapTransparentType(type);
|
|
430
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
431
|
+
return unwrapped.members.flatMap((member) => member.type === "TSIndexSignature" && member.typeAnnotation !== null ? [{ type: member.typeAnnotation.typeAnnotation, substitutions }] : []);
|
|
432
|
+
}
|
|
433
|
+
if (unwrapped.type === "TSMappedType") {
|
|
434
|
+
return unwrapped.typeAnnotation === null ? [] : [{ type: unwrapped.typeAnnotation, substitutions }];
|
|
435
|
+
}
|
|
436
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
437
|
+
return [];
|
|
438
|
+
const name = typeReferenceName2(unwrapped);
|
|
439
|
+
if (name === null)
|
|
440
|
+
return [];
|
|
441
|
+
const substitution = substitutions.get(name);
|
|
442
|
+
if (substitution !== undefined) {
|
|
443
|
+
return isUnappliedReferenceTo(substitution, name) ? [] : dictionaryValueTypes(substitution, environment, substitutions, resolvingAliases);
|
|
444
|
+
}
|
|
445
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
446
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
447
|
+
return wrapped === undefined ? [] : dictionaryValueTypes(wrapped, environment, substitutions, resolvingAliases);
|
|
448
|
+
}
|
|
449
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) {
|
|
450
|
+
const value = unwrapped.typeArguments?.params[1] ?? null;
|
|
451
|
+
return value === null ? [] : [{ type: value, substitutions }];
|
|
452
|
+
}
|
|
453
|
+
if ((name === "Pick" || name === "Omit") && isBuiltIn(name, unwrapped, environment)) {
|
|
454
|
+
const source = unwrapped.typeArguments?.params[0];
|
|
455
|
+
return source === undefined ? [] : dictionaryValueTypes(source, environment, substitutions, resolvingAliases);
|
|
456
|
+
}
|
|
457
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
458
|
+
if (alias === null || resolvingAliases.has(name))
|
|
459
|
+
return [];
|
|
460
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
461
|
+
if (nextSubstitutions === null)
|
|
462
|
+
return [];
|
|
463
|
+
const nextResolving = new Set(resolvingAliases);
|
|
464
|
+
nextResolving.add(name);
|
|
465
|
+
return dictionaryValueTypes(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
466
|
+
}
|
|
467
|
+
function classifyUnsafeDictionaryValue(valueType, environment) {
|
|
468
|
+
const unsafeValue = unsafeDirectValue(valueType, environment, new Map, new Set);
|
|
469
|
+
return unsafeValue === null ? null : { kind: "unsafe-dictionary", unsafeValue };
|
|
470
|
+
}
|
|
471
|
+
function classifyUnsafeDictionary(type, environment) {
|
|
472
|
+
for (const valueType of dictionaryValueTypes(type, environment, new Map, new Set)) {
|
|
473
|
+
const unsafeValue = unsafeDirectValue(valueType.type, environment, valueType.substitutions, new Set);
|
|
474
|
+
if (unsafeValue !== null)
|
|
475
|
+
return { kind: "unsafe-dictionary", unsafeValue };
|
|
476
|
+
}
|
|
477
|
+
return null;
|
|
478
|
+
}
|
|
479
|
+
function classifyWideningTarget(type, environment) {
|
|
480
|
+
const unwrapped = unwrapTransparentType(type);
|
|
481
|
+
if (unwrapped.type === "TSUnknownKeyword")
|
|
482
|
+
return { kind: "unknown" };
|
|
483
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
484
|
+
return { kind: "object" };
|
|
485
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
486
|
+
return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : unwrapped.members.length > 0 ? { kind: "anonymous object" } : null;
|
|
487
|
+
}
|
|
488
|
+
if (unwrapped.type === "TSMappedType")
|
|
489
|
+
return { kind: "open dictionary" };
|
|
490
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
491
|
+
return null;
|
|
492
|
+
const name = typeReferenceName2(unwrapped);
|
|
493
|
+
if (name === null)
|
|
494
|
+
return null;
|
|
495
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
496
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
497
|
+
return wrapped === undefined ? null : classifyWideningTarget(wrapped, environment);
|
|
498
|
+
}
|
|
499
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) {
|
|
500
|
+
return hasBroadRecordKey(unwrapped, environment, new Map) ? { kind: "open dictionary" } : null;
|
|
501
|
+
}
|
|
502
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
503
|
+
if (alias === null)
|
|
504
|
+
return null;
|
|
505
|
+
if ((alias.typeParameters?.params.length ?? 0) > 0) {
|
|
506
|
+
const substitutions2 = aliasSubstitution(alias, unwrapped, new Map);
|
|
507
|
+
const resolved2 = substitutions2 === null ? null : classifyAliasBroadTarget(alias.typeAnnotation, environment, substitutions2, new Set([name]));
|
|
508
|
+
return resolved2?.kind === "open dictionary" ? { kind: "generic container" } : null;
|
|
509
|
+
}
|
|
510
|
+
const substitutions = aliasSubstitution(alias, unwrapped, new Map);
|
|
511
|
+
if (substitutions === null)
|
|
512
|
+
return null;
|
|
513
|
+
const resolved = classifyAliasBroadTarget(alias.typeAnnotation, environment, substitutions, new Set([name]));
|
|
514
|
+
return resolved;
|
|
515
|
+
}
|
|
516
|
+
function hasBroadRecordKey(type, environment, substitutions) {
|
|
517
|
+
const key = type.typeArguments?.params[0];
|
|
518
|
+
return key === undefined || isBroadMappedKey(key, environment, substitutions);
|
|
519
|
+
}
|
|
520
|
+
function isBroadMappedKey(type, environment, substitutions, visitedAliases = new Set) {
|
|
521
|
+
const unwrapped = unwrapTransparentType(type);
|
|
522
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") {
|
|
523
|
+
return true;
|
|
524
|
+
}
|
|
525
|
+
if (unwrapped.type === "TSUnionType") {
|
|
526
|
+
return unwrapped.types.some((member) => isBroadMappedKey(member, environment, substitutions, visitedAliases));
|
|
527
|
+
}
|
|
528
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
529
|
+
return false;
|
|
530
|
+
const name = typeReferenceName2(unwrapped);
|
|
531
|
+
if (name === null)
|
|
532
|
+
return false;
|
|
533
|
+
const substitution = substitutions.get(name);
|
|
534
|
+
if (substitution !== undefined && !isUnappliedReferenceTo(substitution, name)) {
|
|
535
|
+
return isBroadMappedKey(substitution, environment, substitutions, visitedAliases);
|
|
536
|
+
}
|
|
537
|
+
if (name === "PropertyKey" && isBuiltIn(name, unwrapped, environment))
|
|
538
|
+
return true;
|
|
539
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
540
|
+
if (alias === null || (alias.typeParameters?.params.length ?? 0) > 0 || visitedAliases.has(name)) {
|
|
541
|
+
return false;
|
|
542
|
+
}
|
|
543
|
+
const nextVisited = new Set(visitedAliases);
|
|
544
|
+
nextVisited.add(name);
|
|
545
|
+
return isBroadMappedKey(alias.typeAnnotation, environment, substitutions, nextVisited);
|
|
546
|
+
}
|
|
547
|
+
function classifyAliasBroadTarget(type, environment, substitutions, resolvingAliases) {
|
|
548
|
+
const unwrapped = unwrapTransparentType(type);
|
|
549
|
+
if (unwrapped.type === "TSUnknownKeyword")
|
|
550
|
+
return { kind: "unknown" };
|
|
551
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
552
|
+
return { kind: "object" };
|
|
553
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
554
|
+
return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : null;
|
|
555
|
+
}
|
|
556
|
+
if (unwrapped.type === "TSMappedType") {
|
|
557
|
+
return isBroadMappedKey(unwrapped.constraint, environment, substitutions) ? { kind: "open dictionary" } : null;
|
|
558
|
+
}
|
|
559
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
560
|
+
return null;
|
|
561
|
+
const name = typeReferenceName2(unwrapped);
|
|
562
|
+
if (name === null)
|
|
563
|
+
return null;
|
|
564
|
+
const substitution = substitutions.get(name);
|
|
565
|
+
if (substitution !== undefined) {
|
|
566
|
+
return isUnappliedReferenceTo(substitution, name) ? null : classifyAliasBroadTarget(substitution, environment, substitutions, resolvingAliases);
|
|
567
|
+
}
|
|
568
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
569
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
570
|
+
return wrapped === undefined ? null : classifyAliasBroadTarget(wrapped, environment, substitutions, resolvingAliases);
|
|
571
|
+
}
|
|
572
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) {
|
|
573
|
+
return hasBroadRecordKey(unwrapped, environment, substitutions) ? { kind: "open dictionary" } : null;
|
|
574
|
+
}
|
|
575
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
576
|
+
if (alias === null || resolvingAliases.has(name))
|
|
577
|
+
return null;
|
|
578
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
579
|
+
if (nextSubstitutions === null)
|
|
580
|
+
return null;
|
|
581
|
+
const nextResolving = new Set(resolvingAliases);
|
|
582
|
+
nextResolving.add(name);
|
|
583
|
+
return classifyAliasBroadTarget(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
584
|
+
}
|
|
585
|
+
function isKnownEvidenceExpression(expression) {
|
|
586
|
+
let current = expression;
|
|
587
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression") {
|
|
588
|
+
current = current.expression;
|
|
589
|
+
}
|
|
590
|
+
if (current.type === "ObjectExpression")
|
|
591
|
+
return true;
|
|
592
|
+
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";
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// vendor/anti-slop/src/shared/function-parameters.ts
|
|
596
|
+
function containsUnknownType(type) {
|
|
597
|
+
if (type.type === "TSUnknownKeyword")
|
|
598
|
+
return true;
|
|
599
|
+
if (type.type === "TSParenthesizedType")
|
|
600
|
+
return containsUnknownType(type.typeAnnotation);
|
|
601
|
+
return type.type === "TSUnionType" && type.types.some(containsUnknownType);
|
|
602
|
+
}
|
|
603
|
+
function functionParameterTypeAnnotation(parameter) {
|
|
604
|
+
if (parameter.type === "TSParameterProperty") {
|
|
605
|
+
return functionParameterTypeAnnotation(parameter.parameter);
|
|
606
|
+
}
|
|
607
|
+
if (parameter.type === "RestElement") {
|
|
608
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
|
|
609
|
+
}
|
|
610
|
+
if (parameter.type === "AssignmentPattern") {
|
|
611
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
|
|
612
|
+
}
|
|
613
|
+
return parameter.typeAnnotation;
|
|
614
|
+
}
|
|
615
|
+
function functionParameterBindingName(parameter, sourceCode) {
|
|
616
|
+
if (parameter.type === "TSParameterProperty") {
|
|
617
|
+
return functionParameterBindingName(parameter.parameter, sourceCode);
|
|
618
|
+
}
|
|
619
|
+
if (parameter.type === "AssignmentPattern") {
|
|
620
|
+
return functionParameterBindingName(parameter.left, sourceCode);
|
|
621
|
+
}
|
|
622
|
+
if (parameter.type === "RestElement") {
|
|
623
|
+
return functionParameterBindingName(parameter.argument, sourceCode);
|
|
624
|
+
}
|
|
625
|
+
if (parameter.type === "Identifier")
|
|
626
|
+
return parameter.name;
|
|
627
|
+
const sourceText = sourceCode.getText(parameter);
|
|
628
|
+
const annotationStart = parameter.typeAnnotation?.start;
|
|
629
|
+
return annotationStart === undefined ? sourceText : sourceText.slice(0, annotationStart - parameter.start).trimEnd();
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
// vendor/anti-slop/src/rules/no-known-value-widening.ts
|
|
633
|
+
function unwrapExpression(expression) {
|
|
634
|
+
let current = expression;
|
|
635
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression") {
|
|
636
|
+
current = current.expression;
|
|
637
|
+
}
|
|
638
|
+
return current;
|
|
639
|
+
}
|
|
640
|
+
function resolveVariable(sourceCode, identifier) {
|
|
641
|
+
let scope = sourceCode.getScope(identifier);
|
|
642
|
+
while (scope !== null) {
|
|
643
|
+
const variable = scope.set.get(identifier.name);
|
|
644
|
+
if (variable !== undefined)
|
|
645
|
+
return variable;
|
|
646
|
+
scope = scope.upper;
|
|
647
|
+
}
|
|
648
|
+
return null;
|
|
649
|
+
}
|
|
650
|
+
function variableDeclarator(variable) {
|
|
651
|
+
if (variable.defs.length !== 1)
|
|
652
|
+
return null;
|
|
653
|
+
const [definition] = variable.defs;
|
|
654
|
+
return definition?.type === "Variable" && definition.node.type === "VariableDeclarator" ? definition.node : null;
|
|
655
|
+
}
|
|
656
|
+
function isStableConstVariable(variable, declarator) {
|
|
657
|
+
return declarator.parent.type === "VariableDeclaration" && declarator.parent.kind === "const" && variable.references.every((reference) => reference.init || !reference.isWrite());
|
|
658
|
+
}
|
|
659
|
+
function hasKnownEvidence(sourceCode, expression, visitedVariables = new Set) {
|
|
660
|
+
if (isKnownEvidenceExpression(expression))
|
|
661
|
+
return true;
|
|
662
|
+
const unwrapped = unwrapExpression(expression);
|
|
663
|
+
if (unwrapped.type !== "Identifier")
|
|
664
|
+
return false;
|
|
665
|
+
const variable = resolveVariable(sourceCode, unwrapped);
|
|
666
|
+
if (variable === null || visitedVariables.has(variable))
|
|
667
|
+
return false;
|
|
668
|
+
const declarator = variableDeclarator(variable);
|
|
669
|
+
if (declarator === null || declarator.init === null || !isStableConstVariable(variable, declarator)) {
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
visitedVariables.add(variable);
|
|
673
|
+
return hasKnownEvidence(sourceCode, declarator.init, visitedVariables);
|
|
674
|
+
}
|
|
675
|
+
function isFunctionExpression(node) {
|
|
676
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "TSDeclareFunction" || node.type === "TSEmptyBodyFunctionExpression";
|
|
677
|
+
}
|
|
678
|
+
function localFunctionForCall(sourceCode, callee) {
|
|
679
|
+
const unwrapped = unwrapExpression(callee);
|
|
680
|
+
if (isFunctionExpression(unwrapped))
|
|
681
|
+
return unwrapped;
|
|
682
|
+
if (unwrapped.type !== "Identifier")
|
|
683
|
+
return null;
|
|
684
|
+
const variable = resolveVariable(sourceCode, unwrapped);
|
|
685
|
+
if (variable === null || variable.defs.length !== 1)
|
|
686
|
+
return null;
|
|
687
|
+
const [definition] = variable.defs;
|
|
688
|
+
if (definition === undefined)
|
|
689
|
+
return null;
|
|
690
|
+
if (definition.type === "FunctionName" && isFunctionExpression(definition.node)) {
|
|
691
|
+
return definition.node;
|
|
692
|
+
}
|
|
693
|
+
if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") {
|
|
694
|
+
return null;
|
|
695
|
+
}
|
|
696
|
+
const initializer = definition.node.init;
|
|
697
|
+
if (initializer === null)
|
|
698
|
+
return null;
|
|
699
|
+
const unwrappedInitializer = unwrapExpression(initializer);
|
|
700
|
+
return isFunctionExpression(unwrappedInitializer) ? unwrappedInitializer : null;
|
|
701
|
+
}
|
|
702
|
+
function variableTypeAnnotation(sourceCode, variable) {
|
|
703
|
+
if (variable.defs.length !== 1)
|
|
704
|
+
return null;
|
|
705
|
+
const [definition] = variable.defs;
|
|
706
|
+
if (definition === undefined)
|
|
707
|
+
return null;
|
|
708
|
+
if (definition.type === "Variable" && definition.node.type === "VariableDeclarator" && definition.node.id.type === "Identifier") {
|
|
709
|
+
return definition.node.id.typeAnnotation ?? null;
|
|
710
|
+
}
|
|
711
|
+
if (definition.type !== "Parameter" || !isFunctionExpression(definition.node)) {
|
|
712
|
+
return null;
|
|
713
|
+
}
|
|
714
|
+
const parameter = definition.node.params.find((candidate) => functionParameterBindingName(candidate, sourceCode) === variable.name);
|
|
715
|
+
return parameter === undefined ? null : functionParameterTypeAnnotation(parameter) ?? null;
|
|
716
|
+
}
|
|
717
|
+
function hasInformativeType(type, environment) {
|
|
718
|
+
return classifyUnsafeDictionaryValue(type, environment) === null;
|
|
719
|
+
}
|
|
720
|
+
function hasKnownCallArgumentEvidence(sourceCode, expression, environment, visitedVariables = new Set) {
|
|
721
|
+
if (expression.type === "ParenthesizedExpression" || expression.type === "TSNonNullExpression") {
|
|
722
|
+
return hasKnownCallArgumentEvidence(sourceCode, expression.expression, environment, visitedVariables);
|
|
723
|
+
}
|
|
724
|
+
if (expression.type === "TSAsExpression" || expression.type === "TSTypeAssertion") {
|
|
725
|
+
return hasInformativeType(expression.typeAnnotation, environment);
|
|
726
|
+
}
|
|
727
|
+
if (expression.type === "TSSatisfiesExpression") {
|
|
728
|
+
return hasKnownCallArgumentEvidence(sourceCode, expression.expression, environment, visitedVariables);
|
|
729
|
+
}
|
|
730
|
+
if (expression.type === "CallExpression") {
|
|
731
|
+
const owner = localFunctionForCall(sourceCode, expression.callee);
|
|
732
|
+
const returnType = owner?.returnType?.typeAnnotation;
|
|
733
|
+
return returnType !== undefined && hasInformativeType(returnType, environment);
|
|
734
|
+
}
|
|
735
|
+
if (expression.type !== "Identifier")
|
|
736
|
+
return isKnownEvidenceExpression(expression);
|
|
737
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
738
|
+
if (variable === null || visitedVariables.has(variable))
|
|
739
|
+
return false;
|
|
740
|
+
const annotation = variableTypeAnnotation(sourceCode, variable);
|
|
741
|
+
if (annotation !== null) {
|
|
742
|
+
return hasInformativeType(annotation.typeAnnotation, environment);
|
|
743
|
+
}
|
|
744
|
+
const declarator = variableDeclarator(variable);
|
|
745
|
+
if (declarator === null || declarator.init === null || !isStableConstVariable(variable, declarator)) {
|
|
746
|
+
return false;
|
|
747
|
+
}
|
|
748
|
+
visitedVariables.add(variable);
|
|
749
|
+
return hasKnownCallArgumentEvidence(sourceCode, declarator.init, environment, visitedVariables);
|
|
750
|
+
}
|
|
751
|
+
function typePredicateSubjectIndex(sourceCode, owner) {
|
|
752
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
753
|
+
if (predicate?.type !== "TSTypePredicate" || predicate.parameterName.type !== "Identifier") {
|
|
754
|
+
return null;
|
|
755
|
+
}
|
|
756
|
+
const predicateParameterName = predicate.parameterName.name;
|
|
757
|
+
const index = owner.params.findIndex((parameter) => functionParameterBindingName(parameter, sourceCode) === predicateParameterName);
|
|
758
|
+
return index === -1 ? null : index;
|
|
759
|
+
}
|
|
760
|
+
function annotationTarget(annotation, environment) {
|
|
761
|
+
return annotation === null || annotation === undefined ? null : classifyWideningTarget(annotation.typeAnnotation, environment);
|
|
762
|
+
}
|
|
763
|
+
function enclosingFunction(node) {
|
|
764
|
+
let current = node.parent;
|
|
765
|
+
while (current !== null && current.type !== "Program") {
|
|
766
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionDeclaration" || current.type === "FunctionExpression") {
|
|
767
|
+
return current;
|
|
768
|
+
}
|
|
769
|
+
current = current.parent;
|
|
770
|
+
}
|
|
771
|
+
return null;
|
|
772
|
+
}
|
|
773
|
+
function sourceKeyName(sourceCode, key) {
|
|
774
|
+
if (key.type === "Identifier" || key.type === "PrivateIdentifier")
|
|
775
|
+
return key.name;
|
|
776
|
+
if (key.type === "Literal")
|
|
777
|
+
return String(key.value);
|
|
778
|
+
return sourceCode.getText(key);
|
|
779
|
+
}
|
|
780
|
+
function functionName(sourceCode, owner) {
|
|
781
|
+
if (owner === null)
|
|
782
|
+
return "anonymous function";
|
|
783
|
+
if (owner.id !== null)
|
|
784
|
+
return owner.id.name;
|
|
785
|
+
const parent = owner.parent;
|
|
786
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier")
|
|
787
|
+
return parent.id.name;
|
|
788
|
+
if (parent.type === "MethodDefinition")
|
|
789
|
+
return sourceKeyName(sourceCode, parent.key);
|
|
790
|
+
return "anonymous function";
|
|
791
|
+
}
|
|
792
|
+
function isEmptyObjectExpression2(expression) {
|
|
793
|
+
const unwrapped = unwrapExpression(expression);
|
|
794
|
+
return unwrapped.type === "ObjectExpression" && unwrapped.properties.length === 0;
|
|
795
|
+
}
|
|
796
|
+
function isDictionaryAccumulatorTarget(destination) {
|
|
797
|
+
return destination.kind === "open dictionary" || destination.kind === "generic container";
|
|
798
|
+
}
|
|
799
|
+
function hasParentAssertion(node) {
|
|
800
|
+
return node.parent?.type === "TSAsExpression" || node.parent?.type === "TSTypeAssertion";
|
|
801
|
+
}
|
|
802
|
+
var noKnownValueWideningRule = defineRule3({
|
|
803
|
+
meta: {
|
|
804
|
+
type: "problem",
|
|
805
|
+
docs: {
|
|
806
|
+
description: "Disallow syntactically established values from flowing into explicitly broad or anonymous target types that discard useful evidence."
|
|
807
|
+
},
|
|
808
|
+
messages: {
|
|
809
|
+
widening: "The explicit {{target}} type on {{subject}} discards known type evidence. Keep inference, validate with `satisfies`, or use a named owner contract."
|
|
810
|
+
}
|
|
811
|
+
},
|
|
812
|
+
createOnce(context) {
|
|
813
|
+
let environment = null;
|
|
814
|
+
const reportFlow = (expression, destination, subject) => {
|
|
815
|
+
if (destination === null)
|
|
816
|
+
return;
|
|
817
|
+
if (isDictionaryAccumulatorTarget(destination) && isEmptyObjectExpression2(expression)) {
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
if (!hasKnownEvidence(context.sourceCode, expression))
|
|
821
|
+
return;
|
|
822
|
+
context.report({
|
|
823
|
+
node: expression,
|
|
824
|
+
messageId: "widening",
|
|
825
|
+
data: { subject, target: destination.kind }
|
|
826
|
+
});
|
|
827
|
+
};
|
|
828
|
+
const targetFromAnnotation = (annotation) => environment === null ? null : annotationTarget(annotation, environment);
|
|
829
|
+
return {
|
|
830
|
+
Program(node) {
|
|
831
|
+
environment = createTypeEnvironment(node, context.sourceCode.visitorKeys);
|
|
832
|
+
},
|
|
833
|
+
VariableDeclarator(node) {
|
|
834
|
+
if (node.init === null || node.id.type !== "Identifier")
|
|
835
|
+
return;
|
|
836
|
+
reportFlow(node.init, targetFromAnnotation(node.id.typeAnnotation), `binding \`${node.id.name}\``);
|
|
837
|
+
},
|
|
838
|
+
PropertyDefinition(node) {
|
|
839
|
+
if (node.value === null)
|
|
840
|
+
return;
|
|
841
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
842
|
+
},
|
|
843
|
+
AccessorProperty(node) {
|
|
844
|
+
if (node.value === null)
|
|
845
|
+
return;
|
|
846
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
847
|
+
},
|
|
848
|
+
AssignmentExpression(node) {
|
|
849
|
+
if (node.operator !== "=" || node.left.type !== "Identifier")
|
|
850
|
+
return;
|
|
851
|
+
const variable = resolveVariable(context.sourceCode, node.left);
|
|
852
|
+
if (variable === null)
|
|
853
|
+
return;
|
|
854
|
+
const declarator = variableDeclarator(variable);
|
|
855
|
+
if (declarator === null || declarator.id.type !== "Identifier")
|
|
856
|
+
return;
|
|
857
|
+
reportFlow(node.right, targetFromAnnotation(declarator.id.typeAnnotation), `binding \`${declarator.id.name}\``);
|
|
858
|
+
},
|
|
859
|
+
CallExpression(node) {
|
|
860
|
+
if (environment === null)
|
|
861
|
+
return;
|
|
862
|
+
const owner = localFunctionForCall(context.sourceCode, node.callee);
|
|
863
|
+
if (owner === null)
|
|
864
|
+
return;
|
|
865
|
+
const parameterIndex = typePredicateSubjectIndex(context.sourceCode, owner);
|
|
866
|
+
if (parameterIndex === null)
|
|
867
|
+
return;
|
|
868
|
+
const parameter = owner.params[parameterIndex];
|
|
869
|
+
const argument = node.arguments[parameterIndex];
|
|
870
|
+
if (parameter === undefined || argument === undefined || argument.type === "SpreadElement") {
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
const parameterAnnotation = functionParameterTypeAnnotation(parameter);
|
|
874
|
+
if (parameterAnnotation === null || parameterAnnotation === undefined || !containsUnknownType(parameterAnnotation.typeAnnotation)) {
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
if (!hasKnownCallArgumentEvidence(context.sourceCode, argument, environment)) {
|
|
878
|
+
return;
|
|
879
|
+
}
|
|
880
|
+
context.report({
|
|
881
|
+
node: argument,
|
|
882
|
+
messageId: "widening",
|
|
883
|
+
data: {
|
|
884
|
+
subject: `argument for parameter \`${functionParameterBindingName(parameter, context.sourceCode)}\` of \`${functionName(context.sourceCode, owner)}\``,
|
|
885
|
+
target: "unknown"
|
|
886
|
+
}
|
|
887
|
+
});
|
|
888
|
+
},
|
|
889
|
+
ReturnStatement(node) {
|
|
890
|
+
if (node.argument === null)
|
|
891
|
+
return;
|
|
892
|
+
const owner = enclosingFunction(node);
|
|
893
|
+
reportFlow(node.argument, targetFromAnnotation(owner?.returnType), `return value of \`${functionName(context.sourceCode, owner)}\``);
|
|
894
|
+
},
|
|
895
|
+
ArrowFunctionExpression(node) {
|
|
896
|
+
if (node.body.type === "BlockStatement")
|
|
897
|
+
return;
|
|
898
|
+
reportFlow(node.body, targetFromAnnotation(node.returnType), `return value of \`${functionName(context.sourceCode, node)}\``);
|
|
899
|
+
},
|
|
900
|
+
TSAsExpression(node) {
|
|
901
|
+
if (environment === null || hasParentAssertion(node))
|
|
902
|
+
return;
|
|
903
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
904
|
+
},
|
|
905
|
+
TSTypeAssertion(node) {
|
|
906
|
+
if (environment === null || hasParentAssertion(node))
|
|
907
|
+
return;
|
|
908
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
});
|
|
913
|
+
|
|
914
|
+
// vendor/anti-slop/src/rules/no-module-mocking.ts
|
|
915
|
+
import { defineRule as defineRule4 } from "@oxlint/plugins";
|
|
916
|
+
var moduleMockMethods = new Set(["doMock", "mock", "unstable_mockModule"]);
|
|
917
|
+
function resolveVariable2(sourceCode, identifier) {
|
|
918
|
+
let scope = sourceCode.getScope(identifier);
|
|
919
|
+
while (scope !== null) {
|
|
920
|
+
const variable = scope.set.get(identifier.name);
|
|
921
|
+
if (variable !== undefined)
|
|
922
|
+
return variable;
|
|
923
|
+
scope = scope.upper;
|
|
924
|
+
}
|
|
925
|
+
return null;
|
|
926
|
+
}
|
|
927
|
+
function importedName(node) {
|
|
928
|
+
if (node.type !== "ImportSpecifier")
|
|
929
|
+
return null;
|
|
930
|
+
return node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
931
|
+
}
|
|
932
|
+
function isTestFrameworkObject(sourceCode, expression) {
|
|
933
|
+
if (expression.type !== "Identifier")
|
|
934
|
+
return false;
|
|
935
|
+
if ((expression.name === "vi" || expression.name === "jest") && sourceCode.isGlobalReference(expression)) {
|
|
936
|
+
return true;
|
|
937
|
+
}
|
|
938
|
+
const variable = resolveVariable2(sourceCode, expression);
|
|
939
|
+
if (variable === null || variable.defs.length === 0) {
|
|
940
|
+
return expression.name === "vi" || expression.name === "jest";
|
|
941
|
+
}
|
|
942
|
+
return variable.defs.some((definition) => {
|
|
943
|
+
if (definition.type !== "ImportBinding" || definition.parent?.type !== "ImportDeclaration") {
|
|
944
|
+
return false;
|
|
945
|
+
}
|
|
946
|
+
const source = definition.parent.source.value;
|
|
947
|
+
const name = importedName(definition.node);
|
|
948
|
+
return source === "vitest" && name === "vi" || source === "@jest/globals" && name === "jest";
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
function moduleMockCall(sourceCode, callee) {
|
|
952
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee))
|
|
953
|
+
return false;
|
|
954
|
+
if (!isTestFrameworkObject(sourceCode, callee.object))
|
|
955
|
+
return false;
|
|
956
|
+
const property = callee.property;
|
|
957
|
+
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;
|
|
958
|
+
return method !== null && moduleMockMethods.has(method);
|
|
959
|
+
}
|
|
960
|
+
var noModuleMockingRule = defineRule4({
|
|
961
|
+
meta: {
|
|
962
|
+
type: "problem",
|
|
963
|
+
docs: {
|
|
964
|
+
description: "Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces."
|
|
965
|
+
},
|
|
966
|
+
messages: {
|
|
967
|
+
moduleMock: "Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation."
|
|
968
|
+
}
|
|
969
|
+
},
|
|
970
|
+
createOnce(context) {
|
|
971
|
+
return {
|
|
972
|
+
CallExpression(node) {
|
|
973
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
974
|
+
return;
|
|
975
|
+
if (moduleMockCall(context.sourceCode, node.callee)) {
|
|
976
|
+
context.report({ node, messageId: "moduleMock" });
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
});
|
|
982
|
+
|
|
983
|
+
// vendor/anti-slop/src/rules/no-object-parameters.ts
|
|
984
|
+
import { defineRule as defineRule5 } from "@oxlint/plugins";
|
|
985
|
+
var noObjectParametersRule = defineRule5({
|
|
986
|
+
meta: {
|
|
987
|
+
type: "problem",
|
|
988
|
+
docs: {
|
|
989
|
+
description: "Disallow object function parameters; inputs must use an owner-provided type and be parsed at their boundary."
|
|
990
|
+
},
|
|
991
|
+
messages: {
|
|
992
|
+
objectParameter: "Parameter `{{parameter}}` uses the broad `object` type. Accept a named owner type; parse external input at its boundary before calling this function."
|
|
993
|
+
}
|
|
994
|
+
},
|
|
995
|
+
createOnce(context) {
|
|
996
|
+
let environment = null;
|
|
997
|
+
const resolvesToObject = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
998
|
+
if (resolved.type === "TSObjectKeyword")
|
|
999
|
+
return true;
|
|
1000
|
+
if (resolved.type === "TSParenthesizedType") {
|
|
1001
|
+
return matches(resolved.typeAnnotation);
|
|
1002
|
+
}
|
|
1003
|
+
return resolved.type === "TSUnionType" && resolved.types.some(matches);
|
|
1004
|
+
});
|
|
1005
|
+
const checkParameters = (node) => {
|
|
1006
|
+
for (const parameter of node.params) {
|
|
1007
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
1008
|
+
if (annotation === null || annotation === undefined)
|
|
1009
|
+
continue;
|
|
1010
|
+
if (!resolvesToObject(annotation.typeAnnotation))
|
|
1011
|
+
continue;
|
|
1012
|
+
context.report({
|
|
1013
|
+
node: annotation.typeAnnotation,
|
|
1014
|
+
messageId: "objectParameter",
|
|
1015
|
+
data: { parameter: functionParameterBindingName(parameter, context.sourceCode) }
|
|
1016
|
+
});
|
|
1017
|
+
}
|
|
1018
|
+
};
|
|
1019
|
+
return {
|
|
1020
|
+
Program(node) {
|
|
1021
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
1022
|
+
},
|
|
1023
|
+
ArrowFunctionExpression: checkParameters,
|
|
1024
|
+
FunctionDeclaration: checkParameters,
|
|
1025
|
+
FunctionExpression: checkParameters,
|
|
1026
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
1027
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
1028
|
+
TSConstructorType: checkParameters,
|
|
1029
|
+
TSDeclareFunction: checkParameters,
|
|
1030
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
1031
|
+
TSFunctionType: checkParameters,
|
|
1032
|
+
TSMethodSignature: checkParameters
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
// vendor/anti-slop/src/rules/no-reflect-apply.ts
|
|
1038
|
+
import { defineRule as defineRule6 } from "@oxlint/plugins";
|
|
1039
|
+
|
|
1040
|
+
// vendor/anti-slop/src/shared/reflect-method.ts
|
|
1041
|
+
function resolveVariable3(sourceCode, identifier) {
|
|
1042
|
+
let scope = sourceCode.getScope(identifier);
|
|
1043
|
+
while (scope !== null) {
|
|
1044
|
+
const variable = scope.set.get(identifier.name);
|
|
1045
|
+
if (variable !== undefined)
|
|
1046
|
+
return variable;
|
|
1047
|
+
scope = scope.upper;
|
|
1048
|
+
}
|
|
1049
|
+
return null;
|
|
1050
|
+
}
|
|
1051
|
+
function isGlobalReflect(sourceCode, expression) {
|
|
1052
|
+
if (expression.type !== "Identifier" || expression.name !== "Reflect")
|
|
1053
|
+
return false;
|
|
1054
|
+
if (sourceCode.isGlobalReference(expression))
|
|
1055
|
+
return true;
|
|
1056
|
+
const variable = resolveVariable3(sourceCode, expression);
|
|
1057
|
+
return variable === null || variable.defs.length === 0;
|
|
1058
|
+
}
|
|
1059
|
+
function isGlobalReflectMethodCall(sourceCode, callee, methodName) {
|
|
1060
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee))
|
|
1061
|
+
return false;
|
|
1062
|
+
if (!isGlobalReflect(sourceCode, callee.object))
|
|
1063
|
+
return false;
|
|
1064
|
+
const property = callee.property;
|
|
1065
|
+
return callee.computed ? property.type === "Literal" && property.value === methodName : property.type === "Identifier" && property.name === methodName;
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
// vendor/anti-slop/src/rules/no-reflect-apply.ts
|
|
1069
|
+
var noReflectApplyRule = defineRule6({
|
|
1070
|
+
meta: {
|
|
1071
|
+
type: "problem",
|
|
1072
|
+
docs: {
|
|
1073
|
+
description: "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface."
|
|
1074
|
+
},
|
|
1075
|
+
messages: {
|
|
1076
|
+
reflectApply: "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface."
|
|
1077
|
+
}
|
|
1078
|
+
},
|
|
1079
|
+
createOnce(context) {
|
|
1080
|
+
return {
|
|
1081
|
+
CallExpression(node) {
|
|
1082
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
1083
|
+
return;
|
|
1084
|
+
if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "apply")) {
|
|
1085
|
+
context.report({ node, messageId: "reflectApply" });
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
};
|
|
1089
|
+
}
|
|
1090
|
+
});
|
|
1091
|
+
|
|
1092
|
+
// vendor/anti-slop/src/rules/no-reflect-get.ts
|
|
1093
|
+
import { defineRule as defineRule7 } from "@oxlint/plugins";
|
|
1094
|
+
var noReflectGetRule = defineRule7({
|
|
1095
|
+
meta: {
|
|
1096
|
+
type: "problem",
|
|
1097
|
+
docs: {
|
|
1098
|
+
description: "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type."
|
|
1099
|
+
},
|
|
1100
|
+
messages: {
|
|
1101
|
+
reflectGet: "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it."
|
|
1102
|
+
}
|
|
1103
|
+
},
|
|
1104
|
+
createOnce(context) {
|
|
1105
|
+
return {
|
|
1106
|
+
CallExpression(node) {
|
|
1107
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression")
|
|
1108
|
+
return;
|
|
1109
|
+
if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "get")) {
|
|
1110
|
+
context.report({ node, messageId: "reflectGet" });
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
// vendor/anti-slop/src/rules/no-runtime-typeof.ts
|
|
1118
|
+
import { defineRule as defineRule8 } from "@oxlint/plugins";
|
|
1119
|
+
function isRuntimeFunction(node) {
|
|
1120
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression";
|
|
1121
|
+
}
|
|
1122
|
+
function isInsideTypeGuard(node) {
|
|
1123
|
+
let current = node.parent;
|
|
1124
|
+
while (current !== null && current.type !== "Program") {
|
|
1125
|
+
if (isRuntimeFunction(current)) {
|
|
1126
|
+
return current.returnType?.typeAnnotation.type === "TSTypePredicate";
|
|
1127
|
+
}
|
|
1128
|
+
current = current.parent;
|
|
1129
|
+
}
|
|
1130
|
+
return false;
|
|
1131
|
+
}
|
|
1132
|
+
function isExistenceProbe(node) {
|
|
1133
|
+
const parent = node.parent;
|
|
1134
|
+
if (parent.type !== "BinaryExpression")
|
|
1135
|
+
return false;
|
|
1136
|
+
if (!["===", "!==", "==", "!="].includes(parent.operator))
|
|
1137
|
+
return false;
|
|
1138
|
+
const other = parent.left === node ? parent.right : parent.left;
|
|
1139
|
+
return other.type === "Literal" && other.value === "undefined";
|
|
1140
|
+
}
|
|
1141
|
+
var noRuntimeTypeofRule = defineRule8({
|
|
1142
|
+
meta: {
|
|
1143
|
+
type: "problem",
|
|
1144
|
+
docs: {
|
|
1145
|
+
description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary."
|
|
1146
|
+
},
|
|
1147
|
+
messages: {
|
|
1148
|
+
runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value."
|
|
1149
|
+
},
|
|
1150
|
+
schema: [
|
|
1151
|
+
{
|
|
1152
|
+
type: "object",
|
|
1153
|
+
properties: {
|
|
1154
|
+
allowInTypeGuards: { type: "boolean" }
|
|
1155
|
+
},
|
|
1156
|
+
additionalProperties: false
|
|
1157
|
+
}
|
|
1158
|
+
],
|
|
1159
|
+
defaultOptions: [{ allowInTypeGuards: false }]
|
|
1160
|
+
},
|
|
1161
|
+
createOnce(context) {
|
|
1162
|
+
return {
|
|
1163
|
+
UnaryExpression(node) {
|
|
1164
|
+
const option = context.options?.[0];
|
|
1165
|
+
const allowInTypeGuards = typeof option === "object" && option !== null && !Array.isArray(option) && option.allowInTypeGuards === true;
|
|
1166
|
+
if (node.operator === "typeof" && !isExistenceProbe(node) && (!allowInTypeGuards || !isInsideTypeGuard(node))) {
|
|
1167
|
+
context.report({ node, messageId: "runtimeTypeof" });
|
|
1168
|
+
}
|
|
1169
|
+
}
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
});
|
|
1173
|
+
|
|
1174
|
+
// vendor/anti-slop/src/rules/no-shape-in-symbol-names.ts
|
|
1175
|
+
import { defineRule as defineRule9 } from "@oxlint/plugins";
|
|
1176
|
+
var FORBIDDEN_SYMBOL_NAME = "shape";
|
|
1177
|
+
function containsForbiddenSymbolName(name) {
|
|
1178
|
+
return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
|
|
1179
|
+
}
|
|
1180
|
+
function isBorrowedMemberName(node) {
|
|
1181
|
+
const parent = node.parent;
|
|
1182
|
+
if (parent === null || parent.type !== "MemberExpression")
|
|
1183
|
+
return false;
|
|
1184
|
+
return parent.property === node && parent.computed === false;
|
|
1185
|
+
}
|
|
1186
|
+
var noForbiddenTermInSymbolNamesRule = defineRule9({
|
|
1187
|
+
meta: {
|
|
1188
|
+
type: "problem",
|
|
1189
|
+
docs: {
|
|
1190
|
+
description: 'Disallow the case-insensitive substring "shape" in JavaScript, TypeScript, private, and JSX symbol names.'
|
|
1191
|
+
},
|
|
1192
|
+
messages: {
|
|
1193
|
+
forbiddenSymbolName: 'Rename symbol "{{name}}" for its domain role; "shape" describes structure rather than ownership.'
|
|
1194
|
+
}
|
|
1195
|
+
},
|
|
1196
|
+
createOnce(context) {
|
|
1197
|
+
const reportForbiddenSymbolName = (node) => {
|
|
1198
|
+
if (!containsForbiddenSymbolName(node.name) || isBorrowedMemberName(node))
|
|
1199
|
+
return;
|
|
1200
|
+
context.report({
|
|
1201
|
+
node,
|
|
1202
|
+
messageId: "forbiddenSymbolName",
|
|
1203
|
+
data: { name: node.name }
|
|
1204
|
+
});
|
|
1205
|
+
};
|
|
1206
|
+
return {
|
|
1207
|
+
Identifier: reportForbiddenSymbolName,
|
|
1208
|
+
PrivateIdentifier: reportForbiddenSymbolName,
|
|
1209
|
+
JSXIdentifier: reportForbiddenSymbolName
|
|
1210
|
+
};
|
|
1211
|
+
}
|
|
1212
|
+
});
|
|
1213
|
+
|
|
1214
|
+
// vendor/anti-slop/src/rules/no-unknown-parameters.ts
|
|
1215
|
+
import { defineRule as defineRule10 } from "@oxlint/plugins";
|
|
1216
|
+
function isTypePredicateSubject(owner, parameterName) {
|
|
1217
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
1218
|
+
return predicate?.type === "TSTypePredicate" && predicate.parameterName.type === "Identifier" && predicate.parameterName.name === parameterName;
|
|
1219
|
+
}
|
|
1220
|
+
var noUnknownParametersRule = defineRule10({
|
|
1221
|
+
meta: {
|
|
1222
|
+
type: "problem",
|
|
1223
|
+
docs: {
|
|
1224
|
+
description: "Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead."
|
|
1225
|
+
},
|
|
1226
|
+
messages: {
|
|
1227
|
+
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."
|
|
1228
|
+
}
|
|
1229
|
+
},
|
|
1230
|
+
createOnce(context) {
|
|
1231
|
+
const checkParameters = (node) => {
|
|
1232
|
+
for (const parameter of node.params) {
|
|
1233
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
1234
|
+
if (annotation === null || annotation === undefined)
|
|
1235
|
+
continue;
|
|
1236
|
+
if (!containsUnknownType(annotation.typeAnnotation))
|
|
1237
|
+
continue;
|
|
1238
|
+
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
1239
|
+
if (name === "cause" || isTypePredicateSubject(node, name))
|
|
1240
|
+
continue;
|
|
1241
|
+
context.report({
|
|
1242
|
+
node: annotation.typeAnnotation,
|
|
1243
|
+
messageId: "unknownParameter",
|
|
1244
|
+
data: { parameter: name }
|
|
1245
|
+
});
|
|
1246
|
+
}
|
|
1247
|
+
};
|
|
1248
|
+
return {
|
|
1249
|
+
ArrowFunctionExpression: checkParameters,
|
|
1250
|
+
FunctionDeclaration: checkParameters,
|
|
1251
|
+
FunctionExpression: checkParameters,
|
|
1252
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
1253
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
1254
|
+
TSConstructorType: checkParameters,
|
|
1255
|
+
TSDeclareFunction: checkParameters,
|
|
1256
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
1257
|
+
TSFunctionType: checkParameters,
|
|
1258
|
+
TSMethodSignature: checkParameters
|
|
1259
|
+
};
|
|
1260
|
+
}
|
|
1261
|
+
});
|
|
1262
|
+
|
|
1263
|
+
// vendor/anti-slop/src/rules/no-unknown-returns.ts
|
|
1264
|
+
import { defineRule as defineRule11 } from "@oxlint/plugins";
|
|
1265
|
+
var noUnknownReturnsRule = defineRule11({
|
|
1266
|
+
meta: {
|
|
1267
|
+
type: "problem",
|
|
1268
|
+
docs: {
|
|
1269
|
+
description: "Disallow functions whose explicit return contract is unknown or Promise<unknown>."
|
|
1270
|
+
},
|
|
1271
|
+
messages: {
|
|
1272
|
+
unknownReturn: "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type."
|
|
1273
|
+
}
|
|
1274
|
+
},
|
|
1275
|
+
createOnce(context) {
|
|
1276
|
+
let environment = null;
|
|
1277
|
+
const resolvesToUnknown = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
1278
|
+
if (resolved.type === "TSUnknownKeyword")
|
|
1279
|
+
return true;
|
|
1280
|
+
if (resolved.type === "TSParenthesizedType") {
|
|
1281
|
+
return matches(resolved.typeAnnotation);
|
|
1282
|
+
}
|
|
1283
|
+
if (resolved.type === "TSUnionType")
|
|
1284
|
+
return resolved.types.some(matches);
|
|
1285
|
+
if (resolved.type !== "TSTypeReference" || resolved.typeName.type !== "Identifier" || resolved.typeName.name !== "Promise" && resolved.typeName.name !== "PromiseLike") {
|
|
1286
|
+
return false;
|
|
1287
|
+
}
|
|
1288
|
+
const value = resolved.typeArguments?.params[0];
|
|
1289
|
+
return value !== undefined && matches(value);
|
|
1290
|
+
});
|
|
1291
|
+
const checkReturnType = (node) => {
|
|
1292
|
+
const annotation = node.returnType;
|
|
1293
|
+
if (annotation === null || annotation === undefined)
|
|
1294
|
+
return;
|
|
1295
|
+
if (!resolvesToUnknown(annotation.typeAnnotation))
|
|
1296
|
+
return;
|
|
1297
|
+
context.report({ node: annotation.typeAnnotation, messageId: "unknownReturn" });
|
|
1298
|
+
};
|
|
1299
|
+
return {
|
|
1300
|
+
Program(node) {
|
|
1301
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
1302
|
+
},
|
|
1303
|
+
ArrowFunctionExpression: checkReturnType,
|
|
1304
|
+
FunctionDeclaration: checkReturnType,
|
|
1305
|
+
FunctionExpression: checkReturnType,
|
|
1306
|
+
TSCallSignatureDeclaration: checkReturnType,
|
|
1307
|
+
TSConstructSignatureDeclaration: checkReturnType,
|
|
1308
|
+
TSConstructorType: checkReturnType,
|
|
1309
|
+
TSDeclareFunction: checkReturnType,
|
|
1310
|
+
TSEmptyBodyFunctionExpression: checkReturnType,
|
|
1311
|
+
TSFunctionType: checkReturnType,
|
|
1312
|
+
TSMethodSignature: checkReturnType
|
|
1313
|
+
};
|
|
1314
|
+
}
|
|
1315
|
+
});
|
|
1316
|
+
|
|
1317
|
+
// vendor/anti-slop/src/rules/no-unknown-type-aliases.ts
|
|
1318
|
+
import { defineRule as defineRule12 } from "@oxlint/plugins";
|
|
1319
|
+
var noUnknownTypeAliasesRule = defineRule12({
|
|
1320
|
+
meta: {
|
|
1321
|
+
type: "problem",
|
|
1322
|
+
docs: {
|
|
1323
|
+
description: "Disallow type aliases whose resolved type is unknown; unknown must remain visible at an allowed boundary."
|
|
1324
|
+
},
|
|
1325
|
+
messages: {
|
|
1326
|
+
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."
|
|
1327
|
+
}
|
|
1328
|
+
},
|
|
1329
|
+
createOnce(context) {
|
|
1330
|
+
let environment = null;
|
|
1331
|
+
const resolvesToUnknown = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
1332
|
+
if (resolved.type === "TSUnknownKeyword")
|
|
1333
|
+
return true;
|
|
1334
|
+
if (resolved.type === "TSParenthesizedType") {
|
|
1335
|
+
return matches(resolved.typeAnnotation);
|
|
1336
|
+
}
|
|
1337
|
+
return resolved.type === "TSUnionType" && resolved.types.some(matches);
|
|
1338
|
+
});
|
|
1339
|
+
return {
|
|
1340
|
+
Program(node) {
|
|
1341
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
1342
|
+
},
|
|
1343
|
+
TSTypeAliasDeclaration(node) {
|
|
1344
|
+
if (!resolvesToUnknown(node.typeAnnotation))
|
|
1345
|
+
return;
|
|
1346
|
+
context.report({
|
|
1347
|
+
node: node.id,
|
|
1348
|
+
messageId: "unknownAlias",
|
|
1349
|
+
data: { alias: node.id.name }
|
|
1350
|
+
});
|
|
1351
|
+
}
|
|
1352
|
+
};
|
|
1353
|
+
}
|
|
1354
|
+
});
|
|
1355
|
+
|
|
1356
|
+
// vendor/anti-slop/src/rules/no-unsafe-dictionary-type.ts
|
|
1357
|
+
import { defineRule as defineRule13 } from "@oxlint/plugins";
|
|
1358
|
+
var typeNodeKinds = new Set([
|
|
1359
|
+
"JSDocNonNullableType",
|
|
1360
|
+
"JSDocNullableType",
|
|
1361
|
+
"JSDocUnknownType",
|
|
1362
|
+
"TSAnyKeyword",
|
|
1363
|
+
"TSArrayType",
|
|
1364
|
+
"TSBigIntKeyword",
|
|
1365
|
+
"TSBooleanKeyword",
|
|
1366
|
+
"TSConditionalType",
|
|
1367
|
+
"TSConstructorType",
|
|
1368
|
+
"TSFunctionType",
|
|
1369
|
+
"TSImportType",
|
|
1370
|
+
"TSIndexedAccessType",
|
|
1371
|
+
"TSInferType",
|
|
1372
|
+
"TSIntersectionType",
|
|
1373
|
+
"TSIntrinsicKeyword",
|
|
1374
|
+
"TSLiteralType",
|
|
1375
|
+
"TSMappedType",
|
|
1376
|
+
"TSNamedTupleMember",
|
|
1377
|
+
"TSNeverKeyword",
|
|
1378
|
+
"TSNullKeyword",
|
|
1379
|
+
"TSNumberKeyword",
|
|
1380
|
+
"TSObjectKeyword",
|
|
1381
|
+
"TSParenthesizedType",
|
|
1382
|
+
"TSStringKeyword",
|
|
1383
|
+
"TSSymbolKeyword",
|
|
1384
|
+
"TSTemplateLiteralType",
|
|
1385
|
+
"TSThisType",
|
|
1386
|
+
"TSTupleType",
|
|
1387
|
+
"TSTypeLiteral",
|
|
1388
|
+
"TSTypeOperator",
|
|
1389
|
+
"TSTypePredicate",
|
|
1390
|
+
"TSTypeQuery",
|
|
1391
|
+
"TSTypeReference",
|
|
1392
|
+
"TSUndefinedKeyword",
|
|
1393
|
+
"TSUnionType",
|
|
1394
|
+
"TSUnknownKeyword",
|
|
1395
|
+
"TSVoidKeyword"
|
|
1396
|
+
]);
|
|
1397
|
+
function isTypeNode(node) {
|
|
1398
|
+
return typeNodeKinds.has(node.type);
|
|
1399
|
+
}
|
|
1400
|
+
function typeReferenceName3(type) {
|
|
1401
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
1402
|
+
}
|
|
1403
|
+
function isInsideTypeAliasDeclaration(node) {
|
|
1404
|
+
let current = node.parent;
|
|
1405
|
+
while (current !== null && current.type !== "Program") {
|
|
1406
|
+
if (current.type === "TSTypeAliasDeclaration")
|
|
1407
|
+
return true;
|
|
1408
|
+
current = current.parent;
|
|
1409
|
+
}
|
|
1410
|
+
return false;
|
|
1411
|
+
}
|
|
1412
|
+
function isPlainAliasConsumerUse(node, environment) {
|
|
1413
|
+
if (node.type !== "TSTypeReference" || node.typeArguments?.params.length)
|
|
1414
|
+
return false;
|
|
1415
|
+
const name = typeReferenceName3(node);
|
|
1416
|
+
return name !== null && visibleTypeAlias(name, node, environment.typeAliases) !== null && !isInsideTypeAliasDeclaration(node);
|
|
1417
|
+
}
|
|
1418
|
+
function isInsideTypeParameterConstraint(node) {
|
|
1419
|
+
let child = node;
|
|
1420
|
+
let parent = child.parent;
|
|
1421
|
+
while (parent !== null && parent.type !== "Program") {
|
|
1422
|
+
if (parent.type === "TSTypeParameter" && parent.constraint === child)
|
|
1423
|
+
return true;
|
|
1424
|
+
child = parent;
|
|
1425
|
+
parent = child.parent;
|
|
1426
|
+
}
|
|
1427
|
+
return false;
|
|
1428
|
+
}
|
|
1429
|
+
function shouldReportType(node, environment) {
|
|
1430
|
+
if (isInsideTypeParameterConstraint(node))
|
|
1431
|
+
return false;
|
|
1432
|
+
if (isPlainAliasConsumerUse(node, environment))
|
|
1433
|
+
return false;
|
|
1434
|
+
if (classifyUnsafeDictionary(node, environment) === null)
|
|
1435
|
+
return false;
|
|
1436
|
+
let current = node.parent;
|
|
1437
|
+
while (current !== null && current.type !== "Program") {
|
|
1438
|
+
if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null)
|
|
1439
|
+
return false;
|
|
1440
|
+
current = current.parent;
|
|
1441
|
+
}
|
|
1442
|
+
return true;
|
|
1443
|
+
}
|
|
1444
|
+
var noUnsafeDictionaryTypeRule = defineRule13({
|
|
1445
|
+
meta: {
|
|
1446
|
+
type: "problem",
|
|
1447
|
+
docs: {
|
|
1448
|
+
description: "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches."
|
|
1449
|
+
},
|
|
1450
|
+
messages: {
|
|
1451
|
+
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."
|
|
1452
|
+
}
|
|
1453
|
+
},
|
|
1454
|
+
createOnce(context) {
|
|
1455
|
+
let environment = null;
|
|
1456
|
+
const report = (node, value) => {
|
|
1457
|
+
context.report({ node, messageId: "unsafeDictionary", data: { value } });
|
|
1458
|
+
};
|
|
1459
|
+
const reportIfUnsafe = (node) => {
|
|
1460
|
+
if (environment === null || !shouldReportType(node, environment))
|
|
1461
|
+
return;
|
|
1462
|
+
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
1463
|
+
if (unsafe === null)
|
|
1464
|
+
return;
|
|
1465
|
+
report(node, unsafe.unsafeValue);
|
|
1466
|
+
};
|
|
1467
|
+
return {
|
|
1468
|
+
Program(node) {
|
|
1469
|
+
environment = createTypeEnvironment(node, context.sourceCode.visitorKeys);
|
|
1470
|
+
},
|
|
1471
|
+
TSTypeReference: reportIfUnsafe,
|
|
1472
|
+
TSTypeLiteral: reportIfUnsafe,
|
|
1473
|
+
TSMappedType: reportIfUnsafe,
|
|
1474
|
+
TSIndexSignature(node) {
|
|
1475
|
+
if (environment === null || node.typeAnnotation === null || node.parent.type === "TSTypeLiteral")
|
|
1476
|
+
return;
|
|
1477
|
+
const unsafe = classifyUnsafeDictionaryValue(node.typeAnnotation.typeAnnotation, environment);
|
|
1478
|
+
if (unsafe !== null)
|
|
1479
|
+
report(node, unsafe.unsafeValue);
|
|
1480
|
+
}
|
|
1481
|
+
};
|
|
1482
|
+
}
|
|
1483
|
+
});
|
|
1484
|
+
|
|
1485
|
+
// vendor/anti-slop/src/rules/no-widen-then-assert.ts
|
|
1486
|
+
import { defineRule as defineRule14 } from "@oxlint/plugins";
|
|
1487
|
+
var functionBoundaryTypes = new Set([
|
|
1488
|
+
"ArrowFunctionExpression",
|
|
1489
|
+
"FunctionDeclaration",
|
|
1490
|
+
"FunctionExpression",
|
|
1491
|
+
"TSDeclareFunction",
|
|
1492
|
+
"TSEmptyBodyFunctionExpression"
|
|
1493
|
+
]);
|
|
1494
|
+
function unwrapExpressionParentheses(expression) {
|
|
1495
|
+
let current = expression;
|
|
1496
|
+
while (current.type === "ParenthesizedExpression")
|
|
1497
|
+
current = current.expression;
|
|
1498
|
+
return current;
|
|
1499
|
+
}
|
|
1500
|
+
function unwrapTypeParentheses(type) {
|
|
1501
|
+
let current = type;
|
|
1502
|
+
while (current.type === "TSParenthesizedType")
|
|
1503
|
+
current = current.typeAnnotation;
|
|
1504
|
+
return current;
|
|
1505
|
+
}
|
|
1506
|
+
function typeReferenceName4(type) {
|
|
1507
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
1508
|
+
}
|
|
1509
|
+
function isUnknownOrAnyType(type) {
|
|
1510
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1511
|
+
return unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword";
|
|
1512
|
+
}
|
|
1513
|
+
function isBroadRecordKeyType(type) {
|
|
1514
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1515
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") {
|
|
1516
|
+
return true;
|
|
1517
|
+
}
|
|
1518
|
+
if (unwrapped.type === "TSUnionType")
|
|
1519
|
+
return unwrapped.types.every(isBroadRecordKeyType);
|
|
1520
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName4(unwrapped) === "PropertyKey";
|
|
1521
|
+
}
|
|
1522
|
+
function isBroadRecordType(type) {
|
|
1523
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1524
|
+
if (unwrapped.type === "TSTypeReference") {
|
|
1525
|
+
if (typeReferenceName4(unwrapped) === "Readonly") {
|
|
1526
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
1527
|
+
return inner !== undefined && isBroadRecordType(inner);
|
|
1528
|
+
}
|
|
1529
|
+
if (typeReferenceName4(unwrapped) !== "Record")
|
|
1530
|
+
return false;
|
|
1531
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
1532
|
+
return parameters.length === 2 && parameters[0] !== undefined && parameters[1] !== undefined && isBroadRecordKeyType(parameters[0]) && isUnknownOrAnyType(parameters[1]);
|
|
1533
|
+
}
|
|
1534
|
+
if (unwrapped.type !== "TSTypeLiteral" || unwrapped.members.length !== 1)
|
|
1535
|
+
return false;
|
|
1536
|
+
const [member] = unwrapped.members;
|
|
1537
|
+
const [parameter] = member?.type === "TSIndexSignature" ? member.parameters : [];
|
|
1538
|
+
return member?.type === "TSIndexSignature" && member.parameters.length === 1 && parameter !== undefined && isBroadRecordKeyType(parameter.typeAnnotation.typeAnnotation) && isUnknownOrAnyType(member.typeAnnotation.typeAnnotation);
|
|
1539
|
+
}
|
|
1540
|
+
function broadTypeKind(type) {
|
|
1541
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1542
|
+
if (unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword")
|
|
1543
|
+
return "top";
|
|
1544
|
+
if (unwrapped.type === "TSObjectKeyword")
|
|
1545
|
+
return "object";
|
|
1546
|
+
return isBroadRecordType(unwrapped) ? "record" : null;
|
|
1547
|
+
}
|
|
1548
|
+
function assertedExpression(node) {
|
|
1549
|
+
return unwrapExpressionParentheses(node.expression);
|
|
1550
|
+
}
|
|
1551
|
+
function assertionFromExpression(expression) {
|
|
1552
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
1553
|
+
return unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion" ? unwrapped : null;
|
|
1554
|
+
}
|
|
1555
|
+
function normalizedTypeText(sourceText, type) {
|
|
1556
|
+
return sourceText.slice(type.start, type.end).replaceAll(/\s+/gu, "");
|
|
1557
|
+
}
|
|
1558
|
+
function typesHaveSameSyntax(sourceText, left, right) {
|
|
1559
|
+
return left !== null && normalizedTypeText(sourceText, unwrapTypeParentheses(left)) === normalizedTypeText(sourceText, unwrapTypeParentheses(right));
|
|
1560
|
+
}
|
|
1561
|
+
function isDefinitelyObjectType(type) {
|
|
1562
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1563
|
+
switch (unwrapped.type) {
|
|
1564
|
+
case "TSArrayType":
|
|
1565
|
+
case "TSConstructorType":
|
|
1566
|
+
case "TSFunctionType":
|
|
1567
|
+
case "TSMappedType":
|
|
1568
|
+
case "TSObjectKeyword":
|
|
1569
|
+
case "TSTupleType":
|
|
1570
|
+
return true;
|
|
1571
|
+
case "TSTypeLiteral":
|
|
1572
|
+
return unwrapped.members.length > 0;
|
|
1573
|
+
case "TSIntersectionType":
|
|
1574
|
+
return unwrapped.types.every(isDefinitelyObjectType);
|
|
1575
|
+
case "TSTypeOperator":
|
|
1576
|
+
return unwrapped.operator === "readonly" && isDefinitelyObjectType(unwrapped.typeAnnotation);
|
|
1577
|
+
default:
|
|
1578
|
+
return false;
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
function isDefinitelyNarrowerRecordType(type) {
|
|
1582
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
1583
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
1584
|
+
return unwrapped.members.some((member) => member.type !== "TSIndexSignature");
|
|
1585
|
+
}
|
|
1586
|
+
if (unwrapped.type !== "TSTypeReference")
|
|
1587
|
+
return false;
|
|
1588
|
+
if (typeReferenceName4(unwrapped) === "Readonly") {
|
|
1589
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
1590
|
+
return inner !== undefined && isDefinitelyNarrowerRecordType(inner);
|
|
1591
|
+
}
|
|
1592
|
+
if (typeReferenceName4(unwrapped) !== "Record")
|
|
1593
|
+
return false;
|
|
1594
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
1595
|
+
return parameters.length === 2 && parameters[1] !== undefined && !isUnknownOrAnyType(parameters[1]);
|
|
1596
|
+
}
|
|
1597
|
+
function functionBoundary(node) {
|
|
1598
|
+
let current = node.parent;
|
|
1599
|
+
while (current !== null && current.type !== "Program") {
|
|
1600
|
+
if (functionBoundaryTypes.has(current.type))
|
|
1601
|
+
return current;
|
|
1602
|
+
current = current.parent;
|
|
1603
|
+
}
|
|
1604
|
+
return null;
|
|
1605
|
+
}
|
|
1606
|
+
function resolvedVariableForIdentifier(scopes, identifier) {
|
|
1607
|
+
for (const scope of scopes) {
|
|
1608
|
+
const reference = scope.references.find((candidate) => candidate.identifier.start === identifier.start && candidate.identifier.end === identifier.end);
|
|
1609
|
+
if (reference !== undefined)
|
|
1610
|
+
return reference.resolved;
|
|
1611
|
+
}
|
|
1612
|
+
return null;
|
|
1613
|
+
}
|
|
1614
|
+
function variableDeclarator2(variable) {
|
|
1615
|
+
for (const definition of variable.defs) {
|
|
1616
|
+
if (definition.type === "Variable" && definition.node.type === "VariableDeclarator") {
|
|
1617
|
+
return definition.node;
|
|
1618
|
+
}
|
|
1619
|
+
}
|
|
1620
|
+
return null;
|
|
1621
|
+
}
|
|
1622
|
+
function knownValueEvidence(expression, scopes, boundary, visitedVariables) {
|
|
1623
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
1624
|
+
if (unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion") {
|
|
1625
|
+
if (broadTypeKind(unwrapped.typeAnnotation) !== null)
|
|
1626
|
+
return null;
|
|
1627
|
+
return { type: unwrapped.typeAnnotation };
|
|
1628
|
+
}
|
|
1629
|
+
if (unwrapped.type === "Literal" || unwrapped.type === "TemplateLiteral") {
|
|
1630
|
+
return { type: null };
|
|
1631
|
+
}
|
|
1632
|
+
if (unwrapped.type === "ArrayExpression" || unwrapped.type === "ArrowFunctionExpression" || unwrapped.type === "ClassExpression" || unwrapped.type === "FunctionExpression" || unwrapped.type === "NewExpression" || unwrapped.type === "ObjectExpression") {
|
|
1633
|
+
return { type: null };
|
|
1634
|
+
}
|
|
1635
|
+
if (unwrapped.type !== "Identifier")
|
|
1636
|
+
return null;
|
|
1637
|
+
const variable = resolvedVariableForIdentifier(scopes, unwrapped);
|
|
1638
|
+
if (variable === null || visitedVariables.has(variable))
|
|
1639
|
+
return null;
|
|
1640
|
+
const annotatedIdentifier = variable.identifiers.find((identifier) => identifier.typeAnnotation !== null && identifier.typeAnnotation !== undefined);
|
|
1641
|
+
const annotation = annotatedIdentifier?.typeAnnotation?.typeAnnotation;
|
|
1642
|
+
if (annotation !== undefined && annotatedIdentifier !== undefined) {
|
|
1643
|
+
if (functionBoundary(annotatedIdentifier) !== boundary || broadTypeKind(annotation) !== null) {
|
|
1644
|
+
return null;
|
|
1645
|
+
}
|
|
1646
|
+
return { type: annotation };
|
|
1647
|
+
}
|
|
1648
|
+
const declarator = variableDeclarator2(variable);
|
|
1649
|
+
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) {
|
|
1650
|
+
return null;
|
|
1651
|
+
}
|
|
1652
|
+
return knownValueEvidence(declarator.init, scopes, boundary, new Set([...visitedVariables, variable]));
|
|
1653
|
+
}
|
|
1654
|
+
function widenedBinding(variable, scopes) {
|
|
1655
|
+
const declarator = variableDeclarator2(variable);
|
|
1656
|
+
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)) {
|
|
1657
|
+
return null;
|
|
1658
|
+
}
|
|
1659
|
+
const boundary = functionBoundary(declarator);
|
|
1660
|
+
const declaredType = declarator.id.typeAnnotation?.typeAnnotation;
|
|
1661
|
+
const initializerAssertion = assertionFromExpression(declarator.init);
|
|
1662
|
+
const initializerBroadKind = initializerAssertion === null ? null : broadTypeKind(initializerAssertion.typeAnnotation);
|
|
1663
|
+
const declaredBroadKind = declaredType === undefined ? null : broadTypeKind(declaredType);
|
|
1664
|
+
const broadKind = declaredBroadKind ?? initializerBroadKind;
|
|
1665
|
+
if (broadKind === null)
|
|
1666
|
+
return null;
|
|
1667
|
+
const originalExpression = initializerAssertion !== null && initializerBroadKind !== null ? assertedExpression(initializerAssertion) : declarator.init;
|
|
1668
|
+
const evidence = knownValueEvidence(originalExpression, scopes, boundary, new Set([variable]));
|
|
1669
|
+
return evidence === null ? null : { broadKind, evidence, declaredAt: declarator.end, boundary };
|
|
1670
|
+
}
|
|
1671
|
+
function assertionIsNarrower(sourceText, broadKind, evidence, assertedType) {
|
|
1672
|
+
if (broadTypeKind(assertedType) !== null)
|
|
1673
|
+
return false;
|
|
1674
|
+
if (broadKind === "top")
|
|
1675
|
+
return true;
|
|
1676
|
+
if (typesHaveSameSyntax(sourceText, evidence.type, assertedType))
|
|
1677
|
+
return true;
|
|
1678
|
+
if (broadKind === "object")
|
|
1679
|
+
return isDefinitelyObjectType(assertedType);
|
|
1680
|
+
return isDefinitelyNarrowerRecordType(assertedType);
|
|
1681
|
+
}
|
|
1682
|
+
var noWidenThenAssertRule = defineRule14({
|
|
1683
|
+
meta: {
|
|
1684
|
+
type: "problem",
|
|
1685
|
+
docs: {
|
|
1686
|
+
description: "Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type."
|
|
1687
|
+
},
|
|
1688
|
+
messages: {
|
|
1689
|
+
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.'
|
|
1690
|
+
}
|
|
1691
|
+
},
|
|
1692
|
+
createOnce(context) {
|
|
1693
|
+
let scopes = [];
|
|
1694
|
+
const checkAssertion = (node) => {
|
|
1695
|
+
const expression = assertedExpression(node);
|
|
1696
|
+
if (expression.type !== "Identifier")
|
|
1697
|
+
return;
|
|
1698
|
+
const variable = resolvedVariableForIdentifier(scopes, expression);
|
|
1699
|
+
if (variable === null)
|
|
1700
|
+
return;
|
|
1701
|
+
const widened = widenedBinding(variable, scopes);
|
|
1702
|
+
if (widened === null || node.start <= widened.declaredAt || functionBoundary(node) !== widened.boundary || !assertionIsNarrower(context.sourceCode.text, widened.broadKind, widened.evidence, node.typeAnnotation)) {
|
|
1703
|
+
return;
|
|
1704
|
+
}
|
|
1705
|
+
context.report({
|
|
1706
|
+
node,
|
|
1707
|
+
messageId: "widenThenAssert",
|
|
1708
|
+
data: { name: expression.name }
|
|
1709
|
+
});
|
|
1710
|
+
};
|
|
1711
|
+
return {
|
|
1712
|
+
Program() {
|
|
1713
|
+
scopes = context.sourceCode.scopeManager.scopes;
|
|
1714
|
+
},
|
|
1715
|
+
TSAsExpression: checkAssertion,
|
|
1716
|
+
TSTypeAssertion: checkAssertion
|
|
1717
|
+
};
|
|
1718
|
+
}
|
|
1719
|
+
});
|
|
1720
|
+
|
|
1721
|
+
// vendor/anti-slop/src/rules/require-safety-comment-for-type-assertion.ts
|
|
1722
|
+
import { defineRule as defineRule15 } from "@oxlint/plugins";
|
|
1723
|
+
var DEFAULT_SAFETY_MARKERS = ["SAFETY"];
|
|
1724
|
+
var commentOwnerKinds = new Set([
|
|
1725
|
+
"ExpressionStatement",
|
|
1726
|
+
"PropertyDefinition",
|
|
1727
|
+
"ReturnStatement",
|
|
1728
|
+
"ThrowStatement",
|
|
1729
|
+
"VariableDeclaration"
|
|
1730
|
+
]);
|
|
1731
|
+
function isConstAssertion2(node) {
|
|
1732
|
+
return node.typeAnnotation.type === "TSTypeReference" && node.typeAnnotation.typeName.type === "Identifier" && node.typeAnnotation.typeName.name === "const";
|
|
1733
|
+
}
|
|
1734
|
+
function configuredSafetyMarkers(option) {
|
|
1735
|
+
if (typeof option !== "object" || option === null || !("markers" in option)) {
|
|
1736
|
+
return DEFAULT_SAFETY_MARKERS;
|
|
1737
|
+
}
|
|
1738
|
+
const configured = option.markers;
|
|
1739
|
+
if (!Array.isArray(configured))
|
|
1740
|
+
return DEFAULT_SAFETY_MARKERS;
|
|
1741
|
+
const markers = configured.flatMap((marker) => typeof marker === "string" && marker.trim().length > 0 ? [marker.trim()] : []);
|
|
1742
|
+
return markers.length > 0 ? markers : DEFAULT_SAFETY_MARKERS;
|
|
1743
|
+
}
|
|
1744
|
+
function markerPattern(markers) {
|
|
1745
|
+
const alternation = markers.map((marker) => marker.replaceAll(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`)).join("|");
|
|
1746
|
+
return new RegExp(String.raw`(?:^|[^\p{L}\p{N}_])(?:${alternation})\s*:\s*\S`, "u");
|
|
1747
|
+
}
|
|
1748
|
+
function hasSafetyJustificationBefore(sourceCode, owner, assertion, pattern) {
|
|
1749
|
+
return sourceCode.getCommentsBefore(owner).some((comment) => comment.end <= assertion.start && pattern.test(comment.value));
|
|
1750
|
+
}
|
|
1751
|
+
function hasSafetyComment(sourceCode, node, pattern) {
|
|
1752
|
+
let current = node;
|
|
1753
|
+
while (true) {
|
|
1754
|
+
if (hasSafetyJustificationBefore(sourceCode, current, node, pattern))
|
|
1755
|
+
return true;
|
|
1756
|
+
if (commentOwnerKinds.has(current.type)) {
|
|
1757
|
+
const exportDeclaration = current.parent;
|
|
1758
|
+
return exportDeclaration.type === "ExportNamedDeclaration" && exportDeclaration.declaration === current && hasSafetyJustificationBefore(sourceCode, exportDeclaration, node, pattern);
|
|
1759
|
+
}
|
|
1760
|
+
if (current.parent.type === "Program")
|
|
1761
|
+
return false;
|
|
1762
|
+
current = current.parent;
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
var requireSafetyCommentForTypeAssertionRule = defineRule15({
|
|
1766
|
+
meta: {
|
|
1767
|
+
type: "problem",
|
|
1768
|
+
docs: {
|
|
1769
|
+
description: "Require a nearby SAFETY comment for every TypeScript type assertion except const assertions."
|
|
1770
|
+
},
|
|
1771
|
+
messages: {
|
|
1772
|
+
missingSafetyComment: "This type assertion has no `{{marker}}:` justification. State the checked invariant immediately before the assertion or its containing statement."
|
|
1773
|
+
},
|
|
1774
|
+
schema: [
|
|
1775
|
+
{
|
|
1776
|
+
type: "object",
|
|
1777
|
+
properties: {
|
|
1778
|
+
markers: {
|
|
1779
|
+
type: "array",
|
|
1780
|
+
items: { type: "string", minLength: 1 },
|
|
1781
|
+
minItems: 1,
|
|
1782
|
+
uniqueItems: true
|
|
1783
|
+
}
|
|
1784
|
+
},
|
|
1785
|
+
additionalProperties: false
|
|
1786
|
+
}
|
|
1787
|
+
],
|
|
1788
|
+
defaultOptions: [{ markers: ["SAFETY"] }]
|
|
1789
|
+
},
|
|
1790
|
+
createOnce(context) {
|
|
1791
|
+
const patterns = new Map;
|
|
1792
|
+
const checkAssertion = (node) => {
|
|
1793
|
+
if (isConstAssertion2(node))
|
|
1794
|
+
return;
|
|
1795
|
+
const markers = configuredSafetyMarkers(context.options?.[0]);
|
|
1796
|
+
const patternKey = markers.join("\x00");
|
|
1797
|
+
const pattern = patterns.get(patternKey) ?? markerPattern(markers);
|
|
1798
|
+
patterns.set(patternKey, pattern);
|
|
1799
|
+
if (hasSafetyComment(context.sourceCode, node, pattern))
|
|
1800
|
+
return;
|
|
1801
|
+
context.report({
|
|
1802
|
+
node,
|
|
1803
|
+
messageId: "missingSafetyComment",
|
|
1804
|
+
data: { marker: markers[0] ?? DEFAULT_SAFETY_MARKERS[0] }
|
|
1805
|
+
});
|
|
1806
|
+
};
|
|
1807
|
+
return {
|
|
1808
|
+
TSAsExpression: checkAssertion,
|
|
1809
|
+
TSTypeAssertion: checkAssertion
|
|
1810
|
+
};
|
|
1811
|
+
}
|
|
1812
|
+
});
|
|
1813
|
+
|
|
1814
|
+
// vendor/anti-slop/src/index.ts
|
|
1815
|
+
var antiSlopPlugin = eslintCompatPlugin({
|
|
1816
|
+
meta: { name: "anti-slop" },
|
|
1817
|
+
rules: {
|
|
1818
|
+
"no-chained-type-assertions": noChainedTypeAssertionsRule,
|
|
1819
|
+
"no-conditional-empty-object-spread": noConditionalEmptyObjectSpreadRule,
|
|
1820
|
+
"no-known-value-widening": noKnownValueWideningRule,
|
|
1821
|
+
"no-module-mocking": noModuleMockingRule,
|
|
1822
|
+
"no-object-parameters": noObjectParametersRule,
|
|
1823
|
+
"no-reflect-apply": noReflectApplyRule,
|
|
1824
|
+
"no-reflect-get": noReflectGetRule,
|
|
1825
|
+
"no-runtime-typeof": noRuntimeTypeofRule,
|
|
1826
|
+
"no-unsafe-dictionary-type": noUnsafeDictionaryTypeRule,
|
|
1827
|
+
"no-shape-in-symbol-names": noForbiddenTermInSymbolNamesRule,
|
|
1828
|
+
"no-unknown-parameters": noUnknownParametersRule,
|
|
1829
|
+
"no-unknown-returns": noUnknownReturnsRule,
|
|
1830
|
+
"no-unknown-type-aliases": noUnknownTypeAliasesRule,
|
|
1831
|
+
"no-widen-then-assert": noWidenThenAssertRule,
|
|
1832
|
+
"require-safety-comment-for-type-assertion": requireSafetyCommentForTypeAssertionRule
|
|
1833
|
+
}
|
|
1834
|
+
});
|
|
1835
|
+
var src_default = antiSlopPlugin;
|
|
1836
|
+
export {
|
|
1837
|
+
src_default as default
|
|
1838
|
+
};
|