@kubb/ast 5.0.0-beta.18 → 5.0.0-beta.19

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.d.ts CHANGED
@@ -3229,13 +3229,6 @@ declare function syncSchemaRef(node: SchemaNode): SchemaNode;
3229
3229
  * types, returns `true` only when `representation` is `'string'` rather than `'date'`.
3230
3230
  */
3231
3231
  declare function isStringType(node: SchemaNode): boolean;
3232
- /**
3233
- * Applies casing rules to parameter names and returns a new parameter array.
3234
- *
3235
- * Use this before passing parameters to schema builders so output property keys match
3236
- * the desired casing while preserving `OperationNode.parameters` for other consumers.
3237
- * The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
3238
- */
3239
3232
  declare function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode>;
3240
3233
  /**
3241
3234
  * Creates a single-property object schema used as a discriminator literal.
@@ -3407,34 +3400,6 @@ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): st
3407
3400
  */
3408
3401
  declare function resolveRefName(node: SchemaNode | undefined): string | undefined;
3409
3402
  declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
3410
- /**
3411
- * Collects the names of all top-level schemas transitively used by a set of operations.
3412
- *
3413
- * An operation uses a schema when any of its parameters, request body content, or responses
3414
- * reference it — directly or indirectly through other named schemas.
3415
- * The walk is iterative and safe against reference cycles.
3416
- *
3417
- * Use this together with `include` filters to determine which schemas from `components/schemas`
3418
- * are reachable from the allowed operations, so that schemas used only by excluded operations
3419
- * are not generated.
3420
- *
3421
- * @example Only generate schemas referenced by included operations
3422
- * ```ts
3423
- * const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
3424
- * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
3425
- *
3426
- * for (const schema of inputNode.schemas) {
3427
- * if (schema.name && !allowed.has(schema.name)) continue
3428
- * // … generate schema
3429
- * }
3430
- * ```
3431
- *
3432
- * @example Check whether a specific schema is needed
3433
- * ```ts
3434
- * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
3435
- * allowed.has('OrderStatus') // false when no included operation references OrderStatus
3436
- * ```
3437
- */
3438
3403
  declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
3439
3404
  /**
3440
3405
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
package/dist/index.js CHANGED
@@ -600,9 +600,7 @@ function transform(node, options) {
600
600
  const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
601
601
  switch (node.kind) {
602
602
  case "Input": {
603
- let input = node;
604
- const replaced = visitor.input?.(input, { parent });
605
- if (replaced) input = replaced;
603
+ const input = visitor.input?.(node, { parent }) ?? node;
606
604
  return {
607
605
  ...input,
608
606
  schemas: input.schemas.map((s) => transform(s, {
@@ -615,16 +613,9 @@ function transform(node, options) {
615
613
  }))
616
614
  };
617
615
  }
618
- case "Output": {
619
- let output = node;
620
- const replaced = visitor.output?.(output, { parent });
621
- if (replaced) output = replaced;
622
- return output;
623
- }
616
+ case "Output": return visitor.output?.(node, { parent }) ?? node;
624
617
  case "Operation": {
625
- let op = node;
626
- const replaced = visitor.operation?.(op, { parent });
627
- if (replaced) op = replaced;
618
+ const op = visitor.operation?.(node, { parent }) ?? node;
628
619
  return {
629
620
  ...op,
630
621
  parameters: op.parameters.map((p) => transform(p, {
@@ -648,9 +639,7 @@ function transform(node, options) {
648
639
  };
649
640
  }
650
641
  case "Schema": {
651
- let schema = node;
652
- const replaced = visitor.schema?.(schema, { parent });
653
- if (replaced) schema = replaced;
642
+ const schema = visitor.schema?.(node, { parent }) ?? node;
654
643
  const childOptions = {
655
644
  ...options,
656
645
  parent: schema
@@ -664,9 +653,7 @@ function transform(node, options) {
664
653
  };
665
654
  }
666
655
  case "Property": {
667
- let prop = node;
668
- const replaced = visitor.property?.(prop, { parent });
669
- if (replaced) prop = replaced;
656
+ const prop = visitor.property?.(node, { parent }) ?? node;
670
657
  return createProperty({
671
658
  ...prop,
672
659
  schema: transform(prop.schema, {
@@ -676,9 +663,7 @@ function transform(node, options) {
676
663
  });
677
664
  }
678
665
  case "Parameter": {
679
- let param = node;
680
- const replaced = visitor.parameter?.(param, { parent });
681
- if (replaced) param = replaced;
666
+ const param = visitor.parameter?.(node, { parent }) ?? node;
682
667
  return createParameter({
683
668
  ...param,
684
669
  schema: transform(param.schema, {
@@ -688,9 +673,7 @@ function transform(node, options) {
688
673
  });
689
674
  }
690
675
  case "Response": {
691
- let response = node;
692
- const replaced = visitor.response?.(response, { parent });
693
- if (replaced) response = replaced;
676
+ const response = visitor.response?.(node, { parent }) ?? node;
694
677
  return {
695
678
  ...response,
696
679
  schema: transform(response.schema, {
@@ -817,15 +800,25 @@ function isStringType(node) {
817
800
  * the desired casing while preserving `OperationNode.parameters` for other consumers.
818
801
  * The input array is not mutated. When `casing` is not set, the original array is returned unchanged.
819
802
  */
803
+ const caseParamsCache = /* @__PURE__ */ new WeakMap();
820
804
  function caseParams(params, casing) {
821
805
  if (!casing) return params;
822
- return params.map((param) => {
806
+ let byParams = caseParamsCache.get(params);
807
+ if (!byParams) {
808
+ byParams = /* @__PURE__ */ new Map();
809
+ caseParamsCache.set(params, byParams);
810
+ }
811
+ const cached = byParams.get(casing);
812
+ if (cached) return cached;
813
+ const result = params.map((param) => {
823
814
  const transformed = casing === "camelcase" || !isValidVarName(param.name) ? camelCase(param.name) : param.name;
824
815
  return {
825
816
  ...param,
826
817
  name: transformed
827
818
  };
828
819
  });
820
+ byParams.set(casing, result);
821
+ return result;
829
822
  }
830
823
  /**
831
824
  * Creates a single-property object schema used as a discriminator literal.
@@ -1310,7 +1303,15 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1310
1303
  * allowed.has('OrderStatus') // false when no included operation references OrderStatus
1311
1304
  * ```
1312
1305
  */
1306
+ const usedSchemaNamesCache = /* @__PURE__ */ new WeakMap();
1313
1307
  function collectUsedSchemaNames(operations, schemas) {
1308
+ let byOps = usedSchemaNamesCache.get(operations);
1309
+ if (!byOps) {
1310
+ byOps = /* @__PURE__ */ new WeakMap();
1311
+ usedSchemaNamesCache.set(operations, byOps);
1312
+ }
1313
+ const cached = byOps.get(schemas);
1314
+ if (cached) return cached;
1314
1315
  const schemaMap = /* @__PURE__ */ new Map();
1315
1316
  for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
1316
1317
  const result = /* @__PURE__ */ new Set();
@@ -1326,8 +1327,11 @@ function collectUsedSchemaNames(operations, schemas) {
1326
1327
  depth: "shallow",
1327
1328
  schema: (node) => node
1328
1329
  })) visitSchema(schema);
1330
+ byOps.set(schemas, result);
1329
1331
  return result;
1330
1332
  }
1333
+ const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
1334
+ const circularSchemaCache = /* @__PURE__ */ new WeakMap();
1331
1335
  /**
1332
1336
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
1333
1337
  *
@@ -1338,6 +1342,9 @@ function collectUsedSchemaNames(operations, schemas) {
1338
1342
  * @note Call this once on the full schema graph, then use `containsCircularRef()` to check individual schemas.
1339
1343
  */
1340
1344
  function findCircularSchemas(schemas) {
1345
+ if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
1346
+ const cached = circularSchemaCache.get(schemas);
1347
+ if (cached) return cached;
1341
1348
  const graph = /* @__PURE__ */ new Map();
1342
1349
  for (const schema of schemas) {
1343
1350
  if (!schema.name) continue;
@@ -1359,6 +1366,7 @@ function findCircularSchemas(schemas) {
1359
1366
  if (next) for (const r of next) stack.push(r);
1360
1367
  }
1361
1368
  }
1369
+ circularSchemaCache.set(schemas, circular);
1362
1370
  return circular;
1363
1371
  }
1364
1372
  /**