@kubb/ast 5.0.0-alpha.7 → 5.0.0-alpha.71

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/src/utils.ts CHANGED
@@ -1,18 +1,62 @@
1
1
  import { camelCase, isValidVarName } from '@internals/utils'
2
2
 
3
+ import { createFunctionParameter, createFunctionParameters, createParameterGroup, createParamsType, createProperty, createSchema } from './factory.ts'
3
4
  import { narrowSchema } from './guards.ts'
4
- import type { ParameterNode, SchemaNode } from './nodes/index.ts'
5
+ import type {
6
+ CodeNode,
7
+ ExportNode,
8
+ FunctionParameterNode,
9
+ FunctionParametersNode,
10
+ ImportNode,
11
+ OperationNode,
12
+ ParameterGroupNode,
13
+ ParameterNode,
14
+ ParamsTypeNode,
15
+ SchemaNode,
16
+ SourceNode,
17
+ } from './nodes/index.ts'
5
18
  import type { SchemaType } from './nodes/schema.ts'
19
+ import { extractRefName } from './refs.ts'
20
+ import { collect } from './visitor.ts'
6
21
 
7
- const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'])
22
+ const plainStringTypes = new Set<SchemaType>(['string', 'uuid', 'email', 'url', 'datetime'] as const)
8
23
 
9
24
  /**
10
- * Returns `true` when a schema node will be represented as a plain string in generated code.
25
+ * Returns a merged schema view for a ref node, combining the resolved `node.schema`
26
+ * (base from the referenced definition) with any usage-site sibling fields set directly
27
+ * on the ref node (description, readOnly, nullable, deprecated, etc.).
28
+ *
29
+ * Usage-site fields take precedence over the resolved schema's own fields when both are defined.
30
+ *
31
+ * For non-ref nodes the node itself is returned unchanged.
32
+ */
33
+ export function syncSchemaRef(node: SchemaNode): SchemaNode {
34
+ const ref = narrowSchema(node, 'ref')
35
+
36
+ if (!ref) return node
37
+ if (!ref.schema) return node
38
+
39
+ const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref
40
+
41
+ // Filter out undefined override values so they don't shadow the resolved schema's fields.
42
+ const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== undefined))
43
+
44
+ return createSchema({ ...ref.schema, ...definedOverrides })
45
+ }
46
+
47
+ /**
48
+ * Returns `true` when a schema is emitted as a plain `string` type.
11
49
  *
12
50
  * - `string`, `uuid`, `email`, `url`, `datetime` are always plain strings.
13
51
  * - `date` and `time` are plain strings when their `representation` is `'string'` rather than `'date'`.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * isStringType(createSchema({ type: 'uuid' })) // true
56
+ * isStringType(createSchema({ type: 'date', representation: 'date' })) // false
57
+ * ```
14
58
  */
15
- export function isPlainStringType(node: SchemaNode): boolean {
59
+ export function isStringType(node: SchemaNode): boolean {
16
60
  if (plainStringTypes.has(node.type)) {
17
61
  return true
18
62
  }
@@ -26,16 +70,23 @@ export function isPlainStringType(node: SchemaNode): boolean {
26
70
  }
27
71
 
28
72
  /**
29
- * Transforms the `name` field of each parameter node according to the given casing strategy.
73
+ * Applies casing rules to parameter names and returns a new parameter array.
30
74
  *
31
- * The original `params` array is never mutated — a new array of cloned nodes is returned.
32
- * When no `casing` is provided the original array is returned as-is.
75
+ * The input array is not mutated.
76
+ * If `casing` is not set, the original array is returned unchanged.
33
77
  *
34
78
  * Use this before passing parameters to schema builders so that property keys
35
- * in the generated output match the desired casing while the original
36
- * `OperationNode.parameters` array remains untouched for other consumers.
79
+ * in generated output match the desired casing while preserving
80
+ * `OperationNode.parameters` for other consumers.
81
+ *
82
+ * @example
83
+ * ```ts
84
+ * const params = [createParameter({ name: 'pet_id', in: 'query', schema: createSchema({ type: 'string' }) })]
85
+ * const cased = caseParams(params, 'camelcase')
86
+ * // cased[0].name === 'petId'
87
+ * ```
37
88
  */
38
- export function applyParamsCasing(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
89
+ export function caseParams(params: Array<ParameterNode>, casing: 'camelcase' | undefined): Array<ParameterNode> {
39
90
  if (!casing) {
40
91
  return params
41
92
  }
@@ -46,3 +97,777 @@ export function applyParamsCasing(params: Array<ParameterNode>, casing: 'camelca
46
97
  return { ...param, name: transformed }
47
98
  })
48
99
  }
100
+
101
+ /**
102
+ * Creates a single-property object schema used as a discriminator literal.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * createDiscriminantNode({ propertyName: 'type', value: 'dog' })
107
+ * // -> { type: 'object', properties: [{ name: 'type', required: true, schema: enum('dog') }] }
108
+ * ```
109
+ */
110
+ export function createDiscriminantNode({ propertyName, value }: { propertyName: string; value: string }): SchemaNode {
111
+ return createSchema({
112
+ type: 'object',
113
+ primitive: 'object',
114
+ properties: [
115
+ createProperty({
116
+ name: propertyName,
117
+ schema: createSchema({
118
+ type: 'enum',
119
+ primitive: 'string',
120
+ enumValues: [value],
121
+ }),
122
+ required: true,
123
+ }),
124
+ ],
125
+ })
126
+ }
127
+
128
+ /**
129
+ * Named type for a group of parameters (query or header) emitted as a single typed parameter.
130
+ */
131
+ export type ParamGroupType = {
132
+ /**
133
+ * TypeNode for the group type.
134
+ */
135
+ type: ParamsTypeNode
136
+ /**
137
+ * Whether the parameter group is optional.
138
+ */
139
+ optional: boolean
140
+ }
141
+
142
+ /**
143
+ * Resolver interface for {@link createOperationParams}.
144
+ *
145
+ * `ResolverTs` from `@kubb/plugin-ts` satisfies this interface and can be passed directly.
146
+ */
147
+ export type OperationParamsResolver = {
148
+ /**
149
+ * Resolves the type name for an individual parameter.
150
+ *
151
+ * @example Individual path parameter name
152
+ * `resolver.resolveParamName(node, param) // → 'DeletePetPathPetId'`
153
+ */
154
+ resolveParamName(node: OperationNode, param: ParameterNode): string
155
+ /**
156
+ * Resolves the request body type name.
157
+ *
158
+ * @example Request body type name
159
+ * `resolver.resolveDataName(node) // → 'CreatePetData'`
160
+ */
161
+ resolveDataName(node: OperationNode): string
162
+ /**
163
+ * Resolves the grouped path parameters type name.
164
+ * When the return value equals `resolveParamName`, no indexed access is emitted.
165
+ *
166
+ * @example Grouped path params type name
167
+ * `resolver.resolvePathParamsName(node, param) // → 'DeletePetPathParams'`
168
+ */
169
+ resolvePathParamsName(node: OperationNode, param: ParameterNode): string
170
+ /**
171
+ * Resolves the grouped query parameters type name.
172
+ * When the return value equals `resolveParamName`, an inline struct type is emitted instead.
173
+ *
174
+ * @example Grouped query params type name
175
+ * `resolver.resolveQueryParamsName(node, param) // → 'FindPetsByStatusQueryParams'`
176
+ */
177
+ resolveQueryParamsName(node: OperationNode, param: ParameterNode): string
178
+ /**
179
+ * Resolves the grouped header parameters type name.
180
+ * When the return value equals `resolveParamName`, an inline struct type is emitted instead.
181
+ *
182
+ * @example Grouped header params type name
183
+ * `resolver.resolveHeaderParamsName(node, param) // → 'DeletePetHeaderParams'`
184
+ */
185
+ resolveHeaderParamsName(node: OperationNode, param: ParameterNode): string
186
+ }
187
+
188
+ /**
189
+ * Options for {@link createOperationParams}.
190
+ */
191
+ export type CreateOperationParamsOptions = {
192
+ /**
193
+ * How all operation parameters are grouped in the function signature.
194
+ * - `'object'` wraps all params into a single destructured object `{ petId, data, params }`
195
+ * - `'inline'` emits each param category as a separate top-level parameter
196
+ */
197
+ paramsType: 'object' | 'inline'
198
+ /**
199
+ * How path parameters are emitted when `paramsType` is `'inline'`.
200
+ * - `'object'` groups them as `{ petId, storeId }: PathParams`
201
+ * - `'inline'` spreads them as individual parameters `petId: string, storeId: string`
202
+ * - `'inlineSpread'` emits a single rest parameter `...pathParams: PathParams`
203
+ */
204
+ pathParamsType: 'object' | 'inline' | 'inlineSpread'
205
+ /**
206
+ * Converts parameter names to camelCase before output.
207
+ */
208
+ paramsCasing?: 'camelcase'
209
+ /**
210
+ * Resolver for parameter and request body type names.
211
+ * Pass `ResolverTs` from `@kubb/plugin-ts` directly.
212
+ * When omitted, falls back to the schema primitive or `'unknown'`.
213
+ */
214
+ resolver?: OperationParamsResolver
215
+ /**
216
+ * Default value for the path parameters binding when `pathParamsType` is `'object'`.
217
+ * Falls back to `'{}'` when all path params are optional.
218
+ */
219
+ pathParamsDefault?: string
220
+ /**
221
+ * Extra parameters appended after the standard operation parameters.
222
+ *
223
+ * @example Plugin-specific trailing parameter
224
+ * ```ts
225
+ * extraParams: [createFunctionParameter({ name: 'options', type: 'Partial<RequestOptions>', default: '{}' })]
226
+ * ```
227
+ */
228
+ extraParams?: Array<FunctionParameterNode | ParameterGroupNode>
229
+ /**
230
+ * Override the default parameter names used for body, query, header, and rest-path groups.
231
+ *
232
+ * Useful when targeting languages or frameworks with different naming conventions.
233
+ *
234
+ * @default { data: 'data', params: 'params', headers: 'headers', path: 'pathParams' }
235
+ */
236
+ paramNames?: {
237
+ /**
238
+ * Name for the request body parameter.
239
+ * @default 'data'
240
+ */
241
+ data?: string
242
+ /**
243
+ * Name for the query parameters group parameter.
244
+ * @default 'params'
245
+ */
246
+ params?: string
247
+ /**
248
+ * Name for the header parameters group parameter.
249
+ * @default 'headers'
250
+ */
251
+ headers?: string
252
+ /**
253
+ * Name for the rest path-parameters parameter when `pathParamsType` is `'inlineSpread'`.
254
+ * @default 'pathParams'
255
+ */
256
+ path?: string
257
+ }
258
+ /**
259
+ * Applies a uniform transformation to every resolved type name before it is used
260
+ * in a parameter node. Use this for framework-level type wrappers.
261
+ *
262
+ * @example Vue Query — wrap every parameter type with `MaybeRefOrGetter`
263
+ * `typeWrapper: (t) => \`MaybeRefOrGetter<${t}>\``
264
+ */
265
+ typeWrapper?: (type: string) => string
266
+ }
267
+
268
+ function resolveParamsType({
269
+ node,
270
+ param,
271
+ resolver,
272
+ }: {
273
+ node: OperationNode
274
+ param: ParameterNode
275
+ resolver: OperationParamsResolver | undefined
276
+ }): ParamsTypeNode {
277
+ if (!resolver) {
278
+ return createParamsType({
279
+ variant: 'reference',
280
+ name: param.schema.primitive ?? 'unknown',
281
+ })
282
+ }
283
+
284
+ const individualName = resolver.resolveParamName(node, param)
285
+
286
+ const groupLocation = param.in === 'path' || param.in === 'query' || param.in === 'header' ? param.in : undefined
287
+
288
+ const groupResolvers = {
289
+ path: resolver.resolvePathParamsName,
290
+ query: resolver.resolveQueryParamsName,
291
+ header: resolver.resolveHeaderParamsName,
292
+ } as const
293
+
294
+ const groupName = groupLocation ? groupResolvers[groupLocation].call(resolver, node, param) : undefined
295
+
296
+ if (groupName && groupName !== individualName) {
297
+ return createParamsType({
298
+ variant: 'member',
299
+ base: groupName,
300
+ key: param.name,
301
+ })
302
+ }
303
+
304
+ return createParamsType({ variant: 'reference', name: individualName })
305
+ }
306
+
307
+ /**
308
+ * Converts an {@link OperationNode} into a {@link FunctionParametersNode}.
309
+ *
310
+ * Centralizes the per-plugin `getParams()` pattern. Provide a `resolver` for
311
+ * type resolution and `extraParams` for plugin-specific trailing parameters.
312
+ *
313
+ * @example
314
+ * ```ts
315
+ * const params = createOperationParams(node, {
316
+ * paramsType: 'inline',
317
+ * pathParamsType: 'inline',
318
+ * resolver: tsResolver,
319
+ * extraParams: [createFunctionParameter({ name: 'options', type: createParamsType({ variant: 'reference', name: 'Partial<RequestOptions>' }), default: '{}' })],
320
+ * })
321
+ * ```
322
+ */
323
+ export function createOperationParams(node: OperationNode, options: CreateOperationParamsOptions): FunctionParametersNode {
324
+ const { paramsType, pathParamsType, paramsCasing, resolver, pathParamsDefault, extraParams = [], paramNames, typeWrapper } = options
325
+
326
+ const dataName = paramNames?.data ?? 'data'
327
+ const paramsName = paramNames?.params ?? 'params'
328
+ const headersName = paramNames?.headers ?? 'headers'
329
+ const pathName = paramNames?.path ?? 'pathParams'
330
+
331
+ const wrapType = (type: string): ParamsTypeNode =>
332
+ createParamsType({
333
+ variant: 'reference',
334
+ name: typeWrapper ? typeWrapper(type) : type,
335
+ })
336
+ // Only reference-variant TypeNodes are wrapped — they hold a plain type name string that needs casing applied.
337
+ // Member and struct TypeNodes are pre-resolved structured expressions and are passed through unchanged.
338
+ const wrapTypeNode = (type: ParamsTypeNode): ParamsTypeNode => (type.kind === 'ParamsType' && type.variant === 'reference' ? wrapType(type.name) : type)
339
+
340
+ const casedParams = caseParams(node.parameters, paramsCasing)
341
+ const pathParams = casedParams.filter((p) => p.in === 'path')
342
+ const queryParams = casedParams.filter((p) => p.in === 'query')
343
+ const headerParams = casedParams.filter((p) => p.in === 'header')
344
+
345
+ const bodyType = node.requestBody?.content?.[0]?.schema ? wrapType(resolver?.resolveDataName(node) ?? 'unknown') : undefined
346
+ const bodyRequired = node.requestBody?.required ?? false
347
+
348
+ const queryGroupType = resolver
349
+ ? resolveGroupType({
350
+ node,
351
+ params: queryParams,
352
+ groupMethod: resolver.resolveQueryParamsName,
353
+ resolver,
354
+ })
355
+ : undefined
356
+ const headerGroupType = resolver
357
+ ? resolveGroupType({
358
+ node,
359
+ params: headerParams,
360
+ groupMethod: resolver.resolveHeaderParamsName,
361
+ resolver,
362
+ })
363
+ : undefined
364
+
365
+ const params: Array<FunctionParameterNode | ParameterGroupNode> = []
366
+
367
+ if (paramsType === 'object') {
368
+ const children: Array<FunctionParameterNode> = [
369
+ ...pathParams.map((p) => {
370
+ const type = resolveParamsType({ node, param: p, resolver })
371
+ return createFunctionParameter({
372
+ name: p.name,
373
+ type: wrapTypeNode(type),
374
+ optional: !p.required,
375
+ })
376
+ }),
377
+ ...(bodyType
378
+ ? [
379
+ createFunctionParameter({
380
+ name: dataName,
381
+ type: bodyType,
382
+ optional: !bodyRequired,
383
+ }),
384
+ ]
385
+ : []),
386
+ ...buildGroupParam({
387
+ name: paramsName,
388
+ node,
389
+ params: queryParams,
390
+ groupType: queryGroupType,
391
+ resolver,
392
+ wrapType,
393
+ }),
394
+ ...buildGroupParam({
395
+ name: headersName,
396
+ node,
397
+ params: headerParams,
398
+ groupType: headerGroupType,
399
+ resolver,
400
+ wrapType,
401
+ }),
402
+ ]
403
+
404
+ if (children.length) {
405
+ params.push(
406
+ createParameterGroup({
407
+ properties: children,
408
+ default: children.every((c) => c.optional) ? '{}' : undefined,
409
+ }),
410
+ )
411
+ }
412
+ } else {
413
+ if (pathParams.length) {
414
+ if (pathParamsType === 'inlineSpread') {
415
+ const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]!) ?? undefined
416
+ params.push(
417
+ createFunctionParameter({
418
+ name: pathName,
419
+ type: spreadType ? wrapType(spreadType) : undefined,
420
+ rest: true,
421
+ }),
422
+ )
423
+ } else {
424
+ const pathChildren = pathParams.map((p) => {
425
+ const type = resolveParamsType({ node, param: p, resolver })
426
+ return createFunctionParameter({
427
+ name: p.name,
428
+ type: wrapTypeNode(type),
429
+ optional: !p.required,
430
+ })
431
+ })
432
+ params.push(
433
+ createParameterGroup({
434
+ properties: pathChildren,
435
+ inline: pathParamsType === 'inline',
436
+ default: pathParamsDefault ?? (pathChildren.every((c) => c.optional) ? '{}' : undefined),
437
+ }),
438
+ )
439
+ }
440
+ }
441
+
442
+ if (bodyType) {
443
+ params.push(
444
+ createFunctionParameter({
445
+ name: dataName,
446
+ type: bodyType,
447
+ optional: !bodyRequired,
448
+ }),
449
+ )
450
+ }
451
+
452
+ params.push(
453
+ ...buildGroupParam({
454
+ name: paramsName,
455
+ node,
456
+ params: queryParams,
457
+ groupType: queryGroupType,
458
+ resolver,
459
+ wrapType,
460
+ }),
461
+ )
462
+ params.push(
463
+ ...buildGroupParam({
464
+ name: headersName,
465
+ node,
466
+ params: headerParams,
467
+ groupType: headerGroupType,
468
+ resolver,
469
+ wrapType,
470
+ }),
471
+ )
472
+ }
473
+
474
+ params.push(...extraParams)
475
+
476
+ return createFunctionParameters({ params })
477
+ }
478
+
479
+ /**
480
+ * Builds a single {@link FunctionParameterNode} for a query or header group.
481
+ * Returns an empty array when there are no params to emit.
482
+ *
483
+ * If a pre-resolved `groupType` is provided it emits `name: GroupType`.
484
+ * Otherwise, it builds an inline struct from the individual params.
485
+ */
486
+ function buildGroupParam({
487
+ name,
488
+ node,
489
+ params,
490
+ groupType,
491
+ resolver,
492
+ wrapType,
493
+ }: {
494
+ name: string
495
+ node: OperationNode
496
+ params: Array<ParameterNode>
497
+ groupType: ParamGroupType | undefined
498
+ resolver: OperationParamsResolver | undefined
499
+ wrapType: (type: string) => ParamsTypeNode
500
+ }): Array<FunctionParameterNode> {
501
+ if (groupType) {
502
+ const type = groupType.type.kind === 'ParamsType' && groupType.type.variant === 'reference' ? wrapType(groupType.type.name) : groupType.type
503
+ return [createFunctionParameter({ name, type, optional: groupType.optional })]
504
+ }
505
+ if (params.length) {
506
+ return [
507
+ createFunctionParameter({
508
+ name,
509
+ type: toStructType({ node, params, resolver }),
510
+ optional: params.every((p) => !p.required),
511
+ }),
512
+ ]
513
+ }
514
+ return []
515
+ }
516
+
517
+ /**
518
+ * Derives a {@link ParamGroupType} from the resolver's group method.
519
+ * Returns `undefined` when the group name equals the individual param name (no real group).
520
+ */
521
+ function resolveGroupType({
522
+ node,
523
+ params,
524
+ groupMethod,
525
+ resolver,
526
+ }: {
527
+ node: OperationNode
528
+ params: Array<ParameterNode>
529
+ groupMethod: (_node: OperationNode, _param: ParameterNode) => string
530
+ resolver: OperationParamsResolver
531
+ }): ParamGroupType | undefined {
532
+ if (!params.length) {
533
+ return undefined
534
+ }
535
+ const firstParam = params[0]!
536
+ const groupName = groupMethod.call(resolver, node, firstParam)
537
+ if (groupName === resolver.resolveParamName(node, firstParam)) {
538
+ return undefined
539
+ }
540
+ const allOptional = params.every((p) => !p.required)
541
+ return {
542
+ type: createParamsType({ variant: 'reference', name: groupName }),
543
+ optional: allOptional,
544
+ }
545
+ }
546
+
547
+ /**
548
+ * Builds a {@link TypeNode} with `variant: 'struct'` for an inline anonymous type grouping named fields.
549
+ *
550
+ * Used when query or header parameters have no dedicated group type name.
551
+ * Each language printer renders this appropriately (TypeScript: `{ petId: string; name?: string }`).
552
+ */
553
+ function toStructType({
554
+ node,
555
+ params,
556
+ resolver,
557
+ }: {
558
+ node: OperationNode
559
+ params: Array<ParameterNode>
560
+ resolver: OperationParamsResolver | undefined
561
+ }): ParamsTypeNode {
562
+ return createParamsType({
563
+ variant: 'struct',
564
+ properties: params.map((p) => ({
565
+ name: p.name,
566
+ optional: !p.required,
567
+ type: resolveParamsType({ node, param: p, resolver }),
568
+ })),
569
+ })
570
+ }
571
+
572
+ function sourceKey(source: SourceNode): string {
573
+ const nameKey = source.name ?? extractStringsFromNodes(source.nodes)
574
+ return `${nameKey}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`
575
+ }
576
+
577
+ function pathTypeKey(path: string, isTypeOnly: boolean | undefined): string {
578
+ return `${path}:${isTypeOnly ?? false}`
579
+ }
580
+
581
+ function exportKey(path: string, name: string | undefined, isTypeOnly: boolean | undefined, asAlias: boolean | undefined): string {
582
+ return `${path}:${name ?? ''}:${isTypeOnly ?? false}:${asAlias ?? ''}`
583
+ }
584
+
585
+ function importKey(path: string, name: string | undefined, isTypeOnly: boolean | undefined): string {
586
+ return `${path}:${name ?? ''}:${isTypeOnly ?? false}`
587
+ }
588
+
589
+ /**
590
+ * Computes a multi-level sort key for exports and imports:
591
+ * non-array names first (wildcards/namespace aliases); type-only before value; alphabetical path; unnamed before named.
592
+ */
593
+ function sortKey(node: { name?: string | Array<unknown>; isTypeOnly?: boolean; path: string }): string {
594
+ const isArray = Array.isArray(node.name) ? '1' : '0'
595
+ const typeOnly = node.isTypeOnly ? '0' : '1'
596
+ const hasName = node.name != null ? '1' : '0'
597
+ const name = Array.isArray(node.name) ? [...node.name].sort().join('\0') : (node.name ?? '')
598
+ return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`
599
+ }
600
+
601
+ /**
602
+ * Deduplicates an array of `SourceNode` objects.
603
+ * Named sources are deduplicated by `name + isExportable + isTypeOnly`.
604
+ * Unnamed sources are deduplicated by object reference.
605
+ */
606
+ export function combineSources(sources: Array<SourceNode>): Array<SourceNode> {
607
+ const seen = new Map<string, SourceNode>()
608
+ for (const source of sources) {
609
+ const key = sourceKey(source)
610
+ if (!seen.has(key)) seen.set(key, source)
611
+ }
612
+ return [...seen.values()]
613
+ }
614
+
615
+ /**
616
+ * Deduplicates and merges an array of `ExportNode` objects.
617
+ * Exports with the same path and `isTypeOnly` flag have their names merged.
618
+ */
619
+ export function combineExports(exports: Array<ExportNode>): Array<ExportNode> {
620
+ const result: Array<ExportNode> = []
621
+ // Accumulates array-named exports keyed by `path:isTypeOnly` for name-merging
622
+ const namedByPath = new Map<string, ExportNode>()
623
+ // Deduplicates non-array exports by their exact identity
624
+ const seen = new Set<string>()
625
+
626
+ // Precompute sort keys once — avoids recomputing per comparison.
627
+ const keyed = exports.map((node) => ({ node, key: sortKey(node) }))
628
+ keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
629
+
630
+ for (const { node: curr } of keyed) {
631
+ const { name, path, isTypeOnly, asAlias } = curr
632
+
633
+ if (Array.isArray(name)) {
634
+ if (!name.length) continue
635
+
636
+ const key = pathTypeKey(path, isTypeOnly)
637
+ const existing = namedByPath.get(key)
638
+
639
+ if (existing && Array.isArray(existing.name)) {
640
+ const merged = new Set(existing.name)
641
+ for (const n of name) merged.add(n)
642
+ existing.name = [...merged]
643
+ } else {
644
+ const newItem: ExportNode = { ...curr, name: [...new Set(name)] }
645
+ result.push(newItem)
646
+ namedByPath.set(key, newItem)
647
+ }
648
+ } else {
649
+ const key = exportKey(path, name, isTypeOnly, asAlias)
650
+ if (!seen.has(key)) {
651
+ result.push(curr)
652
+ seen.add(key)
653
+ }
654
+ }
655
+ }
656
+
657
+ return result
658
+ }
659
+
660
+ /**
661
+ * Deduplicates and merges an array of `ImportNode` objects.
662
+ * Filters out unused imports (names not referenced in `source` or re-exported).
663
+ * Imports with the same path and `isTypeOnly` flag have their names merged.
664
+ */
665
+ export function combineImports(imports: Array<ImportNode>, exports: Array<ExportNode>, source?: string): Array<ImportNode> {
666
+ // Build a lookup of all exported names to retain imports that are re-exported
667
+ const exportedNames = new Set(exports.flatMap((e) => (Array.isArray(e.name) ? e.name : e.name ? [e.name] : [])))
668
+ const isUsed = (importName: string): boolean => !source || source.includes(importName) || exportedNames.has(importName)
669
+
670
+ const result: Array<ImportNode> = []
671
+ // Accumulates array-named imports keyed by `path:isTypeOnly` for name-merging
672
+ const namedByPath = new Map<string, ImportNode>()
673
+ // Deduplicates non-array imports by their exact identity
674
+ const seen = new Set<string>()
675
+
676
+ // Precompute sort keys once — avoids recomputing per comparison.
677
+ const keyed = imports.map((node) => ({ node, key: sortKey(node) }))
678
+ keyed.sort((a, b) => (a.key < b.key ? -1 : a.key > b.key ? 1 : 0))
679
+
680
+ for (const { node: curr } of keyed) {
681
+ if (curr.path === curr.root) continue
682
+
683
+ const { path, isTypeOnly } = curr
684
+ let { name } = curr
685
+
686
+ if (Array.isArray(name)) {
687
+ name = [...new Set(name)].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.propertyName)))
688
+ if (!name.length) continue
689
+
690
+ const key = pathTypeKey(path, isTypeOnly)
691
+ const existing = namedByPath.get(key)
692
+
693
+ if (existing && Array.isArray(existing.name)) {
694
+ const merged = new Set(existing.name)
695
+ for (const n of name) merged.add(n)
696
+ existing.name = [...merged]
697
+ } else {
698
+ const newItem: ImportNode = { ...curr, name }
699
+ result.push(newItem)
700
+ namedByPath.set(key, newItem)
701
+ }
702
+ } else {
703
+ if (name && !isUsed(name)) continue
704
+
705
+ const key = importKey(path, name, isTypeOnly)
706
+ if (!seen.has(key)) {
707
+ result.push(curr)
708
+ seen.add(key)
709
+ }
710
+ }
711
+ }
712
+
713
+ return result
714
+ }
715
+
716
+ /**
717
+ * Recursively extracts all string content embedded in a {@link CodeNode} tree.
718
+ *
719
+ * Includes text node values, and string attribute fields (`params`, `generics`,
720
+ * `returnType`, `type`) that may reference identifiers needing imports.
721
+ * Used by `createFile` to build the full source string for import filtering.
722
+ */
723
+ export function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): string {
724
+ if (!nodes?.length) return ''
725
+ return nodes
726
+ .map((node) => {
727
+ // Backward-compat: compiled plugins may still pass bare strings at runtime
728
+ if (typeof node === 'string') return node as string
729
+ if (node.kind === 'Text') return node.value
730
+ if (node.kind === 'Break') return ''
731
+ if (node.kind === 'Jsx') return node.value
732
+ const parts: string[] = []
733
+ if ('params' in node && node.params) parts.push(node.params)
734
+ if ('generics' in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(', ') : node.generics)
735
+ if ('returnType' in node && node.returnType) parts.push(node.returnType)
736
+ if ('type' in node && typeof node.type === 'string') parts.push(node.type)
737
+ const nested = extractStringsFromNodes(node.nodes)
738
+ if (nested) parts.push(nested)
739
+ return parts.join('\n')
740
+ })
741
+ .filter(Boolean)
742
+ .join('\n')
743
+ }
744
+
745
+ /**
746
+ * Resolves the referenced schema name of a `ref` node, falling back through
747
+ * `ref` → `name` → nested `schema.name`. Returns `undefined` for non-ref
748
+ * nodes or when no name can be resolved.
749
+ *
750
+ * @example
751
+ * ```ts
752
+ * resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
753
+ * // => 'Pet'
754
+ * ```
755
+ */
756
+ export function resolveRefName(node: SchemaNode | undefined): string | undefined {
757
+ if (!node || node.type !== 'ref') return undefined
758
+ if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? undefined
759
+
760
+ return node.name ?? node.schema?.name ?? undefined
761
+ }
762
+
763
+ /**
764
+ * Recursively collects every named schema referenced (transitively) from
765
+ * `node` via `ref` edges. Refs are followed by name only — the resolved
766
+ * `node.schema` of a ref is not traversed inline.
767
+ *
768
+ * @example
769
+ * ```ts
770
+ * const refs = collectReferencedSchemaNames(petSchema)
771
+ * // => Set { 'Cat', 'Dog' }
772
+ * ```
773
+ */
774
+ export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
775
+ if (!node) return out
776
+ collect<void>(node, {
777
+ schema(child) {
778
+ if (child.type === 'ref') {
779
+ const name = resolveRefName(child)
780
+
781
+ if (name) out.add(name)
782
+ }
783
+ return undefined
784
+ },
785
+ })
786
+ return out
787
+ }
788
+
789
+ /**
790
+ * Identifies every named schema that participates in a circular dependency
791
+ * chain — including direct self-loops (e.g. `TreeNode → TreeNode`) and indirect
792
+ * cycles spanning multiple schemas (e.g. `Pet → Cat → Pet`).
793
+ *
794
+ * The returned set contains schema names. Plugins that translate schemas into
795
+ * a host language can use this to wrap recursive positions in a deferred
796
+ * construct (lazy getter, `z.lazy(() => …)`, etc.) and avoid runtime stack
797
+ * overflows when the generated code is executed.
798
+ *
799
+ * Refs are followed by name only — `node.schema` (the resolved referent) is
800
+ * not traversed inline, which keeps the algorithm linear in the size of the
801
+ * schema graph.
802
+ *
803
+ * @example
804
+ * ```ts
805
+ * const circular = findCircularSchemas(inputNode.schemas)
806
+ * if (circular.has('Pet')) {
807
+ * // emit lazy wrapper for any property whose schema references Pet
808
+ * }
809
+ * ```
810
+ */
811
+ export function findCircularSchemas(schemas: ReadonlyArray<SchemaNode>): Set<string> {
812
+ const graph = new Map<string, Set<string>>()
813
+
814
+ for (const schema of schemas) {
815
+ if (!schema.name) continue
816
+ graph.set(schema.name, collectReferencedSchemaNames(schema))
817
+ }
818
+
819
+ const circular = new Set<string>()
820
+ for (const start of graph.keys()) {
821
+ const visited = new Set<string>()
822
+ const stack: string[] = [...(graph.get(start) ?? [])]
823
+ while (stack.length > 0) {
824
+ const node = stack.pop()!
825
+ if (node === start) {
826
+ circular.add(start)
827
+ break
828
+ }
829
+ if (visited.has(node)) continue
830
+ visited.add(node)
831
+
832
+ const next = graph.get(node)
833
+ if (next) for (const r of next) stack.push(r)
834
+ }
835
+ }
836
+
837
+ return circular
838
+ }
839
+
840
+ /**
841
+ * Returns true when `node` (or anything nested within it) carries a `ref`
842
+ * whose resolved name belongs to `circularSchemas`.
843
+ *
844
+ * When `excludeName` is provided, refs to that name are ignored — useful
845
+ * when self-references are already handled separately from cross-schema
846
+ * cycles (e.g. the faker plugin emits `undefined as any` for direct
847
+ * self-recursion but a lazy getter for indirect cycles).
848
+ *
849
+ * @example
850
+ * ```ts
851
+ * const circular = findCircularSchemas(schemas)
852
+ * if (containsCircularRef(property.schema, { circularSchemas: circular, excludeName: 'Pet' })) {
853
+ * // emit `get foo() { return fakeCat() }` instead of eager call
854
+ * }
855
+ * ```
856
+ */
857
+ export function containsCircularRef(
858
+ node: SchemaNode | undefined,
859
+ { circularSchemas, excludeName }: { circularSchemas: ReadonlySet<string>; excludeName?: string },
860
+ ): boolean {
861
+ if (!node || circularSchemas.size === 0) return false
862
+
863
+ const matches = collect<true>(node, {
864
+ schema(child) {
865
+ if (child.type !== 'ref') return undefined
866
+ const name = resolveRefName(child)
867
+
868
+ return name && name !== excludeName && circularSchemas.has(name) ? true : undefined
869
+ },
870
+ })
871
+
872
+ return matches.length > 0
873
+ }