@kubb/ast 5.0.0-beta.13 → 5.0.0-beta.14

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
@@ -533,32 +533,36 @@ function createLimit(concurrency) {
533
533
  * // returns parameters, requestBody schema (if present), and responses
534
534
  * ```
535
535
  */
536
- function getChildren(node, recurse) {
536
+ function* getChildren(node, recurse) {
537
537
  switch (node.kind) {
538
- case "Input": return [...node.schemas, ...node.operations];
539
- case "Output": return [];
540
- case "Operation": return [
541
- ...node.parameters,
542
- ...node.requestBody?.content?.flatMap((c) => c.schema ? [c.schema] : []) ?? [],
543
- ...node.responses
544
- ];
545
- case "Schema": {
546
- const children = [];
547
- if (!recurse) return [];
548
- if ("properties" in node && node.properties.length > 0) children.push(...node.properties);
549
- if ("items" in node && node.items) children.push(...node.items);
550
- if ("members" in node && node.members) children.push(...node.members);
551
- if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties);
552
- return children;
553
- }
554
- case "Property": return [node.schema];
555
- case "Parameter": return [node.schema];
556
- case "Response": return node.schema ? [node.schema] : [];
557
- case "FunctionParameter":
558
- case "ParameterGroup":
559
- case "FunctionParameters":
560
- case "Type": return [];
561
- default: return [];
538
+ case "Input":
539
+ yield* node.schemas;
540
+ yield* node.operations;
541
+ break;
542
+ case "Output": break;
543
+ case "Operation":
544
+ yield* node.parameters;
545
+ if (node.requestBody?.content) {
546
+ for (const c of node.requestBody.content) if (c.schema) yield c.schema;
547
+ }
548
+ yield* node.responses;
549
+ break;
550
+ case "Schema":
551
+ if (!recurse) break;
552
+ if ("properties" in node && node.properties.length > 0) yield* node.properties;
553
+ if ("items" in node && node.items) yield* node.items;
554
+ if ("members" in node && node.members) yield* node.members;
555
+ if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) yield node.additionalProperties;
556
+ break;
557
+ case "Property":
558
+ yield node.schema;
559
+ break;
560
+ case "Parameter":
561
+ yield node.schema;
562
+ break;
563
+ case "Response":
564
+ if (node.schema) yield node.schema;
565
+ break;
562
566
  }
563
567
  }
564
568
  /**
@@ -745,10 +749,9 @@ function transform(node, options) {
745
749
  * const values = collect(root, { depth: 'shallow', root: () => 'root' })
746
750
  * ```
747
751
  */
748
- function collect(node, options) {
752
+ function* collectLazy(node, options) {
749
753
  const { depth, parent, ...visitor } = options;
750
754
  const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
751
- const results = [];
752
755
  let v;
753
756
  switch (node.kind) {
754
757
  case "Input":
@@ -776,12 +779,14 @@ function collect(node, options) {
776
779
  case "ParameterGroup":
777
780
  case "FunctionParameters": break;
778
781
  }
779
- if (v !== void 0) results.push(v);
780
- for (const child of getChildren(node, recurse)) for (const item of collect(child, {
782
+ if (v !== void 0) yield v;
783
+ for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
781
784
  ...options,
782
785
  parent: node
783
- })) results.push(item);
784
- return results;
786
+ });
787
+ }
788
+ function collect(node, options) {
789
+ return Array.from(collectLazy(node, options));
785
790
  }
786
791
  //#endregion
787
792
  //#region src/utils.ts
@@ -1331,7 +1336,7 @@ function collectUsedSchemaNames(operations, schemas) {
1331
1336
  if (namedSchema) visitSchema(namedSchema);
1332
1337
  }
1333
1338
  }
1334
- for (const op of operations) for (const schema of collect(op, {
1339
+ for (const op of operations) for (const schema of collectLazy(op, {
1335
1340
  depth: "shallow",
1336
1341
  schema: (node) => node
1337
1342
  })) visitSchema(schema);
@@ -1380,11 +1385,12 @@ function findCircularSchemas(schemas) {
1380
1385
  */
1381
1386
  function containsCircularRef(node, { circularSchemas, excludeName }) {
1382
1387
  if (!node || circularSchemas.size === 0) return false;
1383
- return collect(node, { schema(child) {
1388
+ for (const _ of collectLazy(node, { schema(child) {
1384
1389
  if (child.type !== "ref") return void 0;
1385
1390
  const name = resolveRefName(child);
1386
1391
  return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
1387
- } }).length > 0;
1392
+ } })) return true;
1393
+ return false;
1388
1394
  }
1389
1395
  //#endregion
1390
1396
  //#region src/factory.ts
@@ -2219,6 +2225,7 @@ exports.caseParams = caseParams;
2219
2225
  exports.childName = childName;
2220
2226
  exports.collect = collect;
2221
2227
  exports.collectImports = collectImports;
2228
+ exports.collectLazy = collectLazy;
2222
2229
  exports.collectReferencedSchemaNames = collectReferencedSchemaNames;
2223
2230
  exports.collectUsedSchemaNames = collectUsedSchemaNames;
2224
2231
  exports.containsCircularRef = containsCircularRef;