@kubb/ast 5.18.6 → 5.18.8

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/dist/index.cjs CHANGED
@@ -10,14 +10,14 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
10
10
  var __getOwnPropNames = Object.getOwnPropertyNames;
11
11
  var __getProtoOf = Object.getPrototypeOf;
12
12
  var __hasOwnProp = Object.prototype.hasOwnProperty;
13
- var __exportAll = (all, no_symbols) => {
14
- let target = {};
13
+ var __exportAll = (all, flags) => {
14
+ let target = Object.setPrototypeOf({}, null);
15
15
  for (var name in all) __defProp(target, name, {
16
16
  get: all[name],
17
17
  enumerable: true
18
18
  });
19
- if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
20
- return target;
19
+ if (!(flags & 1)) __defProp(target, Symbol.toStringTag, { value: "Module" });
20
+ return flags & 2 ? target : Object.preventExtensions(target);
21
21
  };
22
22
  var __copyProps = (to, from, except, desc) => {
23
23
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
@@ -46,11 +46,32 @@ let node_crypto = require("node:crypto");
46
46
  * `const refs = memo(refCache, node, () => collectRefs(node))`
47
47
  */
48
48
  function memo(store, key, compute) {
49
- if (store.has(key)) return store.get(key);
49
+ const cached = store.get(key);
50
+ if (cached !== void 0 || store.has(key)) return cached;
50
51
  const value = compute();
51
52
  store.set(key, value);
52
53
  return value;
53
54
  }
55
+ function createStore({ type }) {
56
+ if (type === "weak") return /* @__PURE__ */ new WeakMap();
57
+ const entry = /* @__PURE__ */ new Map();
58
+ if (type === "map") return entry;
59
+ const set = (key, value) => {
60
+ entry.clear();
61
+ entry.set(key, value);
62
+ };
63
+ const touch = (key) => {
64
+ if (!entry.has(key)) return false;
65
+ set(key, entry.get(key));
66
+ return true;
67
+ };
68
+ return {
69
+ has: touch,
70
+ get: (key) => touch(key) ? entry.get(key) : void 0,
71
+ set
72
+ };
73
+ }
74
+ memo.create = createStore;
54
75
  // src/constants.ts
55
76
  /**
56
77
  * Node kind discriminators used by every AST node (for example `node.kind === nodeKinds.text`).
@@ -321,14 +342,19 @@ function walkSchema(node, through, visit) {
321
342
  * ```
322
343
  */
323
344
  function getSchemaLiteralValues$1(node) {
345
+ const cached = literalValues.get(node);
346
+ if (cached) return cached;
324
347
  const values = /* @__PURE__ */ new Set();
325
348
  walkSchema(node, "union", (current) => {
326
349
  if (current.type !== "enum") return;
327
350
  for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
328
351
  });
329
- return [...values];
352
+ const result = Object.freeze([...values]);
353
+ literalValues.set(node, result);
354
+ return result;
330
355
  }
331
356
  __name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
357
+ const literalValues = /* @__PURE__ */ new WeakMap();
332
358
  function collectPropertyNames(node) {
333
359
  const names = /* @__PURE__ */ new Set();
334
360
  walkSchema(node, "intersection", (current) => {
@@ -477,24 +503,6 @@ function isNode(value) {
477
503
  function isNodeRecord(value) {
478
504
  return typeof value === "object" && value !== null && !Array.isArray(value);
479
505
  }
480
- /**
481
- * Returns the immediate traversable children of `node`.
482
- *
483
- * @example
484
- * ```ts
485
- * const children = getChildren(operationNode, true)
486
- * // returns parameters, the request body, and responses
487
- * ```
488
- */
489
- function getChildren(node, recurse) {
490
- if (node.kind === nodeKinds$1.schema && !recurse) return [];
491
- const children = [];
492
- visitChildNodes(node, (child) => {
493
- children.push(child);
494
- return false;
495
- });
496
- return children;
497
- }
498
506
  function isNeverPrinted({ node, key }) {
499
507
  if (node.kind !== nodeKinds$1.schema) return false;
500
508
  if (key === "contains" && (node.type === "array" || node.type === "tuple")) return node.contains?.min === 0 && node.contains.max === void 0;
@@ -502,41 +510,63 @@ function isNeverPrinted({ node, key }) {
502
510
  return false;
503
511
  }
504
512
  /**
513
+ * Child keys of a schema walked with `checks: true`: the shape keys, then {@link schemaCheckKeys}.
514
+ */
515
+ const SCHEMA_CHECK_VISITOR_KEYS = [...VISITOR_KEYS.Schema, ...schemaCheckKeys];
516
+ function visitOnce(child, seen, visit) {
517
+ if (seen.first === void 0) {
518
+ seen.first = child;
519
+ return visit(child);
520
+ }
521
+ if (seen.first === child || seen.rest?.has(child)) return false;
522
+ (seen.rest ??= /* @__PURE__ */ new Set()).add(child);
523
+ return visit(child);
524
+ }
525
+ function visitRecord(value, seen, visit) {
526
+ for (const name in value) {
527
+ if (!Object.hasOwn(value, name)) continue;
528
+ const item = value[name];
529
+ if (isNode(item) && visitOnce(item, seen, visit)) return true;
530
+ }
531
+ return false;
532
+ }
533
+ /**
505
534
  * Calls `visit` for each direct child node and stops at the first call that returns `true`.
506
535
  * With `checks: true`, a schema's {@link schemaCheckKeys} fields are walked too, except checks that cannot fail.
507
536
  */
508
537
  function visitChildNodes(node, visit, { checks = false } = {}) {
509
- const keys = checks && node.kind === nodeKinds$1.schema ? [...VISITOR_KEYS[node.kind] ?? [], ...schemaCheckKeys] : VISITOR_KEYS[node.kind];
538
+ const withChecks = checks && node.kind === nodeKinds$1.schema;
539
+ const keys = withChecks ? SCHEMA_CHECK_VISITOR_KEYS : VISITOR_KEYS[node.kind];
510
540
  if (!keys) return false;
511
541
  const record = node;
512
- const seen = /* @__PURE__ */ new Set();
513
- const visitOnce = (child) => !seen.has(child) && (seen.add(child), visit(child));
514
- const visitRecord = (value) => {
515
- for (const name in value) {
516
- if (!Object.hasOwn(value, name)) continue;
517
- const item = value[name];
518
- if (isNode(item) && visitOnce(item)) return true;
519
- }
520
- return false;
521
- };
542
+ let seen;
522
543
  for (const key of keys) {
523
- if (checks && isNeverPrinted({
544
+ const value = record[key];
545
+ if (value === void 0 || value === null) continue;
546
+ if (withChecks && isNeverPrinted({
524
547
  node,
525
548
  key
526
549
  })) continue;
527
- const value = record[key];
550
+ seen ??= {
551
+ first: void 0,
552
+ rest: void 0
553
+ };
528
554
  if (Array.isArray(value)) {
529
- for (const item of value) if (isNode(item) ? visitOnce(item) : isNodeRecord(item) && visitRecord(item)) return true;
555
+ for (const item of value) if (isNode(item) ? visitOnce(item, seen, visit) : isNodeRecord(item) && visitRecord(item, seen, visit)) return true;
530
556
  continue;
531
557
  }
532
558
  if (isNode(value)) {
533
- if (visitOnce(value)) return true;
559
+ if (visitOnce(value, seen, visit)) return true;
534
560
  continue;
535
561
  }
536
- if (isNodeRecord(value) && visitRecord(value)) return true;
562
+ if (isNodeRecord(value) && visitRecord(value, seen, visit)) return true;
537
563
  }
538
564
  return false;
539
565
  }
566
+ const notFound = {
567
+ found: void 0,
568
+ low: Number.POSITIVE_INFINITY
569
+ };
540
570
  /**
541
571
  * Finds the first schema that matches `predicate` in depth-first order. Set `followRefs` to include resolved ref targets.
542
572
  *
@@ -547,28 +577,68 @@ function visitChildNodes(node, visit, { checks = false } = {}) {
547
577
  * })
548
578
  * ```
549
579
  */
550
- function findSchema$1(node, { predicate, followRefs = false }) {
551
- const seen = followRefs ? /* @__PURE__ */ new WeakSet() : void 0;
552
- const walk = (current) => {
553
- if (current.kind === nodeKinds$1.schema) {
554
- if (predicate(current)) return current;
555
- if (seen) {
556
- if (seen.has(current)) return void 0;
557
- seen.add(current);
558
- if (current.type === "ref" && current.schema) {
559
- const found = walk(current.schema);
560
- if (found) return found;
561
- }
562
- }
563
- }
564
- let found;
580
+ function findSchema$1(node, { predicate, followRefs = false, cache }) {
581
+ const state = followRefs ? /* @__PURE__ */ new Map() : void 0;
582
+ const pending = [];
583
+ let order = 0;
584
+ const walkChildren = (current, initial) => {
585
+ let result = initial;
565
586
  visitChildNodes(current, (child) => {
566
- found = walk(child);
567
- return found !== void 0;
587
+ const next = walk(child);
588
+ result = next.found || next.low < result.low ? next : result;
589
+ return next.found !== void 0;
568
590
  });
569
- if (found) return found;
591
+ return result;
592
+ };
593
+ const walkSchema = (current) => {
594
+ const target = state && current.type === "ref" && current.schema ? walk(current.schema) : notFound;
595
+ if (target.found) return target;
596
+ return walkChildren(current, target);
570
597
  };
571
- return walk(node);
598
+ const walk = (current) => {
599
+ if (current.kind !== nodeKinds$1.schema) return walkChildren(current, notFound);
600
+ const cached = cache?.get(current);
601
+ if (cached === null) return notFound;
602
+ if (cached) return {
603
+ found: cached,
604
+ low: Number.POSITIVE_INFINITY
605
+ };
606
+ if (predicate(current)) {
607
+ cache?.set(current, current);
608
+ return {
609
+ found: current,
610
+ low: Number.POSITIVE_INFINITY
611
+ };
612
+ }
613
+ const known = state?.get(current);
614
+ if (known !== void 0) return {
615
+ found: void 0,
616
+ low: known
617
+ };
618
+ const index = order++;
619
+ const start = pending.length;
620
+ state?.set(current, index);
621
+ const result = walkSchema(current);
622
+ if (result.found) {
623
+ cache?.set(current, result.found);
624
+ return result;
625
+ }
626
+ if (!state) {
627
+ cache?.set(current, null);
628
+ return notFound;
629
+ }
630
+ if (result.low < index) {
631
+ state.set(current, result.low);
632
+ pending.push(current);
633
+ return result;
634
+ }
635
+ for (const done of [...pending.splice(start), current]) {
636
+ state.set(done, Number.POSITIVE_INFINITY);
637
+ cache?.set(done, null);
638
+ }
639
+ return notFound;
640
+ };
641
+ return walk(node).found;
572
642
  }
573
643
  __name(findSchema$1, "findSchema");
574
644
  /**
@@ -632,15 +702,16 @@ function transformChildren(node, visitor, recurse) {
632
702
  if (!keys) return node;
633
703
  const record = node;
634
704
  let updates;
635
- const done = /* @__PURE__ */ new Map();
705
+ let done;
636
706
  const transformOnce = (child) => {
707
+ done ??= /* @__PURE__ */ new Map();
637
708
  const next = done.get(child) ?? transformNode(child, visitor, recurse, node);
638
709
  done.set(child, next);
639
710
  return next;
640
711
  };
641
712
  for (const key of keys) {
642
- if (!(key in record)) continue;
643
713
  const value = record[key];
714
+ if (value === void 0 || value === null) continue;
644
715
  if (Array.isArray(value)) {
645
716
  let mapped;
646
717
  for (const [i, item] of value.entries()) {
@@ -697,7 +768,11 @@ __name(collect$1, "collect");
697
768
  function collectInto(node, visitor, recurse, parent, result) {
698
769
  const v = applyVisitor(node, visitor, parent);
699
770
  if (v != null) result.push(v);
700
- for (const child of getChildren(node, recurse)) collectInto(child, visitor, recurse, node, result);
771
+ if (node.kind === nodeKinds$1.schema && !recurse) return;
772
+ visitChildNodes(node, (child) => {
773
+ collectInto(child, visitor, recurse, node, result);
774
+ return false;
775
+ });
701
776
  }
702
777
  // src/utils/refs.ts
703
778
  /**