@kubb/ast 5.12.8 → 5.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -241,24 +241,26 @@ function isNodeRecord(value) {
241
241
  * // returns parameters, the request body, and responses
242
242
  * ```
243
243
  */
244
- function* getChildren(node, recurse) {
244
+ function getChildren(node, recurse) {
245
+ const children = [];
245
246
  const keys = node.kind === "Schema" && !recurse ? void 0 : VISITOR_KEYS[node.kind];
246
- if (!keys) return;
247
+ if (!keys) return children;
247
248
  const record = node;
248
249
  for (const key of keys) {
249
250
  const value = record[key];
250
251
  if (Array.isArray(value)) {
251
- for (const item of value) if (isNode(item)) yield item;
252
+ for (const item of value) if (isNode(item)) children.push(item);
252
253
  continue;
253
254
  }
254
255
  if (isNode(value)) {
255
- yield value;
256
+ children.push(value);
256
257
  continue;
257
258
  }
258
259
  if (isNodeRecord(value)) {
259
- for (const item of Object.values(value)) if (isNode(item)) yield item;
260
+ for (const item of Object.values(value)) if (isNode(item)) children.push(item);
260
261
  }
261
262
  }
263
+ return children;
262
264
  }
263
265
  /**
264
266
  * Runs the visitor callback that matches `node.kind` with the traversal
@@ -395,7 +397,15 @@ function* collectNode(node, visitor, recurse, parent) {
395
397
  * ```
396
398
  */
397
399
  function collectSync(node, options) {
398
- return Array.from(collect(node, options));
400
+ const { depth, parent, ...visitor } = options;
401
+ const result = [];
402
+ collectInto(node, visitor, (depth ?? "deep") === "deep", parent, result);
403
+ return result;
404
+ }
405
+ function collectInto(node, visitor, recurse, parent, result) {
406
+ const v = applyVisitor(node, visitor, parent);
407
+ if (v != null) result.push(v);
408
+ for (const child of getChildren(node, recurse)) collectInto(child, visitor, recurse, node, result);
399
409
  }
400
410
  //#endregion
401
411
  //#region src/defineMacro.ts
@@ -634,6 +644,14 @@ function resolveRefName(node) {
634
644
  if (node.ref) return refName(node.ref);
635
645
  return node.name ?? node.schema?.name ?? null;
636
646
  }
647
+ /** Fields of a ref node that describe the ref itself, so they never override the resolved schema. */
648
+ const REF_OWN_KEYS = /* @__PURE__ */ new Set([
649
+ "kind",
650
+ "type",
651
+ "name",
652
+ "ref",
653
+ "schema"
654
+ ]);
637
655
  /**
638
656
  * Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
639
657
  *
@@ -650,12 +668,12 @@ function resolveRefName(node) {
650
668
  */
651
669
  function mergeRefWithSchema(node) {
652
670
  if (!node.schema) return node;
653
- const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = node;
654
- const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== void 0));
655
- return {
656
- ...node.schema,
657
- ...definedOverrides
658
- };
671
+ const merged = { ...node.schema };
672
+ for (const key of Object.keys(node)) {
673
+ const value = node[key];
674
+ if (value !== void 0 && !REF_OWN_KEYS.has(key)) merged[key] = value;
675
+ }
676
+ return merged;
659
677
  }
660
678
  /**
661
679
  * Returns `true` when a schema is a bare ref without modifiers, metadata, or omit keys.
@@ -1109,16 +1127,23 @@ function mergeByPath(result) {
1109
1127
  return ({ node, names }) => {
1110
1128
  const key = `${node.path}:${node.isTypeOnly ?? false}`;
1111
1129
  const existing = byPath.get(key);
1112
- if (existing && Array.isArray(existing.name)) {
1113
- existing.name = [.../* @__PURE__ */ new Set([...existing.name, ...names])];
1130
+ if (existing) {
1131
+ for (const name of names) {
1132
+ if (existing.seen.has(name)) continue;
1133
+ existing.seen.add(name);
1134
+ existing.names.push(name);
1135
+ }
1114
1136
  return;
1115
1137
  }
1116
1138
  const item = {
1117
1139
  ...node,
1118
- name: names
1140
+ name: [...names]
1119
1141
  };
1120
1142
  result.push(item);
1121
- byPath.set(key, item);
1143
+ byPath.set(key, {
1144
+ names: item.name,
1145
+ seen: new Set(names)
1146
+ });
1122
1147
  };
1123
1148
  }
1124
1149
  /**