@antelopejs/tooling-configs 0.5.8 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -49
- package/dist/{api-CCOiLN3w.mjs → api-BQLrI_S1.mjs} +19 -2
- package/dist/bin/api-diff.mjs +155 -37
- package/dist/bin/body-check.mjs +6 -3
- package/dist/bin/check-size.mjs +1 -1
- package/dist/bin/checks.mjs +1 -1
- package/dist/bin/comment-proof.mjs +3 -5
- package/dist/bin/garden.mjs +86 -34
- package/dist/bin/lease.mjs +314 -26
- package/dist/bin/ratchet.mjs +4 -4
- package/dist/bin/readme-strip.mjs +1 -1
- package/dist/bin/suppressions.mjs +10 -6
- package/dist/bin/sync.mjs +23 -98
- package/dist/bin/triage.mjs +1 -1
- package/dist/bin/verify.mjs +1 -1
- package/dist/{checks-BC-F8bsv.mjs → checks-BefrkjiT.mjs} +729 -25
- package/dist/{docs-compat-Ft1r2sf0.mjs → docs-compat-CSljVi3O.mjs} +13 -8
- package/dist/eslint.d.mts +13 -4
- package/dist/eslint.mjs +48 -6
- package/dist/guardrails-BfGwehVj.mjs +2414 -0
- package/dist/installed-typescript-6MtIWlE4.mjs +84 -0
- package/dist/oxc/anti-slop/index.mjs +541 -34
- package/dist/oxc/guardrails/index.d.mts +1 -1
- package/dist/oxc/guardrails/index.mjs +2 -1854
- package/dist/oxc/lint.d.mts +10 -1
- package/dist/oxc/lint.mjs +13 -5
- package/dist/{oxlint-CCOJqj3d.mjs → oxlint-CkHhK5K_.mjs} +33 -11
- package/dist/{package-DnxZ2CXZ.mjs → package-DDG3YH7W.mjs} +10 -5
- package/dist/{ports-DGP6bPJy.mjs → ports-DAeEJsYh.mjs} +22 -0
- package/dist/{proof-stamp-lyD84zmj.mjs → proof-stamp-BFV7JTZT.mjs} +5 -4
- package/dist/{ratchet-SQ7vTR0i.mjs → ratchet-2FeMs1kh.mjs} +4 -3
- package/dist/{suppression-base-whhvw-IJ.mjs → suppression-base-CYaLZAq7.mjs} +2 -2
- package/dist/{suppression-budget-CtIYSoDO.mjs → suppression-budget-CPDSFg4X.mjs} +1 -1
- package/dist/{suppressions-C79kRvYI.mjs → suppressions-CxPnh4sC.mjs} +2 -2
- package/dist/{templates-Dg188oRC.mjs → templates-HG0fel2S.mjs} +1 -1
- package/dist/{type-aware-rules-DMuN6sHb.mjs → type-aware-rules-Da3769fI.mjs} +9 -1
- package/dist/{typescript--_8og8AM.mjs → typescript-CxfNxyr2.mjs} +59 -98
- package/dist/{workflow-CPcNc8KL.mjs → workflow-C-oBwL2H.mjs} +25 -1
- package/package.json +9 -5
- package/dist/bin/check-interface-ranges.d.mts +0 -1
- package/dist/bin/check-interface-ranges.mjs +0 -44
- package/dist/interface-ranges-CcVSr59f.mjs +0 -116
|
@@ -540,6 +540,14 @@ function isKnownEvidenceExpression(expression) {
|
|
|
540
540
|
}
|
|
541
541
|
//#endregion
|
|
542
542
|
//#region src/oxc/anti-slop/shared/function-parameters.ts
|
|
543
|
+
/** Return whether a parameter is a TypeScript `this` declaration, which takes no call argument. */
|
|
544
|
+
function isThisParameter(parameter) {
|
|
545
|
+
return parameter.type === "Identifier" && parameter.name === "this";
|
|
546
|
+
}
|
|
547
|
+
/** Return the parameters that receive call arguments, in argument order: a declared `this` takes none. */
|
|
548
|
+
function argumentParameters(fn) {
|
|
549
|
+
return fn.params.filter((parameter) => !isThisParameter(parameter));
|
|
550
|
+
}
|
|
543
551
|
/** Return whether a type is or contains TypeScript's absorbing unknown top type. */
|
|
544
552
|
function containsUnknownType(type) {
|
|
545
553
|
if (type.type === "TSUnknownKeyword") return true;
|
|
@@ -646,11 +654,11 @@ function hasKnownCallArgumentEvidence(sourceCode, expression, environment, visit
|
|
|
646
654
|
visitedVariables.add(variable);
|
|
647
655
|
return hasKnownCallArgumentEvidence(sourceCode, declarator.init, environment, visitedVariables);
|
|
648
656
|
}
|
|
649
|
-
function typePredicateSubjectIndex(sourceCode, owner) {
|
|
657
|
+
function typePredicateSubjectIndex(sourceCode, owner, parameters) {
|
|
650
658
|
const predicate = owner.returnType?.typeAnnotation;
|
|
651
659
|
if (predicate?.type !== "TSTypePredicate" || predicate.parameterName.type !== "Identifier") return null;
|
|
652
660
|
const predicateParameterName = predicate.parameterName.name;
|
|
653
|
-
const index =
|
|
661
|
+
const index = parameters.findIndex((parameter) => functionParameterBindingName(parameter, sourceCode) === predicateParameterName);
|
|
654
662
|
return index === -1 ? null : index;
|
|
655
663
|
}
|
|
656
664
|
function annotationTarget(annotation, environment) {
|
|
@@ -738,9 +746,10 @@ const noKnownValueWideningRule = defineRule({
|
|
|
738
746
|
if (environment === null) return;
|
|
739
747
|
const owner = localFunctionForCall(context.sourceCode, node.callee);
|
|
740
748
|
if (owner === null) return;
|
|
741
|
-
const
|
|
749
|
+
const parameters = argumentParameters(owner);
|
|
750
|
+
const parameterIndex = typePredicateSubjectIndex(context.sourceCode, owner, parameters);
|
|
742
751
|
if (parameterIndex === null) return;
|
|
743
|
-
const parameter =
|
|
752
|
+
const parameter = parameters[parameterIndex];
|
|
744
753
|
const argument = node.arguments[parameterIndex];
|
|
745
754
|
if (parameter === void 0 || argument === void 0 || argument.type === "SpreadElement") return;
|
|
746
755
|
const parameterAnnotation = functionParameterTypeAnnotation(parameter);
|
|
@@ -1121,7 +1130,7 @@ function isProbe(reference, sourceCode) {
|
|
|
1121
1130
|
if (user.type === "MemberExpression") return user.object === reference && user.computed && isOwnSymbol(user.property, sourceCode);
|
|
1122
1131
|
return user.type === "BinaryExpression" && user.operator === "in" && user.right === reference && isOwnSymbol(user.left, sourceCode);
|
|
1123
1132
|
}
|
|
1124
|
-
/** Return the
|
|
1133
|
+
/** Return the function a callee names when this file declares it: a function declaration, or a `const` bound to an arrow or function expression. */
|
|
1125
1134
|
function declaredFunctionOf(callee, sourceCode) {
|
|
1126
1135
|
if (callee.type !== "Identifier") return null;
|
|
1127
1136
|
const definition = variableOf(callee, sourceCode)?.defs[0];
|
|
@@ -1131,14 +1140,14 @@ function declaredFunctionOf(callee, sourceCode) {
|
|
|
1131
1140
|
return null;
|
|
1132
1141
|
}
|
|
1133
1142
|
function parameterAt(owner, position, sourceCode) {
|
|
1134
|
-
const parameter = owner
|
|
1143
|
+
const parameter = argumentParameters(owner)[position];
|
|
1135
1144
|
if (parameter?.type !== "Identifier") return null;
|
|
1136
1145
|
return sourceCode.getDeclaredVariables(owner).find((variable) => variable.name === parameter.name && variable.defs[0]?.type === "Parameter") ?? null;
|
|
1137
1146
|
}
|
|
1138
1147
|
/** Return whether a function declared in this file is a type guard on the parameter at `position`. */
|
|
1139
1148
|
function guardsParameterAt(owner, position) {
|
|
1140
1149
|
const predicate = owner.returnType?.typeAnnotation;
|
|
1141
|
-
const parameter = owner
|
|
1150
|
+
const parameter = argumentParameters(owner)[position];
|
|
1142
1151
|
return predicate?.type === "TSTypePredicate" && !predicate.asserts && predicate.parameterName.type === "Identifier" && parameter?.type === "Identifier" && predicate.parameterName.name === parameter.name;
|
|
1143
1152
|
}
|
|
1144
1153
|
/** Operators whose result is a boolean about the operands, which a probe may hand back. */
|
|
@@ -1316,7 +1325,7 @@ function isCaughtValueCallbackType(owner, sourceCode) {
|
|
|
1316
1325
|
if (callback.type !== "Identifier") return false;
|
|
1317
1326
|
const holder = callback.parent;
|
|
1318
1327
|
if (!isRuntimeFunction(holder) || holder.body === null || holder.body === void 0) return false;
|
|
1319
|
-
const unknownPositions = owner.
|
|
1328
|
+
const unknownPositions = argumentParameters(owner).flatMap((parameter, position) => {
|
|
1320
1329
|
const type = functionParameterTypeAnnotation(parameter)?.typeAnnotation;
|
|
1321
1330
|
return type !== void 0 && containsUnknownType(type) ? [position] : [];
|
|
1322
1331
|
});
|
|
@@ -1356,6 +1365,7 @@ const PROPERTY_WRITERS = /* @__PURE__ */ new Set([
|
|
|
1356
1365
|
"Reflect.setPrototypeOf"
|
|
1357
1366
|
]);
|
|
1358
1367
|
const PROMISE_TYPES = /* @__PURE__ */ new Set(["Promise", "PromiseLike"]);
|
|
1368
|
+
/** Return the name a call is made by: an identifier callee's name, or a non-computed member callee's property name. */
|
|
1359
1369
|
function calleeName(call) {
|
|
1360
1370
|
const { callee } = call;
|
|
1361
1371
|
if (callee.type === "Identifier") return callee.name;
|
|
@@ -1373,6 +1383,10 @@ function isSchemaLibrary(source) {
|
|
|
1373
1383
|
function dottedName(node) {
|
|
1374
1384
|
return node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.property.type === "Identifier" ? `${node.object.name}.${node.property.name}` : null;
|
|
1375
1385
|
}
|
|
1386
|
+
/** Return whether a call is one of `PROPERTY_WRITERS`, such as `Object.assign`, which write properties of the object they are handed first. */
|
|
1387
|
+
function writesProperties(call) {
|
|
1388
|
+
return PROPERTY_WRITERS.has(dottedName(call.callee) ?? "");
|
|
1389
|
+
}
|
|
1376
1390
|
/** Return the outermost member expression a reference is the object of, or the reference itself. */
|
|
1377
1391
|
function memberChainTop(identifier) {
|
|
1378
1392
|
let current = identifier;
|
|
@@ -1393,7 +1407,7 @@ function hasPropertyWrites(variable) {
|
|
|
1393
1407
|
if (holder.type === "UpdateExpression") return true;
|
|
1394
1408
|
if (holder.type === "UnaryExpression" && holder.operator === "delete") return true;
|
|
1395
1409
|
}
|
|
1396
|
-
return holder.type === "CallExpression" && holder.arguments[0] === top &&
|
|
1410
|
+
return holder.type === "CallExpression" && holder.arguments[0] === top && writesProperties(holder);
|
|
1397
1411
|
});
|
|
1398
1412
|
}
|
|
1399
1413
|
/** Return whether a variable is an import from a schema library, or a `const` whose initializer is rooted at one. */
|
|
@@ -1433,11 +1447,15 @@ function decoderCallOf(identifier, sourceCode) {
|
|
|
1433
1447
|
const call = identifier.parent;
|
|
1434
1448
|
return call.type === "CallExpression" && DECODER_NAME.test(calleeName(call) ?? "") && isRootedAtSchemaLibrary(call.callee, sourceCode, /* @__PURE__ */ new Set()) ? call : null;
|
|
1435
1449
|
}
|
|
1450
|
+
/** Return the type a declared return type hands back, read through `Promise<T>` and `PromiseLike<T>`. */
|
|
1451
|
+
function promisedType(annotation) {
|
|
1452
|
+
const type = annotation?.typeAnnotation;
|
|
1453
|
+
if (type === void 0) return void 0;
|
|
1454
|
+
return type.type === "TSTypeReference" && type.typeName.type === "Identifier" && PROMISE_TYPES.has(type.typeName.name) ? type.typeArguments?.params[0] : type;
|
|
1455
|
+
}
|
|
1436
1456
|
/** Return whether a declared return type states a value, not `unknown` or `any`, promised or not. */
|
|
1437
1457
|
function declaresParsedType(annotation) {
|
|
1438
|
-
const
|
|
1439
|
-
if (type === void 0) return false;
|
|
1440
|
-
const promised = type.type === "TSTypeReference" && type.typeName.type === "Identifier" && PROMISE_TYPES.has(type.typeName.name) ? type.typeArguments?.params[0] : type;
|
|
1458
|
+
const promised = promisedType(annotation);
|
|
1441
1459
|
return promised !== void 0 && promised.type !== "TSAnyKeyword" && !containsUnknownType(promised);
|
|
1442
1460
|
}
|
|
1443
1461
|
/** Strip what leaves a decoder's result the parsed value: `await` and the `.data` of a safe parse. */
|
|
@@ -1473,6 +1491,446 @@ function decodesParameterIntoResult(owner, name, annotated, facts, sourceCode) {
|
|
|
1473
1491
|
});
|
|
1474
1492
|
}
|
|
1475
1493
|
//#endregion
|
|
1494
|
+
//#region src/oxc/anti-slop/shared/guarded-reads.ts
|
|
1495
|
+
/** No type test on the parameter holds. */
|
|
1496
|
+
const UNTESTED = 0;
|
|
1497
|
+
/** A type test holds that leaves a type TypeScript lets into a narrower one unchecked: `any[]`, a class with `any` type arguments, `object`, `Function`. */
|
|
1498
|
+
const LOOSE = 1;
|
|
1499
|
+
/** A type test holds that leaves a primitive, or the type a predicate names. */
|
|
1500
|
+
const PRECISE = 2;
|
|
1501
|
+
/** Return what holds where two tests both hold: the narrower of the two. */
|
|
1502
|
+
function narrower(first, second) {
|
|
1503
|
+
return first > second ? first : second;
|
|
1504
|
+
}
|
|
1505
|
+
/** Return what holds where one of two tests holds: the looser of the two. */
|
|
1506
|
+
function looser(first, second) {
|
|
1507
|
+
return first < second ? first : second;
|
|
1508
|
+
}
|
|
1509
|
+
const NONE = {
|
|
1510
|
+
truthy: UNTESTED,
|
|
1511
|
+
falsy: UNTESTED
|
|
1512
|
+
};
|
|
1513
|
+
/** Names an imported function is trusted as a type guard by: `isX` or `IsX`, called with one argument. TypeScript rejects a narrowed read after one that is not a predicate. */
|
|
1514
|
+
const IMPORTED_GUARD_NAME = /^[Ii]s[A-Z0-9_$]/;
|
|
1515
|
+
/** What `typeof` answers for a primitive, which a test against it narrows to precisely. `"object"` and `"function"` leave `object` and `Function`. */
|
|
1516
|
+
const PRIMITIVE_TYPEOF = /* @__PURE__ */ new Set([
|
|
1517
|
+
"bigint",
|
|
1518
|
+
"boolean",
|
|
1519
|
+
"number",
|
|
1520
|
+
"string",
|
|
1521
|
+
"symbol",
|
|
1522
|
+
"undefined"
|
|
1523
|
+
]);
|
|
1524
|
+
const EQUALITY = /* @__PURE__ */ new Set([
|
|
1525
|
+
"===",
|
|
1526
|
+
"!==",
|
|
1527
|
+
"==",
|
|
1528
|
+
"!="
|
|
1529
|
+
]);
|
|
1530
|
+
const POSITIVE_EQUALITY = /* @__PURE__ */ new Set(["===", "=="]);
|
|
1531
|
+
/** Nodes a test outside does not narrow inside: a function declaration, hoisted above the test, and a class body with its methods, accessors and property initializers. */
|
|
1532
|
+
const NARROWING_BARRIERS = /* @__PURE__ */ new Set(["FunctionDeclaration", "ClassBody"]);
|
|
1533
|
+
/** Parents that change the type a read is seen at. A read under one of them is never accepted. */
|
|
1534
|
+
const CASTS = /* @__PURE__ */ new Set([
|
|
1535
|
+
"TSAsExpression",
|
|
1536
|
+
"TSTypeAssertion",
|
|
1537
|
+
"TSSatisfiesExpression",
|
|
1538
|
+
"TSNonNullExpression"
|
|
1539
|
+
]);
|
|
1540
|
+
/** Return whether a node is the non-computed member `object.property` of a global `object` no binding of the file shadows. */
|
|
1541
|
+
function isGlobalMember(node, object, property, sourceCode) {
|
|
1542
|
+
return node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.object.name === object && isGlobalName(node.object, sourceCode) && node.property.type === "Identifier" && node.property.name === property;
|
|
1543
|
+
}
|
|
1544
|
+
/**
|
|
1545
|
+
* Return how a call narrows its argument at `position`: loosely for `Array.isArray`, which leaves `any[]`; precisely for a
|
|
1546
|
+
* function declared in this file whose type predicate names the parameter at that position, and for an `isX` imported from
|
|
1547
|
+
* another module called with that argument alone.
|
|
1548
|
+
*/
|
|
1549
|
+
function guardPrecision(call, position, sourceCode) {
|
|
1550
|
+
const { callee } = call;
|
|
1551
|
+
if (isGlobalMember(callee, "Array", "isArray", sourceCode)) return position === 0 ? LOOSE : UNTESTED;
|
|
1552
|
+
if (callee.type !== "Identifier") return UNTESTED;
|
|
1553
|
+
const declared = declaredFunctionOf(callee, sourceCode);
|
|
1554
|
+
if (declared !== null) return guardsParameterAt(declared, position) ? PRECISE : UNTESTED;
|
|
1555
|
+
return variableOf(callee, sourceCode)?.defs[0]?.type === "ImportBinding" && position === 0 && call.arguments.length === 1 && IMPORTED_GUARD_NAME.test(callee.name) ? PRECISE : UNTESTED;
|
|
1556
|
+
}
|
|
1557
|
+
/** Return whether a call is a type guard on its argument at `position`, see `guardPrecision`. */
|
|
1558
|
+
function guardsArgument(call, position, sourceCode) {
|
|
1559
|
+
return guardPrecision(call, position, sourceCode) !== UNTESTED;
|
|
1560
|
+
}
|
|
1561
|
+
/** Return how a comparison of `typeof` of the parameter with a string literal narrows it: precisely to a primitive, loosely otherwise. */
|
|
1562
|
+
function typeofPrecision(test, reads) {
|
|
1563
|
+
const sides = [test.left, test.right];
|
|
1564
|
+
if (!sides.some((side) => side.type === "UnaryExpression" && side.operator === "typeof" && reads.references.has(side.argument))) return UNTESTED;
|
|
1565
|
+
const named = sides.flatMap((side) => side.type === "Literal" && typeof side.value === "string" ? [side.value] : []);
|
|
1566
|
+
if (named.length === 0) return UNTESTED;
|
|
1567
|
+
return named.every((each) => PRIMITIVE_TYPEOF.has(each)) ? PRECISE : LOOSE;
|
|
1568
|
+
}
|
|
1569
|
+
function binaryNarrowing(test, reads) {
|
|
1570
|
+
if (test.operator === "instanceof") return reads.references.has(test.left) ? {
|
|
1571
|
+
truthy: LOOSE,
|
|
1572
|
+
falsy: UNTESTED
|
|
1573
|
+
} : NONE;
|
|
1574
|
+
if (!EQUALITY.has(test.operator)) return NONE;
|
|
1575
|
+
const precision = typeofPrecision(test, reads);
|
|
1576
|
+
return POSITIVE_EQUALITY.has(test.operator) ? {
|
|
1577
|
+
truthy: precision,
|
|
1578
|
+
falsy: UNTESTED
|
|
1579
|
+
} : {
|
|
1580
|
+
truthy: UNTESTED,
|
|
1581
|
+
falsy: precision
|
|
1582
|
+
};
|
|
1583
|
+
}
|
|
1584
|
+
/**
|
|
1585
|
+
* Return how a test narrows the parameter. A guard call, `instanceof` and `typeof p === "lit"` narrow where they hold;
|
|
1586
|
+
* `!`, `typeof p !== "lit"` and the `&&` and `||` that join tests carry that through. A comparison with `null` narrows nowhere.
|
|
1587
|
+
*/
|
|
1588
|
+
function narrowingOf(test, reads) {
|
|
1589
|
+
switch (test.type) {
|
|
1590
|
+
case "UnaryExpression": {
|
|
1591
|
+
if (test.operator !== "!") return NONE;
|
|
1592
|
+
const inner = narrowingOf(test.argument, reads);
|
|
1593
|
+
return {
|
|
1594
|
+
truthy: inner.falsy,
|
|
1595
|
+
falsy: inner.truthy
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
case "LogicalExpression": {
|
|
1599
|
+
const left = narrowingOf(test.left, reads);
|
|
1600
|
+
const right = narrowingOf(test.right, reads);
|
|
1601
|
+
if (test.operator === "&&") return {
|
|
1602
|
+
truthy: narrower(left.truthy, right.truthy),
|
|
1603
|
+
falsy: looser(left.falsy, right.falsy)
|
|
1604
|
+
};
|
|
1605
|
+
if (test.operator === "||") return {
|
|
1606
|
+
truthy: looser(left.truthy, right.truthy),
|
|
1607
|
+
falsy: narrower(left.falsy, right.falsy)
|
|
1608
|
+
};
|
|
1609
|
+
return NONE;
|
|
1610
|
+
}
|
|
1611
|
+
case "CallExpression": return {
|
|
1612
|
+
truthy: test.arguments.reduce((found, argument, position) => reads.references.has(argument) ? narrower(found, guardPrecision(test, position, reads.sourceCode)) : found, UNTESTED),
|
|
1613
|
+
falsy: UNTESTED
|
|
1614
|
+
};
|
|
1615
|
+
case "BinaryExpression": return binaryNarrowing(test, reads);
|
|
1616
|
+
default: return NONE;
|
|
1617
|
+
}
|
|
1618
|
+
}
|
|
1619
|
+
/** Return whether a statement always leaves its function: a `return` or `throw`, or a block that ends in one. */
|
|
1620
|
+
function exits(statement) {
|
|
1621
|
+
if (statement?.type === "BlockStatement") return exits(statement.body.at(-1));
|
|
1622
|
+
return statement?.type === "ReturnStatement" || statement?.type === "ThrowStatement";
|
|
1623
|
+
}
|
|
1624
|
+
/** Return how the `if`s with no `else` before a statement of a block, which leave the function unless a test holds, narrow the parameter there. */
|
|
1625
|
+
function exitingGuardNarrowing(block, statement, reads) {
|
|
1626
|
+
const index = block.body.findIndex((each) => each === statement);
|
|
1627
|
+
return block.body.slice(0, index).reduce((found, earlier) => earlier.type === "IfStatement" && earlier.alternate === null && exits(earlier.consequent) ? narrower(found, narrowingOf(earlier.test, reads).falsy) : found, UNTESTED);
|
|
1628
|
+
}
|
|
1629
|
+
/** Return how `parent` narrows the parameter for its part `child`: a branch, the right of `&&` or `||`, or a statement after an exiting guard. */
|
|
1630
|
+
function partNarrowing(parent, child, reads) {
|
|
1631
|
+
switch (parent.type) {
|
|
1632
|
+
case "IfStatement":
|
|
1633
|
+
case "ConditionalExpression":
|
|
1634
|
+
if (child === parent.consequent) return narrowingOf(parent.test, reads).truthy;
|
|
1635
|
+
return child === parent.alternate ? narrowingOf(parent.test, reads).falsy : UNTESTED;
|
|
1636
|
+
case "LogicalExpression":
|
|
1637
|
+
if (child !== parent.right) return UNTESTED;
|
|
1638
|
+
if (parent.operator === "&&") return narrowingOf(parent.left, reads).truthy;
|
|
1639
|
+
return parent.operator === "||" ? narrowingOf(parent.left, reads).falsy : UNTESTED;
|
|
1640
|
+
case "BlockStatement": return exitingGuardNarrowing(parent, child, reads);
|
|
1641
|
+
default: return UNTESTED;
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
/** Return how the tests that hold at a read narrow the parameter, in the owner or an arrow, function expression or object method inside it. */
|
|
1645
|
+
function narrowingAt(read, reads) {
|
|
1646
|
+
let found = UNTESTED;
|
|
1647
|
+
for (let child = read; child !== reads.owner && child.parent !== null && !NARROWING_BARRIERS.has(child.type); child = child.parent) found = narrower(found, partNarrowing(child.parent, child, reads));
|
|
1648
|
+
return found;
|
|
1649
|
+
}
|
|
1650
|
+
/** Return the expression a read's value leaves by: the read, or the `?:` branch, `&&`, `||`, `??` or `await` it flows out through. */
|
|
1651
|
+
function outflowOf(read) {
|
|
1652
|
+
let value = read;
|
|
1653
|
+
while (value.parent !== null && (value.parent.type === "LogicalExpression" || value.parent.type === "AwaitExpression" || value.parent.type === "ConditionalExpression" && value.parent.test !== value)) value = value.parent;
|
|
1654
|
+
return value;
|
|
1655
|
+
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Return whether a read hands its value on where TypeScript lets a loose type in unchecked: returned, yielded, called,
|
|
1658
|
+
* constructed, assigned, or held in a `const` with no annotation.
|
|
1659
|
+
*/
|
|
1660
|
+
function handsOnLooseValue(read) {
|
|
1661
|
+
const value = outflowOf(read);
|
|
1662
|
+
const holder = value.parent;
|
|
1663
|
+
switch (holder?.type) {
|
|
1664
|
+
case "ReturnStatement":
|
|
1665
|
+
case "YieldExpression": return true;
|
|
1666
|
+
case "ArrowFunctionExpression": return holder.body === value;
|
|
1667
|
+
case "CallExpression":
|
|
1668
|
+
case "NewExpression": return holder.callee === value;
|
|
1669
|
+
case "TaggedTemplateExpression": return holder.tag === value;
|
|
1670
|
+
case "AssignmentExpression":
|
|
1671
|
+
case "AssignmentPattern": return holder.right === value;
|
|
1672
|
+
case "VariableDeclarator": return holder.init === value && holder.id.typeAnnotation?.typeAnnotation === void 0;
|
|
1673
|
+
default: return false;
|
|
1674
|
+
}
|
|
1675
|
+
}
|
|
1676
|
+
/**
|
|
1677
|
+
* A read that sits where a type test on the parameter holds, in the owner or a closure inside it. Under a precise test any use
|
|
1678
|
+
* is accepted there; under a loose one, any use that does not hand the value on as is.
|
|
1679
|
+
*/
|
|
1680
|
+
function isNarrowedRead(read, reads) {
|
|
1681
|
+
const precision = narrowingAt(read, reads);
|
|
1682
|
+
return precision === PRECISE || precision === LOOSE && !handsOnLooseValue(read);
|
|
1683
|
+
}
|
|
1684
|
+
/** A read a type test is made of: a guard's argument, the left of `instanceof`, or the operand of `typeof`. */
|
|
1685
|
+
function isTypeTestOperand(read, reads) {
|
|
1686
|
+
const user = read.parent;
|
|
1687
|
+
switch (user.type) {
|
|
1688
|
+
case "CallExpression": return guardsArgument(user, user.arguments.findIndex((argument) => argument === read), reads.sourceCode);
|
|
1689
|
+
case "BinaryExpression": return user.operator === "instanceof" && user.left === read;
|
|
1690
|
+
case "UnaryExpression": return user.operator === "typeof";
|
|
1691
|
+
default: return false;
|
|
1692
|
+
}
|
|
1693
|
+
}
|
|
1694
|
+
/** A read compared with `===`, `!==`, `==` or `!=`, which answers a boolean and narrows nothing this rule follows. */
|
|
1695
|
+
function isEqualityOperand(read) {
|
|
1696
|
+
return read.parent.type === "BinaryExpression" && EQUALITY.has(read.parent.operator);
|
|
1697
|
+
}
|
|
1698
|
+
/** A read passed back to the function itself at the parameter's own position, with no spread before it, so the same check judges it there. */
|
|
1699
|
+
function isRecursiveArgument(read, reads) {
|
|
1700
|
+
const user = read.parent;
|
|
1701
|
+
return user.type === "CallExpression" && user.arguments[reads.position] === read && user.arguments.slice(0, reads.position).every((argument) => argument.type !== "SpreadElement") && declaredFunctionOf(user.callee, reads.sourceCode) === reads.owner;
|
|
1702
|
+
}
|
|
1703
|
+
/** Return whether a callee is the global `String` or `JSON.stringify`, which take any value and return text. */
|
|
1704
|
+
function convertsToText(callee, sourceCode) {
|
|
1705
|
+
return callee.type === "Identifier" && callee.name === "String" && isGlobalName(callee, sourceCode) || isGlobalMember(callee, "JSON", "stringify", sourceCode);
|
|
1706
|
+
}
|
|
1707
|
+
/** A read handed as is, as the first argument, to the global `String(...)` or `JSON.stringify(...)`. */
|
|
1708
|
+
function isTotalStringConversion(read, reads) {
|
|
1709
|
+
const user = read.parent;
|
|
1710
|
+
return user.type === "CallExpression" && user.arguments[0] === read && convertsToText(user.callee, reads.sourceCode);
|
|
1711
|
+
}
|
|
1712
|
+
/** The positions a read of an unknown parameter may take without leaving it unparsed. */
|
|
1713
|
+
const ALLOWED_READS = [
|
|
1714
|
+
isTypeTestOperand,
|
|
1715
|
+
isEqualityOperand,
|
|
1716
|
+
isRecursiveArgument,
|
|
1717
|
+
isTotalStringConversion,
|
|
1718
|
+
isNarrowedRead,
|
|
1719
|
+
(read, reads) => reads.forwards(read)
|
|
1720
|
+
];
|
|
1721
|
+
/** Return the declarator a read's value initializes, through the branches of `?:`, the operands of `&&`, `||` and `??`, and `await`. */
|
|
1722
|
+
function declaratorHolding(read) {
|
|
1723
|
+
const value = outflowOf(read);
|
|
1724
|
+
const holder = value.parent;
|
|
1725
|
+
return holder?.type === "VariableDeclarator" && holder.init === value ? holder : null;
|
|
1726
|
+
}
|
|
1727
|
+
/** Return whether a type states only that a value is an array, claiming nothing of its items: `unknown[]` or `readonly unknown[]`. */
|
|
1728
|
+
function isUnknownArray(type) {
|
|
1729
|
+
if (type.type === "TSTypeOperator" && type.operator === "readonly") return isUnknownArray(type.typeAnnotation);
|
|
1730
|
+
return type.type === "TSArrayType" && type.elementType.type === "TSUnknownKeyword";
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Return whether a read is recast: wrapped in `as`, `<T>`, `satisfies` or `!`, directly or around the `?:`, `&&`, `||`,
|
|
1734
|
+
* `??` or `await` it flows out through, or held in a `let`, a `var`, or a `const` annotated with a type other than
|
|
1735
|
+
* `unknown[]`. TypeScript lets an `object` into a type whose members are all optional without checking one of them, so such
|
|
1736
|
+
* an annotation is a cast.
|
|
1737
|
+
*/
|
|
1738
|
+
function isRecast(read) {
|
|
1739
|
+
if (CASTS.has(read.parent.type) || CASTS.has(outflowOf(read).parent?.type ?? "")) return true;
|
|
1740
|
+
const declarator = declaratorHolding(read);
|
|
1741
|
+
if (declarator === null) return false;
|
|
1742
|
+
const declared = declarator.id.typeAnnotation?.typeAnnotation;
|
|
1743
|
+
const declaration = declarator.parent;
|
|
1744
|
+
return declaration.type !== "VariableDeclaration" || declaration.kind !== "const" || declared !== void 0 && !isUnknownArray(declared);
|
|
1745
|
+
}
|
|
1746
|
+
/** Union members that leave a verdict a verdict: `boolean | undefined` still answers yes, no or nothing. */
|
|
1747
|
+
const NO_ANSWER_TYPES = /* @__PURE__ */ new Set([
|
|
1748
|
+
"TSUndefinedKeyword",
|
|
1749
|
+
"TSNullKeyword",
|
|
1750
|
+
"TSVoidKeyword"
|
|
1751
|
+
]);
|
|
1752
|
+
/**
|
|
1753
|
+
* Return whether a declared return type answers yes or no about the input rather than hand back a value parsed from it:
|
|
1754
|
+
* `boolean`, `true` or `false`, a type predicate or `asserts` clause on any parameter, or a union of them, also with
|
|
1755
|
+
* `undefined`, `null` or `void`.
|
|
1756
|
+
*/
|
|
1757
|
+
function answersVerdict(type) {
|
|
1758
|
+
switch (type.type) {
|
|
1759
|
+
case "TSBooleanKeyword":
|
|
1760
|
+
case "TSTypePredicate": return true;
|
|
1761
|
+
case "TSLiteralType": return type.literal.type === "Literal" && typeof type.literal.value === "boolean";
|
|
1762
|
+
case "TSUnionType": return type.types.some(answersVerdict) && type.types.every((member) => answersVerdict(member) || NO_ANSWER_TYPES.has(member.type));
|
|
1763
|
+
case "TSParenthesizedType": return answersVerdict(type.typeAnnotation);
|
|
1764
|
+
default: return false;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
/** Return whether a function declares that it hands back a value parsed from its input: not `unknown`, `any`, `void` or a verdict, promised or not. */
|
|
1768
|
+
function handsBackParsedValue(owner) {
|
|
1769
|
+
const handed = promisedType(owner.returnType);
|
|
1770
|
+
return handed !== void 0 && handed.type !== "TSVoidKeyword" && !answersVerdict(handed) && declaresParsedType(owner.returnType);
|
|
1771
|
+
}
|
|
1772
|
+
/**
|
|
1773
|
+
* Return the reads of a parameter typed exactly `unknown` of a function with a body, when the function never reassigns it and
|
|
1774
|
+
* never mentions `arguments` or `eval`, and reads it at least once. Return null otherwise.
|
|
1775
|
+
*/
|
|
1776
|
+
function unknownParameterReads(owner, name, annotated, facts, sourceCode) {
|
|
1777
|
+
if (annotated.type !== "TSUnknownKeyword" || facts.readsImplicitly || owner.body === null || owner.body === void 0) return null;
|
|
1778
|
+
const parameter = sourceCode.getDeclaredVariables(owner).find((variable) => variable.name === name && variable.defs[0]?.type === "Parameter");
|
|
1779
|
+
if (parameter === void 0 || parameter.references.some((reference) => reference.isWrite() && !reference.init)) return null;
|
|
1780
|
+
const identifiers = parameter.references.filter((reference) => !reference.init).map((reference) => reference.identifier);
|
|
1781
|
+
return identifiers.length > 0 ? identifiers : null;
|
|
1782
|
+
}
|
|
1783
|
+
/**
|
|
1784
|
+
* Return whether a parameter typed exactly `unknown` is only classified: the function declares a return type other than
|
|
1785
|
+
* `unknown`, `any`, `void` or a verdict, never reassigns the parameter, never mentions `arguments` or `eval`, tests the
|
|
1786
|
+
* parameter's type or converts it to text at least once, and reads it each time as a type test's operand, a side of an
|
|
1787
|
+
* equality, an argument to itself at its own position, the first argument of `String` or `JSON.stringify`, or where a type
|
|
1788
|
+
* test on it holds, short of handing the value on as is where only a loose test holds. A recast of it anywhere, or a property
|
|
1789
|
+
* read, a call argument or a store where no type test holds, is not one.
|
|
1790
|
+
*/
|
|
1791
|
+
function readsOnlyThroughGuards(owner, name, annotated, facts, { sourceCode, forwards }) {
|
|
1792
|
+
const identifiers = handsBackParsedValue(owner) ? unknownParameterReads(owner, name, annotated, facts, sourceCode) : null;
|
|
1793
|
+
if (identifiers === null) return false;
|
|
1794
|
+
const reads = {
|
|
1795
|
+
owner,
|
|
1796
|
+
position: argumentParameters(owner).findIndex((parameter) => functionParameterBindingName(parameter, sourceCode) === name),
|
|
1797
|
+
references: new Set(identifiers),
|
|
1798
|
+
sourceCode,
|
|
1799
|
+
forwards
|
|
1800
|
+
};
|
|
1801
|
+
const tested = identifiers.some((read) => isTypeTestOperand(read, reads));
|
|
1802
|
+
const converted = identifiers.some((read) => isTotalStringConversion(read, reads));
|
|
1803
|
+
return (tested || converted && !identifiers.some(forwards)) && identifiers.every((read) => !isRecast(read) && ALLOWED_READS.some((allowed) => allowed(read, reads)));
|
|
1804
|
+
}
|
|
1805
|
+
//#endregion
|
|
1806
|
+
//#region src/oxc/anti-slop/shared/forward-only.ts
|
|
1807
|
+
/** Callee names that read as a validator: `assert` and `validate`, and `assertX` and `validateX`. */
|
|
1808
|
+
const VALIDATOR_NAME = /^(?:[Aa]ssert|[Vv]alidate)(?:[A-Z]\w*)?$/;
|
|
1809
|
+
/** Methods that keep a value in a collection. A call of one stores the value instead of handing it on. */
|
|
1810
|
+
const STORE_METHODS = /* @__PURE__ */ new Set([
|
|
1811
|
+
"add",
|
|
1812
|
+
"push",
|
|
1813
|
+
"set",
|
|
1814
|
+
"splice",
|
|
1815
|
+
"unshift"
|
|
1816
|
+
]);
|
|
1817
|
+
/** Return the object a member callee is read from, through further members, calls and type wrappers: `this` for `this.a?.b`, `p` for `p.a().b`. */
|
|
1818
|
+
function rootOf(node) {
|
|
1819
|
+
switch (node.type) {
|
|
1820
|
+
case "MemberExpression": return rootOf(node.object);
|
|
1821
|
+
case "CallExpression": return rootOf(node.callee);
|
|
1822
|
+
case "ChainExpression":
|
|
1823
|
+
case "TSAsExpression":
|
|
1824
|
+
case "TSNonNullExpression":
|
|
1825
|
+
case "TSSatisfiesExpression":
|
|
1826
|
+
case "TSTypeAssertion": return rootOf(node.expression);
|
|
1827
|
+
default: return node;
|
|
1828
|
+
}
|
|
1829
|
+
}
|
|
1830
|
+
/**
|
|
1831
|
+
* Return whether a value is rooted at a parameter, which would make a member of it a callee the caller hands in: read from a
|
|
1832
|
+
* parameter, or from a variable whose initializer, destructuring source or assigned value is rooted at one, transitively, through
|
|
1833
|
+
* either operand of `&&`, `||` and `??`, either branch of `?:`, and `await`.
|
|
1834
|
+
*/
|
|
1835
|
+
function isRootedAtParameter(node, sourceCode, seen) {
|
|
1836
|
+
const root = rootOf(node);
|
|
1837
|
+
const rooted = (each) => isRootedAtParameter(each, sourceCode, seen);
|
|
1838
|
+
if (root.type === "LogicalExpression") return rooted(root.left) || rooted(root.right);
|
|
1839
|
+
if (root.type === "ConditionalExpression") return rooted(root.consequent) || rooted(root.alternate);
|
|
1840
|
+
if (root.type === "AwaitExpression") return rooted(root.argument);
|
|
1841
|
+
const variable = root.type === "Identifier" ? variableOf(root, sourceCode) : null;
|
|
1842
|
+
if (variable === null || seen.has(variable)) return false;
|
|
1843
|
+
if (variable.defs[0]?.type === "Parameter") return true;
|
|
1844
|
+
const traced = /* @__PURE__ */ new Set([...seen, variable]);
|
|
1845
|
+
return writtenValues(variable).some((value) => isRootedAtParameter(value, sourceCode, traced));
|
|
1846
|
+
}
|
|
1847
|
+
/** Return the values a variable is initialized, destructured or assigned from. oxlint leaves `writeExpr` undefined on a read, so only writes are asked. */
|
|
1848
|
+
function writtenValues(variable) {
|
|
1849
|
+
return variable.references.flatMap((reference) => reference.isWrite() && reference.writeExpr ? [reference.writeExpr] : []);
|
|
1850
|
+
}
|
|
1851
|
+
/** Return whether a function declared in this file declares its parameter at `position` exactly `unknown`. */
|
|
1852
|
+
function takesUnknownAt(callee, position) {
|
|
1853
|
+
const parameter = argumentParameters(callee)[position];
|
|
1854
|
+
return parameter !== void 0 && parameter.type !== "RestElement" && functionParameterTypeAnnotation(parameter)?.typeAnnotation.type === "TSUnknownKeyword";
|
|
1855
|
+
}
|
|
1856
|
+
/**
|
|
1857
|
+
* Return whether a call may take a forward at `position`: a member call that is not a collection store, not a property writer
|
|
1858
|
+
* such as `Object.assign`, and not rooted at a parameter, which would be a callee the caller hands in; an imported function; or
|
|
1859
|
+
* a function this file declares whose parameter there is exactly `unknown` and exempt from this rule. A forward back to a
|
|
1860
|
+
* parameter still under judgment is not exempt.
|
|
1861
|
+
*/
|
|
1862
|
+
function reachesTrustedCallee(call, position, targets) {
|
|
1863
|
+
const { callee } = call;
|
|
1864
|
+
const { sourceCode } = targets;
|
|
1865
|
+
if (callee.type === "MemberExpression") {
|
|
1866
|
+
if (STORE_METHODS.has(calleeName(call) ?? "") || writesProperties(call)) return false;
|
|
1867
|
+
return !isRootedAtParameter(callee, sourceCode, /* @__PURE__ */ new Set());
|
|
1868
|
+
}
|
|
1869
|
+
if (callee.type !== "Identifier") return false;
|
|
1870
|
+
const declared = declaredFunctionOf(callee, sourceCode);
|
|
1871
|
+
if (declared !== null) return takesUnknownAt(declared, position) && targets.accepts(declared, position);
|
|
1872
|
+
const variable = variableOf(callee, sourceCode);
|
|
1873
|
+
if (variable?.defs[0]?.type === "ImportBinding") return true;
|
|
1874
|
+
return variable !== null && holdsBuiltFunction(variable, sourceCode) && !isRootedAtParameter(callee, sourceCode, /* @__PURE__ */ new Set());
|
|
1875
|
+
}
|
|
1876
|
+
/** Return the expression under `await` and the type wrappers `as`, `<T>`, `satisfies` and `!`. */
|
|
1877
|
+
function unwrapped(node) {
|
|
1878
|
+
switch (node.type) {
|
|
1879
|
+
case "AwaitExpression": return unwrapped(node.argument);
|
|
1880
|
+
case "TSAsExpression":
|
|
1881
|
+
case "TSNonNullExpression":
|
|
1882
|
+
case "TSSatisfiesExpression":
|
|
1883
|
+
case "TSTypeAssertion": return unwrapped(node.expression);
|
|
1884
|
+
default: return node;
|
|
1885
|
+
}
|
|
1886
|
+
}
|
|
1887
|
+
/** Return whether a value is `null` or the global `undefined`, which a lazily built callee starts as. */
|
|
1888
|
+
function isNothing(node, sourceCode) {
|
|
1889
|
+
return node.type === "Literal" && node.value === null || node.type === "Identifier" && node.name === "undefined" && isGlobalName(node, sourceCode);
|
|
1890
|
+
}
|
|
1891
|
+
/** Return whether a variable is bound by the head of a `for...of` or `for...in` loop, which writes it an element of what it iterates. */
|
|
1892
|
+
function isLoopBinding(variable) {
|
|
1893
|
+
return variable.defs.some(({ node }) => node.type === "VariableDeclarator" && (node.parent.parent?.type === "ForOfStatement" || node.parent.parent?.type === "ForInStatement"));
|
|
1894
|
+
}
|
|
1895
|
+
/**
|
|
1896
|
+
* Return whether a variable holds a function another owner built and this file does not write: every value it is initialized,
|
|
1897
|
+
* destructured or assigned from is a call's result, or `null` or `undefined` before one, and at least one is a call. A function
|
|
1898
|
+
* literal, an alias or any other value is not one.
|
|
1899
|
+
*/
|
|
1900
|
+
function holdsBuiltFunction(variable, sourceCode) {
|
|
1901
|
+
if (isLoopBinding(variable)) return false;
|
|
1902
|
+
const values = writtenValues(variable).map(unwrapped);
|
|
1903
|
+
return values.some((value) => value.type === "CallExpression") && values.every((value) => value.type === "CallExpression" || isNothing(value, sourceCode));
|
|
1904
|
+
}
|
|
1905
|
+
/**
|
|
1906
|
+
* Return whether a read is handed on unread: a direct argument, not the callee, of a call that reaches a trusted callee and is
|
|
1907
|
+
* not a type guard, the global `String` or `JSON.stringify`, or a decoder or validator by name, which the parse-boundary
|
|
1908
|
+
* exemption judges.
|
|
1909
|
+
*/
|
|
1910
|
+
function isForward(read, targets) {
|
|
1911
|
+
const call = read.parent;
|
|
1912
|
+
if (call.type !== "CallExpression") return false;
|
|
1913
|
+
const position = call.arguments.findIndex((argument) => argument === read);
|
|
1914
|
+
if (position < 0 || convertsToText(call.callee, targets.sourceCode) || guardsArgument(call, position, targets.sourceCode)) return false;
|
|
1915
|
+
const name = calleeName(call) ?? "";
|
|
1916
|
+
if (DECODER_NAME.test(name) || VALIDATOR_NAME.test(name)) return false;
|
|
1917
|
+
return reachesTrustedCallee(call, position, targets);
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Return whether a parameter typed exactly `unknown` is only forwarded: every read of it is a direct argument of a call that
|
|
1921
|
+
* takes `unknown` there, the function never reassigns it and never mentions `arguments` or `eval`, and it declares a return
|
|
1922
|
+
* type other than `unknown`, `any` or a verdict (see `answersVerdict`), promised or not. A callee this file declares must
|
|
1923
|
+
* declare that parameter exactly `unknown` and have it exempt. A member call or an imported callee is trusted unseen:
|
|
1924
|
+
* TypeScript rejects an `unknown` argument to any parameter narrower than `unknown` or `any`. One whose parameter is `any` is
|
|
1925
|
+
* not caught.
|
|
1926
|
+
*/
|
|
1927
|
+
function forwardsOnly(owner, name, annotated, facts, targets) {
|
|
1928
|
+
const handed = promisedType(owner.returnType);
|
|
1929
|
+
if (handed === void 0 || answersVerdict(handed) || !declaresParsedType(owner.returnType)) return false;
|
|
1930
|
+
const reads = unknownParameterReads(owner, name, annotated, facts, targets.sourceCode);
|
|
1931
|
+
return reads !== null && reads.every((read) => isForward(read, targets));
|
|
1932
|
+
}
|
|
1933
|
+
//#endregion
|
|
1476
1934
|
//#region src/oxc/anti-slop/rules/no-unknown-parameters.ts
|
|
1477
1935
|
function isTypePredicateSubject(owner, parameterName) {
|
|
1478
1936
|
const predicate = owner.returnType?.typeAnnotation;
|
|
@@ -1494,9 +1952,6 @@ function hasBoundaryDecorator(parameter) {
|
|
|
1494
1952
|
return name !== null && BOUNDARY_PARAMETER_DECORATORS.has(name);
|
|
1495
1953
|
});
|
|
1496
1954
|
}
|
|
1497
|
-
function isThisParameter(parameter) {
|
|
1498
|
-
return parameter.type === "Identifier" && parameter.name === "this";
|
|
1499
|
-
}
|
|
1500
1955
|
/** Return whether a function is passed directly as argument `position` of a `.method(...)` call. */
|
|
1501
1956
|
function isMethodArgument(owner, method, position) {
|
|
1502
1957
|
const call = owner.parent;
|
|
@@ -1509,13 +1964,13 @@ function isRejectionCallback(owner) {
|
|
|
1509
1964
|
/** Return whether a binding is the second parameter of a `new Promise(...)` executor. */
|
|
1510
1965
|
function isPromiseExecutorReject(binding) {
|
|
1511
1966
|
const executor = binding.parent;
|
|
1512
|
-
if (executor === null || !isRuntimeFunction(executor) || executor
|
|
1967
|
+
if (executor === null || !isRuntimeFunction(executor) || argumentParameters(executor)[1] !== binding) return false;
|
|
1513
1968
|
const construction = executor.parent;
|
|
1514
1969
|
return construction.type === "NewExpression" && construction.arguments[0] === executor && construction.callee.type === "Identifier" && construction.callee.name === "Promise";
|
|
1515
1970
|
}
|
|
1516
1971
|
/** Return whether a `(reason: unknown) => void` type is declared as a promise's `reject` or typed on a `new Promise` executor's reject parameter. */
|
|
1517
1972
|
function isRejectFunctionType(owner) {
|
|
1518
|
-
if (owner.type !== "TSFunctionType" || owner.
|
|
1973
|
+
if (owner.type !== "TSFunctionType" || argumentParameters(owner).length !== 1 || owner.returnType.typeAnnotation.type !== "TSVoidKeyword" || owner.parent.type !== "TSTypeAnnotation") return false;
|
|
1519
1974
|
const holder = owner.parent.parent;
|
|
1520
1975
|
if (holder.type === "TSPropertySignature" || holder.type === "PropertyDefinition") return !holder.computed && holder.key.type === "Identifier" && holder.key.name === "reject";
|
|
1521
1976
|
return holder.type === "Identifier" && (holder.name === "reject" || isPromiseExecutorReject(holder));
|
|
@@ -1523,7 +1978,7 @@ function isRejectFunctionType(owner) {
|
|
|
1523
1978
|
/** Return whether a parameter is exempt whatever its name: `this`, a boundary-decorated request value, or a promise rejection reason. */
|
|
1524
1979
|
function isExemptPosition(owner, parameter, sourceCode) {
|
|
1525
1980
|
if (isThisParameter(parameter) || hasBoundaryDecorator(parameter)) return true;
|
|
1526
|
-
if (owner
|
|
1981
|
+
if (argumentParameters(owner)[0] === parameter && isRejectionCallback(owner)) return true;
|
|
1527
1982
|
return isRejectFunctionType(owner) || owner.type === "TSFunctionType" && isCaughtValueCallbackType(owner, sourceCode);
|
|
1528
1983
|
}
|
|
1529
1984
|
/** Return whether a parameter typed exactly `unknown` is only probed for a symbol-keyed brand this file creates, see `isBrandProbe`. */
|
|
@@ -1540,28 +1995,67 @@ const NO_FACTS = {
|
|
|
1540
1995
|
const noUnknownParametersRule = defineRule({
|
|
1541
1996
|
meta: {
|
|
1542
1997
|
type: "problem",
|
|
1543
|
-
docs: { description: "Disallow explicitly unknown function parameters except `cause`, type-predicate subjects, `this`, request values injected by `@JSONBody`, `@JsonBody` or `@Parameter`, promise rejection reasons, the parameter of a callback type that the function only calls with a caught value, a symbol-keyed brand probe,
|
|
1998
|
+
docs: { description: "Disallow explicitly unknown function parameters except `cause`, type-predicate subjects, `this`, request values injected by `@JSONBody`, `@JsonBody` or `@Parameter`, promise rejection reasons, the parameter of a callback type that the function only calls with a caught value, a symbol-keyed brand probe, a parameter a function only decodes through a schema or decoder into the value it returns, a parameter a function only reads through type guards, where they hold and short of handing on as is a value only a loose guard narrowed, and through `String` or `JSON.stringify`, to classify it into a declared value other than a yes-or-no verdict or `void`, also beside forwards once one of them is a type test, and a parameter a function only forwards, unread, to member calls not rooted at another parameter, imports, callees a variable holds from another call's result, or exempt same-file parameters that take `unknown`; decode unknown input at its I/O boundary instead." },
|
|
1544
1999
|
messages: { unknownParameter: "Parameter `{{parameter}}` leaves input unparsed. Accept a named domain type and run the expected schema or parser at the I/O boundary before calling this function, or make this function that parser: pass the parameter only to the schema and return its result under a declared type." }
|
|
1545
2000
|
},
|
|
1546
2001
|
createOnce(context) {
|
|
1547
2002
|
const open = [];
|
|
1548
|
-
|
|
2003
|
+
/** The checks of one file, in the order they were met. They run at its end, so a forward is judged by a function declared after it. */
|
|
2004
|
+
const pending = [];
|
|
2005
|
+
const walked = /* @__PURE__ */ new Map();
|
|
2006
|
+
/** The parameters a forward is being judged into. A forward back to one of them is not exempt. */
|
|
2007
|
+
const judging = /* @__PURE__ */ new Set();
|
|
2008
|
+
/** The verdicts on parameters a forward landed on, so a chain of forwarders judges each parameter once. */
|
|
2009
|
+
const verdicts = /* @__PURE__ */ new Map();
|
|
2010
|
+
const isExempt = (node, parameter, annotated, facts) => {
|
|
2011
|
+
const { sourceCode } = context;
|
|
2012
|
+
if (isExemptPosition(node, parameter, sourceCode)) return true;
|
|
2013
|
+
const name = functionParameterBindingName(parameter, sourceCode);
|
|
2014
|
+
if (name === "cause" || isTypePredicateSubject(node, name)) return true;
|
|
2015
|
+
if (!isRuntimeFunction(node)) return false;
|
|
2016
|
+
const targets = {
|
|
2017
|
+
sourceCode,
|
|
2018
|
+
accepts: acceptsForward
|
|
2019
|
+
};
|
|
2020
|
+
return decodesParameterIntoResult(node, name, annotated, facts, sourceCode) || probesBrand(node, name, annotated, facts, sourceCode) || readsOnlyThroughGuards(node, name, annotated, facts, {
|
|
2021
|
+
sourceCode,
|
|
2022
|
+
forwards: (read) => isForward(read, targets)
|
|
2023
|
+
}) || forwardsOnly(node, name, annotated, facts, targets);
|
|
2024
|
+
};
|
|
2025
|
+
/** Return whether this rule exempts the parameter at `position` of a function the file declares, so a forward to it hands the value to a parser. */
|
|
2026
|
+
const acceptsForward = (callee, position) => {
|
|
2027
|
+
const parameter = argumentParameters(callee)[position];
|
|
2028
|
+
if (parameter === void 0 || judging.has(parameter)) return false;
|
|
2029
|
+
const known = verdicts.get(parameter);
|
|
2030
|
+
if (known !== void 0) return known;
|
|
2031
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
2032
|
+
const facts = walked.get(callee);
|
|
2033
|
+
if (annotation === null || annotation === void 0 || facts === void 0) return false;
|
|
2034
|
+
judging.add(parameter);
|
|
2035
|
+
const exempt = isExempt(callee, parameter, annotation.typeAnnotation, facts);
|
|
2036
|
+
judging.delete(parameter);
|
|
2037
|
+
verdicts.set(parameter, exempt);
|
|
2038
|
+
return exempt;
|
|
2039
|
+
};
|
|
2040
|
+
const checkParameters = (node, facts) => {
|
|
1549
2041
|
for (const parameter of node.params) {
|
|
1550
2042
|
const annotation = functionParameterTypeAnnotation(parameter);
|
|
1551
2043
|
if (annotation === null || annotation === void 0) continue;
|
|
1552
2044
|
if (!containsUnknownType(annotation.typeAnnotation)) continue;
|
|
1553
|
-
if (
|
|
1554
|
-
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
1555
|
-
if (name === "cause" || isTypePredicateSubject(node, name)) continue;
|
|
1556
|
-
if (isRuntimeFunction(node) && decodesParameterIntoResult(node, name, annotation.typeAnnotation, facts, context.sourceCode)) continue;
|
|
1557
|
-
if (isRuntimeFunction(node) && probesBrand(node, name, annotation.typeAnnotation, facts, context.sourceCode)) continue;
|
|
2045
|
+
if (isExempt(node, parameter, annotation.typeAnnotation, facts)) continue;
|
|
1558
2046
|
context.report({
|
|
1559
2047
|
node: annotation.typeAnnotation,
|
|
1560
2048
|
messageId: "unknownParameter",
|
|
1561
|
-
data: { parameter:
|
|
2049
|
+
data: { parameter: functionParameterBindingName(parameter, context.sourceCode) }
|
|
1562
2050
|
});
|
|
1563
2051
|
}
|
|
1564
2052
|
};
|
|
2053
|
+
const checkSignature = (node) => {
|
|
2054
|
+
pending.push({
|
|
2055
|
+
node,
|
|
2056
|
+
facts: NO_FACTS
|
|
2057
|
+
});
|
|
2058
|
+
};
|
|
1565
2059
|
const enterFunction = () => {
|
|
1566
2060
|
open.push({
|
|
1567
2061
|
returns: [],
|
|
@@ -1569,9 +2063,22 @@ const noUnknownParametersRule = defineRule({
|
|
|
1569
2063
|
});
|
|
1570
2064
|
};
|
|
1571
2065
|
const leaveFunction = (node) => {
|
|
1572
|
-
|
|
2066
|
+
const facts = open.pop() ?? NO_FACTS;
|
|
2067
|
+
walked.set(node, facts);
|
|
2068
|
+
pending.push({
|
|
2069
|
+
node,
|
|
2070
|
+
facts
|
|
2071
|
+
});
|
|
1573
2072
|
};
|
|
1574
2073
|
return {
|
|
2074
|
+
Program() {
|
|
2075
|
+
pending.length = 0;
|
|
2076
|
+
walked.clear();
|
|
2077
|
+
verdicts.clear();
|
|
2078
|
+
},
|
|
2079
|
+
"Program:exit"() {
|
|
2080
|
+
for (const { node, facts } of pending) checkParameters(node, facts);
|
|
2081
|
+
},
|
|
1575
2082
|
ArrowFunctionExpression: enterFunction,
|
|
1576
2083
|
FunctionDeclaration: enterFunction,
|
|
1577
2084
|
FunctionExpression: enterFunction,
|
|
@@ -1585,13 +2092,13 @@ const noUnknownParametersRule = defineRule({
|
|
|
1585
2092
|
if (!IMPLICIT_READERS.has(node.name)) return;
|
|
1586
2093
|
for (const frame of open) frame.readsImplicitly = true;
|
|
1587
2094
|
},
|
|
1588
|
-
TSCallSignatureDeclaration:
|
|
1589
|
-
TSConstructSignatureDeclaration:
|
|
1590
|
-
TSConstructorType:
|
|
1591
|
-
TSDeclareFunction:
|
|
1592
|
-
TSEmptyBodyFunctionExpression:
|
|
1593
|
-
TSFunctionType:
|
|
1594
|
-
TSMethodSignature:
|
|
2095
|
+
TSCallSignatureDeclaration: checkSignature,
|
|
2096
|
+
TSConstructSignatureDeclaration: checkSignature,
|
|
2097
|
+
TSConstructorType: checkSignature,
|
|
2098
|
+
TSDeclareFunction: checkSignature,
|
|
2099
|
+
TSEmptyBodyFunctionExpression: checkSignature,
|
|
2100
|
+
TSFunctionType: checkSignature,
|
|
2101
|
+
TSMethodSignature: checkSignature
|
|
1595
2102
|
};
|
|
1596
2103
|
}
|
|
1597
2104
|
});
|