@kubb/ast 5.0.0-beta.2 → 5.0.0-beta.21

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/package.json CHANGED
@@ -1,12 +1,11 @@
1
1
  {
2
2
  "name": "@kubb/ast",
3
- "version": "5.0.0-beta.2",
4
- "description": "Spec-agnostic AST layer for Kubb. Defines nodes, visitor pattern, and factory functions used across codegen plugins.",
3
+ "version": "5.0.0-beta.21",
4
+ "description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",
5
5
  "keywords": [
6
6
  "ast",
7
7
  "codegen",
8
8
  "kubb",
9
- "openapi",
10
9
  "typescript"
11
10
  ],
12
11
  "license": "MIT",
@@ -40,7 +39,7 @@
40
39
  "registry": "https://registry.npmjs.org/"
41
40
  },
42
41
  "devDependencies": {
43
- "@types/node": "^25.6.0",
42
+ "@types/node": "^22.19.19",
44
43
  "@internals/utils": "0.0.0"
45
44
  },
46
45
  "engines": {
package/src/factory.ts CHANGED
@@ -12,7 +12,9 @@ import type {
12
12
  FunctionParameterNode,
13
13
  FunctionParametersNode,
14
14
  ImportNode,
15
+ InputMeta,
15
16
  InputNode,
17
+ InputStreamNode,
16
18
  JsxNode,
17
19
  ObjectSchemaNode,
18
20
  OperationNode,
@@ -84,11 +86,24 @@ export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): I
84
86
  return {
85
87
  schemas: [],
86
88
  operations: [],
89
+ meta: { circularNames: [], enumNames: [] },
87
90
  ...overrides,
88
91
  kind: 'Input',
89
92
  }
90
93
  }
91
94
 
95
+ /**
96
+ * Creates an `InputStreamNode` from pre-built `AsyncIterable` sources.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const node = createStreamInput(schemasIterable, operationsIterable, { title: 'My API' })
101
+ * ```
102
+ */
103
+ export function createStreamInput(schemas: AsyncIterable<SchemaNode>, operations: AsyncIterable<OperationNode>, meta?: InputMeta): InputStreamNode {
104
+ return { kind: 'Input', schemas, operations, meta }
105
+ }
106
+
92
107
  /**
93
108
  * Creates an `OutputNode` with a stable default for `files`.
94
109
  *
package/src/guards.ts CHANGED
@@ -20,11 +20,11 @@ import type {
20
20
  * @example
21
21
  * ```ts
22
22
  * const schema = createSchema({ type: 'string' })
23
- * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
23
+ * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
24
24
  * ```
25
25
  */
26
- export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined {
27
- return node?.type === type ? (node as SchemaNodeByType[T]) : undefined
26
+ export function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null {
27
+ return node?.type === type ? (node as SchemaNodeByType[T]) : null
28
28
  }
29
29
 
30
30
  function isKind<T extends Node>(kind: NodeKind) {
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export {
10
10
  createFunctionParameters,
11
11
  createImport,
12
12
  createInput,
13
+ createStreamInput,
13
14
  createJsx,
14
15
  createOperation,
15
16
  createOutput,
@@ -28,7 +29,7 @@ export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema
28
29
  export { createPrinterFactory, definePrinter } from './printer.ts'
29
30
  export { extractRefName } from './refs.ts'
30
31
  export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
31
- export { mergeAdjacentObjects, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
32
+ export { mergeAdjacentObjects, mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
32
33
  export type * from './types.ts'
33
34
  export {
34
35
  caseParams,
@@ -43,4 +44,4 @@ export {
43
44
  resolveRefName,
44
45
  syncSchemaRef,
45
46
  } from './utils.ts'
46
- export { collect, transform, walk } from './visitor.ts'
47
+ export { collect, collectLazy, transform, walk } from './visitor.ts'
@@ -19,7 +19,7 @@ export type { OutputNode } from './output.ts'
19
19
  export type { ParameterLocation, ParameterNode } from './parameter.ts'
20
20
  export type { PropertyNode } from './property.ts'
21
21
  export type { ResponseNode } from './response.ts'
22
- export type { InputMeta, InputNode } from './root.ts'
22
+ export type { InputMeta, InputNode, InputStreamNode } from './root.ts'
23
23
  export type {
24
24
  ArraySchemaNode,
25
25
  ComplexSchemaType,
@@ -101,7 +101,7 @@ export type OperationNode = BaseNode & {
101
101
  * Property keys to exclude from the generated request body type via `Omit<Type, Keys>`.
102
102
  * Set when a referenced schema has `readOnly` fields that should be omitted in request types.
103
103
  */
104
- keysToOmit?: Array<string>
104
+ keysToOmit?: Array<string> | null
105
105
  }>
106
106
  }
107
107
  /**
@@ -39,5 +39,5 @@ export type ResponseNode = BaseNode & {
39
39
  * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
40
40
  * Set when a referenced schema has `writeOnly` fields that should not appear in response types.
41
41
  */
42
- keysToOmit?: Array<string>
42
+ keysToOmit?: Array<string> | null
43
43
  }
package/src/nodes/root.ts CHANGED
@@ -3,32 +3,62 @@ import type { OperationNode } from './operation.ts'
3
3
  import type { SchemaNode } from './schema.ts'
4
4
 
5
5
  /**
6
- * Basic metadata for an API document.
7
- * Adapters fill fields that exist in their source format.
6
+ * Metadata for an API document, populated by the adapter and available to every generator.
7
+ *
8
+ * All fields are plain JSON-serializable values — no `Set`, no `Map`, no class instances.
9
+ * Computed fields (`circularNames`, `enumNames`) are pre-calculated once during the adapter
10
+ * pre-scan so generators never need to iterate the full schema list themselves.
8
11
  *
9
12
  * @example
10
13
  * ```ts
11
- * const meta: InputMeta = { title: 'Pet API', version: '1.0.0' }
14
+ * const meta: InputMeta = { title: 'Pet Store', version: '1.0.0', baseURL: 'https://petstore.swagger.io/v2', circularNames: [], enumNames: [] }
12
15
  * ```
13
16
  */
14
17
  export type InputMeta = {
15
18
  /**
16
- * API title (from `info.title` in OAS/AsyncAPI).
19
+ * API title from `info.title` in the source document.
17
20
  */
18
21
  title?: string
19
22
  /**
20
- * API description (from `info.description` in OAS/AsyncAPI).
23
+ * API description from `info.description` in the source document.
21
24
  */
22
25
  description?: string
23
26
  /**
24
- * API version string (from `info.version` in OAS/AsyncAPI).
27
+ * API version string from `info.version` in the source document.
25
28
  */
26
29
  version?: string
27
30
  /**
28
- * Resolved API base URL.
29
- * For OpenAPI and AsyncAPI, this comes from the selected server URL.
31
+ * Resolved base URL from the first matching server entry in the source document.
32
+ */
33
+ baseURL?: string | null
34
+ /**
35
+ * Names of schemas that participate in a circular reference chain.
36
+ * Computed once during the adapter pre-scan — use this instead of calling
37
+ * `findCircularSchemas` per generator call.
38
+ *
39
+ * Convert to a `Set` once at the start of a generator, not per-schema,
40
+ * to keep lookup O(1) without repeated allocations.
41
+ *
42
+ * @example Wrap a circular schema in z.lazy()
43
+ * ```ts
44
+ * const circular = new Set(meta.circularNames)
45
+ * if (circular.has(schema.name)) { ... }
46
+ * ```
30
47
  */
31
- baseURL?: string
48
+ circularNames: readonly string[]
49
+ /**
50
+ * Names of schemas whose type is `enum`.
51
+ * Computed once during the adapter pre-scan — use this instead of filtering
52
+ * schemas per generator call.
53
+ *
54
+ * Convert to a `Set` once at the start of a generator when you need repeated
55
+ * membership checks, rather than calling `.includes()` per schema.
56
+ *
57
+ * @example Check if a referenced schema is an enum
58
+ * `const enums = new Set(meta.enumNames)`
59
+ * `const isEnum = enums.has(schemaName)`
60
+ */
61
+ enumNames: readonly string[]
32
62
  }
33
63
 
34
64
  /**
@@ -58,7 +88,39 @@ export type InputNode = BaseNode & {
58
88
  */
59
89
  operations: Array<OperationNode>
60
90
  /**
61
- * Optional document metadata populated by the adapter.
91
+ * Document metadata populated by the adapter.
92
+ */
93
+ meta: InputMeta
94
+ }
95
+
96
+ /**
97
+ * Streaming variant of `InputNode` for memory-efficient processing of large API specs.
98
+ *
99
+ * `schemas` and `operations` are `AsyncIterable` rather than arrays — each `for await`
100
+ * loop creates a fresh parse pass from the cached in-memory document, so multiple
101
+ * consumers (plugins) can iterate independently without keeping all nodes in memory.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * for await (const schema of inputStreamNode.schemas) {
106
+ * // only this one SchemaNode is live here; previous ones are GC-eligible
107
+ * }
108
+ * ```
109
+ */
110
+ export type InputStreamNode = {
111
+ kind: 'Input'
112
+ /**
113
+ * Lazily parsed schema nodes. Each `for await` creates a fresh parse pass, so
114
+ * multiple plugins can iterate independently without sharing state.
115
+ */
116
+ schemas: AsyncIterable<SchemaNode>
117
+ /**
118
+ * Lazily parsed operation nodes. Each `for await` creates a fresh parse pass, so
119
+ * multiple plugins can iterate independently without sharing state.
120
+ */
121
+ operations: AsyncIterable<OperationNode>
122
+ /**
123
+ * Document metadata available immediately, before the first yielded node.
62
124
  */
63
125
  meta?: InputMeta
64
126
  }
@@ -154,6 +154,10 @@ type SchemaNodeBase = BaseNode & {
154
154
  * For example, this is `'string'` for a `uuid` schema.
155
155
  */
156
156
  primitive?: PrimitiveSchemaType
157
+ /**
158
+ * Schema `format` value.
159
+ */
160
+ format?: string
157
161
  }
158
162
 
159
163
  /**
@@ -364,8 +368,9 @@ export type RefSchemaNode = SchemaNodeBase & {
364
368
  type: 'ref'
365
369
  /**
366
370
  * Referenced schema name.
371
+ * `null` means Kubb has processed this and determined there is no applicable name.
367
372
  */
368
- name?: string
373
+ name?: string | null
369
374
  /**
370
375
  * Original `$ref` path, for example, `#/components/schemas/Order`.
371
376
  * Used to resolve names later.
@@ -378,12 +383,13 @@ export type RefSchemaNode = SchemaNodeBase & {
378
383
  /**
379
384
  * The fully-parsed schema that this ref resolves to.
380
385
  * Populated during OAS parsing when the referenced definition can be resolved.
381
- * `undefined` when the ref cannot be resolved or is part of a circular chain.
386
+ * `null` when the ref cannot be resolved or is part of a circular chain.
387
+ * `undefined` when resolution has not been attempted.
382
388
  *
383
389
  * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
384
390
  * without following the reference manually.
385
391
  */
386
- schema?: SchemaNode
392
+ schema?: SchemaNode | null
387
393
  }
388
394
 
389
395
  /**
package/src/printer.ts CHANGED
@@ -18,7 +18,7 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
18
18
  * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
19
19
  * Use `this.transform` inside `nodes` handlers and inside the `print` override.
20
20
  */
21
- transform: (node: SchemaNode) => TOutput | null | undefined
21
+ transform: (node: SchemaNode) => TOutput | null
22
22
  /**
23
23
  * Options for this printer instance.
24
24
  */
@@ -40,7 +40,7 @@ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
40
40
  export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
41
41
  this: PrinterHandlerContext<TOutput, TOptions>,
42
42
  node: SchemaNodeByType[T],
43
- ) => TOutput | null | undefined
43
+ ) => TOutput | null
44
44
 
45
45
  /**
46
46
  * Partial map of per-node-type handler overrides for a printer.
@@ -108,13 +108,13 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
108
108
  * Always dispatches through the `nodes` map; never calls the `print` override.
109
109
  * Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
110
110
  */
111
- transform: (node: SchemaNode) => T['output'] | null | undefined
111
+ transform: (node: SchemaNode) => T['output'] | null
112
112
  /**
113
113
  * Public printer. If the builder provides a root-level `print`, this calls that
114
114
  * higher-level function (which may produce full declarations).
115
115
  * Otherwise, falls back to the node-level dispatcher.
116
116
  */
117
- print: (node: SchemaNode) => T['printOutput'] | null | undefined
117
+ print: (node: SchemaNode) => T['printOutput'] | null
118
118
  }
119
119
 
120
120
  /**
@@ -147,7 +147,6 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
147
147
  */
148
148
  print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
149
149
  }
150
-
151
150
  /**
152
151
  * Creates a schema printer factory.
153
152
  *
@@ -194,7 +193,7 @@ export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOp
194
193
  * )
195
194
  * ```
196
195
  */
197
- export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined) {
196
+ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null) {
198
197
  return function <T extends PrinterFactoryOptions>(
199
198
  build: (options: T['options']) => {
200
199
  name: T['name']
@@ -202,40 +201,40 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
202
201
  nodes: Partial<{
203
202
  [K in TKey]: (
204
203
  this: {
205
- transform: (node: TNode) => T['output'] | null | undefined
204
+ transform: (node: TNode) => T['output'] | null
206
205
  options: T['options']
207
206
  },
208
207
  node: TNodeByKey[K],
209
- ) => T['output'] | null | undefined
208
+ ) => T['output'] | null
210
209
  }>
211
210
  print?: (
212
211
  this: {
213
- transform: (node: TNode) => T['output'] | null | undefined
212
+ transform: (node: TNode) => T['output'] | null
214
213
  options: T['options']
215
214
  },
216
215
  node: TNode,
217
- ) => T['printOutput'] | null | undefined
216
+ ) => T['printOutput'] | null
218
217
  },
219
218
  ): (options?: T['options']) => {
220
219
  name: T['name']
221
220
  options: T['options']
222
- transform: (node: TNode) => T['output'] | null | undefined
223
- print: (node: TNode) => T['printOutput'] | null | undefined
221
+ transform: (node: TNode) => T['output'] | null
222
+ print: (node: TNode) => T['printOutput'] | null
224
223
  } {
225
224
  return (options) => {
226
225
  const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
227
226
 
228
227
  const context = {
229
228
  options: resolvedOptions,
230
- transform: (node: TNode): T['output'] | null | undefined => {
229
+ transform: (node: TNode): T['output'] | null => {
231
230
  const key = getKey(node)
232
- if (key === undefined) return null
231
+ if (key === null) return null
233
232
 
234
233
  const handler = nodes[key]
235
234
 
236
235
  if (!handler) return null
237
236
 
238
- return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
237
+ return (handler as (this: typeof context, node: TNode) => T['output'] | null).call(context, node)
239
238
  },
240
239
  }
241
240
 
@@ -243,7 +242,7 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
243
242
  name,
244
243
  options: resolvedOptions,
245
244
  transform: context.transform,
246
- print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null | undefined,
245
+ print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null,
247
246
  }
248
247
  }
249
248
  }
package/src/refs.ts CHANGED
@@ -45,13 +45,15 @@ export function buildRefMap(input: InputNode): RefMap {
45
45
  /**
46
46
  * Resolves a schema by name from a `RefMap`.
47
47
  *
48
+ * Returns `null` when the ref is not found.
49
+ *
48
50
  * @example
49
51
  * ```ts
50
52
  * const petSchema = resolveRef(refMap, 'Pet')
51
53
  * ```
52
54
  */
53
- export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
54
- return refMap.get(ref)
55
+ export function resolveRef(refMap: RefMap, ref: string): SchemaNode | null {
56
+ return refMap.get(ref) ?? null
55
57
  }
56
58
 
57
59
  /**
package/src/resolvers.ts CHANGED
@@ -27,17 +27,17 @@ export function collectImports<TImport>({
27
27
  }: {
28
28
  node: SchemaNode
29
29
  nameMapping: Map<string, string>
30
- resolve: (schemaName: string) => TImport | undefined
30
+ resolve: (schemaName: string) => TImport | null
31
31
  }): Array<TImport> {
32
32
  return collect<TImport>(node, {
33
- schema(schemaNode): TImport | undefined {
33
+ schema(schemaNode): TImport | null {
34
34
  const schemaRef = narrowSchema(schemaNode, 'ref')
35
- if (!schemaRef?.ref) return
35
+ if (!schemaRef?.ref) return null
36
36
 
37
37
  const rawName = extractRefName(schemaRef.ref)
38
38
  const schemaName = nameMapping.get(rawName) ?? rawName
39
39
  const result = resolve(schemaName)
40
- if (!result) return
40
+ if (!result) return null
41
41
 
42
42
  return result
43
43
  },
@@ -73,25 +73,30 @@ export function setDiscriminatorEnum({
73
73
  * ])
74
74
  * ```
75
75
  */
76
- export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
77
- return members.reduce<Array<SchemaNode>>((acc, member) => {
76
+ export function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {
77
+ let acc: SchemaNode | undefined
78
+
79
+ for (const member of members) {
78
80
  const objectMember = narrowSchema(member, 'object')
79
- if (objectMember && !objectMember.name) {
80
- const previous = acc.at(-1)
81
- const previousObject = previous ? narrowSchema(previous, 'object') : undefined
82
-
83
- if (previousObject && !previousObject.name) {
84
- acc[acc.length - 1] = createSchema({
85
- ...previousObject,
86
- properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
81
+ if (objectMember && !objectMember.name && acc !== undefined) {
82
+ const accObject = narrowSchema(acc, 'object')
83
+ if (accObject && !accObject.name) {
84
+ acc = createSchema({
85
+ ...accObject,
86
+ properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],
87
87
  })
88
- return acc
88
+ continue
89
89
  }
90
90
  }
91
+ if (acc !== undefined) yield acc
92
+ acc = member
93
+ }
91
94
 
92
- acc.push(member)
93
- return acc
94
- }, [])
95
+ if (acc !== undefined) yield acc
96
+ }
97
+
98
+ export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
99
+ return [...mergeAdjacentObjectsLazy(members)]
95
100
  }
96
101
 
97
102
  /**
@@ -145,7 +150,7 @@ export function setEnumName(propNode: SchemaNode, parentName: string | null | un
145
150
  const enumNode = narrowSchema(propNode, 'enum')
146
151
 
147
152
  if (enumNode?.primitive === 'boolean') {
148
- return { ...propNode, name: undefined }
153
+ return { ...propNode, name: null }
149
154
  }
150
155
 
151
156
  if (enumNode) {
package/src/types.ts CHANGED
@@ -26,6 +26,7 @@ export type {
26
26
  ImportNode,
27
27
  InputMeta,
28
28
  InputNode,
29
+ InputStreamNode,
29
30
  IntersectionSchemaNode,
30
31
  Ipv4SchemaNode,
31
32
  Ipv6SchemaNode,