@kubb/renderer-jsx 5.0.0-beta.7 → 5.0.0-beta.70

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.
@@ -1,186 +0,0 @@
1
- import type { ExportNode, ImportNode, SourceNode } from '@kubb/ast'
2
- import type { Key, KubbReactElement, KubbReactNode } from '../types.ts'
3
-
4
- type BasePropsWithBaseName = {
5
- /**
6
- * Base file name including extension, derived from the input path.
7
- * Based on UNIX basename convention: `${name}${extname}`.
8
- *
9
- * @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
10
- *
11
- * @example
12
- * `baseName: 'petStore.ts'`
13
- */
14
- baseName: `${string}.${string}`
15
- /**
16
- * Fully qualified path to the generated file.
17
- *
18
- * @example
19
- * `path: 'src/models/petStore.ts'`
20
- */
21
- path: string
22
- }
23
-
24
- type BasePropsWithoutBaseName = {
25
- baseName?: never
26
- /**
27
- * Fully qualified path to the generated file.
28
- * Optional when `baseName` is omitted — the component renders its children inline.
29
- */
30
- path?: string
31
- }
32
-
33
- type BaseProps = BasePropsWithBaseName | BasePropsWithoutBaseName
34
-
35
- type Props<TMeta> = BaseProps & {
36
- key?: Key
37
- /**
38
- * Arbitrary metadata attached to the file node.
39
- * Used by plugins for barrel generation and custom post-processing.
40
- */
41
- meta?: TMeta
42
- /**
43
- * Text prepended to the generated file content before any source blocks.
44
- */
45
- banner?: string
46
- /**
47
- * Text appended to the generated file content after all source blocks.
48
- */
49
- footer?: string
50
- /**
51
- * Child nodes rendered as the content of this file (source blocks, imports, exports).
52
- */
53
- children?: KubbReactNode
54
- }
55
-
56
- /**
57
- * Declares a generated file entry to be collected by the renderer.
58
- *
59
- * When both `baseName` and `path` are provided, the component registers a new
60
- * `FileNode` and passes its children through as source blocks.
61
- * When either is omitted the children are rendered inline without creating a file entry.
62
- *
63
- * @example Basic file with a source block
64
- * ```tsx
65
- * <File baseName="petStore.ts" path="src/models/petStore.ts">
66
- * <File.Source name="Pet" isExportable isIndexable>
67
- * {`export type Pet = { id: number; name: string }`}
68
- * </File.Source>
69
- * </File>
70
- * ```
71
- */
72
- export function File<TMeta extends object = object>({ children, ...props }: Props<TMeta>): KubbReactElement {
73
- const { baseName, path } = props
74
-
75
- if (!baseName || !path) {
76
- return <>{children}</>
77
- }
78
-
79
- return <kubb-file {...props}>{children}</kubb-file>
80
- }
81
-
82
- File.displayName = 'File'
83
-
84
- type FileSourceProps = Omit<SourceNode, 'kind' | 'value'> & {
85
- key?: Key
86
- /**
87
- * Child nodes rendered as the source content of this block.
88
- */
89
- children?: KubbReactNode
90
- }
91
-
92
- /**
93
- * Marks a block of source text to be associated with the enclosing {@link File}.
94
- *
95
- * Children are treated as the source string. When `isExportable` is `true` the
96
- * `name` is used for deduplication and barrel generation.
97
- *
98
- * @example Exportable, indexable source block
99
- * ```tsx
100
- * <File.Source name="Pet" isExportable isIndexable>
101
- * {`export type Pet = { id: number; name: string }`}
102
- * </File.Source>
103
- * ```
104
- *
105
- * @example Type-only source block
106
- * ```tsx
107
- * <File.Source name="PetId" isTypeOnly isExportable>
108
- * {`export type PetId = string`}
109
- * </File.Source>
110
- * ```
111
- */
112
- function FileSource({ children, ...props }: FileSourceProps): KubbReactElement {
113
- const { name, isExportable, isIndexable, isTypeOnly } = props
114
-
115
- return (
116
- <kubb-source name={name} isTypeOnly={isTypeOnly} isExportable={isExportable} isIndexable={isIndexable}>
117
- {children}
118
- </kubb-source>
119
- )
120
- }
121
-
122
- FileSource.displayName = 'FileSource'
123
-
124
- type FileExportProps = Omit<ExportNode, 'kind'> & { key?: Key }
125
-
126
- /**
127
- * Declares an export entry for the enclosing {@link File}.
128
- *
129
- * The export is collected by the renderer and emitted at the top of the generated file.
130
- *
131
- * @example Named export
132
- * ```tsx
133
- * <File.Export name={['Pet']} path="./models/petStore" />
134
- * // export { Pet } from './models/petStore'
135
- * ```
136
- *
137
- * @example Type-only wildcard export
138
- * ```tsx
139
- * <File.Export path="./models/petStore" isTypeOnly />
140
- * // export type * from './models/petStore'
141
- * ```
142
- */
143
- function FileExport(props: FileExportProps): KubbReactElement {
144
- const { name, path, isTypeOnly, asAlias } = props
145
-
146
- return <kubb-export name={name} path={path} isTypeOnly={isTypeOnly} asAlias={asAlias} />
147
- }
148
-
149
- FileExport.displayName = 'FileExport'
150
-
151
- type FileImportProps = Omit<ImportNode, 'kind'> & { key?: Key }
152
-
153
- /**
154
- * Declares an import entry for the enclosing {@link File}.
155
- *
156
- * The import is collected by the renderer and emitted at the top of the generated file.
157
- *
158
- * @example Named import
159
- * ```tsx
160
- * <File.Import name={['useState']} path="react" />
161
- * // import { useState } from 'react'
162
- * ```
163
- *
164
- * @example Type-only import
165
- * ```tsx
166
- * <File.Import name={['Pet']} path="./models" isTypeOnly />
167
- * // import type { Pet } from './models'
168
- * ```
169
- *
170
- * @example Namespace import
171
- * ```tsx
172
- * <File.Import name="z" path="zod" isNameSpace />
173
- * // import * as z from 'zod'
174
- * ```
175
- */
176
- function FileImport(props: FileImportProps): KubbReactElement {
177
- const { name, root, path, isTypeOnly, isNameSpace } = props
178
-
179
- return <kubb-import name={name} root={root} path={path} isNameSpace={isNameSpace} isTypeOnly={isTypeOnly} />
180
- }
181
-
182
- FileImport.displayName = 'FileImport'
183
-
184
- File.Export = FileExport
185
- File.Import = FileImport
186
- File.Source = FileSource
@@ -1,152 +0,0 @@
1
- import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
2
-
3
- type Props = {
4
- key?: Key
5
- /**
6
- * Identifier of the generated function declaration.
7
- *
8
- * @example
9
- * `name: 'getPet'`
10
- */
11
- name: string
12
- /**
13
- * Emit `default` after the `export` keyword, making this the module's default export.
14
- * Requires `export` to also be `true`.
15
- * @default false
16
- */
17
- default?: boolean
18
- /**
19
- * Parameter list written verbatim between the function's parentheses.
20
- *
21
- * @example
22
- * `params: 'petId: string, options?: RequestOptions'`
23
- */
24
- params?: string
25
- /**
26
- * Emit the `export` keyword before the function declaration.
27
- * - `true` generates `export function name(…) { … }`
28
- * - `false` generates `function name(…) { … }`
29
- * @default false
30
- */
31
- export?: boolean
32
- /**
33
- * Emit the `async` keyword, making this an async function.
34
- * The return type is automatically wrapped in `Promise<returnType>` when both
35
- * `async` and `returnType` are set.
36
- * @default false
37
- */
38
- async?: boolean
39
- /**
40
- * TypeScript generic type parameters written verbatim between `<` and `>`.
41
- * Pass an array to emit multiple parameters separated by commas.
42
- *
43
- * @example Single generic
44
- * `generics: 'TData'`
45
- *
46
- * @example Multiple generics
47
- * `generics: ['TData', 'TError = unknown']`
48
- */
49
- generics?: string | string[]
50
- /**
51
- * TypeScript return type annotation written verbatim after `:`.
52
- * When `async` is `true`, the value is automatically wrapped in `Promise<…>`.
53
- *
54
- * @example
55
- * `returnType: 'Pet'`
56
- */
57
- returnType?: string
58
- /**
59
- * JSDoc block to prepend to the function declaration.
60
- * Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
61
- */
62
- JSDoc?: JSDoc
63
- /**
64
- * Child nodes rendered as the body of the function.
65
- */
66
- children?: KubbReactNode
67
- }
68
-
69
- /**
70
- * Generates a TypeScript function declaration.
71
- *
72
- * @example Async exported function with generics
73
- * ```tsx
74
- * <Function export async name="getPet" generics={['TData = Pet']} params="petId: string" returnType="TData">
75
- * {`return client.get(\`/pets/\${petId}\`)`}
76
- * </Function>
77
- * // export async function getPet<TData = Pet>(petId: string): Promise<TData> {
78
- * // return client.get(`/pets/${petId}`)
79
- * // }
80
- * ```
81
- */
82
- export function Function({ children, ...props }: Props): KubbReactElement {
83
- const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc } = props
84
-
85
- // Normalize generics array to comma-separated string for DOM attribute storage
86
- const genericsString = Array.isArray(generics) ? generics.join(', ').trim() : generics
87
-
88
- return (
89
- <kubb-function
90
- name={name}
91
- params={params}
92
- export={canExport}
93
- default={isDefault}
94
- async={isAsync}
95
- generics={genericsString}
96
- returnType={returnType}
97
- JSDoc={JSDoc}
98
- >
99
- {children}
100
- </kubb-function>
101
- )
102
- }
103
-
104
- Function.displayName = 'Function'
105
-
106
- type ArrowFunctionProps = Props & {
107
- /**
108
- * Render the arrow function as a single-line expression (no braces around the body).
109
- * - `true` generates `const name = (…) => expression`
110
- * - `false` generates `const name = (…) => { … }`
111
- * @default false
112
- */
113
- singleLine?: boolean
114
- }
115
-
116
- /**
117
- * Generates an arrow function expression assigned to a `const`.
118
- * Supports the same flags as {@link Function}.
119
- * Use `singleLine` to render the body as a concise expression rather than a block.
120
- *
121
- * @example Single-line arrow function
122
- * ```tsx
123
- * <Function.Arrow export name="double" params="n: number" returnType="number" singleLine>
124
- * {`n * 2`}
125
- * </Function.Arrow>
126
- * // export const double = (n: number): number => n * 2
127
- * ```
128
- */
129
- function ArrowFunction({ children, ...props }: ArrowFunctionProps) {
130
- const { name, default: isDefault, export: canExport, async, generics, params, returnType, JSDoc, singleLine } = props
131
-
132
- const genericsString = Array.isArray(generics) ? generics.join(', ').trim() : generics
133
-
134
- return (
135
- <kubb-arrow-function
136
- name={name}
137
- params={params}
138
- export={canExport}
139
- default={isDefault}
140
- async={async}
141
- generics={genericsString}
142
- returnType={returnType}
143
- singleLine={singleLine}
144
- JSDoc={JSDoc}
145
- >
146
- {children}
147
- </kubb-arrow-function>
148
- )
149
- }
150
-
151
- ArrowFunction.displayName = 'ArrowFunction'
152
- Function.Arrow = ArrowFunction
@@ -1,34 +0,0 @@
1
- import type { KubbReactElement } from '../types.ts'
2
-
3
- type Props = {
4
- /**
5
- * Raw JSX string to embed verbatim in the generated code.
6
- * Supports JSX fragments (`<>…</>`), elements, and any valid JSX syntax.
7
- * @example
8
- * ```tsx
9
- * <Jsx>{'<>\n <a href={href}>Open</a>\n</>'}</Jsx>
10
- * ```
11
- */
12
- children?: string
13
- }
14
-
15
- /**
16
- * Embeds a raw JSX string verbatim in the generated source code.
17
- *
18
- * Use this component when you need to include JSX markup (including fragments
19
- * `<>…</>`) in the body of a generated function or component. The `children`
20
- * prop must be a plain string — expression attributes that reference runtime
21
- * values should be written as template literals.
22
- *
23
- * @example
24
- * ```tsx
25
- * <Function name="MyComponent" export>
26
- * <Jsx>{'return (\n <>\n <div>Hello</div>\n </>\n)'}</Jsx>
27
- * </Function>
28
- * ```
29
- */
30
- export function Jsx({ children }: Props): KubbReactElement {
31
- return <kubb-jsx>{children}</kubb-jsx>
32
- }
33
-
34
- Jsx.displayName = 'Jsx'
@@ -1,70 +0,0 @@
1
- import { Component } from 'react'
2
- import type { KubbReactElement, KubbReactNode } from '../types.ts'
3
-
4
- type ErrorBoundaryProps = {
5
- onError: (error: Error) => void
6
- children?: KubbReactNode
7
- }
8
-
9
- class ErrorBoundary extends Component<{
10
- onError: ErrorBoundaryProps['onError']
11
- children?: KubbReactNode
12
- }> {
13
- state = { hasError: false }
14
-
15
- static displayName = 'ErrorBoundary'
16
- static getDerivedStateFromError(_error: Error) {
17
- return { hasError: true }
18
- }
19
-
20
- componentDidCatch(error: Error) {
21
- if (error) {
22
- this.props.onError(error)
23
- }
24
- }
25
-
26
- render() {
27
- if (this.state.hasError) {
28
- return null
29
- }
30
- return this.props.children
31
- }
32
- }
33
-
34
- type RootProps = {
35
- /**
36
- * Callback invoked to unmount the entire renderer tree.
37
- * Called with an `Error` when the exit is caused by a render error,
38
- * or with `undefined` for a clean shutdown.
39
- */
40
- onExit: (error?: Error) => void
41
- /**
42
- * Callback invoked whenever a render error is caught by the error boundary.
43
- * Use this to propagate errors up to the caller of {@link createRenderer}.
44
- */
45
- onError: (error: Error) => void
46
- /**
47
- * Child nodes rendered inside the error boundary.
48
- */
49
- children?: KubbReactNode
50
- }
51
-
52
- /**
53
- * Root component for the Kubb renderer tree.
54
- *
55
- * Wraps all children in an `ErrorBoundary` so that render errors are caught
56
- * and forwarded to `onError` rather than crashing the process.
57
- */
58
- export function Root({ onError, children }: RootProps): KubbReactElement {
59
- return (
60
- <ErrorBoundary
61
- onError={(error) => {
62
- onError(error)
63
- }}
64
- >
65
- {children}
66
- </ErrorBoundary>
67
- )
68
- }
69
-
70
- Root.displayName = 'Root'
@@ -1,66 +0,0 @@
1
- import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
2
-
3
- type TypeProps = {
4
- key?: Key
5
- /**
6
- * Identifier of the generated type alias.
7
- * Must start with an uppercase letter to follow TypeScript naming conventions.
8
- *
9
- * @example
10
- * `name: 'Pet'`
11
- */
12
- name: string
13
- /**
14
- * Emit the `export` keyword before the type alias declaration.
15
- * - `true` generates `export type Name = …`
16
- * - `false` generates `type Name = …`
17
- * @default false
18
- */
19
- export?: boolean
20
- /**
21
- * JSDoc block to prepend to the type alias declaration.
22
- * Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
23
- */
24
- JSDoc?: JSDoc
25
- /**
26
- * Child nodes rendered as the type expression on the right-hand side of the alias.
27
- */
28
- children?: KubbReactNode
29
- }
30
-
31
- /**
32
- * Generates a TypeScript type alias declaration.
33
- *
34
- * Throws if `name` does not start with an uppercase letter — TypeScript type aliases
35
- * should follow PascalCase naming conventions.
36
- *
37
- * @example Simple exported type alias
38
- * ```tsx
39
- * <Type export name="PetId">
40
- * {`string | number`}
41
- * </Type>
42
- * // export type PetId = string | number
43
- * ```
44
- *
45
- * @example Type alias with JSDoc
46
- * ```tsx
47
- * <Type export name="Pet" JSDoc={{ comments: ['@description A pet in the store.'] }}>
48
- * {`{ id: number; name: string }`}
49
- * </Type>
50
- * ```
51
- */
52
- export function Type({ children, ...props }: TypeProps): KubbReactElement {
53
- const { name, export: canExport, JSDoc } = props
54
-
55
- if (name.charAt(0).toUpperCase() !== name.charAt(0)) {
56
- throw new Error('Name should start with a capital letter(see TypeScript types)')
57
- }
58
-
59
- return (
60
- <kubb-type name={name} export={canExport} JSDoc={JSDoc}>
61
- {children}
62
- </kubb-type>
63
- )
64
- }
65
-
66
- Type.displayName = 'Type'
package/src/constants.ts DELETED
@@ -1,28 +0,0 @@
1
- import type { ElementNames } from './types.ts'
2
-
3
- /**
4
- * Name used for text-node entries in the virtual DOM.
5
- */
6
- export const TEXT_NODE_NAME = '#text' as const
7
-
8
- /**
9
- * Set of all element names recognized by the Kubb renderer.
10
- * Used to distinguish Kubb-owned elements from unrecognized or text nodes during tree traversal.
11
- */
12
- export const nodeNames = new Set<ElementNames>([
13
- 'kubb-export',
14
- 'kubb-file',
15
- 'kubb-source',
16
- 'kubb-import',
17
- 'kubb-function',
18
- 'kubb-arrow-function',
19
- 'kubb-const',
20
- 'kubb-type',
21
- 'kubb-jsx',
22
- 'kubb-text',
23
- 'kubb-root',
24
- 'kubb-app',
25
- 'br',
26
- 'indent',
27
- 'dedent',
28
- ] as const)
@@ -1,93 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
- import { Runtime } from './Runtime.tsx'
3
- import type { KubbReactElement } from './types.ts'
4
-
5
- type Options = {
6
- /**
7
- * Print each render result to the console for debugging.
8
- * Useful when diagnosing output differences between renders.
9
- * @default false
10
- */
11
- debug?: boolean
12
- }
13
-
14
- /**
15
- * The renderer instance returned by {@link createRenderer}.
16
- */
17
- type Renderer = {
18
- /**
19
- * Render a JSX element tree and collect the resulting {@link FileNode} entries.
20
- * Resolves once all synchronous render work (including React's flush) is done.
21
- */
22
- render(Element: KubbReactElement): Promise<void>
23
- /**
24
- * Tear down the renderer and release all React resources.
25
- * Pass an `Error` to signal an abnormal shutdown.
26
- */
27
- unmount(error?: Error | number | null): void
28
- /**
29
- * The {@link FileNode} entries collected from the most recent `render` call.
30
- */
31
- files: Array<FileNode>
32
- }
33
-
34
- /**
35
- * Create a Kubb JSX renderer.
36
- *
37
- * The renderer converts a React JSX element tree — built from the components in this
38
- * package — into an array of {@link FileNode} entries representing the generated files.
39
- *
40
- * @example Basic usage
41
- * ```ts
42
- * import { createRenderer, File } from '@kubb/renderer-jsx'
43
- *
44
- * const renderer = createRenderer()
45
- * await renderer.render(
46
- * <File baseName="pet.ts" path="src/models/pet.ts">
47
- * <File.Source name="Pet" isExportable isIndexable>
48
- * {`export type Pet = { id: number; name: string }`}
49
- * </File.Source>
50
- * </File>
51
- * )
52
- * console.log(renderer.files) // [FileNode]
53
- * renderer.unmount()
54
- * ```
55
- */
56
- export function createRenderer(options: Options = {}): Renderer {
57
- const runtime = new Runtime(options)
58
-
59
- return {
60
- async render(Element) {
61
- await runtime.render(Element)
62
- },
63
- get files() {
64
- return runtime.nodes
65
- },
66
- unmount(error) {
67
- runtime.unmount(error)
68
- },
69
- }
70
- }
71
-
72
- /**
73
- * A renderer factory for generators that produce JSX output.
74
- *
75
- * Pass this as the `renderer` property of a `defineGenerator` call so that
76
- * core can render the JSX element tree returned by your generator methods
77
- * without a hard dependency on `@kubb/renderer-jsx`.
78
- *
79
- * @example
80
- * ```ts
81
- * import { jsxRenderer } from '@kubb/renderer-jsx'
82
- * import { defineGenerator } from '@kubb/core'
83
- *
84
- * export const myGenerator = defineGenerator<PluginTs>({
85
- * name: 'my-generator',
86
- * renderer: jsxRenderer,
87
- * schema(node, options) {
88
- * return <File baseName="output.ts" path="src/output.ts">...</File>
89
- * },
90
- * })
91
- * ```
92
- */
93
- export const jsxRenderer: () => Renderer = () => createRenderer()