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