@antelopejs/tooling-configs 0.4.0 → 0.5.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.
Files changed (40) hide show
  1. package/README.md +38 -26
  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 +141 -14
  7. package/dist/bin/comment-proof.mjs +29 -7
  8. package/dist/bin/garden.mjs +2 -57
  9. package/dist/bin/ratchet.mjs +48 -14
  10. package/dist/bin/suppressions.mjs +3 -3
  11. package/dist/bin/sync.mjs +74 -41
  12. package/dist/bin/verify.mjs +898 -366
  13. package/dist/capture/index.d.mts +6 -1
  14. package/dist/capture/index.mjs +1 -1
  15. package/dist/{ci-workflow-37qJa9LX.mjs → ci-workflow-COJUT12b.mjs} +121 -1
  16. package/dist/eslint.d.mts +16 -3
  17. package/dist/eslint.mjs +21 -6
  18. package/dist/formal/index.d.mts +20 -4
  19. package/dist/formal/index.mjs +71 -19
  20. package/dist/{map-B23_WMGE.mjs → map-DRyYz_Fw.mjs} +20 -1
  21. package/dist/oxc/anti-slop/index.mjs +50 -55
  22. package/dist/oxc/guardrails/index.d.mts +1 -1
  23. package/dist/oxc/guardrails/index.mjs +520 -103
  24. package/dist/oxc/lint.d.mts +7 -15
  25. package/dist/oxc/lint.mjs +16 -28
  26. package/dist/{package-CqqBgE8Y.mjs → package-Dn7VTFUW.mjs} +71 -17
  27. package/dist/{proof-stamp-DDDC0J6l.mjs → proof-stamp-DLWpPwQE.mjs} +72 -10
  28. package/dist/{ratchet-Q-JXuZIX.mjs → ratchet-BTiPMqam.mjs} +17 -15
  29. package/dist/require-safety-comment-for-type-assertion-mnb0CIxa.mjs +181 -0
  30. package/dist/{scenes-DZVXkcxS.mjs → scenes-d-VQ0LOm.mjs} +4 -1
  31. package/dist/semver-BDxeEcP_.mjs +57 -0
  32. package/dist/{suppression-base-9L8dlMQK.mjs → suppression-base-Drnl6Y7F.mjs} +2 -2
  33. package/dist/{suppression-budget-C0SkPO5R.mjs → suppression-budget-Dvb_IqBo.mjs} +1 -1
  34. package/dist/{suppressions-DM60NkvL.mjs → suppressions-BroaaGJ_.mjs} +1 -1
  35. package/dist/{templates-BKZv-BRw.mjs → templates-lJBZFskp.mjs} +8 -3
  36. package/dist/type-aware-rules-DMuN6sHb.mjs +30 -0
  37. package/dist/{typescript-DAgQIAAR.mjs → typescript-BBbry7uU.mjs} +123 -27
  38. package/dist/verify/index.d.mts +60 -15
  39. package/package.json +3 -1
  40. package/dist/require-safety-comment-for-type-assertion-BBQ669kj.mjs +0 -83
@@ -1,6 +1,6 @@
1
1
  import { a as matcherChain, i as isAssertion, n as TEST_FILE_GLOBS, o as memberName, r as callName, s as testCallback, t as ASSERTION_FUNCTION_NAMES } from "../../shared-WC0z3y9F.mjs";
2
2
  import { existsSync, lstatSync, readFileSync, realpathSync } from "node:fs";
3
- import { dirname, join, resolve } from "node:path";
3
+ import { dirname, join, relative, resolve } from "node:path";
4
4
  import { defineRule, eslintCompatPlugin } from "@oxlint/plugins";
5
5
  import ts from "typescript";
6
6
  //#region src/oxc/guardrails/rules/no-comments.ts
@@ -394,13 +394,157 @@ function isHasOwnPropertyCall(callee) {
394
394
  return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "call" && callee.object.type === "MemberExpression" && !callee.object.computed && callee.object.property.type === "Identifier" && callee.object.property.name === "hasOwnProperty";
395
395
  }
396
396
  //#endregion
397
+ //#region src/oxc/guardrails/own-key-guard.ts
398
+ const HAS_OWN_CALLS = /* @__PURE__ */ new Set(["Object.hasOwn", "hasOwnProperty.call"]);
399
+ const EXITS = /* @__PURE__ */ new Set([
400
+ "BreakStatement",
401
+ "ContinueStatement",
402
+ "ReturnStatement",
403
+ "ThrowStatement"
404
+ ]);
405
+ function checksOwnKey(node, read) {
406
+ const [first, second] = node.arguments;
407
+ if (first === void 0) return false;
408
+ const text = (argument) => read.source.getText(argument);
409
+ if (HAS_OWN_CALLS.has(callName(node.callee) ?? "") || isHasOwnPropertyCall(node.callee)) return second !== void 0 && text(first) === read.table && text(second) === read.key;
410
+ const callee = node.callee;
411
+ return callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "hasOwnProperty" && text(callee.object) === read.table && text(first) === read.key;
412
+ }
413
+ const EQUALITY = /* @__PURE__ */ new Set(["==", "==="]);
414
+ const INEQUALITY = /* @__PURE__ */ new Set(["!=", "!=="]);
415
+ const MAX_DEPTH$2 = 8;
416
+ const ASSERTS = /* @__PURE__ */ new Set([
417
+ "assert",
418
+ "assert.ok",
419
+ "assert.strict"
420
+ ]);
421
+ /** The initializer of a `const` the identifier names, such as `has` in `const has = Object.hasOwn(t, k)`. */
422
+ function constValue(node, read) {
423
+ const [definition] = resolveVariable(read.source, node)?.defs ?? [];
424
+ if (definition?.type !== "Variable" || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.type !== "VariableDeclarator") return null;
425
+ return definition.node.init;
426
+ }
427
+ /** `x === true`, `x !== false` and their mirrors: the outcome `x` has when the comparison has `outcome`. */
428
+ function comparedOutcome(node, outcome) {
429
+ const equal = EQUALITY.has(node.operator);
430
+ if (!equal && !INEQUALITY.has(node.operator)) return null;
431
+ const [literal, operand] = node.right.type === "Literal" ? [node.right, node.left] : [node.left, node.right];
432
+ if (literal.type !== "Literal") return null;
433
+ const value = literal.value;
434
+ if (value !== true && value !== false) return null;
435
+ return {
436
+ operand,
437
+ outcome: outcome === equal === value
438
+ };
439
+ }
440
+ /** A value that is `true` or `false`, so comparing it with a boolean literal tests its truth: a call, a `!`, or a `const` of one. */
441
+ function isBooleanValued(node, read, depth) {
442
+ if (depth > MAX_DEPTH$2) return false;
443
+ if (node.type === "ParenthesizedExpression") return isBooleanValued(node.expression, read, depth);
444
+ if (node.type === "Identifier") {
445
+ const value = constValue(node, read);
446
+ return value !== null && isBooleanValued(value, read, depth + 1);
447
+ }
448
+ return node.type === "CallExpression" || node.type === "UnaryExpression" && node.operator === "!";
449
+ }
450
+ /** Whether the own-key check holds whenever `node` evaluates to `outcome`. */
451
+ function holds(node, outcome, read, depth = 0) {
452
+ if (depth > MAX_DEPTH$2) return false;
453
+ switch (node.type) {
454
+ case "ParenthesizedExpression": return holds(node.expression, outcome, read, depth);
455
+ case "CallExpression": return outcome && checksOwnKey(node, read);
456
+ case "Identifier": {
457
+ const value = constValue(node, read);
458
+ return value !== null && holds(value, outcome, read, depth + 1);
459
+ }
460
+ case "UnaryExpression": return node.operator === "!" && holds(node.argument, !outcome, read, depth);
461
+ case "BinaryExpression": {
462
+ const compared = comparedOutcome(node, outcome);
463
+ return compared !== null && isBooleanValued(compared.operand, read, depth) && holds(compared.operand, compared.outcome, read, depth);
464
+ }
465
+ case "LogicalExpression": return node.operator === (outcome ? "&&" : "||") && (holds(node.left, outcome, read, depth) || holds(node.right, outcome, read, depth));
466
+ default: return false;
467
+ }
468
+ }
469
+ function exits(statement) {
470
+ if (statement.type === "BlockStatement") {
471
+ const last = statement.body.at(-1);
472
+ return last !== void 0 && exits(last);
473
+ }
474
+ if (statement.type === "IfStatement") return statement.alternate !== null && exits(statement.consequent) && exits(statement.alternate);
475
+ return EXITS.has(statement.type);
476
+ }
477
+ /** Whether a statement ends by storing `table[key]`, which makes the key own. */
478
+ function storesKey(statement, read) {
479
+ if (statement.type === "BlockStatement") {
480
+ const last = statement.body.at(-1);
481
+ return last !== void 0 && storesKey(last, read);
482
+ }
483
+ if (statement.type !== "ExpressionStatement") return false;
484
+ const assignment = statement.expression;
485
+ return assignment.type === "AssignmentExpression" && assignment.operator === "=" && assignment.left.type === "MemberExpression" && assignment.left.computed && read.source.getText(assignment.left.object) === read.table && read.source.getText(assignment.left.property) === read.key;
486
+ }
487
+ function settles(statement, read) {
488
+ return exits(statement) || storesKey(statement, read);
489
+ }
490
+ /**
491
+ * A statement after which the key is own: an `assert` of the check, or an `if` whose branch where
492
+ * the check fails leaves the block or stores the key.
493
+ */
494
+ function establishes(statement, read) {
495
+ if (statement.type === "ExpressionStatement" && statement.expression.type === "CallExpression") {
496
+ const [condition] = statement.expression.arguments;
497
+ return ASSERTS.has(callName(statement.expression.callee) ?? "") && condition !== void 0 && holds(condition, true, read);
498
+ }
499
+ if (statement.type !== "IfStatement") return false;
500
+ if (settles(statement.consequent, read) && holds(statement.test, false, read)) return true;
501
+ return statement.alternate !== null && settles(statement.alternate, read) && holds(statement.test, true, read);
502
+ }
503
+ function statementsOf(node) {
504
+ if (node.type === "BlockStatement" || node.type === "Program" || node.type === "StaticBlock") return node.body;
505
+ return node.type === "SwitchCase" ? node.consequent : null;
506
+ }
507
+ function guardsChild(parent, child, read) {
508
+ if (parent.type === "ConditionalExpression" || parent.type === "IfStatement") {
509
+ if (child === parent.consequent) return holds(parent.test, true, read);
510
+ return child === parent.alternate && holds(parent.test, false, read);
511
+ }
512
+ if (parent.type === "LogicalExpression" && child === parent.right) return (parent.operator === "&&" || parent.operator === "||") && holds(parent.left, parent.operator === "&&", read);
513
+ if (parent.type === "WhileStatement") return child === parent.body && holds(parent.test, true, read);
514
+ if (parent.type === "ForStatement") return (child === parent.body || child === parent.update) && parent.test !== null && holds(parent.test, true, read);
515
+ const statements = statementsOf(parent);
516
+ if (statements === null) return false;
517
+ const index = statements.indexOf(child);
518
+ return statements.slice(0, Math.max(index, 0)).some((statement) => establishes(statement, read));
519
+ }
520
+ /**
521
+ * Whether an own-key check of `table` and `key` (`Object.hasOwn`, `hasOwnProperty.call`,
522
+ * `table.hasOwnProperty`, directly, negated, compared with a boolean or held in a `const`) guards
523
+ * `node`: it holds in the branch of a conditional, `if`, `&&`/`||`, `while` or `for` that holds
524
+ * `node`, or an earlier statement asserts it, or leaves the block or stores the key where it fails.
525
+ */
526
+ function isOwnKeyGuarded(node, table, key, source) {
527
+ const read = {
528
+ table,
529
+ key,
530
+ source
531
+ };
532
+ let child = node;
533
+ let parent = node.parent;
534
+ while (parent !== null) {
535
+ if (guardsChild(parent, child, read)) return true;
536
+ child = parent;
537
+ parent = parent.parent;
538
+ }
539
+ return false;
540
+ }
541
+ //#endregion
397
542
  //#region src/oxc/guardrails/rules/no-inherited-key-lookup.ts
398
543
  const WRAPPER_TYPES = /* @__PURE__ */ new Set([
399
544
  "Readonly",
400
545
  "Partial",
401
546
  "Required"
402
547
  ]);
403
- const HAS_OWN_CALLS = /* @__PURE__ */ new Set(["Object.hasOwn", "hasOwnProperty.call"]);
404
548
  const MAX_ALIAS_DEPTH$1 = 8;
405
549
  function isStringKey(type) {
406
550
  if (type.type === "TSStringKeyword") return true;
@@ -461,7 +605,19 @@ function variableKind(variable, environment, depth) {
461
605
  const annotation = annotationOf(definition);
462
606
  if (definition.type === "Parameter") return annotation !== void 0 && isStringKeyedType(annotation, environment, depth) ? "dictionary" : null;
463
607
  if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") return null;
464
- return declaredKind(annotation, definition.node.init, environment, depth);
608
+ const declared = declaredKind(annotation, definition.node.init, environment, depth);
609
+ if (declared !== null || depth >= MAX_ALIAS_DEPTH$1 || definition.parent?.type !== "VariableDeclaration" || definition.parent.kind !== "const" || definition.node.init === null || !isOpaqueAnnotation(annotation)) return declared;
610
+ return tableKind(widened(definition.node.init), environment, depth + 1);
611
+ }
612
+ /** The value under `as unknown` and `as any` casts, which widen the type without changing the value. */
613
+ function widened(init) {
614
+ let value = init;
615
+ while (value.type === "TSAsExpression" && isOpaqueAnnotation(value.typeAnnotation)) value = value.expression;
616
+ return value;
617
+ }
618
+ /** No annotation, `unknown` or `any`: the alias holds whatever its initializer holds. */
619
+ function isOpaqueAnnotation(annotation) {
620
+ return annotation === void 0 || annotation.type === "TSUnknownKeyword" || annotation.type === "TSAnyKeyword";
465
621
  }
466
622
  /** The table a field declares, given its type annotation and initializer. */
467
623
  function declaredKind(annotation, init, environment, depth) {
@@ -493,27 +649,22 @@ function isWrite(node) {
493
649
  if (parent.type === "UnaryExpression") return parent.operator === "delete";
494
650
  return (parent.type === "ForInStatement" || parent.type === "ForOfStatement") && parent.left === node;
495
651
  }
496
- function ownKeyCheck(node, source) {
497
- const [first, second] = node.arguments;
498
- if (first === void 0) return null;
499
- if (HAS_OWN_CALLS.has(callName(node.callee) ?? "") || isHasOwnPropertyCall(node.callee)) return second === void 0 ? null : {
500
- table: source.getText(first),
501
- key: source.getText(second)
502
- };
503
- const callee = node.callee;
504
- if (callee.type === "MemberExpression" && !callee.computed && callee.property.type === "Identifier" && callee.property.name === "hasOwnProperty") return {
505
- table: source.getText(callee.object),
506
- key: source.getText(first)
507
- };
508
- return null;
509
- }
510
652
  function isPredicateKey(node) {
511
653
  const fn = enclosingFunction(node);
512
654
  const predicate = fn === null ? null : parameterPredicate(fn);
513
655
  return predicate !== null && node.left.type === "Identifier" && predicateName(predicate) === node.left.name;
514
656
  }
515
- function contains(outer, inner) {
516
- return outer.range[0] <= inner.range[0] && inner.range[1] <= outer.range[1];
657
+ /** The value a `const { [key]: x } = value` declaration or `({ [key]: x } = value)` assignment reads. */
658
+ function destructuredValue(pattern) {
659
+ const parent = pattern.parent;
660
+ if (parent.type === "VariableDeclarator") return parent.id === pattern ? parent.init : null;
661
+ return parent.type === "AssignmentExpression" && parent.left === pattern ? parent.right : null;
662
+ }
663
+ /** A destructured binding nothing reads or exports, such as `_removed` in `const { [key]: _removed, ...rest } = table`. */
664
+ function isDiscarded(value, source) {
665
+ if (value.type !== "Identifier") return false;
666
+ const variable = resolveVariable(source, value);
667
+ return variable !== null && variable.defs.every((definition) => definition.parent?.parent?.type !== "ExportNamedDeclaration") && !variable.references.some((reference) => reference.isRead());
517
668
  }
518
669
  /** Lookups into string-keyed object tables must not answer inherited `Object.prototype` keys. */
519
670
  const noInheritedKeyLookupRule = defineRule({
@@ -527,56 +678,52 @@ const noInheritedKeyLookupRule = defineRule({
527
678
  },
528
679
  createOnce(context) {
529
680
  let environment = null;
530
- let candidates = [];
531
- let checks = [];
681
+ const report = (candidate) => {
682
+ if (!isOwnKeyGuarded(candidate.node, candidate.table, candidate.key, context.sourceCode)) context.report({
683
+ node: candidate.node,
684
+ messageId: candidate.messageId,
685
+ data: {
686
+ table: candidate.table,
687
+ key: candidate.key
688
+ }
689
+ });
690
+ };
691
+ const lookup = (node, object, key) => {
692
+ if (environment === null) return;
693
+ const kind = tableKind(object, environment);
694
+ if (kind === null || kind === "literal" && key.type !== "TSAsExpression") return;
695
+ const table = context.sourceCode.getText(object);
696
+ if (isKnownKey(key, table, context.sourceCode)) return;
697
+ report({
698
+ node,
699
+ messageId: "lookup",
700
+ table,
701
+ key: context.sourceCode.getText(key)
702
+ });
703
+ };
532
704
  return {
533
705
  Program(node) {
534
706
  environment = {
535
707
  source: context.sourceCode,
536
- types: collectLocalTypes(node),
537
- program: node
708
+ types: collectLocalTypes(node)
538
709
  };
539
- candidates = [];
540
- checks = [];
541
710
  },
542
711
  MemberExpression(node) {
543
- if (environment === null || !node.computed || isWrite(node)) return;
544
- const kind = tableKind(node.object, environment);
545
- if (kind === null || kind === "literal" && node.property.type !== "TSAsExpression") return;
546
- const table = context.sourceCode.getText(node.object);
547
- if (isKnownKey(node.property, table, context.sourceCode)) return;
548
- candidates.push({
549
- node,
550
- messageId: "lookup",
551
- table,
552
- key: context.sourceCode.getText(node.property)
553
- });
712
+ if (node.computed && !isWrite(node)) lookup(node, node.object, node.property);
713
+ },
714
+ ObjectPattern(node) {
715
+ const object = destructuredValue(node);
716
+ if (object === null) return;
717
+ for (const property of node.properties) if (property.type === "Property" && property.computed && !isDiscarded(property.value, context.sourceCode)) lookup(property, object, property.key);
554
718
  },
555
719
  BinaryExpression(node) {
556
720
  if (environment === null || node.operator !== "in" || isStaticKey(node.left) || isPredicateKey(node) || tableKind(node.right, environment) === null) return;
557
- candidates.push({
721
+ report({
558
722
  node,
559
723
  messageId: "inOperator",
560
724
  table: context.sourceCode.getText(node.right),
561
725
  key: context.sourceCode.getText(node.left)
562
726
  });
563
- },
564
- CallExpression(node) {
565
- const check = ownKeyCheck(node, context.sourceCode);
566
- if (check !== null && environment !== null) checks.push({
567
- ...check,
568
- scope: enclosingFunction(node) ?? environment.program
569
- });
570
- },
571
- "Program:exit"() {
572
- for (const candidate of candidates) if (!checks.some((check) => check.table === candidate.table && check.key === candidate.key && contains(check.scope, candidate.node))) context.report({
573
- node: candidate.node,
574
- messageId: candidate.messageId,
575
- data: {
576
- table: candidate.table,
577
- key: candidate.key
578
- }
579
- });
580
727
  }
581
728
  };
582
729
  }
@@ -861,9 +1008,11 @@ function readManifest(directory) {
861
1008
  return;
862
1009
  }
863
1010
  if (!isObject(parsed) || !isText(parsed.name)) return void 0;
1011
+ const { antelopeJs } = parsed;
864
1012
  return {
865
1013
  name: parsed.name,
866
- exports: parsed.exports
1014
+ exports: parsed.exports,
1015
+ testedByAjs: isObject(antelopeJs) && isText(antelopeJs.test)
867
1016
  };
868
1017
  }
869
1018
  /** The nearest enclosing package that has a name, with the directory holding its manifest. */
@@ -913,12 +1062,15 @@ function isSelfLinked(directory, name) {
913
1062
  }
914
1063
  //#endregion
915
1064
  //#region src/oxc/guardrails/rules/no-self-import-in-tests.ts
916
- /** A test that imports its own package name must reach local code: the `exports` map publishes the subpath and `node_modules` links the package to itself. */
1065
+ /** A test that imports its own package name must reach local code: the `exports` map publishes the subpath and `node_modules` links the package to itself. Under `ajs module test` no self-link reaches local code, so every self-import is reported. */
917
1066
  const noSelfImportInTestsRule = defineRule({
918
1067
  meta: {
919
1068
  type: "problem",
920
1069
  docs: { description: "Tests import the module under test from local source, never their own published package name." },
921
- messages: { selfImport: "This test imports its own package `{{ name }}`, which resolves to the installed published copy, so it never exercises the local code. Import the module under test by relative path, or make the self-import local: publish `{{ subpath }}` in package.json `exports` and link the package to itself (`link:.` in devDependencies)." }
1070
+ messages: {
1071
+ selfImport: "This test imports its own package `{{ name }}`, which resolves to the installed published copy, so it never exercises the local code. Import the module under test by relative path, or make the self-import local: publish `{{ subpath }}` in package.json `exports` and link the package to itself (`link:.` in devDependencies).",
1072
+ selfImportUnderAjsTest: "This test imports its own package `{{ name }}`, and `ajs module test` resolves that name through its own module resolver, which can bind it to a copy installed with another module instead of this package, even with a `link:.` self-link. Import the module under test by relative path."
1073
+ }
922
1074
  },
923
1075
  createOnce(context) {
924
1076
  function check(node, specifier) {
@@ -926,7 +1078,16 @@ const noSelfImportInTestsRule = defineRule({
926
1078
  if (owner === void 0) return;
927
1079
  const { directory, manifest } = owner;
928
1080
  const subpath = selfSubpath(manifest.name, specifier);
929
- if (subpath === void 0 || exportsCover(manifest.exports, subpath) && isSelfLinked(directory, manifest.name)) return;
1081
+ if (subpath === void 0) return;
1082
+ if (manifest.testedByAjs) {
1083
+ context.report({
1084
+ node,
1085
+ messageId: "selfImportUnderAjsTest",
1086
+ data: { name: manifest.name }
1087
+ });
1088
+ return;
1089
+ }
1090
+ if (exportsCover(manifest.exports, subpath) && isSelfLinked(directory, manifest.name)) return;
930
1091
  context.report({
931
1092
  node,
932
1093
  messageId: "selfImport",
@@ -1032,46 +1193,60 @@ const noTypeofFunctionOnlyTestRule = defineRule({
1032
1193
  }
1033
1194
  });
1034
1195
  //#endregion
1035
- //#region src/oxc/guardrails/rules/require-assertion.ts
1036
- /** Test callbacks must contain an assertion regardless of their framework or filename. */
1037
- const requireAssertionRule = defineRule({
1038
- meta: {
1039
- type: "problem",
1040
- docs: { description: "Require an assertion in every executable test callback." },
1041
- messages: { missingAssertion: "Call an assertion in this test to verify its behavior." }
1042
- },
1043
- createOnce(context) {
1044
- const frames = [];
1045
- function recordAssertion(node) {
1046
- const frame = frames.at(-1);
1047
- if (frame !== void 0 && node.start >= frame.callback.start && node.end <= frame.callback.end && isAssertion(node)) frame.hasAssertion = true;
1048
- }
1049
- return {
1050
- before() {
1051
- frames.length = 0;
1052
- },
1053
- CallExpression(node) {
1054
- const callback = testCallback(node);
1055
- if (callback !== void 0) frames.push({
1056
- node,
1057
- callback,
1058
- hasAssertion: false
1059
- });
1060
- else recordAssertion(node);
1061
- },
1062
- MemberExpression: recordAssertion,
1063
- "CallExpression:exit"(node) {
1064
- const frame = frames.at(-1);
1065
- if (frame?.node !== node) return;
1066
- frames.pop();
1067
- if (!frame.hasAssertion) context.report({
1068
- node,
1069
- messageId: "missingAssertion"
1070
- });
1071
- }
1072
- };
1196
+ //#region src/oxc/guardrails/literal-fit.ts
1197
+ const MAX_DEPTH$1 = 8;
1198
+ const INDEX = /^\d+$/u;
1199
+ /** The type a written value has: a shorthand property reads its variable, as a direct assignment would. */
1200
+ function valueType(checker, value) {
1201
+ const shorthand = ts.isShorthandPropertyAssignment(value.parent) && value.parent.name === value ? checker.getShorthandAssignmentValueSymbol(value.parent) : void 0;
1202
+ return shorthand === void 0 ? checker.getTypeAtLocation(value) : checker.getTypeOfSymbolAtLocation(shorthand, value);
1203
+ }
1204
+ function propertyName(property) {
1205
+ if (!(ts.isPropertyAssignment(property) || ts.isShorthandPropertyAssignment(property)) || !(ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) || ts.isNumericLiteral(property.name))) return void 0;
1206
+ return {
1207
+ name: property.name.text,
1208
+ value: ts.isPropertyAssignment(property) ? property.initializer : property.name
1209
+ };
1210
+ }
1211
+ function objectFits(checker, literal, type, depth) {
1212
+ const names = /* @__PURE__ */ new Set();
1213
+ for (const property of literal.properties) {
1214
+ const named = propertyName(property);
1215
+ if (named === void 0) return false;
1216
+ names.add(named.name);
1217
+ const field = checker.getPropertyOfType(type, named.name);
1218
+ const fieldType = field === void 0 ? checker.getIndexTypeOfType(type, ts.IndexKind.String) : checker.getTypeOfSymbolAtLocation(field, literal);
1219
+ if (fieldType === void 0 || !literalFits(checker, named.value, fieldType, depth + 1)) return false;
1073
1220
  }
1074
- });
1221
+ return checker.getPropertiesOfType(type).every((field) => (field.flags & ts.SymbolFlags.Optional) !== 0 || names.has(field.name));
1222
+ }
1223
+ function arrayFits(checker, literal, type, depth) {
1224
+ const { elements } = literal;
1225
+ if (elements.some((element) => ts.isSpreadElement(element) || ts.isOmittedExpression(element))) return false;
1226
+ if (checker.isTupleType(type)) {
1227
+ const slots = checker.getPropertiesOfType(type).filter((slot) => INDEX.test(slot.name));
1228
+ const required = slots.filter((slot) => (slot.flags & ts.SymbolFlags.Optional) === 0);
1229
+ return elements.length >= required.length && elements.length <= slots.length && elements.every((element, index) => literalFits(checker, element, checker.getTypeOfSymbolAtLocation(slots[index], literal), depth + 1));
1230
+ }
1231
+ const element = checker.getIndexTypeOfType(type, ts.IndexKind.Number);
1232
+ return element !== void 0 && elements.every((value) => literalFits(checker, value, element, depth + 1));
1233
+ }
1234
+ /**
1235
+ * Whether `expression` fits `type` the way a direct assignment types it:
1236
+ * literals inside object and array literals keep their literal types, which
1237
+ * `Object.assign`'s inferred source type widens. Object literals need every
1238
+ * required property and no undeclared one, tuples their length. Past the
1239
+ * depth cap a literal fits, so the rule stays silent rather than wrong.
1240
+ */
1241
+ function literalFits(checker, expression, type, depth = 0) {
1242
+ let value = expression;
1243
+ while (ts.isParenthesizedExpression(value)) value = value.expression;
1244
+ if (checker.isTypeAssignableTo(valueType(checker, value), type)) return true;
1245
+ if (!ts.isObjectLiteralExpression(value) && !ts.isArrayLiteralExpression(value)) return false;
1246
+ if (depth === MAX_DEPTH$1) return true;
1247
+ if (type.isUnion()) return type.types.some((member) => literalFits(checker, value, member, depth));
1248
+ return ts.isObjectLiteralExpression(value) ? objectFits(checker, value, type, depth) : arrayFits(checker, value, type, depth);
1249
+ }
1075
1250
  //#endregion
1076
1251
  //#region src/oxc/guardrails/tsconfig-program.ts
1077
1252
  const CONFIG_NAMES = ["tsconfig.json", "jsconfig.json"];
@@ -1087,13 +1262,15 @@ function configAbove(directory) {
1087
1262
  if (dirname(current) === current) return void 0;
1088
1263
  }
1089
1264
  }
1090
- /** The files `config` lists and the configs it references, parsed once per lint run. */
1265
+ /** The files `config` lists, its compiler options and the configs it references, parsed once per lint run. */
1091
1266
  function project(config) {
1092
1267
  if (projects.has(config)) return projects.get(config);
1093
1268
  const parsed = ts.getParsedCommandLineOfConfigFile(config, void 0, HOST);
1094
1269
  const result = parsed && {
1095
1270
  files: new Set(parsed.fileNames),
1096
- references: (parsed.projectReferences ?? []).map((reference) => ts.resolveProjectReferencePath(reference))
1271
+ options: parsed.options,
1272
+ references: (parsed.projectReferences ?? []).map((reference) => ts.resolveProjectReferencePath(reference)),
1273
+ automaticTypes: parsed.options.types === void 0 ? ts.getAutomaticTypeDirectiveNames(parsed.options, ts.sys) : []
1097
1274
  };
1098
1275
  projects.set(config, result);
1099
1276
  return result;
@@ -1129,9 +1306,218 @@ function programConfig(file) {
1129
1306
  directory = parent;
1130
1307
  }
1131
1308
  }
1309
+ /**
1310
+ * The `@types` packages tsc loads into `config`'s program only because it lists no
1311
+ * `compilerOptions.types`. oxlint-tsgolint runs TypeScript 7, where `types` defaults
1312
+ * to none, so its program loads none of them.
1313
+ */
1314
+ function automaticTypes(config) {
1315
+ return project(config)?.automaticTypes ?? [];
1316
+ }
1317
+ //#endregion
1318
+ //#region src/oxc/guardrails/type-checker.ts
1319
+ const programs = /* @__PURE__ */ new Map();
1320
+ function normalize(file) {
1321
+ return resolve(file).replaceAll("\\", "/");
1322
+ }
1323
+ function build(config, path, text) {
1324
+ const parsed = project(config);
1325
+ if (parsed === void 0) return void 0;
1326
+ const host = ts.createCompilerHost(parsed.options, true);
1327
+ const read = host.getSourceFile.bind(host);
1328
+ host.getSourceFile = (name, language, ...rest) => normalize(name) === path ? ts.createSourceFile(name, text, language, true) : read(name, language, ...rest);
1329
+ const program = ts.createProgram({
1330
+ rootNames: [...parsed.files],
1331
+ options: parsed.options,
1332
+ host,
1333
+ oldProgram: programs.get(config)
1334
+ });
1335
+ programs.set(config, program);
1336
+ return program;
1337
+ }
1338
+ /**
1339
+ * The type checker of the tsconfig program that lists `file`, built once per
1340
+ * lint run and rebuilt from the previous one when `text` differs from what it
1341
+ * read. Undefined when no tsconfig lists the file.
1342
+ */
1343
+ function typedFile(file, text) {
1344
+ const config = programConfig(file);
1345
+ if (config === void 0) return void 0;
1346
+ const path = normalize(file);
1347
+ const cached = programs.get(config);
1348
+ const program = cached?.getSourceFile(path)?.text === text ? cached : build(config, path, text);
1349
+ const sourceFile = program?.getSourceFile(path);
1350
+ return program && sourceFile ? {
1351
+ checker: program.getTypeChecker(),
1352
+ options: program.getCompilerOptions(),
1353
+ sourceFile
1354
+ } : void 0;
1355
+ }
1356
+ /** The node `test` accepts that spans exactly `start` to `end`, the span oxlint gives the matching ESTree node. */
1357
+ function nodeAt(sourceFile, span, test) {
1358
+ let found;
1359
+ function visit(node) {
1360
+ if (found !== void 0 || node.pos > span.start || node.end < span.end) return;
1361
+ if (test(node) && node.getStart(sourceFile) === span.start && node.end === span.end) found = node;
1362
+ else ts.forEachChild(node, visit);
1363
+ }
1364
+ visit(sourceFile);
1365
+ return found;
1366
+ }
1367
+ //#endregion
1368
+ //#region src/oxc/guardrails/rules/no-unsafe-object-assign.ts
1369
+ const UNTYPED = ts.TypeFlags.Any | ts.TypeFlags.Unknown;
1370
+ const MAX_DEPTH = 4;
1371
+ function members(type) {
1372
+ return type.isUnion() ? type.types : [type];
1373
+ }
1374
+ /** Whether `type` is `any`, or holds `any` in a union, an intersection or the elements of an array or tuple, as in `any[]`. */
1375
+ function holdsAny(checker, type, depth = 0) {
1376
+ if (type.flags & ts.TypeFlags.Any) return true;
1377
+ if (depth === MAX_DEPTH) return false;
1378
+ const element = checker.isArrayType(type) || checker.isTupleType(type) ? checker.getIndexTypeOfType(type, ts.IndexKind.Number) : void 0;
1379
+ return (type.isUnionOrIntersection() ? type.types : element === void 0 ? [] : [element]).some((member) => holdsAny(checker, member, depth + 1));
1380
+ }
1381
+ /** Whether a declaration sits inside a generic class, interface or type alias, whose type parameters the public checker API cannot instantiate for a declaration. */
1382
+ function insideGeneric(declaration) {
1383
+ for (let node = declaration.parent; !ts.isSourceFile(node); node = node.parent) if ((ts.isClassLike(node) || ts.isInterfaceDeclaration(node) || ts.isTypeAliasDeclaration(node)) && node.typeParameters !== void 0) return true;
1384
+ return false;
1385
+ }
1386
+ /** The types a field accepts: its read type, and its setter's parameter type. Undefined when the setter sits inside a generic declaration, whose parameter type this rule cannot instantiate. */
1387
+ function acceptedTypes(checker, field, source) {
1388
+ const setter = field.declarations?.find(ts.isSetAccessorDeclaration);
1389
+ const parameter = setter?.parameters[0];
1390
+ const read = checker.getTypeOfSymbolAtLocation(field, source);
1391
+ if (setter === void 0 || parameter === void 0) return [read];
1392
+ return insideGeneric(setter) ? void 0 : [read, checker.getTypeAtLocation(parameter)];
1393
+ }
1394
+ function untyped(type) {
1395
+ return (type.flags & UNTYPED) !== 0;
1396
+ }
1397
+ function typeVerdict({ checker, exactOptional }, type, accepted, optional) {
1398
+ if (accepted.length === 0 || accepted.some(untyped)) return void 0;
1399
+ if (untyped(type) || holdsAny(checker, type) && !accepted.some((member) => holdsAny(checker, member))) return "untypedField";
1400
+ return (optional && !exactOptional ? members(type).filter((member) => !(member.flags & ts.TypeFlags.Undefined)) : [type]).every((member) => accepted.some((field) => checker.isTypeAssignableTo(member, field))) ? void 0 : "mistypedField";
1401
+ }
1402
+ function fieldWrite(check, property, field, source) {
1403
+ const { checker } = check;
1404
+ const accepted = acceptedTypes(checker, field, source);
1405
+ if (accepted === void 0) return void 0;
1406
+ const declaration = property.valueDeclaration;
1407
+ const written = declaration !== void 0 && declaration.getSourceFile() === source.getSourceFile() && declaration.pos >= source.pos && declaration.end <= source.end && (ts.isPropertyAssignment(declaration) || ts.isShorthandPropertyAssignment(declaration)) ? ts.isPropertyAssignment(declaration) ? declaration.initializer : declaration.name : void 0;
1408
+ const kind = written === void 0 ? typeVerdict(check, checker.getTypeOfSymbolAtLocation(property, source), accepted, (property.flags & ts.SymbolFlags.Optional) !== 0) : typeVerdict(check, valueType(checker, written), accepted, false);
1409
+ if (kind === void 0 || kind === "mistypedField" && written !== void 0 && accepted.some((type) => literalFits(checker, written, type))) return void 0;
1410
+ return {
1411
+ kind,
1412
+ name: property.name
1413
+ };
1414
+ }
1415
+ /** A primitive source member, such as the `false` of `flag && { ... }`, whose apparent members (`valueOf`) `Object.assign` never copies. */
1416
+ const PRIMITIVE = ts.TypeFlags.BooleanLike | ts.TypeFlags.NumberLike | ts.TypeFlags.StringLike | ts.TypeFlags.BigIntLike | ts.TypeFlags.ESSymbolLike | ts.TypeFlags.Null | ts.TypeFlags.Undefined | ts.TypeFlags.Void | ts.TypeFlags.Never;
1417
+ /** Whether the target declares a field that does not take `unknown`, which an untyped string-keyed source could overwrite. */
1418
+ function hasTypedField(checker, target) {
1419
+ return checker.getPropertiesOfType(target).some((field) => !untyped(checker.getTypeOfSymbol(field)));
1420
+ }
1421
+ function untypedKeys(checker, member) {
1422
+ return checker.getIndexInfosOfType(member).some((info) => (info.keyType.flags & ts.TypeFlags.String) !== 0 && untyped(info.type));
1423
+ }
1424
+ function sourceWrites(check, target, source) {
1425
+ const { checker } = check;
1426
+ const writes = /* @__PURE__ */ new Map();
1427
+ for (const member of members(checker.getTypeAtLocation(source))) {
1428
+ if (untyped(member) || untypedKeys(checker, member) && hasTypedField(checker, target)) writes.set("", { kind: "untypedSource" });
1429
+ if (member.flags & PRIMITIVE) continue;
1430
+ for (const property of checker.getPropertiesOfType(member)) {
1431
+ const field = checker.getPropertyOfType(target, property.name);
1432
+ const write = field && fieldWrite(check, property, field, source);
1433
+ if (write !== void 0) writes.set(property.name, write);
1434
+ }
1435
+ }
1436
+ return [...writes.values()];
1437
+ }
1438
+ /** `Object.assign<T, U>` returns `T & U` without comparing `U` with `T`, so a source can write `unknown`, `any` or a wrong type over a field `T` declares. */
1439
+ const noUnsafeObjectAssignRule = defineRule({
1440
+ meta: {
1441
+ type: "problem",
1442
+ docs: { description: "Object.assign writes no unknown, any or unassignable value over a field its target declares." },
1443
+ messages: {
1444
+ untypedSource: "This Object.assign source is typed `unknown` or `any`, or holds such values under an index signature, and it overwrites fields the target declares without a check: `Object.assign<T, U>` returns `T & U`. Parse it into a typed value first.",
1445
+ untypedField: "Object.assign writes an `unknown` or `any` value over `{{name}}`, a field the target declares, and TypeScript never compares the two: `Object.assign<T, U>` returns `T & U`. Parse the value into the field's type first.",
1446
+ mistypedField: "Object.assign writes a value over `{{name}}` that the target's `{{name}}` type does not accept, and TypeScript never compares the two: `Object.assign<T, U>` returns `T & U`. Assign the field directly so the compiler checks it."
1447
+ }
1448
+ },
1449
+ createOnce(context) {
1450
+ return { CallExpression(node) {
1451
+ if (callName(node.callee) !== "Object.assign" || node.arguments.length < 2) return;
1452
+ const typed = typedFile(context.filename, context.sourceCode.text);
1453
+ const call = typed && nodeAt(typed.sourceFile, node, ts.isCallExpression);
1454
+ if (typed === void 0 || call === void 0) return;
1455
+ const declared = typed.checker.getTypeAtLocation(call.arguments[0]);
1456
+ const target = declared.isTypeParameter() ? typed.checker.getBaseConstraintOfType(declared) ?? declared : declared;
1457
+ if (typed.checker.getPropertiesOfType(target).length === 0) return;
1458
+ const check = {
1459
+ checker: typed.checker,
1460
+ exactOptional: typed.options.exactOptionalPropertyTypes === true
1461
+ };
1462
+ call.arguments.slice(1).forEach((source, index) => {
1463
+ if (ts.isSpreadElement(source)) return;
1464
+ for (const write of sourceWrites(check, target, source)) context.report({
1465
+ node: node.arguments[index + 1],
1466
+ messageId: write.kind,
1467
+ data: "name" in write ? { name: write.name } : {}
1468
+ });
1469
+ });
1470
+ } };
1471
+ }
1472
+ });
1473
+ //#endregion
1474
+ //#region src/oxc/guardrails/rules/require-assertion.ts
1475
+ function isInCallback(node, frame) {
1476
+ for (let current = node.parent; current !== null && current !== frame.node; current = current.parent) if (current === frame.callback) return true;
1477
+ return false;
1478
+ }
1479
+ /** Test callbacks must contain an assertion regardless of their framework or filename. */
1480
+ const requireAssertionRule = defineRule({
1481
+ meta: {
1482
+ type: "problem",
1483
+ docs: { description: "Require an assertion in every executable test callback." },
1484
+ messages: { missingAssertion: "Call an assertion in this test to verify its behavior." }
1485
+ },
1486
+ createOnce(context) {
1487
+ const frames = [];
1488
+ function recordAssertion(node) {
1489
+ const frame = frames.at(-1);
1490
+ if (frame !== void 0 && isAssertion(node) && isInCallback(node, frame)) frame.hasAssertion = true;
1491
+ }
1492
+ return {
1493
+ before() {
1494
+ frames.length = 0;
1495
+ },
1496
+ CallExpression(node) {
1497
+ const callback = testCallback(node);
1498
+ if (callback !== void 0) frames.push({
1499
+ node,
1500
+ callback,
1501
+ hasAssertion: false
1502
+ });
1503
+ else recordAssertion(node);
1504
+ },
1505
+ MemberExpression: recordAssertion,
1506
+ "CallExpression:exit"(node) {
1507
+ const frame = frames.at(-1);
1508
+ if (frame?.node !== node) return;
1509
+ frames.pop();
1510
+ if (!frame.hasAssertion) context.report({
1511
+ node,
1512
+ messageId: "missingAssertion"
1513
+ });
1514
+ }
1515
+ };
1516
+ }
1517
+ });
1132
1518
  //#endregion
1133
1519
  //#region src/oxc/guardrails/rules/require-tsconfig-include.ts
1134
- const TYPE_LINTED = /\.[cm]?[jt]sx?$/u;
1520
+ const TYPE_LINTED$1 = /\.[cm]?[jt]sx?$/u;
1135
1521
  /** A file type-aware lint reads must be listed by a tsconfig, or tsgolint types it without the repository's compiler options and `types`. */
1136
1522
  const requireTsconfigIncludeRule = defineRule({
1137
1523
  meta: {
@@ -1141,7 +1527,7 @@ const requireTsconfigIncludeRule = defineRule({
1141
1527
  },
1142
1528
  createOnce(context) {
1143
1529
  return { Program(node) {
1144
- if (TYPE_LINTED.test(context.filename) && programConfig(context.filename) === void 0) context.report({
1530
+ if (TYPE_LINTED$1.test(context.filename) && programConfig(context.filename) === void 0) context.report({
1145
1531
  node,
1146
1532
  messageId: "outside"
1147
1533
  });
@@ -1149,8 +1535,37 @@ const requireTsconfigIncludeRule = defineRule({
1149
1535
  }
1150
1536
  });
1151
1537
  //#endregion
1538
+ //#region src/oxc/guardrails/rules/require-tsconfig-types.ts
1539
+ const TYPE_LINTED = /\.[cm]?[jt]sx?$/u;
1540
+ /** The tsconfig of a file type-aware lint reads must list `compilerOptions.types`, or tsgolint loads none of the `@types` packages tsc loads. */
1541
+ const requireTsconfigTypesRule = defineRule({
1542
+ meta: {
1543
+ type: "problem",
1544
+ docs: { description: "The tsconfig.json of every file type-aware lint reads lists compilerOptions.types, so tsgolint loads the same @types packages as tsc." },
1545
+ messages: { missing: "{{config}} lists no `compilerOptions.types`, so tsc loads every `@types` package ({{packages}}) but type-aware lint, which runs TypeScript 7, loads none: Node and test-runner globals are error types, so `typescript/no-unsafe-*` reports here are false and findings that need those types go missing. Add `\"types\": [{{list}}]` to its compilerOptions, or to the config that generates it, listing the packages this code uses." }
1546
+ },
1547
+ createOnce(context) {
1548
+ return { Program(node) {
1549
+ if (!TYPE_LINTED.test(context.filename)) return;
1550
+ const config = programConfig(context.filename);
1551
+ if (config === void 0) return;
1552
+ const packages = automaticTypes(config);
1553
+ if (packages.length === 0) return;
1554
+ context.report({
1555
+ node,
1556
+ messageId: "missing",
1557
+ data: {
1558
+ config: relative(context.cwd, config).replaceAll("\\", "/"),
1559
+ packages: packages.join(", "),
1560
+ list: packages.map((name) => JSON.stringify(name)).join(", ")
1561
+ }
1562
+ });
1563
+ } };
1564
+ }
1565
+ });
1566
+ //#endregion
1152
1567
  //#region src/oxc/guardrails/index.ts
1153
- /** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, low-evidence tests and files no tsconfig lists. */
1568
+ /** AntelopeJS rules that reject comments, inherited-key lookups, presence-only type guards, unchecked Object.assign writes, low-evidence tests, files no tsconfig lists and tsconfigs that list no `types`. */
1154
1569
  const guardrailsPlugin = eslintCompatPlugin({
1155
1570
  meta: { name: "antelopejs" },
1156
1571
  rules: {
@@ -1161,8 +1576,10 @@ const guardrailsPlugin = eslintCompatPlugin({
1161
1576
  "no-constant-value-assertion": noConstantValueAssertionRule,
1162
1577
  "no-self-import-in-tests": noSelfImportInTestsRule,
1163
1578
  "no-typeof-function-only-test": noTypeofFunctionOnlyTestRule,
1579
+ "no-unsafe-object-assign": noUnsafeObjectAssignRule,
1164
1580
  "require-assertion": requireAssertionRule,
1165
- "require-tsconfig-include": requireTsconfigIncludeRule
1581
+ "require-tsconfig-include": requireTsconfigIncludeRule,
1582
+ "require-tsconfig-types": requireTsconfigTypesRule
1166
1583
  }
1167
1584
  });
1168
1585
  //#endregion