@kubb/core 5.0.0-beta.6 → 5.0.0-beta.61

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.
Files changed (56) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +25 -158
  3. package/dist/diagnostics-DiaUv_iK.d.ts +2904 -0
  4. package/dist/index.cjs +2523 -1071
  5. package/dist/index.cjs.map +1 -1
  6. package/dist/index.d.ts +80 -273
  7. package/dist/index.js +2513 -1067
  8. package/dist/index.js.map +1 -1
  9. package/dist/memoryStorage-CUj1hrxa.cjs +823 -0
  10. package/dist/memoryStorage-CUj1hrxa.cjs.map +1 -0
  11. package/dist/memoryStorage-CWFzAz4o.js +714 -0
  12. package/dist/memoryStorage-CWFzAz4o.js.map +1 -0
  13. package/dist/mocks.cjs +83 -23
  14. package/dist/mocks.cjs.map +1 -1
  15. package/dist/mocks.d.ts +36 -10
  16. package/dist/mocks.js +85 -27
  17. package/dist/mocks.js.map +1 -1
  18. package/package.json +8 -28
  19. package/src/FileManager.ts +86 -64
  20. package/src/FileProcessor.ts +170 -44
  21. package/src/KubbDriver.ts +909 -0
  22. package/src/Transform.ts +105 -0
  23. package/src/constants.ts +111 -20
  24. package/src/createAdapter.ts +112 -17
  25. package/src/createKubb.ts +140 -517
  26. package/src/createRenderer.ts +43 -28
  27. package/src/createReporter.ts +134 -0
  28. package/src/createStorage.ts +36 -23
  29. package/src/defineGenerator.ts +140 -17
  30. package/src/defineParser.ts +30 -12
  31. package/src/definePlugin.ts +375 -21
  32. package/src/defineResolver.ts +402 -212
  33. package/src/diagnostics.ts +662 -0
  34. package/src/index.ts +8 -8
  35. package/src/mocks.ts +97 -26
  36. package/src/reporters/cliReporter.ts +89 -0
  37. package/src/reporters/fileReporter.ts +103 -0
  38. package/src/reporters/jsonReporter.ts +20 -0
  39. package/src/reporters/report.ts +85 -0
  40. package/src/storages/fsStorage.ts +23 -55
  41. package/src/types.ts +411 -887
  42. package/dist/PluginDriver-BkTRD2H2.js +0 -946
  43. package/dist/PluginDriver-BkTRD2H2.js.map +0 -1
  44. package/dist/PluginDriver-Cadu4ORh.cjs +0 -1037
  45. package/dist/PluginDriver-Cadu4ORh.cjs.map +0 -1
  46. package/dist/types-DVPKmzw_.d.ts +0 -2159
  47. package/src/Kubb.ts +0 -300
  48. package/src/PluginDriver.ts +0 -426
  49. package/src/defineLogger.ts +0 -19
  50. package/src/defineMiddleware.ts +0 -62
  51. package/src/devtools.ts +0 -59
  52. package/src/renderNode.ts +0 -35
  53. package/src/utils/diagnostics.ts +0 -18
  54. package/src/utils/isInputPath.ts +0 -10
  55. package/src/utils/packageJSON.ts +0 -99
  56. /package/dist/{chunk--u3MIqq1.js → chunk-C0LytTxp.js} +0 -0
@@ -3,53 +3,68 @@ import type { FileNode } from '@kubb/ast'
3
3
  /**
4
4
  * Minimal interface any Kubb renderer must satisfy.
5
5
  *
6
- * The generic `TElement` is the type of the element the renderer accepts
7
- * e.g. `KubbReactElement` for `@kubb/renderer-jsx`, or a custom type for
8
- * your own renderer. Defaults to `unknown` so that generators which do not
9
- * care about the element type continue to work without specifying it.
10
- *
11
- * This allows core to drive rendering without a hard dependency on
12
- * `@kubb/renderer-jsx` or any specific renderer implementation.
6
+ * `TElement` is the type the renderer accepts, for example `KubbReactElement`
7
+ * for `@kubb/renderer-jsx` or a custom type for your own renderer. Defaults to
8
+ * `unknown` so generators that don't care about the element type work without
9
+ * specifying it.
13
10
  */
14
11
  export type Renderer<TElement = unknown> = {
12
+ /**
13
+ * Renders `element` and populates {@link files} with the resulting {@link FileNode} objects.
14
+ * Called once per render cycle. Must resolve before {@link files} is read.
15
+ */
15
16
  render(element: TElement): Promise<void>
16
- unmount(error?: Error | number | null): void
17
+ /**
18
+ * Accumulated {@link FileNode} results produced by the last {@link render} call.
19
+ * Not populated when {@link stream} is implemented.
20
+ */
17
21
  readonly files: Array<FileNode>
22
+ /**
23
+ * When present, core calls this instead of {@link render} and {@link files},
24
+ * forwarding each file to `FileManager` as soon as it is ready.
25
+ */
26
+ stream?(element: TElement): Iterable<FileNode>
27
+ /**
28
+ * Disposer hook so renderers participate in `using` blocks: `using r = rendererFactory()`
29
+ * runs cleanup on every exit path, including thrown errors.
30
+ */
31
+ [Symbol.dispose](): void
18
32
  }
19
33
 
20
34
  /**
21
- * A factory function that produces a fresh {@link Renderer} per render.
35
+ * A factory function that produces a fresh {@link Renderer} per render cycle.
22
36
  *
23
37
  * Generators use this to declare which renderer handles their output.
24
38
  */
25
39
  export type RendererFactory<TElement = unknown> = () => Renderer<TElement>
26
40
 
27
41
  /**
28
- * Creates a renderer factory for use in generator definitions.
42
+ * Defines a renderer factory. Renderers turn the generator's return value
43
+ * (JSX, a template string, a tree of any shape) into `FileNode`s that get
44
+ * written to disk.
29
45
  *
30
- * Wrap your renderer factory function with this helper to register it as the
31
- * renderer for a generator. Core will call this factory once per render cycle
32
- * to obtain a fresh renderer instance.
46
+ * A renderer can target output formats beyond JSX, for instance a Handlebars
47
+ * renderer or one that writes binary files. Plugins and generators pick the
48
+ * renderer to use via the `renderer` field on `defineGenerator`.
33
49
  *
34
- * @example
50
+ * @example A minimal renderer that wraps a custom runtime
35
51
  * ```ts
36
- * // packages/renderer-jsx/src/index.ts
37
- * export const jsxRenderer = createRenderer(() => {
38
- * const runtime = new Runtime()
52
+ * import { createRenderer } from '@kubb/core'
53
+ *
54
+ * export const myRenderer = createRenderer(() => {
55
+ * const runtime = new MyRuntime()
39
56
  * return {
40
- * async render(element) { await runtime.render(element) },
41
- * get files() { return runtime.nodes },
42
- * unmount(error) { runtime.unmount(error) },
57
+ * async render(element) {
58
+ * await runtime.render(element)
59
+ * },
60
+ * get files() {
61
+ * return runtime.files
62
+ * },
63
+ * [Symbol.dispose]() {
64
+ * runtime.dispose()
65
+ * },
43
66
  * }
44
67
  * })
45
- *
46
- * // packages/plugin-zod/src/generators/zodGenerator.tsx
47
- * import { jsxRenderer } from '@kubb/renderer-jsx'
48
- * export const zodGenerator = defineGenerator<PluginZod>({
49
- * name: 'zod',
50
- * renderer: jsxRenderer,
51
- * schema(node, options) { return <File ...>...</File> },
52
- * })
53
68
  * ```
54
69
  */
55
70
  export function createRenderer<TElement = unknown>(factory: RendererFactory<TElement>): RendererFactory<TElement> {
@@ -0,0 +1,134 @@
1
+ import type { Config } from './types.ts'
2
+ import type { Diagnostic } from './diagnostics.ts'
3
+
4
+ /**
5
+ * Numeric log-level thresholds used internally to compare verbosity.
6
+ *
7
+ * Higher numbers are more verbose.
8
+ */
9
+ export const logLevel = {
10
+ silent: Number.NEGATIVE_INFINITY,
11
+ error: 0,
12
+ warn: 1,
13
+ info: 3,
14
+ verbose: 4,
15
+ } as const
16
+
17
+ /**
18
+ * A built-in reporter that renders a run's output, independent of the live logger view.
19
+ *
20
+ * - `cli` renders the per-config summary to the terminal (the default).
21
+ * - `json` writes a machine-readable report to stdout, for CI.
22
+ * - `file` writes a config's diagnostics to `.kubb/kubb-<name>-<timestamp>.log`.
23
+ */
24
+ export type ReporterName = 'cli' | 'json' | 'file'
25
+
26
+ /**
27
+ * One config's outcome within a run, as handed to a {@link Reporter}.
28
+ */
29
+ export type GenerationResult = {
30
+ config: Config
31
+ /**
32
+ * Diagnostics collected while generating this config.
33
+ */
34
+ diagnostics: Array<Diagnostic>
35
+ /**
36
+ * Number of files written for this config.
37
+ */
38
+ filesCreated: number
39
+ status: 'success' | 'failed'
40
+ /**
41
+ * `process.hrtime()` snapshot taken when this config started generating.
42
+ */
43
+ hrStart: [number, number]
44
+ }
45
+
46
+ /**
47
+ * Render context passed alongside the {@link GenerationResult}, carrying knobs a reporter needs
48
+ * but that are not part of the run data (e.g. verbosity).
49
+ */
50
+ export type ReporterContext = {
51
+ /**
52
+ * Output verbosity. Use the `logLevel` constants exported from `@kubb/core`
53
+ * (`silent`, `error`, `warn`, `info`, `verbose`, `debug`).
54
+ */
55
+ logLevel: (typeof logLevel)[keyof typeof logLevel]
56
+ }
57
+
58
+ /**
59
+ * Host-facing reporter, as installed onto a run. Unlike a Logger (the live TUI view), a reporter
60
+ * never sees the event emitter. `report` runs once per config. `drain`, when present, runs once
61
+ * after the last config.
62
+ */
63
+ export type Reporter = {
64
+ /**
65
+ * Display name, matching a {@link ReporterName} for the built-ins.
66
+ */
67
+ name: string
68
+ /**
69
+ * Called once per config with that config's result and the render context.
70
+ */
71
+ report: (result: GenerationResult, context: ReporterContext) => void | Promise<void>
72
+ /**
73
+ * Optional finalizer called once after the run's last config. The host wires it to
74
+ * `kubb:lifecycle:end`. {@link createReporter} closes it over the reports `report` returned.
75
+ */
76
+ drain?: (context: ReporterContext) => void | Promise<void>
77
+ }
78
+
79
+ /**
80
+ * Reporter definition passed to {@link createReporter}. `report` returns the value to collect for
81
+ * this config (e.g. a built report), and the optional `drain` receives the collected reports to
82
+ * emit as one document. `T` is inferred from `report`'s return type.
83
+ */
84
+ export type UserReporter<T = void> = {
85
+ name: string
86
+ report: (result: GenerationResult, context: ReporterContext) => T | Promise<T>
87
+ drain?: (context: ReporterContext, reports: Array<T>) => void | Promise<void>
88
+ }
89
+
90
+ /**
91
+ * Defines a reporter. When the definition has a `drain`, the returned reporter buffers each value
92
+ * `report` returns and hands the array to `drain` once, then clears it. Without a `drain`, nothing
93
+ * is buffered. Wiring the reporter onto the run's events is the host's job, so the reporter only
94
+ * ever deals with a {@link GenerationResult}.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { createReporter, Diagnostics } from '@kubb/core'
99
+ *
100
+ * export const jsonReporter = createReporter({
101
+ * name: 'json',
102
+ * report(result) {
103
+ * return { status: Diagnostics.hasError(result.diagnostics) ? 'failed' : 'success', diagnostics: result.diagnostics }
104
+ * },
105
+ * drain(context, reports) {
106
+ * process.stdout.write(`${JSON.stringify(reports, null, 2)}\n`)
107
+ * },
108
+ * })
109
+ * ```
110
+ */
111
+ export function createReporter<T = void>(reporter: UserReporter<T>): Reporter {
112
+ const drain = reporter.drain
113
+ if (!drain) {
114
+ return {
115
+ name: reporter.name,
116
+ async report(result, context) {
117
+ await reporter.report(result, context)
118
+ },
119
+ }
120
+ }
121
+
122
+ const reports: Array<T> = []
123
+
124
+ return {
125
+ name: reporter.name,
126
+ async report(result, context) {
127
+ reports.push(await reporter.report(result, context))
128
+ },
129
+ async drain(context) {
130
+ await drain(context, reports)
131
+ reports.length = 0
132
+ },
133
+ }
134
+ }
@@ -1,68 +1,81 @@
1
+ /**
2
+ * Backend that persists generated files. Kubb ships with `fsStorage` (writes
3
+ * to disk) and `memoryStorage` (keeps everything in RAM). Implement this
4
+ * interface to write somewhere else, such as S3 or a database.
5
+ */
1
6
  export type Storage = {
2
7
  /**
3
- * Identifier used for logging and debugging (e.g. `'fs'`, `'s3'`).
8
+ * Identifier used in logs and diagnostics (`'fs'`, `'memory'`, `'s3'`).
4
9
  */
5
10
  readonly name: string
6
11
  /**
7
- * Returns `true` when an entry for `key` exists in storage.
12
+ * Returns `true` when an entry for `key` exists.
8
13
  */
9
14
  hasItem(key: string): Promise<boolean>
10
15
  /**
11
- * Returns the stored string value, or `null` when `key` does not exist.
16
+ * Reads the stored string. Returns `null` when the key is missing.
12
17
  */
13
18
  getItem(key: string): Promise<string | null>
14
19
  /**
15
- * Persists `value` under `key`, creating any required structure.
20
+ * Stores `value` under `key`, creating any required structure (directories,
21
+ * buckets, ...).
16
22
  */
17
23
  setItem(key: string, value: string): Promise<void>
18
24
  /**
19
- * Removes the entry for `key`. No-ops when the key does not exist.
25
+ * Deletes the entry for `key`. No-op when the key does not exist.
20
26
  */
21
27
  removeItem(key: string): Promise<void>
22
28
  /**
23
- * Returns all keys, optionally filtered to those starting with `base`.
29
+ * Returns every key. Pass `base` to filter to keys starting with that prefix.
24
30
  */
25
31
  getKeys(base?: string): Promise<Array<string>>
26
32
  /**
27
- * Removes all entries, optionally scoped to those starting with `base`.
33
+ * Removes every entry. Pass `base` to scope the wipe to a key prefix.
28
34
  */
29
35
  clear(base?: string): Promise<void>
30
36
  /**
31
- * Optional teardown hook called after the build completes.
37
+ * Optional teardown hook called after the build completes. Use to flush
38
+ * buffers, close connections, or release file locks.
32
39
  */
33
40
  dispose?(): Promise<void>
34
41
  }
35
42
 
36
43
  /**
37
- * Factory for implementing custom storage backends that control where generated files are written.
38
- *
39
- * Takes a builder function `(options: TOptions) => Storage` and returns a factory `(options?: TOptions) => Storage`.
40
- * Kubb provides filesystem and in-memory implementations out of the box.
44
+ * Defines a custom storage backend. The builder receives user options and
45
+ * returns a `Storage` implementation. Kubb ships with filesystem and in-memory
46
+ * storages. A custom backend writes generated files elsewhere, such as cloud
47
+ * storage or a database.
41
48
  *
42
- * @note Call the returned factory with optional options to instantiate the storage adapter.
43
- *
44
- * @example
49
+ * @example In-memory storage (the built-in implementation)
45
50
  * ```ts
46
51
  * import { createStorage } from '@kubb/core'
47
52
  *
48
53
  * export const memoryStorage = createStorage(() => {
49
54
  * const store = new Map<string, string>()
55
+ *
50
56
  * return {
51
57
  * name: 'memory',
52
- * async hasItem(key) { return store.has(key) },
53
- * async getItem(key) { return store.get(key) ?? null },
54
- * async setItem(key, value) { store.set(key, value) },
55
- * async removeItem(key) { store.delete(key) },
58
+ * async hasItem(key) {
59
+ * return store.has(key)
60
+ * },
61
+ * async getItem(key) {
62
+ * return store.get(key) ?? null
63
+ * },
64
+ * async setItem(key, value) {
65
+ * store.set(key, value)
66
+ * },
67
+ * async removeItem(key) {
68
+ * store.delete(key)
69
+ * },
56
70
  * async getKeys(base) {
57
71
  * const keys = [...store.keys()]
58
72
  * return base ? keys.filter((k) => k.startsWith(base)) : keys
59
73
  * },
60
- * async clear(base) { if (!base) store.clear() },
74
+ * async clear(base) {
75
+ * if (!base) store.clear()
76
+ * },
61
77
  * }
62
78
  * })
63
- *
64
- * // Instantiate:
65
- * const storage = memoryStorage()
66
79
  * ```
67
80
  */
68
81
  export function createStorage<TOptions = Record<string, never>>(build: (options: TOptions) => Storage): (options?: TOptions) => Storage {
@@ -1,16 +1,117 @@
1
- import type { PossiblePromise } from '@internals/utils'
2
- import type { FileNode, OperationNode, SchemaNode } from '@kubb/ast'
1
+ import type { AsyncEventEmitter, PossiblePromise } from '@internals/utils'
2
+ import type { FileNode, InputMeta, OperationNode, SchemaNode } from '@kubb/ast'
3
+ import type { Adapter } from './createAdapter.ts'
3
4
  import type { RendererFactory } from './createRenderer.ts'
4
- import type { GeneratorContext, PluginFactoryOptions } from './types.ts'
5
+ import type { KubbHooks } from './types.ts'
6
+ import type { KubbDriver } from './KubbDriver.ts'
7
+ import type { Plugin, PluginFactoryOptions } from './definePlugin.ts'
8
+ import type { Resolver } from './defineResolver.ts'
9
+ import type { Config } from './types.ts'
5
10
 
6
- export type { GeneratorContext } from './types.ts'
11
+ /**
12
+ * Context object passed to generator `schema`, `operation`, and `operations` methods.
13
+ *
14
+ * The adapter is always defined (guaranteed by `runPluginAstHooks`) so no runtime checks
15
+ * are needed. `ctx.options` carries resolved per-node options after exclude/include/override
16
+ * filtering for individual schema/operation calls, or plugin-level options for operations.
17
+ */
18
+ export type GeneratorContext<TOptions extends PluginFactoryOptions = PluginFactoryOptions> = {
19
+ /**
20
+ * The resolved Kubb config for this build, including `root`, `input`, `output`, and the
21
+ * full plugin list.
22
+ */
23
+ config: Config
24
+ /**
25
+ * Absolute path to the current plugin's output directory.
26
+ */
27
+ root: string
28
+ /**
29
+ * The driver running this build. Most generators never need it. Prefer the scoped helpers
30
+ * on this context (`getPlugin`, `getResolver`, `upsertFile`) over reaching into the driver.
31
+ */
32
+ driver: KubbDriver
33
+ /**
34
+ * Get a plugin by name, typed via `Kubb.PluginRegistry` when registered.
35
+ */
36
+ getPlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]> | undefined
37
+ getPlugin(name: string): Plugin | undefined
38
+ /**
39
+ * Get a plugin by name, throws an error if not found.
40
+ */
41
+ requirePlugin<TName extends keyof Kubb.PluginRegistry>(name: TName): Plugin<Kubb.PluginRegistry[TName]>
42
+ requirePlugin(name: string): Plugin
43
+ /**
44
+ * Get a resolver by plugin name, typed via `Kubb.PluginRegistry` when registered.
45
+ */
46
+ getResolver<TName extends keyof Kubb.PluginRegistry>(name: TName): Kubb.PluginRegistry[TName]['resolver']
47
+ getResolver(name: string): Resolver
48
+ /**
49
+ * Add files only if they don't exist.
50
+ */
51
+ addFile: (...file: Array<FileNode>) => Promise<void>
52
+ /**
53
+ * Merge sources into the same output file.
54
+ */
55
+ upsertFile: (...file: Array<FileNode>) => Promise<void>
56
+ /**
57
+ * The build's event bus. Emit or listen to any `KubbHooks` event, for example to react to
58
+ * `kubb:build:end` from inside a generator.
59
+ */
60
+ hooks: AsyncEventEmitter<KubbHooks>
61
+ /**
62
+ * The current plugin instance.
63
+ */
64
+ plugin: Plugin<TOptions>
65
+ /**
66
+ * The current plugin's resolver. It decides what every generated symbol and file path is
67
+ * called. Kubb picks a `setResolver` registration first, then the plugin's static
68
+ * `resolver`, then the built-in default.
69
+ *
70
+ * @example Resolve a type name
71
+ * `ctx.resolver.default('pet', 'type') // 'Pet'`
72
+ *
73
+ * @example Resolve an output file
74
+ * `ctx.resolver.resolveFile({ name: 'pet', extname: '.ts' }, { root, output })`
75
+ */
76
+ resolver: TOptions['resolver']
77
+ /**
78
+ * Report a warning. Collected as a `warning` diagnostic attributed to the current
79
+ * plugin. It surfaces in the run summary but does not fail the build. For a structured
80
+ * diagnostic with a code and source location, use `Diagnostics.report` or throw a
81
+ * `Diagnostics.Error` directly.
82
+ */
83
+ warn: (message: string) => void
84
+ /**
85
+ * Report an error. Collected as an `error` diagnostic attributed to the current
86
+ * plugin, which fails the build.
87
+ */
88
+ error: (error: string | Error) => void
89
+ /**
90
+ * Report an informational message. Collected as an `info` diagnostic attributed to
91
+ * the current plugin.
92
+ */
93
+ info: (message: string) => void
94
+ /**
95
+ * The configured adapter instance.
96
+ */
97
+ adapter: Adapter
98
+ /**
99
+ * Document metadata from the adapter: title, version, base URL, and pre-computed
100
+ * schema index fields (`circularNames`, `enumNames`).
101
+ */
102
+ meta: InputMeta
103
+ /**
104
+ * Resolved options after exclude/include/override filtering.
105
+ */
106
+ options: TOptions['resolvedOptions']
107
+ }
7
108
 
8
109
  /**
9
110
  * Declares a named generator unit that walks the AST and emits files.
10
111
  *
11
112
  * Each method (`schema`, `operation`, `operations`) is called for the matching node type.
12
- * Each method returns `TElement | Array<FileNode> | void`. JSX-based generators require a `renderer` factory.
13
- * Return `Array<FileNode>` directly or call `ctx.upsertFile()` manually and return `void` to bypass rendering.
113
+ * JSX-based generators require a `renderer` factory. Return `Array<FileNode>` directly, or call
114
+ * `ctx.upsertFile()` manually and return `null` to bypass rendering.
14
115
  *
15
116
  * @note Generators are consumed by plugins and registered via `ctx.addGenerator()` in `kubb:plugin:setup`.
16
117
  *
@@ -42,8 +143,7 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
42
143
  *
43
144
  * Generators that only return `Array<FileNode>` or `void` do not need to set this.
44
145
  *
45
- * Set `renderer: null` to explicitly opt out of rendering even when the parent plugin
46
- * declares a `renderer` (overrides the plugin-level fallback).
146
+ * Leave it unset or set `renderer: null` to opt out of rendering.
47
147
  *
48
148
  * @example
49
149
  * ```ts
@@ -57,28 +157,51 @@ export type Generator<TOptions extends PluginFactoryOptions = PluginFactoryOptio
57
157
  renderer?: RendererFactory<TElement> | null
58
158
  /**
59
159
  * Called for each schema node in the AST walk.
60
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
160
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
61
161
  * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
62
162
  */
63
- schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
163
+ schema?: (node: SchemaNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
64
164
  /**
65
165
  * Called for each operation node in the AST walk.
66
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
166
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
67
167
  * plus `ctx.options` with the per-node resolved options (after exclude/include/override).
68
168
  */
69
- operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
169
+ operation?: (node: OperationNode, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
70
170
  /**
71
171
  * Called once after all operations have been walked.
72
- * `ctx` carries the plugin context with `adapter` and `inputNode` guaranteed present,
172
+ * `ctx` carries the plugin context with `adapter` and `meta` (document metadata),
73
173
  * plus `ctx.options` with the plugin-level options for the batch call.
74
174
  */
75
- operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | void>
175
+ operations?: (nodes: Array<OperationNode>, ctx: GeneratorContext<TOptions>) => PossiblePromise<TElement | Array<FileNode> | undefined | null>
76
176
  }
77
177
 
78
178
  /**
79
- * Defines a generator. Returns the object as-is with correct `this` typings.
80
- * `applyHookResult` handles renderer elements and `File[]` uniformly using
81
- * the generator's declared `renderer` factory.
179
+ * Defines a generator: a unit of work that runs during the plugin's AST walk
180
+ * and produces files. Plugins register generators via `ctx.addGenerator()`
181
+ * inside `kubb:plugin:setup`.
182
+ *
183
+ * The returned object is the input as-is, but with `this` types preserved so
184
+ * `schema`/`operation`/`operations` methods are correctly typed against the
185
+ * plugin's `PluginFactoryOptions`. Renderer elements and `FileNode[]` returns
186
+ * are both handled by the runtime, so pick whichever style fits.
187
+ *
188
+ * @example JSX-based schema generator
189
+ * ```tsx
190
+ * import { defineGenerator } from '@kubb/core'
191
+ * import { jsxRenderer } from '@kubb/renderer-jsx'
192
+ *
193
+ * export const typeGenerator = defineGenerator({
194
+ * name: 'typescript',
195
+ * renderer: jsxRenderer,
196
+ * schema(node, ctx) {
197
+ * return (
198
+ * <File path={`${ctx.root}/${node.name}.ts`}>
199
+ * <Type node={node} resolver={ctx.resolver} />
200
+ * </File>
201
+ * )
202
+ * },
203
+ * })
204
+ * ```
82
205
  */
83
206
  export function defineGenerator<TOptions extends PluginFactoryOptions = PluginFactoryOptions, TElement = unknown>(
84
207
  generator: Generator<TOptions, TElement>,
@@ -4,41 +4,59 @@ type PrintOptions = {
4
4
  extname?: FileNode['extname']
5
5
  }
6
6
 
7
- export type Parser<TMeta extends object = any> = {
7
+ /**
8
+ * Converts a resolved {@link FileNode} into the final source string that gets
9
+ * written to disk. Kubb ships with TypeScript and TSX parsers. Add your own
10
+ * for new file types (JSON, Markdown, ...).
11
+ */
12
+ export type Parser<TMeta extends object = object, TNode = unknown> = {
13
+ /**
14
+ * Display name used in diagnostics and the parser registry.
15
+ */
8
16
  name: string
9
17
  /**
10
- * File extensions this parser handles.
11
- * Use `undefined` to create a catch-all fallback parser.
18
+ * File extensions this parser handles. Set to `undefined` to define a
19
+ * catch-all fallback used when no other parser claims the extension.
12
20
  *
13
- * @example Handled extensions
21
+ * @example
14
22
  * `['.ts', '.js']`
15
23
  */
16
24
  extNames: Array<FileNode['extname']> | undefined
17
25
  /**
18
- * Convert a resolved file to a string.
26
+ * Serialize the file's AST into source code.
19
27
  */
20
- parse(file: FileNode<TMeta>, options?: PrintOptions): Promise<string> | string
28
+ parse(file: FileNode<TMeta>, options?: PrintOptions): string
29
+ /**
30
+ * Render compiler AST nodes for this parser's language into source text.
31
+ * Plugins call this to format the nodes they assemble before handing them
32
+ * back to the parser as `FileNode.sources`.
33
+ */
34
+ print(...nodes: Array<TNode>): string
21
35
  }
22
36
 
23
37
  /**
24
- * Defines a parser with type safety. Creates parsers that transform generated files to strings based on their extension.
25
- *
26
- * @note Call the returned factory with optional options to instantiate the parser.
38
+ * Defines a parser with type-safe `this`. Used to register handlers for new
39
+ * file extensions or to plug a non-TypeScript output into the build.
27
40
  *
28
41
  * @example
29
42
  * ```ts
30
43
  * import { defineParser } from '@kubb/core'
44
+ * import { extractStringsFromNodes } from '@kubb/ast/utils'
31
45
  *
32
46
  * export const jsonParser = defineParser({
33
47
  * name: 'json',
34
48
  * extNames: ['.json'],
35
49
  * parse(file) {
36
- * const { extractStringsFromNodes } = await import('@kubb/ast')
37
- * return file.sources.map((s) => extractStringsFromNodes(s.nodes ?? [])).join('\n')
50
+ * return file.sources
51
+ * .map((source) => extractStringsFromNodes(source.nodes ?? []))
52
+ * .join('\n')
53
+ * },
54
+ * print(...nodes) {
55
+ * return nodes.map(String).join('\n')
38
56
  * },
39
57
  * })
40
58
  * ```
41
59
  */
42
- export function defineParser<TMeta extends object = any>(parser: Parser<TMeta>): Parser<TMeta> {
60
+ export function defineParser<T extends Parser>(parser: T): T {
43
61
  return parser
44
62
  }