@antelopejs/tooling-configs 0.4.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +68 -29
  2. package/dist/bin/api-diff.mjs +235 -37
  3. package/dist/bin/body-check.mjs +14 -11
  4. package/dist/bin/capture.mjs +173 -54
  5. package/dist/bin/check-size.mjs +1 -1
  6. package/dist/bin/checks.mjs +166 -11
  7. package/dist/bin/comment-proof.mjs +29 -7
  8. package/dist/bin/garden.mjs +8 -58
  9. package/dist/bin/lease.mjs +48 -11
  10. package/dist/bin/ratchet.mjs +48 -14
  11. package/dist/bin/suppressions.mjs +3 -3
  12. package/dist/bin/sync.mjs +199 -80
  13. package/dist/bin/verify.mjs +1163 -474
  14. package/dist/capture/index.d.mts +6 -1
  15. package/dist/capture/index.mjs +1 -1
  16. package/dist/{ci-workflow-37qJa9LX.mjs → ci-workflow-COJUT12b.mjs} +121 -1
  17. package/dist/eslint.d.mts +16 -3
  18. package/dist/eslint.mjs +21 -6
  19. package/dist/formal/index.d.mts +20 -4
  20. package/dist/formal/index.mjs +71 -19
  21. package/dist/{map-B23_WMGE.mjs → map-DRyYz_Fw.mjs} +20 -1
  22. package/dist/oxc/anti-slop/index.mjs +156 -421
  23. package/dist/oxc/guardrails/index.d.mts +1 -1
  24. package/dist/oxc/guardrails/index.mjs +789 -103
  25. package/dist/oxc/lint.d.mts +13 -15
  26. package/dist/oxc/lint.mjs +19 -29
  27. package/dist/{package-CqqBgE8Y.mjs → package-Dn7VTFUW.mjs} +71 -17
  28. package/dist/{proof-stamp-DDDC0J6l.mjs → proof-stamp-DLWpPwQE.mjs} +72 -10
  29. package/dist/{ratchet-Q-JXuZIX.mjs → ratchet-ClfblHNJ.mjs} +18 -15
  30. package/dist/require-safety-comment-for-type-assertion-mnb0CIxa.mjs +181 -0
  31. package/dist/{scenes-DZVXkcxS.mjs → scenes-d-VQ0LOm.mjs} +4 -1
  32. package/dist/semver-BDxeEcP_.mjs +57 -0
  33. package/dist/{suppression-base-9L8dlMQK.mjs → suppression-base-Drnl6Y7F.mjs} +2 -2
  34. package/dist/{suppression-budget-C0SkPO5R.mjs → suppression-budget-Dvb_IqBo.mjs} +1 -1
  35. package/dist/{suppressions-DM60NkvL.mjs → suppressions-BroaaGJ_.mjs} +1 -1
  36. package/dist/{templates-BKZv-BRw.mjs → templates-lJBZFskp.mjs} +8 -3
  37. package/dist/type-aware-rules-DMuN6sHb.mjs +30 -0
  38. package/dist/{typescript-DAgQIAAR.mjs → typescript-BBbry7uU.mjs} +123 -27
  39. package/dist/verify/index.d.mts +60 -15
  40. package/package.json +3 -1
  41. package/dist/require-safety-comment-for-type-assertion-BBQ669kj.mjs +0 -83
@@ -1,4 +1,4 @@
1
- import { r as requireSafetyCommentForTypeAssertionRule } from "../../require-safety-comment-for-type-assertion-BBQ669kj.mjs";
1
+ import { a as isRuntimeFunction, i as enclosingTypeGuard, r as requireSafetyCommentForTypeAssertionRule } from "../../require-safety-comment-for-type-assertion-mnb0CIxa.mjs";
2
2
  import { defineRule, eslintCompatPlugin } from "@oxlint/plugins";
3
3
  //#region src/oxc/anti-slop/rules/no-chained-type-assertions.ts
4
4
  function isTypeAssertionExpression(node) {
@@ -9,6 +9,31 @@ function unwrapParenthesizedExpression(expression) {
9
9
  while (current.type === "ParenthesizedExpression") current = current.expression;
10
10
  return current;
11
11
  }
12
+ function unwrapParenthesizedType(type) {
13
+ let current = type;
14
+ while (current.type === "TSParenthesizedType") current = current.typeAnnotation;
15
+ return current;
16
+ }
17
+ function instanceClassName(thisExpression) {
18
+ let scope = thisExpression.parent;
19
+ while (scope.type !== "FunctionExpression") {
20
+ if (scope.type === "Program" || scope.type === "FunctionDeclaration" || scope.type === "ClassBody") return null;
21
+ scope = scope.parent;
22
+ }
23
+ const declaresThis = scope.params.some((parameter) => parameter.type === "Identifier" && parameter.name === "this");
24
+ const method = scope.parent;
25
+ if (declaresThis || method.type !== "MethodDefinition" || method.static) return null;
26
+ const owner = method.parent.parent;
27
+ if (owner?.type !== "ClassDeclaration" && owner?.type !== "ClassExpression") return null;
28
+ return owner.id?.name ?? null;
29
+ }
30
+ function isPhantomRetypeOfThis(node) {
31
+ const target = unwrapParenthesizedType(node.typeAnnotation);
32
+ const inner = unwrapParenthesizedExpression(node.expression);
33
+ if (target.type !== "TSTypeReference" || target.typeName.type !== "Identifier" || (target.typeArguments?.params.length ?? 0) === 0 || !isTypeAssertionExpression(inner) || unwrapParenthesizedType(inner.typeAnnotation).type !== "TSUnknownKeyword") return false;
34
+ const source = unwrapParenthesizedExpression(inner.expression);
35
+ return source.type === "ThisExpression" && instanceClassName(source) === target.typeName.name;
36
+ }
12
37
  function isConstAssertion(node) {
13
38
  const { typeAnnotation } = node;
14
39
  return typeAnnotation.type === "TSTypeReference" && typeAnnotation.typeName.type === "Identifier" && typeAnnotation.typeName.name === "const";
@@ -31,13 +56,19 @@ function isForbiddenAssertionChain(node) {
31
56
  hasNonConstAssertion ||= !isConstAssertion(current);
32
57
  current = unwrapParenthesizedExpression(current.expression);
33
58
  }
34
- return assertionCount > 1 && hasNonConstAssertion;
59
+ return assertionCount > 1 && hasNonConstAssertion && !isPhantomRetypeOfThis(node);
35
60
  }
36
- /** Disallow nested TypeScript type assertions, while permitting chains made only of const assertions. */
61
+ /**
62
+ * Disallow nested TypeScript type assertions. Chains made only of const assertions pass, and so
63
+ * does `this as unknown as C<...>` in a non-static method of class `C`: the same instance under
64
+ * other type arguments, which TypeScript cannot express in one step when `C` is invariant in a
65
+ * phantom type parameter. require-safety-comment-for-type-assertion still asks it to state why the
66
+ * parameter holds no value.
67
+ */
37
68
  const noChainedTypeAssertionsRule = defineRule({
38
69
  meta: {
39
70
  type: "problem",
40
- docs: { description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains." },
71
+ docs: { description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains, except `this as unknown as C<...>` in a non-static method of class C." },
41
72
  messages: { chained: "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it." }
42
73
  },
43
74
  createOnce(context) {
@@ -908,125 +939,6 @@ const noReflectGetRule = defineRule({
908
939
  }
909
940
  });
910
941
  //#endregion
911
- //#region src/oxc/anti-slop/shared/binding-origin.ts
912
- /** Return the variable an identifier refers to, searching outward from its scope, or null for an unresolved global. */
913
- function variableOf(identifier, sourceCode) {
914
- let scope = sourceCode.getScope(identifier);
915
- while (scope !== null) {
916
- const variable = scope.set.get(identifier.name);
917
- if (variable !== void 0) return variable;
918
- scope = scope.upper;
919
- }
920
- return null;
921
- }
922
- /**
923
- * Return whether a definition takes its value from another module: an `import` declaration, or `import x = require()`.
924
- * An `import x = Namespace.member` only renames a name, so its value comes from where that name is declared.
925
- */
926
- function isModuleImport(definition) {
927
- if (definition.type !== "ImportBinding") return false;
928
- const { node } = definition;
929
- return node.type !== "TSImportEqualsDeclaration" || node.moduleReference.type === "TSExternalModuleReference";
930
- }
931
- /**
932
- * Return whether a name has no definition in this file: a name no scope binds, or a global the environment declares (`Array`,
933
- * `Error`), which oxlint binds to a variable with no definition.
934
- */
935
- function isGlobalName(identifier, sourceCode) {
936
- const variable = variableOf(identifier, sourceCode);
937
- return variable === null || variable.defs.length === 0;
938
- }
939
- /** Return whether a node sits inside another, or is it. */
940
- function isWithin(node, ancestor) {
941
- for (let at = node; at !== null; at = at.parent) if (at === ancestor) return true;
942
- return false;
943
- }
944
- /**
945
- * Return whether a name read inside a namespace block may be a member that another block of the same namespace declares.
946
- * TypeScript merges the blocks of one namespace, and the scope manager does not, so a name that has no definition in the file
947
- * (see `isGlobalName`), or whose definitions sit outside the block, may resolve to such a member and not to the binding the
948
- * scope manager found.
949
- */
950
- function mayBeMergedMember(identifier, variable) {
951
- for (let node = identifier.parent; node !== null; node = node.parent) {
952
- if (node.type !== "TSModuleBlock") continue;
953
- const block = node;
954
- return variable === null || variable.defs.length === 0 || !variable.defs.every((definition) => isWithin(definition.node, block));
955
- }
956
- return false;
957
- }
958
- /**
959
- * Return whether a name no scope binds is a global that nothing in this file writes, including a global that a `declare global`
960
- * introduces.
961
- */
962
- function isUnwrittenGlobal(identifier, sourceCode) {
963
- const scope = sourceCode.scopeManager.globalScope;
964
- return scope !== null && !scope.through.some((reference) => reference.identifier.name === identifier.name && reference.isWrite());
965
- }
966
- /**
967
- * Return whether a name takes its value from outside this file, which only a binding or reference the code can place proves:
968
- * a module import, or a parameter, that nothing here defaults, assigns or redeclares, or a global that nothing here writes,
969
- * and that no block of a namespace may shadow with a merged member. The kind of the first definition does not say it: a
970
- * default, an assignment and a later `var` give a parameter a value written here, and an `import x = Namespace.member` is no
971
- * module import. Any other name is not outside.
972
- */
973
- function isSuppliedFromOutside(identifier, sourceCode) {
974
- const variable = variableOf(identifier, sourceCode);
975
- if (mayBeMergedMember(identifier, variable)) return false;
976
- if (variable === null) return isUnwrittenGlobal(identifier, sourceCode);
977
- return variable.defs.every((definition) => definition.type === "Parameter" || isModuleImport(definition)) && variable.references.every((reference) => !reference.isWrite());
978
- }
979
- //#endregion
980
- //#region src/oxc/anti-slop/shared/type-guards.ts
981
- /** Files whose functions may declare a type predicate in JSDoc instead of a TypeScript return type. */
982
- const JSDOC_PREDICATE_FILE = /\.[cm]?js$/;
983
- /** A `@returns {x is T}` type, `x` captured: `asserts x is T` does not match. */
984
- const JSDOC_PREDICATE = /^\s*([A-Za-z_$][\w$]*)\s+is\s+\S/;
985
- function isRuntimeFunction(node) {
986
- return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression";
987
- }
988
- /** Return the statement or member a JSDoc block documenting this function is attached to. */
989
- function documentedNode(fn) {
990
- const parent = fn.parent;
991
- if ((parent.type === "Property" || parent.type === "MethodDefinition") && parent.value === fn) return parent;
992
- let node = fn;
993
- if (parent.type === "VariableDeclarator" && parent.init === fn && parent.parent.type === "VariableDeclaration" && parent.parent.declarations.length === 1) node = parent.parent;
994
- const container = node.parent;
995
- return container !== null && (container.type === "ExportNamedDeclaration" || container.type === "ExportDefaultDeclaration") ? container : node;
996
- }
997
- /** Return the type inside the braces of the first `@returns {...}` tag of a JSDoc block. */
998
- function jsDocReturnType(text) {
999
- const tag = /@returns\s*\{/.exec(text);
1000
- if (tag === null) return null;
1001
- const start = tag.index + tag[0].length;
1002
- let depth = 1;
1003
- for (let index = start; index < text.length; index++) if (text[index] === "{") depth++;
1004
- else if (text[index] === "}" && --depth === 0) return text.slice(start, index);
1005
- return null;
1006
- }
1007
- /** Return whether the JSDoc block right before the function declares `@returns {param is T}` for one of its parameters. */
1008
- function hasJsDocTypePredicate(fn, sourceCode) {
1009
- const comment = sourceCode.getCommentsBefore(documentedNode(fn)).at(-1);
1010
- if (comment === void 0 || comment.type !== "Block" || !comment.value.startsWith("*")) return false;
1011
- const returnType = jsDocReturnType(comment.value);
1012
- const subject = returnType === null ? void 0 : JSDOC_PREDICATE.exec(returnType)?.[1];
1013
- return subject !== void 0 && fn.params.some((parameter) => parameter.type === "Identifier" && parameter.name === subject);
1014
- }
1015
- /** Return whether a function declares a type predicate or assertion, or a JSDoc `@returns {x is T}` in a `.js`, `.mjs` or `.cjs` file. */
1016
- function isTypeGuard(fn, sourceCode, filename) {
1017
- if (fn.returnType?.typeAnnotation.type === "TSTypePredicate") return true;
1018
- return JSDOC_PREDICATE_FILE.test(filename) && hasJsDocTypePredicate(fn, sourceCode);
1019
- }
1020
- /** Return the nearest runtime function around a node when that function is a type guard. */
1021
- function enclosingTypeGuard(node, sourceCode, filename) {
1022
- let current = node.parent;
1023
- while (current !== null && current.type !== "Program") {
1024
- if (isRuntimeFunction(current)) return isTypeGuard(current, sourceCode, filename) ? current : null;
1025
- current = current.parent;
1026
- }
1027
- return null;
1028
- }
1029
- //#endregion
1030
942
  //#region src/oxc/anti-slop/rules/no-runtime-typeof.ts
1031
943
  /** Return whether typeof safely probes for the existence of a possibly absent binding. */
1032
944
  function isExistenceProbe(node) {
@@ -1041,57 +953,6 @@ function isExistenceProbe(node) {
1041
953
  const other = parent.left === node ? parent.right : parent.left;
1042
954
  return other.type === "Literal" && other.value === "undefined";
1043
955
  }
1044
- /** Return the name a callee is read from, through members and type wrappers, or null when it is read from `this`, a call or anything else. */
1045
- function rootOf(node) {
1046
- switch (node.type) {
1047
- case "Identifier": return node;
1048
- case "MemberExpression": return rootOf(node.object);
1049
- case "TSNonNullExpression":
1050
- case "TSAsExpression":
1051
- case "TSSatisfiesExpression":
1052
- case "TSTypeAssertion": return rootOf(node.expression);
1053
- default: return null;
1054
- }
1055
- }
1056
- /** Return whether a callee is read from a name that takes its value from outside this file, through members and type wrappers. */
1057
- function isReadFromOutside(callee, sourceCode) {
1058
- const root = rootOf(callee);
1059
- return root !== null && isSuppliedFromOutside(root, sourceCode);
1060
- }
1061
- /**
1062
- * Return whether a callee only raises or records the message it is given: a bare `assert(...)`, or the `log` method of any object,
1063
- * when its root is a module import, a global or a parameter. A root written in this file (a function, a class, a `const`, `this`, a
1064
- * parameter this file defaults or assigns) can compare or store the message, so it is no sink. A call to any other name can too
1065
- * (`assert.equal`, `Set#add`, `emit`).
1066
- */
1067
- function isMessageSink(callee, sourceCode) {
1068
- if (!isReadFromOutside(callee, sourceCode)) return false;
1069
- if (callee.type === "Identifier") return callee.name === "assert";
1070
- return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "log";
1071
- }
1072
- /**
1073
- * Return whether a message ends at this parent: it is thrown, handed to the `assert` or `log` call that stands as a statement,
1074
- * or handed to the `new` a `throw` throws when its class is read from outside this file, as `Error` is. Any other call, and a text a function returns, can be tested or used as a key by
1075
- * whatever receives it, so they are not message sinks.
1076
- */
1077
- function endsMessage(parent, sourceCode) {
1078
- switch (parent.type) {
1079
- case "ThrowStatement": return true;
1080
- case "CallExpression": return parent.parent.type === "ExpressionStatement" && isMessageSink(parent.callee, sourceCode);
1081
- case "NewExpression": return parent.parent.type === "ThrowStatement" && isReadFromOutside(parent.callee, sourceCode);
1082
- default: return false;
1083
- }
1084
- }
1085
- /**
1086
- * Return whether a `typeof` only fills in the text of a message that a `throw`, an `assert` or a `log` call, or the `new` under a `throw` takes.
1087
- * Such a template holds other text, so no comparison with a `typeof` result matches it, and TypeScript narrows nothing from it.
1088
- */
1089
- function isMessageText(node, sourceCode) {
1090
- let current = node.parent;
1091
- if (current.type !== "TemplateLiteral" || current.quasis.every((quasi) => quasi.value.raw === "")) return false;
1092
- while (current.parent.type === "TemplateLiteral" || current.parent.type === "ConditionalExpression" && current.parent.test !== current) current = current.parent;
1093
- return endsMessage(current.parent, sourceCode);
1094
- }
1095
956
  /** Built-ins whose `.constructor` comparison re-implements a `typeof` result. */
1096
957
  const TYPEOF_CONSTRUCTORS = /* @__PURE__ */ new Set([
1097
958
  "String",
@@ -1114,14 +975,22 @@ function constructorCheckTarget(node) {
1114
975
  if (side.type === "MemberExpression" && !side.computed && side.property.type === "Identifier" && side.property.name === "constructor" && other.type === "Identifier" && TYPEOF_CONSTRUCTORS.has(other.name)) return other.name;
1115
976
  }
1116
977
  }
978
+ /** Built-ins whose `instanceof` test re-implements a `typeof` result for every value but one from another realm. */
979
+ const TYPEOF_INSTANCEOF_TARGETS = /* @__PURE__ */ new Set(["Function", "Object"]);
980
+ /** Return the built-in name when the comparison is `x instanceof Function` or `x instanceof Object`. */
981
+ function instanceofCheckTarget(node) {
982
+ if (node.operator !== "instanceof" || node.right.type !== "Identifier") return void 0;
983
+ return TYPEOF_INSTANCEOF_TARGETS.has(node.right.name) ? node.right.name : void 0;
984
+ }
1117
985
  /** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
1118
986
  const noRuntimeTypeofRule = defineRule({
1119
987
  meta: {
1120
988
  type: "problem",
1121
- docs: { description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary. A `typeof` that only fills in the text of a message checks nothing." },
989
+ docs: { description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary." },
1122
990
  messages: {
1123
991
  runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
1124
- constructorCheck: "`.constructor === {{name}}` is a `typeof` check in another form. Parse input at its I/O boundary, or move the check into a type guard such as `(value: unknown): value is string`."
992
+ constructorCheck: "`.constructor === {{name}}` is a `typeof` check in another form. Parse input at its I/O boundary, or move the check into a type guard such as `(value: unknown): value is string`.",
993
+ instanceofCheck: "`instanceof {{name}}` is a `typeof` check in another form. Parse input at its I/O boundary, or move the check into a type guard such as `(value: unknown): value is Function`."
1125
994
  },
1126
995
  schema: [{
1127
996
  type: "object",
@@ -1137,7 +1006,7 @@ const noRuntimeTypeofRule = defineRule({
1137
1006
  }
1138
1007
  return {
1139
1008
  UnaryExpression(node) {
1140
- if (node.operator === "typeof" && !isExistenceProbe(node) && !isMessageText(node, context.sourceCode) && !allowedInTypeGuard(node)) context.report({
1009
+ if (node.operator === "typeof" && !isExistenceProbe(node) && !allowedInTypeGuard(node)) context.report({
1141
1010
  node,
1142
1011
  messageId: "runtimeTypeof"
1143
1012
  });
@@ -1149,6 +1018,12 @@ const noRuntimeTypeofRule = defineRule({
1149
1018
  messageId: "constructorCheck",
1150
1019
  data: { name }
1151
1020
  });
1021
+ const instanceofName = instanceofCheckTarget(node);
1022
+ if (instanceofName !== void 0 && !allowedInTypeGuard(node)) context.report({
1023
+ node,
1024
+ messageId: "instanceofCheck",
1025
+ data: { name: instanceofName }
1026
+ });
1152
1027
  }
1153
1028
  };
1154
1029
  }
@@ -1189,6 +1064,26 @@ const noForbiddenTermInSymbolNamesRule = defineRule({
1189
1064
  }
1190
1065
  });
1191
1066
  //#endregion
1067
+ //#region src/oxc/anti-slop/shared/binding-origin.ts
1068
+ /** Return the variable an identifier refers to, searching outward from its scope, or null for an unresolved global. */
1069
+ function variableOf(identifier, sourceCode) {
1070
+ let scope = sourceCode.getScope(identifier);
1071
+ while (scope !== null) {
1072
+ const variable = scope.set.get(identifier.name);
1073
+ if (variable !== void 0) return variable;
1074
+ scope = scope.upper;
1075
+ }
1076
+ return null;
1077
+ }
1078
+ /**
1079
+ * Return whether a name has no definition in this file: a name no scope binds, or a global the environment declares (`Array`,
1080
+ * `Error`), which oxlint binds to a variable with no definition.
1081
+ */
1082
+ function isGlobalName(identifier, sourceCode) {
1083
+ const variable = variableOf(identifier, sourceCode);
1084
+ return variable === null || variable.defs.length === 0;
1085
+ }
1086
+ //#endregion
1192
1087
  //#region src/oxc/anti-slop/shared/brand-probe.ts
1193
1088
  /** Primitive types a probe may hand back: a derived value, never an object shape the function claims to know. */
1194
1089
  const PRIMITIVE_TYPES = /* @__PURE__ */ new Set([
@@ -1434,269 +1329,109 @@ function isCaughtValueCallbackType(owner, sourceCode) {
1434
1329
  });
1435
1330
  }
1436
1331
  //#endregion
1437
- //#region src/oxc/anti-slop/shared/callee-origin.ts
1438
- const NO_VARIABLES = /* @__PURE__ */ new Set();
1439
- /** Return the name an `import x = Outer.Inner.member` reads first, the one the file must declare. */
1440
- function entityRoot(name) {
1441
- return name.type === "TSQualifiedName" ? entityRoot(name.left) : name;
1442
- }
1443
- /** Return the name an `import x = Namespace.member` reads, or null for a module import, which another file gives a value. */
1444
- function aliasRoot(definition) {
1445
- const { node } = definition;
1446
- if (node.type !== "TSImportEqualsDeclaration") return null;
1447
- return node.moduleReference.type === "TSExternalModuleReference" ? null : entityRoot(node.moduleReference);
1448
- }
1449
- /** Return whether a definition is a `const` with an initializer, a parameter or an import, so that nothing but a write reference changes its value. */
1450
- function isFixedDefinition(definition) {
1451
- const { node } = definition;
1452
- return definition.type !== "Variable" || node.type === "VariableDeclarator" && node.init !== null && node.parent.type === "VariableDeclaration" && node.parent.kind === "const";
1453
- }
1454
- /**
1455
- * Classify a name by every definition and write it has, not by its first definition. It is outside this file only when
1456
- * `isSuppliedFromOutside` places it there. A name no scope binds, or that a merged namespace member may shadow, is unplaced.
1457
- * A function, class, namespace or catch binding is a declaration written here. Any other name is a variable read through the
1458
- * expressions written to it.
1459
- */
1460
- function bindingOf(identifier, sourceCode) {
1461
- if (isSuppliedFromOutside(identifier, sourceCode)) return { kind: "outside" };
1462
- const variable = variableOf(identifier, sourceCode);
1463
- if (variable === null || mayBeMergedMember(identifier, variable)) return { kind: "unplaced" };
1464
- const declared = variable.defs.find((definition) => ![
1465
- "Variable",
1466
- "Parameter",
1467
- "ImportBinding"
1468
- ].includes(definition.type));
1469
- if (declared !== void 0) return {
1470
- kind: "declaration",
1471
- type: declared.type
1472
- };
1473
- const writes = variable.references.filter((reference) => reference.isWrite());
1474
- return {
1475
- kind: "variable",
1476
- variable,
1477
- sources: [...variable.defs.map(aliasRoot), ...writes.map((write) => write.writeExpr)].filter((source) => source !== null),
1478
- constant: variable.defs.every(isFixedDefinition) && writes.every((write) => write.writeExpr !== null)
1479
- };
1480
- }
1481
- /** Return the expression a type wrapper, an `await` or an optional chain only passes through, or null when the node is none of these. */
1482
- function passThrough(node) {
1483
- switch (node.type) {
1484
- case "TSAsExpression":
1485
- case "TSSatisfiesExpression":
1486
- case "TSNonNullExpression":
1487
- case "TSTypeAssertion":
1488
- case "ChainExpression": return node.expression;
1489
- case "AwaitExpression": return node.argument;
1490
- default: return null;
1491
- }
1492
- }
1493
- /**
1494
- * Return whether a value reaches back to code written in this file: walk through members, calls, `new`, type
1495
- * wrappers and `const` aliases to the name or literal at the root. An import, a parameter and a global are
1496
- * outside the file. A function, class, namespace, `let`, `var`, `this`, `super`, object literal or anything this
1497
- * walk cannot place is inside it. A call also counts when it is handed an object that holds a function written
1498
- * here, which it can hand back as a method. A call rooted at a global (`Object.freeze`, `Array.from`, `Promise.resolve`) is
1499
- * no factory that wraps a schema, so a function handed to it counts too, as a callback it can call.
1500
- */
1501
- function isWrittenHere(node, sourceCode, seen) {
1502
- const inner = passThrough(node);
1503
- if (inner !== null) return isWrittenHere(inner, sourceCode, seen);
1504
- switch (node.type) {
1505
- case "Identifier": {
1506
- const binding = bindingOf(node, sourceCode);
1507
- if (binding.kind === "outside") return false;
1508
- if (binding.kind !== "variable") return true;
1509
- return !binding.constant || seen.has(binding.variable) || followSources(binding, sourceCode, seen, isWrittenHere);
1510
- }
1511
- case "ImportExpression": return false;
1512
- case "MemberExpression": return node.object.type === "ThisExpression" ? thisFieldIsWrittenHere(node, sourceCode, seen) : isWrittenHere(node.object, sourceCode, seen);
1513
- case "CallExpression":
1514
- case "NewExpression": {
1515
- const handsOver = isRootedAtGlobal(node.callee, sourceCode) ? isFunctionHere : holdsFunctionHere;
1516
- return isWrittenHere(node.callee, sourceCode, seen) || node.arguments.some((argument) => handsOver(argument, sourceCode, seen));
1517
- }
1518
- case "ConditionalExpression": return isWrittenHere(node.consequent, sourceCode, seen) || isWrittenHere(node.alternate, sourceCode, seen);
1519
- case "LogicalExpression": return isWrittenHere(node.left, sourceCode, seen) || isWrittenHere(node.right, sourceCode, seen);
1520
- default: return true;
1521
- }
1332
+ //#region src/oxc/anti-slop/shared/parse-boundary.ts
1333
+ /** Callee names that read as a decoder: `parse`, `safeParse` and their `Async` forms, `decode`, and `parseX` and `decodeX` in either case. */
1334
+ const DECODER_NAME = /^(?:safeParse(?:Async)?|[Pp]arse(?:[A-Z]\w*)?|[Dd]ecode(?:[A-Z]\w*)?)$/;
1335
+ /** Packages whose exports are schemas or decoders. A subpath such as `zod/v4` counts as its package. */
1336
+ const SCHEMA_LIBRARIES = [
1337
+ "zod",
1338
+ "valibot",
1339
+ "yup",
1340
+ "joi",
1341
+ "superstruct",
1342
+ "io-ts",
1343
+ "@sinclair/typebox",
1344
+ "arktype",
1345
+ "effect",
1346
+ "@effect/schema"
1347
+ ];
1348
+ /** Calls that write properties of the object they are handed first. */
1349
+ const PROPERTY_WRITERS = /* @__PURE__ */ new Set([
1350
+ "Object.assign",
1351
+ "Object.defineProperty",
1352
+ "Object.defineProperties",
1353
+ "Object.setPrototypeOf",
1354
+ "Reflect.set",
1355
+ "Reflect.defineProperty",
1356
+ "Reflect.setPrototypeOf"
1357
+ ]);
1358
+ const PROMISE_TYPES = /* @__PURE__ */ new Set(["Promise", "PromiseLike"]);
1359
+ function calleeName(call) {
1360
+ const { callee } = call;
1361
+ if (callee.type === "Identifier") return callee.name;
1362
+ return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" ? callee.property.name : null;
1522
1363
  }
1523
- /** Return whether a callee is read from a global that has no definition in the file, through members, calls and type wrappers. */
1524
- function isRootedAtGlobal(node, sourceCode) {
1525
- const inner = passThrough(node);
1526
- if (inner !== null) return isRootedAtGlobal(inner, sourceCode);
1527
- switch (node.type) {
1528
- case "Identifier": return isGlobalName(node, sourceCode);
1529
- case "MemberExpression": return isRootedAtGlobal(node.object, sourceCode);
1530
- case "CallExpression":
1531
- case "NewExpression": return isRootedAtGlobal(node.callee, sourceCode);
1532
- default: return false;
1533
- }
1364
+ /** Return the initializer of the single-name `const` a variable declares. */
1365
+ function constInitializerOf(variable) {
1366
+ const declarator = variable?.defs[0]?.node;
1367
+ return declarator?.type === "VariableDeclarator" && declarator.id.type === "Identifier" && declarator.parent.type === "VariableDeclaration" && declarator.parent.kind === "const" ? declarator.init : null;
1534
1368
  }
1535
- /** Return whether a node is a primitive value, which holds no function: a literal, a template, or the result of a unary, binary or update operator. */
1536
- function isPrimitive(node) {
1537
- switch (node.type) {
1538
- case "Literal":
1539
- case "TemplateLiteral":
1540
- case "UnaryExpression":
1541
- case "BinaryExpression":
1542
- case "UpdateExpression": return true;
1543
- default: return false;
1544
- }
1369
+ function isSchemaLibrary(source) {
1370
+ return SCHEMA_LIBRARIES.some((library) => source === library || source.startsWith(`${library}/`));
1545
1371
  }
1546
- /** Return the name a class element or member is read by, or null for a computed one. A private name carries its `#`, so it is no element of the public name. */
1547
- function elementName(key, computed) {
1548
- if (computed) return null;
1549
- if (key.type === "PrivateIdentifier") return `#${key.name}`;
1550
- return key.type === "Identifier" ? key.name : null;
1372
+ /** Return the `a.b` name of a non-computed member callee, or null. */
1373
+ function dottedName(node) {
1374
+ return node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.property.type === "Identifier" ? `${node.object.name}.${node.property.name}` : null;
1551
1375
  }
1552
- /** Return the class a node is written in, or null at the top level. */
1553
- function enclosingClass(node) {
1554
- let current = node.parent;
1555
- while (current !== null && current.type !== "ClassDeclaration" && current.type !== "ClassExpression") current = current.parent;
1376
+ /** Return the outermost member expression a reference is the object of, or the reference itself. */
1377
+ function memberChainTop(identifier) {
1378
+ let current = identifier;
1379
+ while (current.parent?.type === "MemberExpression" && current.parent.object === current) current = current.parent;
1556
1380
  return current;
1557
1381
  }
1558
1382
  /**
1559
- * Return what `this` is where a node is written: the instance or the class itself when the nearest non-arrow function is a
1560
- * class method, or when no function sits between the node and a field initializer or static block. Any other function (an
1561
- * object-literal method, a function expression, a declaration) gets its `this` from its caller, as does a node in a class
1562
- * heritage clause, a decorator or a computed key, which the walk meets as the class itself. Those give null.
1563
- */
1564
- function receiverOf(node) {
1565
- let child = node;
1566
- for (let current = node.parent; current !== null; current = current.parent) switch (current.type) {
1567
- case "FunctionExpression": return current.parent.type === "MethodDefinition" && current.parent.value === current ? current.parent.static ? "static" : "instance" : null;
1568
- case "PropertyDefinition":
1569
- case "AccessorProperty": return current.value === child ? current.static ? "static" : "instance" : null;
1570
- case "StaticBlock": return "static";
1571
- case "FunctionDeclaration":
1572
- case "ClassDeclaration":
1573
- case "ClassExpression": return null;
1574
- default: child = current;
1575
- }
1576
- return null;
1577
- }
1578
- /**
1579
- * Return whether `this.field` reaches back to code written here. The element is the one of that exact name, `#` included, and of
1580
- * the receiver's kind, static or instance. A field with an initializer is read through it. A constructor parameter property is
1581
- * an instance element handed in by the caller, as a parameter is. A name that no element or two elements declare, an
1582
- * accessor, a method, a field without an initializer, an inherited field and a computed name are not placed, so they count as
1583
- * written here.
1584
- */
1585
- function thisFieldIsWrittenHere(member, sourceCode, seen) {
1586
- const klass = enclosingClass(member);
1587
- const name = elementName(member.property, member.computed);
1588
- const receiver = receiverOf(member);
1589
- if (klass === null || name === null || receiver === null) return true;
1590
- const elements = klass.body.body.filter((element) => (element.type === "PropertyDefinition" || element.type === "AccessorProperty" || element.type === "MethodDefinition") && element.static === (receiver === "static") && elementName(element.key, element.computed) === name);
1591
- const handedIn = receiver === "instance" && klass.body.body.some((element) => element.type === "MethodDefinition" && declaresParameterProperty(element, name));
1592
- if (elements.length + (handedIn ? 1 : 0) !== 1) return true;
1593
- const [element] = elements;
1594
- if (element === void 0) return false;
1595
- if (element.type !== "PropertyDefinition") return true;
1596
- return element.value === null || seen.has(element) || isWrittenHere(element.value, sourceCode, /* @__PURE__ */ new Set([...seen, element]));
1597
- }
1598
- /** Return whether a method declares a parameter property of this name, which only a constructor can. */
1599
- function declaresParameterProperty(method, name) {
1600
- return method.value.params.some((parameter) => parameter.type === "TSParameterProperty" && parameter.parameter.type === "Identifier" && parameter.parameter.name === name);
1601
- }
1602
- /**
1603
- * Return whether a value is local code: a function or class written here, an object or array of such members, or a member or call
1604
- * that reaches back to this file. Only a primitive, an import, a parameter and an object of such values are not, so a node this
1605
- * walk does not list, a function, a class and any syntax added later included, is reported.
1383
+ * Return whether this file writes a property of a variable's value: an assignment, update or `delete` on a member read from it,
1384
+ * or a call such as `Object.assign` handed it first. A write through another name is not seen.
1606
1385
  */
1607
- function isFunctionHere(node, sourceCode, seen) {
1608
- const inner = passThrough(node);
1609
- if (inner !== null) return isFunctionHere(inner, sourceCode, seen);
1610
- switch (node.type) {
1611
- case "ObjectExpression":
1612
- case "ArrayExpression": return holdsFunctionHere(node, sourceCode, seen);
1613
- case "ConditionalExpression": return isFunctionHere(node.consequent, sourceCode, seen) || isFunctionHere(node.alternate, sourceCode, seen);
1614
- case "LogicalExpression": return isFunctionHere(node.left, sourceCode, seen) || isFunctionHere(node.right, sourceCode, seen);
1615
- case "Identifier": {
1616
- const binding = bindingOf(node, sourceCode);
1617
- if (binding.kind === "unplaced") return true;
1618
- if (binding.kind === "declaration") return true;
1619
- return binding.kind === "variable" && followSources(binding, sourceCode, seen, isFunctionHere);
1386
+ function hasPropertyWrites(variable) {
1387
+ return variable.references.some(({ identifier }) => {
1388
+ const top = memberChainTop(identifier);
1389
+ const holder = top.parent;
1390
+ if (holder === null) return false;
1391
+ if (top !== identifier) {
1392
+ if (holder.type === "AssignmentExpression" && holder.left === top) return true;
1393
+ if (holder.type === "UpdateExpression") return true;
1394
+ if (holder.type === "UnaryExpression" && holder.operator === "delete") return true;
1620
1395
  }
1621
- case "MemberExpression":
1622
- case "CallExpression":
1623
- case "NewExpression": return isWrittenHere(node, sourceCode, seen);
1624
- default: return !isPrimitive(node);
1396
+ return holder.type === "CallExpression" && holder.arguments[0] === top && PROPERTY_WRITERS.has(dottedName(holder.callee) ?? "");
1397
+ });
1398
+ }
1399
+ /** Return whether a variable is an import from a schema library, or a `const` whose initializer is rooted at one. */
1400
+ function isSchemaVariable(variable, sourceCode, seen) {
1401
+ const [definition, ...others] = variable.defs;
1402
+ if (definition === void 0 || others.length > 0 || seen.has(variable) || hasPropertyWrites(variable)) return false;
1403
+ if (variable.references.some((reference) => reference.isWrite() && !reference.init)) return false;
1404
+ if (definition.type === "ImportBinding") {
1405
+ const declaration = definition.parent;
1406
+ return declaration?.type === "ImportDeclaration" && isSchemaLibrary(declaration.source.value);
1625
1407
  }
1408
+ const initializer = constInitializerOf(variable);
1409
+ return initializer !== null && isRootedAtSchemaLibrary(initializer, sourceCode, /* @__PURE__ */ new Set([...seen, variable]));
1626
1410
  }
1627
- /**
1628
- * Return whether an argument is an object, an array or a class whose members include a function written here, or a `this`.
1629
- * A function handed over on its own is not: a schema may take a local refinement. A node this walk does not list is reported
1630
- * unless it is a primitive.
1631
- */
1632
- function holdsFunctionHere(node, sourceCode, seen) {
1633
- const inner = passThrough(node);
1634
- if (inner !== null) return holdsFunctionHere(inner, sourceCode, seen);
1411
+ /** Return whether a value is read, through members, calls, type wrappers and `const` aliases, from a schema library's import. */
1412
+ function isRootedAtSchemaLibrary(node, sourceCode, seen) {
1635
1413
  switch (node.type) {
1636
- case "FunctionExpression":
1637
- case "ArrowFunctionExpression": return false;
1638
- case "SpreadElement": return holdsFunctionHere(node.argument, sourceCode, seen);
1639
- case "ObjectExpression": return node.properties.some((property) => property.type === "SpreadElement" ? isFunctionHere(property.argument, sourceCode, seen) : isFunctionHere(property.value, sourceCode, seen));
1640
- case "ArrayExpression": return node.elements.some((element) => element !== null && (element.type === "SpreadElement" ? holdsFunctionHere(element.argument, sourceCode, seen) : isFunctionHere(element, sourceCode, seen)));
1414
+ case "TSAsExpression":
1415
+ case "TSSatisfiesExpression":
1416
+ case "TSNonNullExpression":
1417
+ case "TSTypeAssertion":
1418
+ case "ChainExpression": return isRootedAtSchemaLibrary(node.expression, sourceCode, seen);
1419
+ case "MemberExpression": return isRootedAtSchemaLibrary(node.object, sourceCode, seen);
1420
+ case "CallExpression": return isRootedAtSchemaLibrary(node.callee, sourceCode, seen);
1641
1421
  case "Identifier": {
1642
- const binding = bindingOf(node, sourceCode);
1643
- if (binding.kind === "unplaced") return true;
1644
- if (binding.kind === "declaration") return binding.type !== "FunctionName";
1645
- return binding.kind === "variable" && followSources(binding, sourceCode, seen, holdsFunctionHere);
1422
+ const variable = variableOf(node, sourceCode);
1423
+ return variable !== null && isSchemaVariable(variable, sourceCode, seen);
1646
1424
  }
1647
- case "MemberExpression":
1648
- case "CallExpression":
1649
- case "NewExpression":
1650
- case "ConditionalExpression":
1651
- case "LogicalExpression": return isWrittenHere(node, sourceCode, seen);
1652
- default: return !isPrimitive(node);
1653
- }
1654
- }
1655
- /** Apply a test to each expression written to a variable, once per variable on the path, and answer false for a variable written nothing. */
1656
- function followSources(binding, sourceCode, seen, test) {
1657
- const { sources, variable } = binding;
1658
- return !seen.has(variable) && sources.some((source) => test(source, sourceCode, /* @__PURE__ */ new Set([...seen, variable])));
1659
- }
1660
- /**
1661
- * Return whether a call goes to code written in this file. A bare callee counts when it is not outside the file, as
1662
- * `isSuppliedFromOutside` decides, or when it is a parameter. A member callee counts when the value it is read from reaches back
1663
- * to this file, so a member of a parameter, which the caller supplies, does not.
1664
- */
1665
- function calleeIsWrittenHere(call, sourceCode) {
1666
- const { callee } = call;
1667
- if (callee.type === "Identifier") {
1668
- const variable = variableOf(callee, sourceCode);
1669
- return !isSuppliedFromOutside(callee, sourceCode) || variable !== null && variable.defs.some((definition) => definition.type === "Parameter");
1425
+ default: return false;
1670
1426
  }
1671
- return callee.type === "MemberExpression" && isWrittenHere(callee.object, sourceCode, NO_VARIABLES);
1672
- }
1673
- //#endregion
1674
- //#region src/oxc/anti-slop/shared/parse-boundary.ts
1675
- /**
1676
- * Callee names that read as a schema or decoder: `parse`, `safeParse` and their `Async` forms, `decode`,
1677
- * `parseX` and `decodeX` helpers in either case, and interface-api-util's `assertValidation`. A name proves
1678
- * nothing alone. A callee declared in the same file never counts (see `calleeIsWrittenHere`), so a chain of wrappers
1679
- * cannot close on itself, and an imported callee that takes `unknown` is checked where it is declared.
1680
- */
1681
- const DECODER_NAME = /^(?:assertValidation|safeParse(?:Async)?|[Pp]arse(?:[A-Z]\w*)?|[Dd]ecode(?:[A-Z]\w*)?)$/;
1682
- const PROMISE_TYPES = /* @__PURE__ */ new Set(["Promise", "PromiseLike"]);
1683
- function calleeName(call) {
1684
- const { callee } = call;
1685
- if (callee.type === "Identifier") return callee.name;
1686
- return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" ? callee.property.name : null;
1687
- }
1688
- /** Return the initializer of the single-name `const` a variable declares. */
1689
- function constInitializerOf(variable) {
1690
- const declarator = variable?.defs[0]?.node;
1691
- return declarator?.type === "VariableDeclarator" && declarator.id.type === "Identifier" && declarator.parent.type === "VariableDeclaration" && declarator.parent.kind === "const" ? declarator.init : null;
1692
1427
  }
1693
1428
  /**
1694
- * Return the decoder call whose direct argument a reference is. An `unknown` value is not callable, so it is
1695
- * never the callee. A callee written in this file carries its own row and is no decoder.
1429
+ * Return the decoder call whose direct argument a reference is: a call of a decoder name read from a schema library's import.
1430
+ * An `unknown` value is not callable, so it is never the callee. A decoder of any other origin can do anything with the value.
1696
1431
  */
1697
1432
  function decoderCallOf(identifier, sourceCode) {
1698
1433
  const call = identifier.parent;
1699
- return call.type === "CallExpression" && DECODER_NAME.test(calleeName(call) ?? "") && !calleeIsWrittenHere(call, sourceCode) ? call : null;
1434
+ return call.type === "CallExpression" && DECODER_NAME.test(calleeName(call) ?? "") && isRootedAtSchemaLibrary(call.callee, sourceCode, /* @__PURE__ */ new Set()) ? call : null;
1700
1435
  }
1701
1436
  /** Return whether a declared return type states a value, not `unknown` or `any`, promised or not. */
1702
1437
  function declaresParsedType(annotation) {
@@ -1719,7 +1454,7 @@ function returnedExpressions(owner, returns) {
1719
1454
  }
1720
1455
  /**
1721
1456
  * Return whether a parameter typed exactly `unknown` is a parse boundary: every read of it is a direct argument
1722
- * to a schema or decoder declared outside this file, every value the function returns is such a call's result,
1457
+ * to a decoder read from a schema library, every value the function returns is such a call's result,
1723
1458
  * and the function declares a return type that states what it parsed. A function that stores, forwards, reads
1724
1459
  * by hand (through `arguments` or `eval` too) or discards the parsed value is not one.
1725
1460
  */