@kubb/ast 5.0.0-alpha.20 → 5.0.0-alpha.22

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.
@@ -36,11 +36,11 @@ const kindToHandlerKey = {
36
36
  * return options.mode === 'declaration' && node.type ? `${node.name}: ${node.type}` : node.name
37
37
  * },
38
38
  * objectBindingParameter(node) {
39
- * const inner = node.properties.map(p => this.print(p)).filter(Boolean).join(', ')
39
+ * const inner = node.properties.map(p => this.transform(p)).filter(Boolean).join(', ')
40
40
  * return `{ ${inner} }`
41
41
  * },
42
42
  * functionParameters(node) {
43
- * return node.params.map(p => this.print(p)).filter(Boolean).join(', ')
43
+ * return node.params.map(p => this.transform(p)).filter(Boolean).join(', ')
44
44
  * },
45
45
  * },
46
46
  * }))
@@ -142,7 +142,7 @@ export const functionPrinter = defineFunctionPrinter<DefaultPrinter>((options) =
142
142
 
143
143
  if (node.inline) {
144
144
  return sorted
145
- .map((p) => this.print(p))
145
+ .map((p) => this.transform(p))
146
146
  .filter(Boolean)
147
147
  .join(', ')
148
148
  }
@@ -188,7 +188,7 @@ export const functionPrinter = defineFunctionPrinter<DefaultPrinter>((options) =
188
188
  const sorted = sortParams(node.params)
189
189
 
190
190
  return sorted
191
- .map((p) => this.print(p))
191
+ .map((p) => this.transform(p))
192
192
  .filter(Boolean)
193
193
  .join(', ')
194
194
  },
@@ -3,21 +3,22 @@ import type { SchemaNode, SchemaNodeByType, SchemaType } from '../nodes/index.ts
3
3
  /**
4
4
  * Runtime context passed as `this` to printer handlers.
5
5
  *
6
- * `this.print` always dispatches to node-level handlers from `nodes`.
6
+ * `this.transform` dispatches to node-level handlers from `nodes`.
7
7
  *
8
8
  * @example
9
9
  * ```ts
10
10
  * const context: PrinterHandlerContext<string, {}> = {
11
11
  * options: {},
12
- * print: () => 'value',
12
+ * transform: () => 'value',
13
13
  * }
14
14
  * ```
15
15
  */
16
16
  export type PrinterHandlerContext<TOutput, TOptions extends object> = {
17
17
  /**
18
- * Recursively print a nested `SchemaNode` using the node-level handlers.
18
+ * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
19
+ * Use `this.transform` inside `nodes` handlers and inside the `print` override.
19
20
  */
20
- print: (node: SchemaNode) => TOutput | null | undefined
21
+ transform: (node: SchemaNode) => TOutput | null | undefined
21
22
  /**
22
23
  * Options for this printer instance.
23
24
  */
@@ -78,6 +79,12 @@ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
78
79
  * Options for this printer instance.
79
80
  */
80
81
  options: T['options']
82
+ /**
83
+ * Node-level dispatcher — converts a `SchemaNode` directly to `TOutput` using the `nodes` handlers.
84
+ * Always dispatches through the `nodes` map; never calls the `print` override.
85
+ * Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
86
+ */
87
+ transform: (node: SchemaNode) => T['output'] | null | undefined
81
88
  /**
82
89
  * Public printer. If the builder provides a root-level `print`, this calls that
83
90
  * higher-level function (which may produce full declarations).
@@ -111,10 +118,10 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
111
118
  }>
112
119
  /**
113
120
  * Optional root-level print override. When provided, becomes the public `printer.print`.
114
- * `this.print(node)` inside this function calls the node-level dispatcher (`nodes` handlers),
121
+ * Use `this.transform(node)` inside this function to dispatch to the node-level handlers (`nodes`),
115
122
  * not the override itself — so recursion is safe.
116
123
  */
117
- print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null | undefined
124
+ print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null
118
125
  }
119
126
 
120
127
  /**
@@ -127,10 +134,10 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
127
134
  * - `options` — options stored on the returned printer instance
128
135
  * - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
129
136
  * - `print` _(optional)_ — top-level override exposed as `printer.print`
130
- * - Inside this function, `this.print(node)` still dispatches to the `nodes` map
137
+ * - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
131
138
  * - This keeps recursion safe and avoids self-calls
132
139
  *
133
- * When no `print` override is provided, `printer.print` is the node-level dispatcher directly.
140
+ * When no `print` override is provided, `printer.print` falls back to `printer.transform` (the node-level dispatcher).
134
141
  *
135
142
  * @example Basic usage — Zod schema printer
136
143
  * ```ts
@@ -142,7 +149,7 @@ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) =
142
149
  * nodes: {
143
150
  * string: () => 'z.string()',
144
151
  * object(node) {
145
- * const props = node.properties.map(p => `${p.name}: ${this.print(p.schema)}`).join(', ')
152
+ * const props = node.properties.map(p => `${p.name}: ${this.transform(p.schema)}`).join(', ')
146
153
  * return `z.object({ ${props} })`
147
154
  * },
148
155
  * },
@@ -170,19 +177,24 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
170
177
  options: T['options']
171
178
  nodes: Partial<{
172
179
  [K in TKey]: (
173
- this: { print: (node: TNode) => T['output'] | null | undefined; options: T['options'] },
180
+ this: { transform: (node: TNode) => T['output'] | null | undefined; options: T['options'] },
174
181
  node: TNodeByKey[K],
175
182
  ) => T['output'] | null | undefined
176
183
  }>
177
- print?: (this: { print: (node: TNode) => T['output'] | null | undefined; options: T['options'] }, node: TNode) => T['printOutput'] | null | undefined
184
+ print?: (this: { transform: (node: TNode) => T['output'] | null | undefined; options: T['options'] }, node: TNode) => T['printOutput'] | null | undefined
178
185
  },
179
- ): (options?: T['options']) => { name: T['name']; options: T['options']; print: (node: TNode) => T['printOutput'] | null | undefined } {
186
+ ): (options?: T['options']) => {
187
+ name: T['name']
188
+ options: T['options']
189
+ transform: (node: TNode) => T['output'] | null | undefined
190
+ print: (node: TNode) => T['printOutput'] | null | undefined
191
+ } {
180
192
  return (options) => {
181
193
  const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
182
194
 
183
195
  const context = {
184
196
  options: resolvedOptions,
185
- print: (node: TNode): T['output'] | null | undefined => {
197
+ transform: (node: TNode): T['output'] | null | undefined => {
186
198
  const key = getKey(node)
187
199
  if (key === undefined) return null
188
200
 
@@ -197,7 +209,8 @@ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey exte
197
209
  return {
198
210
  name,
199
211
  options: resolvedOptions,
200
- print: (printOverride ? printOverride.bind(context) : context.print) as (node: TNode) => T['printOutput'] | null | undefined,
212
+ transform: context.transform,
213
+ print: (printOverride ? printOverride.bind(context) : context.transform) as (node: TNode) => T['printOutput'] | null | undefined,
201
214
  }
202
215
  }
203
216
  }