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

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.
@@ -0,0 +1,196 @@
1
+ import type { FunctionNode, FunctionNodeType } from '../nodes/function.ts'
2
+ import type { FunctionParameterNode, FunctionParametersNode, ObjectBindingParameterNode } from '../nodes/index.ts'
3
+ import type { PrinterFactoryOptions } from './printer.ts'
4
+ import { createPrinterFactory } from './printer.ts'
5
+
6
+ /**
7
+ * Maps each function-printer handler key to its concrete node type.
8
+ */
9
+ export type FunctionNodeByType = {
10
+ functionParameter: FunctionParameterNode
11
+ objectBindingParameter: ObjectBindingParameterNode
12
+ functionParameters: FunctionParametersNode
13
+ }
14
+
15
+ const kindToHandlerKey = {
16
+ FunctionParameter: 'functionParameter',
17
+ ObjectBindingParameter: 'objectBindingParameter',
18
+ FunctionParameters: 'functionParameters',
19
+ } satisfies Record<string, FunctionNodeType>
20
+
21
+ /**
22
+ * Creates a function-parameter printer factory.
23
+ *
24
+ * This wrapper uses `createPrinterFactory` and dispatches handlers by `node.kind`
25
+ * (for function nodes) rather than by `node.type` (for schema nodes).
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * type MyPrinter = PrinterFactoryOptions<'my', { mode: 'declaration' | 'call' }, string>
30
+ *
31
+ * export const myPrinter = defineFunctionPrinter<MyPrinter>((options) => ({
32
+ * name: 'my',
33
+ * options,
34
+ * nodes: {
35
+ * functionParameter(node) {
36
+ * return options.mode === 'declaration' && node.type ? `${node.name}: ${node.type}` : node.name
37
+ * },
38
+ * objectBindingParameter(node) {
39
+ * const inner = node.properties.map(p => this.print(p)).filter(Boolean).join(', ')
40
+ * return `{ ${inner} }`
41
+ * },
42
+ * functionParameters(node) {
43
+ * return node.params.map(p => this.print(p)).filter(Boolean).join(', ')
44
+ * },
45
+ * },
46
+ * }))
47
+ * ```
48
+ */
49
+ export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>((node) => kindToHandlerKey[node.kind])
50
+
51
+ export type FunctionPrinterOptions = {
52
+ /**
53
+ * Rendering modes supported by `functionPrinter`.
54
+ *
55
+ * | Mode | Output example | Use case |
56
+ * |---------------|---------------------------------------------|---------------------------------|
57
+ * | `declaration` | `id: string, config: Config = {}` | Function parameter declaration |
58
+ * | `call` | `id, { method, url }` | Function call arguments |
59
+ * | `keys` | `{ id, config }` | Key names only (destructuring) |
60
+ * | `values` | `{ id: id, config: config }` | Key/value object entries |
61
+ */
62
+ mode: 'declaration' | 'call' | 'keys' | 'values'
63
+ /**
64
+ * Optional transformation applied to every parameter name before printing.
65
+ */
66
+ transformName?: (name: string) => string
67
+ /**
68
+ * Optional transformation applied to every type string before printing.
69
+ */
70
+ transformType?: (type: string) => string
71
+ }
72
+
73
+ type DefaultPrinter = PrinterFactoryOptions<'functionParameters', FunctionPrinterOptions, string>
74
+
75
+ function rank(param: FunctionParameterNode | ObjectBindingParameterNode): number {
76
+ if (param.kind === 'ObjectBindingParameter') {
77
+ if (param.default) return 2
78
+ const isOptional = param.optional ?? param.properties.every((p) => p.optional || p.default !== undefined)
79
+ return isOptional ? 1 : 0
80
+ }
81
+ if (param.rest) return 3
82
+ if (param.default) return 2
83
+ return param.optional ? 1 : 0
84
+ }
85
+
86
+ function sortParams(params: Array<FunctionParameterNode | ObjectBindingParameterNode>): Array<FunctionParameterNode | ObjectBindingParameterNode> {
87
+ return [...params].sort((a, b) => rank(a) - rank(b))
88
+ }
89
+
90
+ function sortChildParams(params: Array<FunctionParameterNode>): Array<FunctionParameterNode> {
91
+ return [...params].sort((a, b) => rank(a) - rank(b))
92
+ }
93
+
94
+ /**
95
+ * Default function-signature printer.
96
+ * Covers the four standard output modes used across Kubb plugins.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const printer = functionPrinter({ mode: 'declaration' })
101
+ *
102
+ * const sig = createFunctionParameters({
103
+ * params: [
104
+ * createFunctionParameter({ name: 'petId', type: 'string', optional: false }),
105
+ * createFunctionParameter({ name: 'config', type: 'Config', optional: false, default: '{}' }),
106
+ * ],
107
+ * })
108
+ *
109
+ * printer.print(sig) // → "petId: string, config: Config = {}"
110
+ * ```
111
+ */
112
+ export const functionPrinter = defineFunctionPrinter<DefaultPrinter>((options) => ({
113
+ name: 'functionParameters',
114
+ options,
115
+ nodes: {
116
+ functionParameter(node) {
117
+ const { mode, transformName, transformType } = this.options
118
+ const name = transformName ? transformName(node.name) : node.name
119
+ const type = node.type && transformType ? transformType(node.type) : node.type
120
+
121
+ if (mode === 'keys' || mode === 'values') {
122
+ return node.rest ? `...${name}` : name
123
+ }
124
+
125
+ if (mode === 'call') {
126
+ return node.rest ? `...${name}` : name
127
+ }
128
+
129
+ if (node.rest) {
130
+ return type ? `...${name}: ${type}` : `...${name}`
131
+ }
132
+ if (type) {
133
+ if (node.optional) return `${name}?: ${type}`
134
+ return node.default ? `${name}: ${type} = ${node.default}` : `${name}: ${type}`
135
+ }
136
+ return node.default ? `${name} = ${node.default}` : name
137
+ },
138
+ objectBindingParameter(node) {
139
+ const { mode, transformName, transformType } = this.options
140
+ const sorted = sortChildParams(node.properties)
141
+ const isOptional = node.optional ?? sorted.every((p) => p.optional || p.default !== undefined)
142
+
143
+ if (node.inline) {
144
+ return sorted
145
+ .map((p) => this.print(p))
146
+ .filter(Boolean)
147
+ .join(', ')
148
+ }
149
+
150
+ if (mode === 'keys' || mode === 'values') {
151
+ const keys = sorted.map((p) => p.name).join(', ')
152
+ return `{ ${keys} }`
153
+ }
154
+
155
+ if (mode === 'call') {
156
+ const keys = sorted.map((p) => p.name).join(', ')
157
+ return `{ ${keys} }`
158
+ }
159
+
160
+ const names = sorted.map((p) => {
161
+ const n = transformName ? transformName(p.name) : p.name
162
+
163
+ return n
164
+ })
165
+
166
+ const nameStr = names.length ? `{ ${names.join(', ')} }` : undefined
167
+ if (!nameStr) return null
168
+
169
+ let typeAnnotation = node.type
170
+ if (!typeAnnotation) {
171
+ const typeParts = sorted
172
+ .filter((p) => p.type)
173
+ .map((p) => {
174
+ const t = transformType && p.type ? transformType(p.type) : p.type!
175
+ return p.optional || p.default !== undefined ? `${p.name}?: ${t}` : `${p.name}: ${t}`
176
+ })
177
+ typeAnnotation = typeParts.length ? `{ ${typeParts.join('; ')} }` : undefined
178
+ }
179
+
180
+ if (typeAnnotation) {
181
+ if (isOptional) return `${nameStr}: ${typeAnnotation} = ${node.default ?? '{}'}`
182
+ return node.default ? `${nameStr}: ${typeAnnotation} = ${node.default}` : `${nameStr}: ${typeAnnotation}`
183
+ }
184
+
185
+ return node.default ? `${nameStr} = ${node.default}` : nameStr
186
+ },
187
+ functionParameters(node) {
188
+ const sorted = sortParams(node.params)
189
+
190
+ return sorted
191
+ .map((p) => this.print(p))
192
+ .filter(Boolean)
193
+ .join(', ')
194
+ },
195
+ },
196
+ }))
@@ -0,0 +1,3 @@
1
+ export { defineFunctionPrinter, functionPrinter } from './functionPrinter.ts'
2
+ export type { Printer, PrinterFactoryOptions } from './printer.ts'
3
+ export { createPrinterFactory, definePrinter } from './printer.ts'
@@ -0,0 +1,204 @@
1
+ import type { SchemaNode, SchemaNodeByType, SchemaType } from '../nodes/index.ts'
2
+
3
+ /**
4
+ * Runtime context passed as `this` to printer handlers.
5
+ *
6
+ * `this.print` always dispatches to node-level handlers from `nodes`.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * const context: PrinterHandlerContext<string, {}> = {
11
+ * options: {},
12
+ * print: () => 'value',
13
+ * }
14
+ * ```
15
+ */
16
+ export type PrinterHandlerContext<TOutput, TOptions extends object> = {
17
+ /**
18
+ * Recursively print a nested `SchemaNode` using the node-level handlers.
19
+ */
20
+ print: (node: SchemaNode) => TOutput | null | undefined
21
+ /**
22
+ * Options for this printer instance.
23
+ */
24
+ options: TOptions
25
+ }
26
+
27
+ /**
28
+ * Handler for one schema node type.
29
+ *
30
+ * Use a regular function (not an arrow function) if you need `this`.
31
+ *
32
+ * @example
33
+ * ```ts
34
+ * const handler: PrinterHandler<string, {}, 'string'> = function () {
35
+ * return 'string'
36
+ * }
37
+ * ```
38
+ */
39
+ export type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (
40
+ this: PrinterHandlerContext<TOutput, TOptions>,
41
+ node: SchemaNodeByType[T],
42
+ ) => TOutput | null | undefined
43
+
44
+ /**
45
+ * Generic shape used by `definePrinter`.
46
+ *
47
+ * - `TName` — unique string identifier (e.g. `'zod'`, `'ts'`)
48
+ * - `TOptions` — options passed to and stored on the printer instance
49
+ * - `TOutput` — the type emitted by node handlers
50
+ * - `TPrintOutput` — type returned by public `print` (defaults to `TOutput`)
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * type MyPrinter = PrinterFactoryOptions<'my', { strict: boolean }, string>
55
+ * ```
56
+ */
57
+ export type PrinterFactoryOptions<TName extends string = string, TOptions extends object = object, TOutput = unknown, TPrintOutput = TOutput> = {
58
+ name: TName
59
+ options: TOptions
60
+ output: TOutput
61
+ printOutput: TPrintOutput
62
+ }
63
+
64
+ /**
65
+ * Printer instance returned by a printer factory.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const printer = definePrinter((options: {}) => ({ name: 'x', options, nodes: {} }))({})
70
+ * ```
71
+ */
72
+ export type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
73
+ /**
74
+ * Unique identifier supplied at creation time.
75
+ */
76
+ name: T['name']
77
+ /**
78
+ * Options for this printer instance.
79
+ */
80
+ options: T['options']
81
+ /**
82
+ * Public printer. If the builder provides a root-level `print`, this calls that
83
+ * higher-level function (which may produce full declarations).
84
+ * Otherwise, falls back to the node-level dispatcher.
85
+ */
86
+ print: (node: SchemaNode) => T['printOutput'] | null | undefined
87
+ }
88
+
89
+ /**
90
+ * Builder function passed to `definePrinter`.
91
+ *
92
+ * It receives resolved options and returns:
93
+ * - `name`
94
+ * - `options`
95
+ * - `nodes` handlers
96
+ * - optional top-level `print` override
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * const build = (options: {}) => ({ name: 'x' as const, options, nodes: {} })
101
+ * ```
102
+ */
103
+ type PrinterBuilder<T extends PrinterFactoryOptions> = (options: T['options']) => {
104
+ name: T['name']
105
+ /**
106
+ * Options to store on the printer.
107
+ */
108
+ options: T['options']
109
+ nodes: Partial<{
110
+ [K in SchemaType]: PrinterHandler<T['output'], T['options'], K>
111
+ }>
112
+ /**
113
+ * 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),
115
+ * not the override itself — so recursion is safe.
116
+ */
117
+ print?: (this: PrinterHandlerContext<T['output'], T['options']>, node: SchemaNode) => T['printOutput'] | null | undefined
118
+ }
119
+
120
+ /**
121
+ * Creates a schema printer factory.
122
+ *
123
+ * This function wraps a builder and makes options optional at call sites.
124
+ *
125
+ * The builder receives resolved options and returns:
126
+ * - `name` — a unique identifier for the printer
127
+ * - `options` — options stored on the returned printer instance
128
+ * - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
129
+ * - `print` _(optional)_ — top-level override exposed as `printer.print`
130
+ * - Inside this function, `this.print(node)` still dispatches to the `nodes` map
131
+ * - This keeps recursion safe and avoids self-calls
132
+ *
133
+ * When no `print` override is provided, `printer.print` is the node-level dispatcher directly.
134
+ *
135
+ * @example Basic usage — Zod schema printer
136
+ * ```ts
137
+ * type ZodPrinter = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
138
+ *
139
+ * export const zodPrinter = definePrinter<ZodPrinter>((options) => ({
140
+ * name: 'zod',
141
+ * options: { strict: options.strict ?? true },
142
+ * nodes: {
143
+ * string: () => 'z.string()',
144
+ * object(node) {
145
+ * const props = node.properties.map(p => `${p.name}: ${this.print(p.schema)}`).join(', ')
146
+ * return `z.object({ ${props} })`
147
+ * },
148
+ * },
149
+ * }))
150
+ * ```
151
+ */
152
+ export function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryOptions>(build: PrinterBuilder<T>): (options?: T['options']) => Printer<T> {
153
+ return createPrinterFactory<SchemaNode, SchemaType, SchemaNodeByType>((node) => node.type)(build) as (options?: T['options']) => Printer<T>
154
+ }
155
+
156
+ /**
157
+ * Generic printer-factory function used by `definePrinter` and `defineFunctionPrinter`.
158
+ **
159
+ * @example
160
+ * ```ts
161
+ * export const defineFunctionPrinter = createPrinterFactory<FunctionNode, FunctionNodeType, FunctionNodeByType>(
162
+ * (node) => kindToHandlerKey[node.kind],
163
+ * )
164
+ * ```
165
+ */
166
+ export function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined) {
167
+ return function <T extends PrinterFactoryOptions>(
168
+ build: (options: T['options']) => {
169
+ name: T['name']
170
+ options: T['options']
171
+ nodes: Partial<{
172
+ [K in TKey]: (
173
+ this: { print: (node: TNode) => T['output'] | null | undefined; options: T['options'] },
174
+ node: TNodeByKey[K],
175
+ ) => T['output'] | null | undefined
176
+ }>
177
+ print?: (this: { print: (node: TNode) => T['output'] | null | undefined; options: T['options'] }, node: TNode) => T['printOutput'] | null | undefined
178
+ },
179
+ ): (options?: T['options']) => { name: T['name']; options: T['options']; print: (node: TNode) => T['printOutput'] | null | undefined } {
180
+ return (options) => {
181
+ const { name, options: resolvedOptions, nodes, print: printOverride } = build(options ?? ({} as T['options']))
182
+
183
+ const context = {
184
+ options: resolvedOptions,
185
+ print: (node: TNode): T['output'] | null | undefined => {
186
+ const key = getKey(node)
187
+ if (key === undefined) return null
188
+
189
+ const handler = nodes[key]
190
+
191
+ if (!handler) return null
192
+
193
+ return (handler as (this: typeof context, node: TNode) => T['output'] | null | undefined).call(context, node)
194
+ },
195
+ }
196
+
197
+ return {
198
+ name,
199
+ options: resolvedOptions,
200
+ print: (printOverride ? printOverride.bind(context) : context.print) as (node: TNode) => T['printOutput'] | null | undefined,
201
+ }
202
+ }
203
+ }
204
+ }
package/src/refs.ts CHANGED
@@ -2,12 +2,34 @@ import type { RootNode } from './nodes/root.ts'
2
2
  import type { SchemaNode } from './nodes/schema.ts'
3
3
 
4
4
  /**
5
- * Schema name to `SchemaNode` mapping.
5
+ * Lookup map from schema name to `SchemaNode`.
6
6
  */
7
7
  export type RefMap = Map<string, SchemaNode>
8
8
 
9
9
  /**
10
- * Indexes named schemas from `root.schemas` by name. Unnamed schemas are skipped.
10
+ * Returns the last path segment of a reference string.
11
+ *
12
+ * Example: `#/components/schemas/Pet` becomes `Pet`.
13
+ *
14
+ * @example
15
+ * ```ts
16
+ * extractRefName('#/components/schemas/Pet') // 'Pet'
17
+ * ```
18
+ */
19
+ export function extractRefName(ref: string): string {
20
+ return ref.split('/').at(-1) ?? ref
21
+ }
22
+
23
+ /**
24
+ * Builds a `RefMap` from `root.schemas` using each schema's `name`.
25
+ *
26
+ * Unnamed schemas are skipped.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const refMap = buildRefMap(root)
31
+ * const pet = refMap.get('Pet')
32
+ * ```
11
33
  */
12
34
  export function buildRefMap(root: RootNode): RefMap {
13
35
  const map: RefMap = new Map()
@@ -21,14 +43,24 @@ export function buildRefMap(root: RootNode): RefMap {
21
43
  }
22
44
 
23
45
  /**
24
- * Looks up a schema by name. Prefer over `RefMap.get()` to keep the resolution strategy swappable.
46
+ * Resolves a schema by name from a `RefMap`.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * const petSchema = resolveRef(refMap, 'Pet')
51
+ * ```
25
52
  */
26
53
  export function resolveRef(refMap: RefMap, ref: string): SchemaNode | undefined {
27
54
  return refMap.get(ref)
28
55
  }
29
56
 
30
57
  /**
31
- * Converts a `RefMap` to a plain object.
58
+ * Converts a `RefMap` into a plain object.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * const refsObject = refMapToObject(refMap)
63
+ * ```
32
64
  */
33
65
  export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
34
66
  return Object.fromEntries(refMap)
@@ -0,0 +1,45 @@
1
+ import { pascalCase } from '@internals/utils'
2
+ import { narrowSchema } from './guards.ts'
3
+ import type { SchemaNode } from './nodes/schema.ts'
4
+ import { extractRefName } from './refs.ts'
5
+ import { collect } from './visitor.ts'
6
+
7
+ export function findDiscriminator(mapping: Record<string, string> | undefined, ref: string | undefined): string | null {
8
+ if (!mapping || !ref) return null
9
+ return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null
10
+ }
11
+
12
+ export function childName(parentName: string | null | undefined, propName: string): string | null {
13
+ return parentName ? pascalCase([parentName, propName].join(' ')) : null
14
+ }
15
+
16
+ export function enumPropName(parentName: string | null | undefined, propName: string, enumSuffix: string): string {
17
+ return pascalCase([parentName, propName, enumSuffix].filter(Boolean).join(' '))
18
+ }
19
+
20
+ /**
21
+ * Collects import entries for all `ref` schema nodes in `node`.
22
+ */
23
+ export function collectImports<TImport>({
24
+ node,
25
+ nameMapping,
26
+ resolve,
27
+ }: {
28
+ node: SchemaNode
29
+ nameMapping: Map<string, string>
30
+ resolve: (schemaName: string) => TImport | undefined
31
+ }): Array<TImport> {
32
+ return collect<TImport>(node, {
33
+ schema(schemaNode): TImport | undefined {
34
+ const schemaRef = narrowSchema(schemaNode, 'ref')
35
+ if (!schemaRef?.ref) return
36
+
37
+ const rawName = extractRefName(schemaRef.ref)
38
+ const schemaName = nameMapping.get(rawName) ?? rawName
39
+ const result = resolve(schemaName)
40
+ if (!result) return
41
+
42
+ return result
43
+ },
44
+ })
45
+ }