@antelopejs/tooling-configs 0.2.16 → 0.2.18
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 +101 -52
- package/dist/bin/api-diff.mjs +430 -107
- package/dist/bin/body-check.d.mts +1 -0
- package/dist/bin/body-check.mjs +344 -0
- package/dist/bin/capture.d.mts +1 -0
- package/dist/bin/capture.mjs +559 -0
- package/dist/bin/check-size.mjs +3 -3
- package/dist/bin/checks.mjs +3 -3
- package/dist/bin/comment-proof.mjs +4 -3
- package/dist/bin/garden.mjs +6 -6
- package/dist/bin/ratchet.mjs +316 -10
- package/dist/bin/suppressions.mjs +118 -80
- package/dist/bin/sync.mjs +554 -153
- package/dist/bin/verify.mjs +240 -127
- package/dist/capture/index.d.mts +50 -0
- package/dist/capture/index.mjs +2 -0
- package/dist/{ceilings-BSw9p4YB.mjs → ceilings-B7KayYIG.mjs} +11 -3
- package/dist/{checks-BlWDFYio.mjs → checks-CxkSu2E6.mjs} +1 -1
- package/dist/{ci-workflow-DIsoEgMd.mjs → ci-workflow-ChW4He0r.mjs} +84 -3
- package/dist/{files-2FikHZ9F.mjs → files-DJ0TeaTU.mjs} +11 -5
- package/dist/formal/index.d.mts +40 -0
- package/dist/formal/index.mjs +205 -0
- package/dist/{json-BqvuCkqr.mjs → json-CG2L7YFl.mjs} +12 -8
- package/dist/oxc/anti-slop/index.mjs +869 -21
- package/dist/oxc/fmt.mjs +1 -1
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +736 -4
- package/dist/oxc/lint.d.mts +20 -3
- package/dist/oxc/lint.mjs +34 -10
- package/dist/oxlint-DIV-lLSS.mjs +98 -0
- package/dist/{package-Dtjt_lSa.mjs → package-BRTjkzt5.mjs} +29 -3
- package/dist/proof-stamp-DDDC0J6l.mjs +279 -0
- package/dist/ratchet-BAcLRYuM.mjs +448 -0
- package/dist/{renames-BGO4dWJ8.mjs → renames-L2owF5GD.mjs} +2 -2
- package/dist/scenes-DZVXkcxS.mjs +104 -0
- package/dist/shared-igPpEDpS.mjs +622 -0
- package/dist/{size-BVgNbLO6.mjs → size-DBatLT-F.mjs} +4 -4
- package/dist/suppression-base-9L8dlMQK.mjs +120 -0
- package/dist/suppression-budget-C0SkPO5R.mjs +535 -0
- package/dist/{suppressions-BJAngUxo.mjs → suppressions-DM60NkvL.mjs} +180 -18
- package/dist/templates-CypDm-j9.mjs +121 -0
- package/dist/typescript-CM0LrVP7.mjs +222 -0
- package/dist/verify/index.d.mts +6 -1
- package/dist/verify/index.mjs +1 -1
- package/package.json +31 -5
- package/dist/kinds-Bzyyi9yk.mjs +0 -25
- package/dist/ratchet-DRdIYwmv.mjs +0 -80
- package/dist/shared-CsXIMK3I.mjs +0 -34
- package/dist/staged-ratchet-CjoRfZaM.mjs +0 -236
- package/dist/suppression-budget-Yyf5_J5l.mjs +0 -283
- package/dist/typescript-C54ONh9m.mjs +0 -433
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { a as matcherChain, i as isAssertion, n as TEST_FILE_GLOBS, o as memberName, r as callName, s as testCallback, t as ASSERTION_FUNCTION_NAMES } from "../../shared-WC0z3y9F.mjs";
|
|
2
|
-
import { lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
-
import { dirname, join } from "node:path";
|
|
2
|
+
import { existsSync, lstatSync, readFileSync, realpathSync } from "node:fs";
|
|
3
|
+
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { defineRule, eslintCompatPlugin } from "@oxlint/plugins";
|
|
5
|
+
import ts from "typescript";
|
|
5
6
|
//#region src/oxc/guardrails/rules/no-comments.ts
|
|
6
7
|
const allowedCommentForms = [
|
|
7
8
|
{
|
|
@@ -193,6 +194,658 @@ const noFsReadInTestsRule = defineRule({
|
|
|
193
194
|
}
|
|
194
195
|
});
|
|
195
196
|
//#endregion
|
|
197
|
+
//#region src/oxc/guardrails/local-types.ts
|
|
198
|
+
/** Collects top-level and exported type declarations; local ones inside functions are not resolved. */
|
|
199
|
+
function collectLocalTypes(program) {
|
|
200
|
+
const types = {
|
|
201
|
+
aliases: /* @__PURE__ */ new Map(),
|
|
202
|
+
interfaces: /* @__PURE__ */ new Map()
|
|
203
|
+
};
|
|
204
|
+
for (const statement of program.body) {
|
|
205
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
206
|
+
if (declaration?.type === "TSTypeAliasDeclaration") types.aliases.set(declaration.id.name, declaration.typeAnnotation);
|
|
207
|
+
if (declaration?.type === "TSInterfaceDeclaration") {
|
|
208
|
+
const merged = types.interfaces.get(declaration.id.name) ?? [];
|
|
209
|
+
merged.push(declaration);
|
|
210
|
+
types.interfaces.set(declaration.id.name, merged);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return types;
|
|
214
|
+
}
|
|
215
|
+
/** The type reference's name when it is a plain identifier, such as `Record` in `Record<string, T>`. */
|
|
216
|
+
function referenceName(type) {
|
|
217
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
218
|
+
}
|
|
219
|
+
/** Strips parentheses and non-null assertions, which change neither the value nor its prototype. */
|
|
220
|
+
function unwrapExpression(node) {
|
|
221
|
+
let current = node;
|
|
222
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSNonNullExpression") current = current.expression;
|
|
223
|
+
return current;
|
|
224
|
+
}
|
|
225
|
+
/** The variable an identifier refers to, found by walking out from its scope. */
|
|
226
|
+
function resolveVariable(source, identifier) {
|
|
227
|
+
let scope = source.getScope(identifier);
|
|
228
|
+
while (scope !== null) {
|
|
229
|
+
const variable = scope.set.get(identifier.name);
|
|
230
|
+
if (variable !== void 0) return variable;
|
|
231
|
+
scope = scope.upper;
|
|
232
|
+
}
|
|
233
|
+
return null;
|
|
234
|
+
}
|
|
235
|
+
/** The nearest function around a node, or null at module level. */
|
|
236
|
+
function enclosingFunction(node) {
|
|
237
|
+
let current = node.parent;
|
|
238
|
+
while (current !== null) {
|
|
239
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionDeclaration" || current.type === "FunctionExpression") return current;
|
|
240
|
+
current = current.parent;
|
|
241
|
+
}
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
/** The predicate a function's return type declares, when it names a parameter (`x is T`, `asserts x is T`). */
|
|
245
|
+
function parameterPredicate(fn) {
|
|
246
|
+
const predicate = fn.returnType?.typeAnnotation;
|
|
247
|
+
return predicate?.type === "TSTypePredicate" && predicate.parameterName.type === "Identifier" ? predicate : null;
|
|
248
|
+
}
|
|
249
|
+
/** The parameter name a predicate narrows, or null for `this` predicates. */
|
|
250
|
+
function predicateName(predicate) {
|
|
251
|
+
return predicate.parameterName.type === "Identifier" ? predicate.parameterName.name : null;
|
|
252
|
+
}
|
|
253
|
+
/** The field `this.<name>` refers to: a property or constructor parameter property of the class around `node`. */
|
|
254
|
+
function classField(node, name) {
|
|
255
|
+
let current = node.parent;
|
|
256
|
+
while (current !== null && current.type !== "ClassBody") {
|
|
257
|
+
if (current.type === "FunctionDeclaration" || current.type === "FunctionExpression" && current.parent.type !== "MethodDefinition") return null;
|
|
258
|
+
current = current.parent;
|
|
259
|
+
}
|
|
260
|
+
if (current === null) return null;
|
|
261
|
+
for (const member of current.body) {
|
|
262
|
+
if (member.type === "PropertyDefinition" && !member.computed && member.key.type === "Identifier" && member.key.name === name) return {
|
|
263
|
+
typeAnnotation: member.typeAnnotation ?? null,
|
|
264
|
+
value: member.value
|
|
265
|
+
};
|
|
266
|
+
if (member.type === "MethodDefinition" && member.kind === "constructor") {
|
|
267
|
+
for (const parameter of member.value.params) if (parameter.type === "TSParameterProperty" && parameter.parameter.type === "Identifier" && parameter.parameter.name === name) return {
|
|
268
|
+
typeAnnotation: parameter.parameter.typeAnnotation ?? null,
|
|
269
|
+
value: null
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
return null;
|
|
274
|
+
}
|
|
275
|
+
//#endregion
|
|
276
|
+
//#region src/oxc/guardrails/key-provenance.ts
|
|
277
|
+
const OWN_KEY_LISTS = /* @__PURE__ */ new Set(["Object.keys", "Object.getOwnPropertyNames"]);
|
|
278
|
+
const KEY_PRESERVING_METHODS = /* @__PURE__ */ new Set([
|
|
279
|
+
"filter",
|
|
280
|
+
"reverse",
|
|
281
|
+
"slice",
|
|
282
|
+
"sort",
|
|
283
|
+
"toReversed",
|
|
284
|
+
"toSorted"
|
|
285
|
+
]);
|
|
286
|
+
const ITERATING_METHODS = /* @__PURE__ */ new Set([
|
|
287
|
+
"every",
|
|
288
|
+
"filter",
|
|
289
|
+
"find",
|
|
290
|
+
"findIndex",
|
|
291
|
+
"flatMap",
|
|
292
|
+
"forEach",
|
|
293
|
+
"map",
|
|
294
|
+
"some"
|
|
295
|
+
]);
|
|
296
|
+
/** A literal, a template without substitutions, or a `typeof` result: never an `Object.prototype` key. */
|
|
297
|
+
function isStaticKey(key) {
|
|
298
|
+
if (key.type === "Literal") return true;
|
|
299
|
+
if (key.type === "UnaryExpression") return key.operator === "typeof";
|
|
300
|
+
return key.type === "TemplateLiteral" && key.expressions.length === 0;
|
|
301
|
+
}
|
|
302
|
+
function constInitializer(identifier, source) {
|
|
303
|
+
const [definition] = resolveVariable(source, identifier)?.defs ?? [];
|
|
304
|
+
if (definition?.type !== "Variable" || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.type !== "VariableDeclarator" || definition.node.init === null) return null;
|
|
305
|
+
let init = definition.node.init;
|
|
306
|
+
while (init.type === "TSAsExpression" || init.type === "TSSatisfiesExpression") init = init.expression;
|
|
307
|
+
return unwrapExpression(init);
|
|
308
|
+
}
|
|
309
|
+
function propertyValue(object, name) {
|
|
310
|
+
for (const property of object.properties) if (property.type === "Property" && !property.computed && property.key.type === "Identifier" && property.key.name === name) return property.value;
|
|
311
|
+
return null;
|
|
312
|
+
}
|
|
313
|
+
/** `const KEY = "a"` or `const LABELS = { a: "x" }` read as `LABELS.a`, declared in this file. */
|
|
314
|
+
function isConstantKey(key, source) {
|
|
315
|
+
if (key.type === "Identifier") {
|
|
316
|
+
const init = constInitializer(key, source);
|
|
317
|
+
return init !== null && isStaticKey(init);
|
|
318
|
+
}
|
|
319
|
+
if (key.type !== "MemberExpression" || key.computed || key.object.type !== "Identifier" || key.property.type !== "Identifier") return false;
|
|
320
|
+
const init = constInitializer(key.object, source);
|
|
321
|
+
const value = init?.type === "ObjectExpression" ? propertyValue(init, key.property.name) : null;
|
|
322
|
+
return value !== null && isStaticKey(value);
|
|
323
|
+
}
|
|
324
|
+
function keyList(node, source, depth = 0) {
|
|
325
|
+
if (depth > 8) return null;
|
|
326
|
+
if (node.type === "Identifier") {
|
|
327
|
+
const init = constInitializer(node, source);
|
|
328
|
+
return init === null ? null : keyList(init, source, depth + 1);
|
|
329
|
+
}
|
|
330
|
+
if (node.type === "ArrayExpression") return node.elements.every((element) => element !== null && isStaticKey(element)) ? { kind: "literal" } : null;
|
|
331
|
+
if (node.type !== "CallExpression") return null;
|
|
332
|
+
const name = callName(node.callee);
|
|
333
|
+
const [first] = node.arguments;
|
|
334
|
+
if (first !== void 0 && OWN_KEY_LISTS.has(name ?? "")) return {
|
|
335
|
+
kind: "keys",
|
|
336
|
+
table: source.getText(first)
|
|
337
|
+
};
|
|
338
|
+
if (first !== void 0 && name === "Object.entries") return {
|
|
339
|
+
kind: "entries",
|
|
340
|
+
table: source.getText(first)
|
|
341
|
+
};
|
|
342
|
+
const callee = node.callee;
|
|
343
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && KEY_PRESERVING_METHODS.has(callee.property.name) ? keyList(callee.object, source, depth + 1) : null;
|
|
344
|
+
}
|
|
345
|
+
/** The pattern that binds a key: the whole binding, or the first element of `[key, value]`. */
|
|
346
|
+
function keyBinding(pattern, name) {
|
|
347
|
+
if (pattern === name) return "plain";
|
|
348
|
+
return pattern.type === "ArrayPattern" && pattern.elements[0] === name ? "entry" : null;
|
|
349
|
+
}
|
|
350
|
+
/** The list a key is iterated from, with how it is bound, or a `for...in` loop. */
|
|
351
|
+
function keySource(definition) {
|
|
352
|
+
if (definition.type === "Parameter") {
|
|
353
|
+
const fn = definition.node;
|
|
354
|
+
const call = fn.parent;
|
|
355
|
+
if (fn.type !== "ArrowFunctionExpression" && fn.type !== "FunctionExpression" || call?.type !== "CallExpression" || call.arguments[0] !== fn || call.callee.type !== "MemberExpression" || call.callee.computed || call.callee.property.type !== "Identifier" || !ITERATING_METHODS.has(call.callee.property.name) || fn.params[0] === void 0) return null;
|
|
356
|
+
return {
|
|
357
|
+
list: call.callee.object,
|
|
358
|
+
binding: keyBinding(fn.params[0], definition.name)
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
const loop = definition.parent?.parent;
|
|
362
|
+
if (definition.node.type !== "VariableDeclarator") return null;
|
|
363
|
+
if (loop?.type === "ForInStatement") return {
|
|
364
|
+
list: loop,
|
|
365
|
+
binding: "plain"
|
|
366
|
+
};
|
|
367
|
+
return loop?.type === "ForOfStatement" ? {
|
|
368
|
+
list: loop.right,
|
|
369
|
+
binding: keyBinding(definition.node.id, definition.name)
|
|
370
|
+
} : null;
|
|
371
|
+
}
|
|
372
|
+
function isOwnKeyBinding(key, table, source) {
|
|
373
|
+
if (key.type !== "Identifier") return false;
|
|
374
|
+
const [definition] = resolveVariable(source, key)?.defs ?? [];
|
|
375
|
+
const found = definition === void 0 ? null : keySource(definition);
|
|
376
|
+
if (found === null || found.binding === null) return false;
|
|
377
|
+
if (found.list.type === "ForInStatement") return source.getText(found.list.right) === table;
|
|
378
|
+
const list = keyList(found.list, source);
|
|
379
|
+
if (list === null) return false;
|
|
380
|
+
if (list.kind === "literal") return found.binding === "plain";
|
|
381
|
+
const binding = list.kind === "keys" ? "plain" : "entry";
|
|
382
|
+
return list.table === table && found.binding === binding;
|
|
383
|
+
}
|
|
384
|
+
/**
|
|
385
|
+
* Keys the rule can prove are not inherited: static keys, same-file constants, and keys iterated
|
|
386
|
+
* from string literals or from `Object.keys`, `Object.entries` or `for...in` of the same table,
|
|
387
|
+
* through `filter`, `sort` and similar key-preserving calls. Imported constants are not resolved.
|
|
388
|
+
*/
|
|
389
|
+
function isKnownKey(key, table, source) {
|
|
390
|
+
return isStaticKey(key) || isConstantKey(key, source) || isOwnKeyBinding(key, table, source);
|
|
391
|
+
}
|
|
392
|
+
/** `Object.prototype.hasOwnProperty.call`, `{}.hasOwnProperty.call` and other `<x>.hasOwnProperty.call` forms. */
|
|
393
|
+
function isHasOwnPropertyCall(callee) {
|
|
394
|
+
return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "call" && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.property.type === "Identifier" && callee.object.property.name === "hasOwnProperty";
|
|
395
|
+
}
|
|
396
|
+
//#endregion
|
|
397
|
+
//#region src/oxc/guardrails/rules/no-inherited-key-lookup.ts
|
|
398
|
+
const WRAPPER_TYPES = /* @__PURE__ */ new Set([
|
|
399
|
+
"Readonly",
|
|
400
|
+
"Partial",
|
|
401
|
+
"Required"
|
|
402
|
+
]);
|
|
403
|
+
const HAS_OWN_CALLS = /* @__PURE__ */ new Set(["Object.hasOwn", "hasOwnProperty.call"]);
|
|
404
|
+
const MAX_ALIAS_DEPTH$1 = 8;
|
|
405
|
+
function isStringKey(type) {
|
|
406
|
+
if (type.type === "TSStringKeyword") return true;
|
|
407
|
+
if (type.type === "TSUnionType") return type.types.some(isStringKey);
|
|
408
|
+
return type.type === "TSTypeReference" && referenceName(type) === "PropertyKey";
|
|
409
|
+
}
|
|
410
|
+
function hasStringIndex(members) {
|
|
411
|
+
return members.some((member) => member.type === "TSIndexSignature" && member.parameters.some((parameter) => isStringKey(parameter.typeAnnotation.typeAnnotation)));
|
|
412
|
+
}
|
|
413
|
+
function isStringKeyedReference(type, environment, depth) {
|
|
414
|
+
const name = referenceName(type);
|
|
415
|
+
if (name === null) return false;
|
|
416
|
+
const alias = environment.types.aliases.get(name);
|
|
417
|
+
if (alias !== void 0) return isStringKeyedType(alias, environment, depth + 1);
|
|
418
|
+
const declarations = environment.types.interfaces.get(name);
|
|
419
|
+
if (declarations !== void 0) return declarations.some((declaration) => hasStringIndex(declaration.body.body));
|
|
420
|
+
const [first] = type.typeArguments?.params ?? [];
|
|
421
|
+
if (first === void 0) return false;
|
|
422
|
+
if (name === "Record") return isStringKey(first);
|
|
423
|
+
return WRAPPER_TYPES.has(name) && isStringKeyedType(first, environment, depth + 1);
|
|
424
|
+
}
|
|
425
|
+
function isStringKeyedType(type, environment, depth = 0) {
|
|
426
|
+
if (depth > MAX_ALIAS_DEPTH$1) return false;
|
|
427
|
+
switch (type.type) {
|
|
428
|
+
case "TSParenthesizedType": return isStringKeyedType(type.typeAnnotation, environment, depth);
|
|
429
|
+
case "TSTypeLiteral": return hasStringIndex(type.members);
|
|
430
|
+
case "TSMappedType": return isStringKey(type.constraint);
|
|
431
|
+
case "TSIntersectionType":
|
|
432
|
+
case "TSUnionType": return type.types.some((part) => isStringKeyedType(part, environment, depth));
|
|
433
|
+
case "TSTypeReference": return isStringKeyedReference(type, environment, depth);
|
|
434
|
+
case "TSTypeQuery": return type.exprName.type === "Identifier" && tableKind(type.exprName, environment, depth + 1) === "dictionary";
|
|
435
|
+
default: return false;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function isProtoKey(key) {
|
|
439
|
+
return key.type === "Identifier" && key.name === "__proto__" || key.type === "Literal" && key.value === "__proto__";
|
|
440
|
+
}
|
|
441
|
+
function isNullPrototype(init) {
|
|
442
|
+
const value = unwrapExpression(init.type === "TSAsExpression" || init.type === "TSSatisfiesExpression" ? init.expression : init);
|
|
443
|
+
if (value.type === "CallExpression") return callName(value.callee) === "Object.create" && value.arguments[0]?.type === "Literal" && value.arguments[0].value === null;
|
|
444
|
+
return value.type === "ObjectExpression" && value.properties.some((property) => property.type === "Property" && !property.computed && isProtoKey(property.key) && property.value.type === "Literal" && property.value.value === null);
|
|
445
|
+
}
|
|
446
|
+
function initializerKind(init, environment, depth) {
|
|
447
|
+
if (init.type === "TSSatisfiesExpression") return isStringKeyedType(init.typeAnnotation, environment, depth) ? "dictionary" : initializerKind(init.expression, environment, depth);
|
|
448
|
+
if (init.type === "TSAsExpression") {
|
|
449
|
+
if (init.typeAnnotation.type === "TSTypeReference" && referenceName(init.typeAnnotation) === "const") return initializerKind(init.expression, environment, depth);
|
|
450
|
+
return isStringKeyedType(init.typeAnnotation, environment, depth) ? "dictionary" : null;
|
|
451
|
+
}
|
|
452
|
+
return unwrapExpression(init).type === "ObjectExpression" ? "literal" : null;
|
|
453
|
+
}
|
|
454
|
+
function annotationOf(definition) {
|
|
455
|
+
const name = definition.name;
|
|
456
|
+
return name.type === "Identifier" && "typeAnnotation" in name ? name.typeAnnotation?.typeAnnotation ?? void 0 : void 0;
|
|
457
|
+
}
|
|
458
|
+
function variableKind(variable, environment, depth) {
|
|
459
|
+
const [definition] = variable.defs;
|
|
460
|
+
if (definition === void 0) return null;
|
|
461
|
+
const annotation = annotationOf(definition);
|
|
462
|
+
if (definition.type === "Parameter") return annotation !== void 0 && isStringKeyedType(annotation, environment, depth) ? "dictionary" : null;
|
|
463
|
+
if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") return null;
|
|
464
|
+
return declaredKind(annotation, definition.node.init, environment, depth);
|
|
465
|
+
}
|
|
466
|
+
/** The table a field declares, given its type annotation and initializer. */
|
|
467
|
+
function declaredKind(annotation, init, environment, depth) {
|
|
468
|
+
if (init !== null && isNullPrototype(init)) return null;
|
|
469
|
+
if (annotation !== void 0) return isStringKeyedType(annotation, environment, depth) ? "dictionary" : null;
|
|
470
|
+
return init === null ? null : initializerKind(init, environment, depth);
|
|
471
|
+
}
|
|
472
|
+
function fieldKind(node, environment, depth) {
|
|
473
|
+
if (node.computed || node.object.type !== "ThisExpression" || node.property.type !== "Identifier") return null;
|
|
474
|
+
const field = classField(node, node.property.name);
|
|
475
|
+
if (field === null) return null;
|
|
476
|
+
return declaredKind(field.typeAnnotation?.typeAnnotation ?? void 0, field.value, environment, depth);
|
|
477
|
+
}
|
|
478
|
+
function tableKind(node, environment, depth = 0) {
|
|
479
|
+
const value = node.type === "Identifier" ? node : unwrapExpression(node);
|
|
480
|
+
if (value.type === "ConditionalExpression") {
|
|
481
|
+
const kinds = [value.consequent, value.alternate].map((branch) => tableKind(branch, environment, depth));
|
|
482
|
+
if (kinds.includes("dictionary")) return "dictionary";
|
|
483
|
+
return kinds.includes("literal") ? "literal" : null;
|
|
484
|
+
}
|
|
485
|
+
if (value.type === "MemberExpression") return fieldKind(value, environment, depth);
|
|
486
|
+
if (value.type !== "Identifier") return null;
|
|
487
|
+
const variable = resolveVariable(environment.source, value);
|
|
488
|
+
return variable === null ? null : variableKind(variable, environment, depth);
|
|
489
|
+
}
|
|
490
|
+
function isWrite(node) {
|
|
491
|
+
const parent = node.parent;
|
|
492
|
+
if (parent.type === "AssignmentExpression") return parent.left === node && parent.operator === "=";
|
|
493
|
+
if (parent.type === "UnaryExpression") return parent.operator === "delete";
|
|
494
|
+
return (parent.type === "ForInStatement" || parent.type === "ForOfStatement") && parent.left === node;
|
|
495
|
+
}
|
|
496
|
+
function ownKeyCheck(node, source) {
|
|
497
|
+
const [first, second] = node.arguments;
|
|
498
|
+
if (first === void 0) return null;
|
|
499
|
+
if (HAS_OWN_CALLS.has(callName(node.callee) ?? "") || isHasOwnPropertyCall(node.callee)) return second === void 0 ? null : {
|
|
500
|
+
table: source.getText(first),
|
|
501
|
+
key: source.getText(second)
|
|
502
|
+
};
|
|
503
|
+
const callee = node.callee;
|
|
504
|
+
if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "hasOwnProperty") return {
|
|
505
|
+
table: source.getText(callee.object),
|
|
506
|
+
key: source.getText(first)
|
|
507
|
+
};
|
|
508
|
+
return null;
|
|
509
|
+
}
|
|
510
|
+
function isPredicateKey(node) {
|
|
511
|
+
const fn = enclosingFunction(node);
|
|
512
|
+
const predicate = fn === null ? null : parameterPredicate(fn);
|
|
513
|
+
return predicate !== null && node.left.type === "Identifier" && predicateName(predicate) === node.left.name;
|
|
514
|
+
}
|
|
515
|
+
function contains(outer, inner) {
|
|
516
|
+
return outer.range[0] <= inner.range[0] && inner.range[1] <= outer.range[1];
|
|
517
|
+
}
|
|
518
|
+
/** Lookups into string-keyed object tables must not answer inherited `Object.prototype` keys. */
|
|
519
|
+
const noInheritedKeyLookupRule = defineRule({
|
|
520
|
+
meta: {
|
|
521
|
+
type: "problem",
|
|
522
|
+
docs: { description: "Disallow reading a string-keyed object table with a computed key, or testing it with `in`, without an own-key check." },
|
|
523
|
+
messages: {
|
|
524
|
+
lookup: "`{{ table }}` is a string-keyed object table, so `{{ table }}[{{ key }}]` also answers keys inherited from `Object.prototype` such as `constructor` and `toString`. Store the table in a `Map` and call `.get({{ key }})`, check `Object.hasOwn({{ table }}, {{ key }})` before the read, or build the table with a null prototype (`Object.create(null)` or `{ __proto__: null, ... }`).",
|
|
525
|
+
inOperator: "`{{ key }} in {{ table }}` is also true for keys inherited from `Object.prototype` such as `constructor` and `toString`. Use `Object.hasOwn({{ table }}, {{ key }})`, or store the table in a `Map` and call `.has({{ key }})`."
|
|
526
|
+
}
|
|
527
|
+
},
|
|
528
|
+
createOnce(context) {
|
|
529
|
+
let environment = null;
|
|
530
|
+
let candidates = [];
|
|
531
|
+
let checks = [];
|
|
532
|
+
return {
|
|
533
|
+
Program(node) {
|
|
534
|
+
environment = {
|
|
535
|
+
source: context.sourceCode,
|
|
536
|
+
types: collectLocalTypes(node),
|
|
537
|
+
program: node
|
|
538
|
+
};
|
|
539
|
+
candidates = [];
|
|
540
|
+
checks = [];
|
|
541
|
+
},
|
|
542
|
+
MemberExpression(node) {
|
|
543
|
+
if (environment === null || !node.computed || isWrite(node)) return;
|
|
544
|
+
const kind = tableKind(node.object, environment);
|
|
545
|
+
if (kind === null || kind === "literal" && node.property.type !== "TSAsExpression") return;
|
|
546
|
+
const table = context.sourceCode.getText(node.object);
|
|
547
|
+
if (isKnownKey(node.property, table, context.sourceCode)) return;
|
|
548
|
+
candidates.push({
|
|
549
|
+
node,
|
|
550
|
+
messageId: "lookup",
|
|
551
|
+
table,
|
|
552
|
+
key: context.sourceCode.getText(node.property)
|
|
553
|
+
});
|
|
554
|
+
},
|
|
555
|
+
BinaryExpression(node) {
|
|
556
|
+
if (environment === null || node.operator !== "in" || isStaticKey(node.left) || isPredicateKey(node) || tableKind(node.right, environment) === null) return;
|
|
557
|
+
candidates.push({
|
|
558
|
+
node,
|
|
559
|
+
messageId: "inOperator",
|
|
560
|
+
table: context.sourceCode.getText(node.right),
|
|
561
|
+
key: context.sourceCode.getText(node.left)
|
|
562
|
+
});
|
|
563
|
+
},
|
|
564
|
+
CallExpression(node) {
|
|
565
|
+
const check = ownKeyCheck(node, context.sourceCode);
|
|
566
|
+
if (check !== null && environment !== null) checks.push({
|
|
567
|
+
...check,
|
|
568
|
+
scope: enclosingFunction(node) ?? environment.program
|
|
569
|
+
});
|
|
570
|
+
},
|
|
571
|
+
"Program:exit"() {
|
|
572
|
+
for (const candidate of candidates) if (!checks.some((check) => check.table === candidate.table && check.key === candidate.key && contains(check.scope, candidate.node))) context.report({
|
|
573
|
+
node: candidate.node,
|
|
574
|
+
messageId: candidate.messageId,
|
|
575
|
+
data: {
|
|
576
|
+
table: candidate.table,
|
|
577
|
+
key: candidate.key
|
|
578
|
+
}
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
});
|
|
584
|
+
//#endregion
|
|
585
|
+
//#region src/oxc/guardrails/rules/no-presence-only-type-guard.ts
|
|
586
|
+
const MAX_ALIAS_DEPTH = 8;
|
|
587
|
+
const OBJECT_PROTOTYPE_KEYS = Object.getOwnPropertyNames(Object.prototype);
|
|
588
|
+
const OPEN_TYPES = /* @__PURE__ */ new Set(["TSUnknownKeyword", "TSAnyKeyword"]);
|
|
589
|
+
const WIDE_TYPES = /* @__PURE__ */ new Set([...OPEN_TYPES, "TSObjectKeyword"]);
|
|
590
|
+
function signatureName(member) {
|
|
591
|
+
if (member.type !== "TSPropertySignature" && member.type !== "TSMethodSignature" || member.computed) return null;
|
|
592
|
+
if (member.key.type === "Identifier") return member.key.name;
|
|
593
|
+
return member.key.type === "Literal" ? String(member.key.value) : null;
|
|
594
|
+
}
|
|
595
|
+
function addSignatures(members, body) {
|
|
596
|
+
for (const member of body) {
|
|
597
|
+
const name = signatureName(member);
|
|
598
|
+
if (name === null) continue;
|
|
599
|
+
members.set(name, member.type === "TSPropertySignature" ? member.typeAnnotation?.typeAnnotation ?? null : null);
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
function referenceMembers(type, types, depth) {
|
|
603
|
+
const name = referenceName(type);
|
|
604
|
+
if (name === null) return null;
|
|
605
|
+
const alias = types.aliases.get(name);
|
|
606
|
+
if (alias !== void 0) return declaredMembers(alias, types, depth + 1);
|
|
607
|
+
const declarations = types.interfaces.get(name);
|
|
608
|
+
if (declarations === void 0) return null;
|
|
609
|
+
const members = /* @__PURE__ */ new Map();
|
|
610
|
+
for (const declaration of declarations) addSignatures(members, declaration.body.body);
|
|
611
|
+
return members;
|
|
612
|
+
}
|
|
613
|
+
/** The members a narrowed type declares in this file, or null when the file does not declare them. */
|
|
614
|
+
function declaredMembers(type, types, depth = 0) {
|
|
615
|
+
if (depth > MAX_ALIAS_DEPTH) return null;
|
|
616
|
+
switch (type.type) {
|
|
617
|
+
case "TSParenthesizedType": return declaredMembers(type.typeAnnotation, types, depth);
|
|
618
|
+
case "TSTypeLiteral": {
|
|
619
|
+
const members = /* @__PURE__ */ new Map();
|
|
620
|
+
addSignatures(members, type.members);
|
|
621
|
+
return members;
|
|
622
|
+
}
|
|
623
|
+
case "TSTypeReference": return referenceMembers(type, types, depth);
|
|
624
|
+
case "TSIntersectionType": {
|
|
625
|
+
const parts = type.types.map((part) => declaredMembers(part, types, depth)).filter((part) => part !== null);
|
|
626
|
+
return parts.length === 0 ? null : new Map(parts.flatMap((part) => [...part]));
|
|
627
|
+
}
|
|
628
|
+
default: return null;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
function parameterType(fn, name) {
|
|
632
|
+
for (const parameter of fn.params) {
|
|
633
|
+
const binding = parameter.type === "AssignmentPattern" ? parameter.left : parameter;
|
|
634
|
+
if (binding.type === "Identifier" && binding.name === name) return binding.typeAnnotation?.typeAnnotation ?? void 0;
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
/**
|
|
638
|
+
* A key typed `prefix${string}` cannot name an inherited member when no `Object.prototype` member starts with that prefix.
|
|
639
|
+
* The prefix is the cooked text TypeScript reads, so `\x63onstructor` is `constructor`. A prefix with no cooked text is open.
|
|
640
|
+
*/
|
|
641
|
+
function excludesPrototypeKeys(type) {
|
|
642
|
+
if (type?.type !== "TSTemplateLiteralType") return false;
|
|
643
|
+
const prefix = type.quasis[0]?.value.cooked;
|
|
644
|
+
return prefix !== void 0 && prefix !== null && !OBJECT_PROTOTYPE_KEYS.some((key) => key.startsWith(prefix));
|
|
645
|
+
}
|
|
646
|
+
function isOpenRecord(type) {
|
|
647
|
+
const value = type.typeArguments?.params[1];
|
|
648
|
+
return referenceName(type) === "Record" && value !== void 0 && OPEN_TYPES.has(value.type);
|
|
649
|
+
}
|
|
650
|
+
/**
|
|
651
|
+
* A parameter typed `unknown`, `object`, `{}` or an open record carries no member types, so the guard
|
|
652
|
+
* alone vouches for them. A named or inferred parameter type already declares what a discriminant leaves out.
|
|
653
|
+
*/
|
|
654
|
+
function isWide(type) {
|
|
655
|
+
if (type === void 0) return false;
|
|
656
|
+
if (WIDE_TYPES.has(type.type)) return true;
|
|
657
|
+
if (type.type === "TSTypeLiteral") return type.members.length === 0;
|
|
658
|
+
if (type.type === "TSTypeReference") return isOpenRecord(type);
|
|
659
|
+
return type.type === "TSUnionType" && type.types.some(isWide);
|
|
660
|
+
}
|
|
661
|
+
function frameFor(fn) {
|
|
662
|
+
const predicate = parameterPredicate(fn);
|
|
663
|
+
const name = predicate === null ? null : predicateName(predicate);
|
|
664
|
+
const narrowed = predicate?.typeAnnotation?.typeAnnotation;
|
|
665
|
+
if (predicate === null || name === null || narrowed === void 0) return null;
|
|
666
|
+
return {
|
|
667
|
+
name,
|
|
668
|
+
predicate,
|
|
669
|
+
narrowed,
|
|
670
|
+
parameter: parameterType(fn, name),
|
|
671
|
+
aliases: /* @__PURE__ */ new Set([name]),
|
|
672
|
+
reads: [],
|
|
673
|
+
dynamicReads: [],
|
|
674
|
+
presence: []
|
|
675
|
+
};
|
|
676
|
+
}
|
|
677
|
+
function guardDeclarations(program) {
|
|
678
|
+
const guards = /* @__PURE__ */ new Map();
|
|
679
|
+
const record = (name, fn) => {
|
|
680
|
+
const narrowed = parameterPredicate(fn)?.typeAnnotation?.typeAnnotation;
|
|
681
|
+
if (narrowed !== void 0) guards.set(name, narrowed);
|
|
682
|
+
};
|
|
683
|
+
for (const statement of program.body) {
|
|
684
|
+
const declaration = statement.type === "ExportNamedDeclaration" ? statement.declaration : statement;
|
|
685
|
+
if (declaration?.type === "FunctionDeclaration" && declaration.id !== null) record(declaration.id.name, declaration);
|
|
686
|
+
if (declaration?.type !== "VariableDeclaration") continue;
|
|
687
|
+
for (const declarator of declaration.declarations) {
|
|
688
|
+
const init = declarator.init;
|
|
689
|
+
if (declarator.id.type === "Identifier" && (init?.type === "ArrowFunctionExpression" || init?.type === "FunctionExpression")) record(declarator.id.name, init);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
return guards;
|
|
693
|
+
}
|
|
694
|
+
function unwrapAlias(node) {
|
|
695
|
+
let current = unwrapExpression(node);
|
|
696
|
+
while (current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion") current = unwrapExpression(current.expression);
|
|
697
|
+
return current;
|
|
698
|
+
}
|
|
699
|
+
const PRESENCE_CALLS = /* @__PURE__ */ new Set([
|
|
700
|
+
"Object.hasOwn",
|
|
701
|
+
"hasOwnProperty.call",
|
|
702
|
+
"Reflect.has"
|
|
703
|
+
]);
|
|
704
|
+
/** `Object.hasOwn(x, "k")`, `hasOwnProperty.call(x, "k")` and `Reflect.has(x, "k")` test presence, as `"k" in x` does. */
|
|
705
|
+
function presenceCall(node) {
|
|
706
|
+
const [first, second] = node.arguments;
|
|
707
|
+
if (!PRESENCE_CALLS.has(callName(node.callee) ?? "") && !isHasOwnPropertyCall(node.callee) || first === void 0 || first.type === "SpreadElement" || second?.type !== "Literal") return null;
|
|
708
|
+
const object = unwrapAlias(first);
|
|
709
|
+
return object.type === "Identifier" ? {
|
|
710
|
+
object: object.name,
|
|
711
|
+
key: String(second.value)
|
|
712
|
+
} : null;
|
|
713
|
+
}
|
|
714
|
+
function callReads(node, environment) {
|
|
715
|
+
const objects = node.arguments.flatMap((argument) => argument.type === "Identifier" ? [argument.name] : []);
|
|
716
|
+
const keys = node.arguments.flatMap((argument) => argument.type === "Literal" ? [String(argument.value)] : []);
|
|
717
|
+
const guard = node.callee.type === "Identifier" ? environment.guards.get(node.callee.name) : void 0;
|
|
718
|
+
if (guard !== void 0) keys.push(...declaredMembers(guard, environment.types)?.keys() ?? []);
|
|
719
|
+
return objects.flatMap((object) => keys.map((key) => [object, key]));
|
|
720
|
+
}
|
|
721
|
+
/** Reports the presence-only checks and unread members of one finished guard. */
|
|
722
|
+
function evaluate(frame, env, context) {
|
|
723
|
+
const wide = isWide(frame.parameter);
|
|
724
|
+
if (!wide && frame.narrowed.type !== "TSIntersectionType") return;
|
|
725
|
+
const members = declaredMembers(frame.narrowed, env.types);
|
|
726
|
+
const isRead = (key) => frame.reads.some(([object, read]) => read === key && frame.aliases.has(object));
|
|
727
|
+
const isOpen = (key) => {
|
|
728
|
+
const type = members?.get(key);
|
|
729
|
+
return type !== void 0 && type !== null && OPEN_TYPES.has(type.type);
|
|
730
|
+
};
|
|
731
|
+
const present = /* @__PURE__ */ new Set();
|
|
732
|
+
for (const presence of frame.presence) {
|
|
733
|
+
if (!frame.aliases.has(presence.object)) continue;
|
|
734
|
+
present.add(presence.key);
|
|
735
|
+
if (isRead(presence.key) || isOpen(presence.key)) continue;
|
|
736
|
+
context.report({
|
|
737
|
+
node: presence.node,
|
|
738
|
+
messageId: "presenceOnly",
|
|
739
|
+
data: {
|
|
740
|
+
key: presence.key,
|
|
741
|
+
param: presence.object
|
|
742
|
+
}
|
|
743
|
+
});
|
|
744
|
+
}
|
|
745
|
+
const dynamic = frame.dynamicReads.some((object) => frame.aliases.has(object));
|
|
746
|
+
if (!wide || dynamic) return;
|
|
747
|
+
for (const member of members?.keys() ?? []) {
|
|
748
|
+
if (isOpen(member) || present.has(member) || isRead(member)) continue;
|
|
749
|
+
context.report({
|
|
750
|
+
node: frame.predicate,
|
|
751
|
+
messageId: "uncheckedMember",
|
|
752
|
+
data: {
|
|
753
|
+
member,
|
|
754
|
+
param: frame.name,
|
|
755
|
+
type: context.sourceCode.getText(frame.narrowed)
|
|
756
|
+
}
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
/** A type predicate must check what it narrows to, not only that a key is present. */
|
|
761
|
+
const noPresenceOnlyTypeGuardRule = defineRule({
|
|
762
|
+
meta: {
|
|
763
|
+
type: "problem",
|
|
764
|
+
docs: { description: "Disallow type predicates that narrow on `in` alone: a key answered by the prototype, or a member whose value is never checked." },
|
|
765
|
+
messages: {
|
|
766
|
+
inheritedKey: "`{{ key }} in ...` is also true for keys inherited from `Object.prototype` such as `constructor` and `toString`, so this guard narrows `{{ key }}` to keys the table does not have. Use `Object.hasOwn(table, {{ key }})`, or keep the keys in a `Map` or `Set` and call `.has({{ key }})`.",
|
|
767
|
+
presenceOnly: "`\"{{ key }}\" in {{ param }}` checks only that `{{ key }}` is present, yet the guard narrows `{{ param }}` to a type that declares its value. Check the value too (`typeof {{ param }}.{{ key }} === \"function\"`, `Array.isArray({{ param }}.{{ key }})`, a nested guard), or parse `{{ param }}` with a schema.",
|
|
768
|
+
uncheckedMember: "This guard narrows `{{ param }}` to `{{ type }}` but never reads `{{ param }}.{{ member }}`, so a value with any `{{ member }}` passes. Check it (`typeof`, `Array.isArray`, a nested guard), narrow to a type without it (`Pick<...>`), or parse `{{ param }}` with a schema."
|
|
769
|
+
}
|
|
770
|
+
},
|
|
771
|
+
createOnce(context) {
|
|
772
|
+
let environment = null;
|
|
773
|
+
const stack = [];
|
|
774
|
+
const active = () => stack.filter((frame) => frame !== null);
|
|
775
|
+
const enter = (fn) => {
|
|
776
|
+
stack.push(frameFor(fn));
|
|
777
|
+
};
|
|
778
|
+
const leave = () => {
|
|
779
|
+
const frame = stack.pop();
|
|
780
|
+
if (frame !== null && frame !== void 0 && environment !== null) evaluate(frame, environment, context);
|
|
781
|
+
};
|
|
782
|
+
return {
|
|
783
|
+
Program(node) {
|
|
784
|
+
environment = {
|
|
785
|
+
types: collectLocalTypes(node),
|
|
786
|
+
guards: guardDeclarations(node)
|
|
787
|
+
};
|
|
788
|
+
stack.length = 0;
|
|
789
|
+
},
|
|
790
|
+
FunctionDeclaration: enter,
|
|
791
|
+
FunctionExpression: enter,
|
|
792
|
+
ArrowFunctionExpression: enter,
|
|
793
|
+
"FunctionDeclaration:exit": leave,
|
|
794
|
+
"FunctionExpression:exit": leave,
|
|
795
|
+
"ArrowFunctionExpression:exit": leave,
|
|
796
|
+
MemberExpression(node) {
|
|
797
|
+
const target = unwrapAlias(node.object);
|
|
798
|
+
if (target.type !== "Identifier") return;
|
|
799
|
+
const object = target.name;
|
|
800
|
+
const key = memberName(node);
|
|
801
|
+
for (const frame of active()) if (key === null) frame.dynamicReads.push(object);
|
|
802
|
+
else frame.reads.push([object, key]);
|
|
803
|
+
},
|
|
804
|
+
CallExpression(node) {
|
|
805
|
+
if (environment === null) return;
|
|
806
|
+
const presence = presenceCall(node);
|
|
807
|
+
if (presence !== null) {
|
|
808
|
+
active().at(-1)?.presence.push({
|
|
809
|
+
node,
|
|
810
|
+
...presence
|
|
811
|
+
});
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
const reads = callReads(node, environment);
|
|
815
|
+
for (const frame of active()) frame.reads.push(...reads);
|
|
816
|
+
},
|
|
817
|
+
VariableDeclarator(node) {
|
|
818
|
+
if (node.init === null) return;
|
|
819
|
+
const init = unwrapAlias(node.init);
|
|
820
|
+
if (init.type !== "Identifier") return;
|
|
821
|
+
for (const frame of active()) {
|
|
822
|
+
if (!frame.aliases.has(init.name)) continue;
|
|
823
|
+
if (node.id.type === "Identifier") frame.aliases.add(node.id.name);
|
|
824
|
+
if (node.id.type !== "ObjectPattern") continue;
|
|
825
|
+
for (const property of node.id.properties) if (property.type === "Property" && property.key.type === "Identifier") frame.reads.push([init.name, property.key.name]);
|
|
826
|
+
}
|
|
827
|
+
},
|
|
828
|
+
BinaryExpression(node) {
|
|
829
|
+
const frame = active().at(-1);
|
|
830
|
+
if (node.operator !== "in" || frame === void 0) return;
|
|
831
|
+
if (node.left.type === "Identifier" && node.left.name === frame.name) {
|
|
832
|
+
if (!excludesPrototypeKeys(frame.parameter)) context.report({
|
|
833
|
+
node,
|
|
834
|
+
messageId: "inheritedKey",
|
|
835
|
+
data: { key: frame.name }
|
|
836
|
+
});
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
if (node.left.type === "Literal" && node.right.type === "Identifier") frame.presence.push({
|
|
840
|
+
node,
|
|
841
|
+
object: node.right.name,
|
|
842
|
+
key: String(node.left.value)
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
};
|
|
846
|
+
}
|
|
847
|
+
});
|
|
848
|
+
//#endregion
|
|
196
849
|
//#region src/oxc/guardrails/self-package.ts
|
|
197
850
|
function isObject(value) {
|
|
198
851
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
@@ -420,17 +1073,96 @@ const requireAssertionRule = defineRule({
|
|
|
420
1073
|
}
|
|
421
1074
|
});
|
|
422
1075
|
//#endregion
|
|
1076
|
+
//#region src/oxc/guardrails/tsconfig-program.ts
|
|
1077
|
+
const CONFIG_NAMES = ["tsconfig.json", "jsconfig.json"];
|
|
1078
|
+
const HOST = {
|
|
1079
|
+
...ts.sys,
|
|
1080
|
+
onUnRecoverableConfigFileDiagnostic: () => void 0
|
|
1081
|
+
};
|
|
1082
|
+
const projects = /* @__PURE__ */ new Map();
|
|
1083
|
+
function configAbove(directory) {
|
|
1084
|
+
for (let current = directory;; current = dirname(current)) {
|
|
1085
|
+
const config = CONFIG_NAMES.map((name) => join(current, name)).find((path) => existsSync(path));
|
|
1086
|
+
if (config !== void 0) return config;
|
|
1087
|
+
if (dirname(current) === current) return void 0;
|
|
1088
|
+
}
|
|
1089
|
+
}
|
|
1090
|
+
/** The files `config` lists and the configs it references, parsed once per lint run. */
|
|
1091
|
+
function project(config) {
|
|
1092
|
+
if (projects.has(config)) return projects.get(config);
|
|
1093
|
+
const parsed = ts.getParsedCommandLineOfConfigFile(config, void 0, HOST);
|
|
1094
|
+
const result = parsed && {
|
|
1095
|
+
files: new Set(parsed.fileNames),
|
|
1096
|
+
references: (parsed.projectReferences ?? []).map((reference) => ts.resolveProjectReferencePath(reference))
|
|
1097
|
+
};
|
|
1098
|
+
projects.set(config, result);
|
|
1099
|
+
return result;
|
|
1100
|
+
}
|
|
1101
|
+
/** The first config the nearest one or its references reach that lists `file`, skipping configs already visited. */
|
|
1102
|
+
function listing(nearest, file, visited) {
|
|
1103
|
+
const queue = [nearest];
|
|
1104
|
+
for (let config = queue.shift(); config; config = queue.shift()) {
|
|
1105
|
+
if (visited.has(config)) continue;
|
|
1106
|
+
visited.add(config);
|
|
1107
|
+
const parsed = project(config);
|
|
1108
|
+
if (parsed?.files.has(file)) return config;
|
|
1109
|
+
queue.push(...parsed?.references ?? []);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
/**
|
|
1113
|
+
* The tsconfig.json or jsconfig.json whose program type-aware lint gives `file`,
|
|
1114
|
+
* found as oxlint-tsgolint finds it: the nearest config, the configs it references,
|
|
1115
|
+
* then the same search from each config above. Undefined when none lists the file,
|
|
1116
|
+
* where tsgolint reads it in its inferred program.
|
|
1117
|
+
*/
|
|
1118
|
+
function programConfig(file) {
|
|
1119
|
+
const path = resolve(file).replaceAll("\\", "/");
|
|
1120
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1121
|
+
let directory = dirname(path);
|
|
1122
|
+
for (;;) {
|
|
1123
|
+
const nearest = configAbove(directory);
|
|
1124
|
+
if (nearest === void 0) return void 0;
|
|
1125
|
+
const config = listing(nearest, path, visited);
|
|
1126
|
+
if (config !== void 0) return config;
|
|
1127
|
+
const parent = dirname(dirname(nearest));
|
|
1128
|
+
if (parent === dirname(nearest)) return void 0;
|
|
1129
|
+
directory = parent;
|
|
1130
|
+
}
|
|
1131
|
+
}
|
|
1132
|
+
//#endregion
|
|
1133
|
+
//#region src/oxc/guardrails/rules/require-tsconfig-include.ts
|
|
1134
|
+
const TYPE_LINTED = /\.[cm]?[jt]sx?$/u;
|
|
1135
|
+
/** A file type-aware lint reads must be listed by a tsconfig, or tsgolint types it without the repository's compiler options and `types`. */
|
|
1136
|
+
const requireTsconfigIncludeRule = defineRule({
|
|
1137
|
+
meta: {
|
|
1138
|
+
type: "problem",
|
|
1139
|
+
docs: { description: "Every file type-aware lint reads is listed by a tsconfig.json, so its findings use the repository's compiler options and types." },
|
|
1140
|
+
messages: { outside: "No tsconfig.json lists this file, so type-aware lint reads it in tsgolint's inferred program, without this repository's compiler options or `types`: Node and test-runner globals are error types, so `typescript/no-unsafe-*` reports here are false and findings that need those types go missing. List the file in a tsconfig.json that names the `types` it uses: one beside it that extends the root tsconfig, or, for a file at the root, a composite tsconfig that the root tsconfig references." }
|
|
1141
|
+
},
|
|
1142
|
+
createOnce(context) {
|
|
1143
|
+
return { Program(node) {
|
|
1144
|
+
if (TYPE_LINTED.test(context.filename) && programConfig(context.filename) === void 0) context.report({
|
|
1145
|
+
node,
|
|
1146
|
+
messageId: "outside"
|
|
1147
|
+
});
|
|
1148
|
+
} };
|
|
1149
|
+
}
|
|
1150
|
+
});
|
|
1151
|
+
//#endregion
|
|
423
1152
|
//#region src/oxc/guardrails/index.ts
|
|
424
|
-
/** AntelopeJS rules that reject comments
|
|
1153
|
+
/** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, low-evidence tests and files no tsconfig lists. */
|
|
425
1154
|
const guardrailsPlugin = eslintCompatPlugin({
|
|
426
1155
|
meta: { name: "antelopejs" },
|
|
427
1156
|
rules: {
|
|
428
1157
|
"no-comments": noCommentsRule,
|
|
429
1158
|
"no-fs-read-in-tests": noFsReadInTestsRule,
|
|
1159
|
+
"no-inherited-key-lookup": noInheritedKeyLookupRule,
|
|
1160
|
+
"no-presence-only-type-guard": noPresenceOnlyTypeGuardRule,
|
|
430
1161
|
"no-constant-value-assertion": noConstantValueAssertionRule,
|
|
431
1162
|
"no-self-import-in-tests": noSelfImportInTestsRule,
|
|
432
1163
|
"no-typeof-function-only-test": noTypeofFunctionOnlyTestRule,
|
|
433
|
-
"require-assertion": requireAssertionRule
|
|
1164
|
+
"require-assertion": requireAssertionRule,
|
|
1165
|
+
"require-tsconfig-include": requireTsconfigIncludeRule
|
|
434
1166
|
}
|
|
435
1167
|
});
|
|
436
1168
|
//#endregion
|