@kubb/ast 5.0.0-beta.103 → 5.0.0-beta.104

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
@@ -1207,7 +1207,7 @@ function chain({ macros, key, node, context }) {
1207
1207
  for (const macro of macros) {
1208
1208
  const callback = macro[key];
1209
1209
  if (!callback) continue;
1210
- if (macro.when && !macro.when(current)) continue;
1210
+ if (macro.match && !macro.match(current)) continue;
1211
1211
  const next = callback(current, context);
1212
1212
  if (next != null) current = next;
1213
1213
  }
@@ -1394,6 +1394,36 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1394
1394
  for (const name of collectSchemaRefs(node)) out.add(name);
1395
1395
  return out;
1396
1396
  }
1397
+ /**
1398
+ * Collects the de-duplicated target names of every pointer-carrying ref in a node's subtree, in
1399
+ * first-occurrence order. The walk is memoized by node identity, so the subtree is scanned once and
1400
+ * `resolver.imports` reads the same result across the ts, zod, and faker plugins instead of
1401
+ * re-scanning the same schema per plugin.
1402
+ *
1403
+ * Only refs that carry a `$ref` pointer count, so a synthesized ref pointing at a sibling in the
1404
+ * same file (a union member created by name) is left out. That leaves exactly the set
1405
+ * `resolver.imports` emits. This is the ordered, import-facing counterpart to
1406
+ * {@link collectReferencedSchemaNames}, which returns an unordered set for graph analysis.
1407
+ *
1408
+ * @example
1409
+ * ```ts
1410
+ * collectImportedRefNames(petSchema)
1411
+ * // ['Category', 'Tag']
1412
+ * ```
1413
+ */
1414
+ const collectImportedRefNames = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
1415
+ const seen = /* @__PURE__ */ new Set();
1416
+ const names = [];
1417
+ collectSync(node, { schema(child) {
1418
+ if (child.type !== "ref" || !child.ref) return;
1419
+ const name = resolveRefName(child);
1420
+ if (name && !seen.has(name)) {
1421
+ seen.add(name);
1422
+ names.push(name);
1423
+ }
1424
+ } });
1425
+ return names;
1426
+ });
1397
1427
  function computeUsedSchemaNames(operations, schemas) {
1398
1428
  const schemaMap = /* @__PURE__ */ new Map();
1399
1429
  for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
@@ -1436,12 +1466,24 @@ function collectUsedSchemaNames(operations, schemas) {
1436
1466
  return computeUsedSchemaNames(operations, schemas);
1437
1467
  }
1438
1468
  const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
1439
- const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
1440
- const graph = /* @__PURE__ */ new Map();
1441
- for (const schema of schemas) {
1442
- if (!schema.name) continue;
1443
- graph.set(schema.name, collectReferencedSchemaNames(schema));
1444
- }
1469
+ /**
1470
+ * Finds every schema that takes part in a circular dependency chain in a schema dependency graph
1471
+ * that maps each schema name to the names it references directly.
1472
+ *
1473
+ * Use this when the graph was already collected during another pass (e.g. the adapter's convert
1474
+ * walk), so the schema nodes are not swept a second time. `findCircularSchemas` builds the graph
1475
+ * from schema nodes and delegates here.
1476
+ *
1477
+ * @example
1478
+ * ```ts
1479
+ * const graph = new Map([
1480
+ * ['Pet', new Set(['Category'])],
1481
+ * ['Category', new Set(['Pet'])],
1482
+ * ])
1483
+ * findCircularSchemasFromGraph(graph) // Set { 'Pet', 'Category' }
1484
+ * ```
1485
+ */
1486
+ function findCircularSchemasFromGraph(graph) {
1445
1487
  const circular = /* @__PURE__ */ new Set();
1446
1488
  for (const start of graph.keys()) {
1447
1489
  const visited = /* @__PURE__ */ new Set();
@@ -1459,6 +1501,14 @@ const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas)
1459
1501
  }
1460
1502
  }
1461
1503
  return circular;
1504
+ }
1505
+ const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
1506
+ const graph = /* @__PURE__ */ new Map();
1507
+ for (const schema of schemas) {
1508
+ if (!schema.name) continue;
1509
+ graph.set(schema.name, collectReferencedSchemaNames(schema));
1510
+ }
1511
+ return findCircularSchemasFromGraph(graph);
1462
1512
  });
1463
1513
  /**
1464
1514
  * Finds every schema that takes part in a circular dependency chain, including direct self-loops.
@@ -1528,6 +1578,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1528
1578
  arrowFunctionDef: () => arrowFunctionDef,
1529
1579
  breakDef: () => breakDef,
1530
1580
  collect: () => collect,
1581
+ collectImportedRefNames: () => collectImportedRefNames,
1531
1582
  collectSync: () => collectSync,
1532
1583
  collectUsedSchemaNames: () => collectUsedSchemaNames,
1533
1584
  combineExports: () => combineExports,
@@ -1544,6 +1595,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1544
1595
  factory: () => factory_exports,
1545
1596
  fileDef: () => fileDef,
1546
1597
  findCircularSchemas: () => findCircularSchemas,
1598
+ findCircularSchemasFromGraph: () => findCircularSchemasFromGraph,
1547
1599
  functionDef: () => functionDef,
1548
1600
  importDef: () => importDef,
1549
1601
  inputDef: () => inputDef,
@@ -1577,6 +1629,7 @@ Object.defineProperty(exports, "ast", {
1577
1629
  });
1578
1630
  exports.breakDef = breakDef;
1579
1631
  exports.collect = collect;
1632
+ exports.collectImportedRefNames = collectImportedRefNames;
1580
1633
  exports.collectSync = collectSync;
1581
1634
  exports.collectUsedSchemaNames = collectUsedSchemaNames;
1582
1635
  exports.combineExports = combineExports;
@@ -1598,6 +1651,7 @@ Object.defineProperty(exports, "factory", {
1598
1651
  });
1599
1652
  exports.fileDef = fileDef;
1600
1653
  exports.findCircularSchemas = findCircularSchemas;
1654
+ exports.findCircularSchemasFromGraph = findCircularSchemasFromGraph;
1601
1655
  exports.functionDef = functionDef;
1602
1656
  exports.importDef = importDef;
1603
1657
  exports.inputDef = inputDef;