@kubb/ast 5.0.0-alpha.8 → 5.0.0-beta.75

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