@kubb/renderer-jsx 5.0.0-beta.75 → 5.0.0-beta.76

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 (47) hide show
  1. package/LICENSE +17 -10
  2. package/README.md +135 -0
  3. package/dist/index.cjs +385 -17926
  4. package/dist/index.d.ts +248 -165
  5. package/dist/index.js +370 -17908
  6. package/dist/jsx-dev-runtime.cjs +3 -10
  7. package/dist/jsx-dev-runtime.d.ts +4 -8
  8. package/dist/jsx-dev-runtime.js +1 -9
  9. package/dist/jsx-runtime.cjs +24 -6
  10. package/dist/jsx-runtime.d.ts +59 -10
  11. package/dist/jsx-runtime.js +24 -7
  12. package/dist/rolldown-runtime-C0LytTxp.js +8 -0
  13. package/dist/rolldown-runtime-ClG-MNz_.cjs +13 -0
  14. package/dist/types-OZ23urxc.d.ts +119 -0
  15. package/dist/types.d.ts +2 -2
  16. package/package.json +8 -37
  17. package/dist/chunk-Bb7HlUDG.js +0 -28
  18. package/dist/index.cjs.map +0 -1
  19. package/dist/index.js.map +0 -1
  20. package/dist/jsx-dev-runtime.cjs.map +0 -1
  21. package/dist/jsx-dev-runtime.js.map +0 -1
  22. package/dist/jsx-namespace-CNp0arTN.d.ts +0 -39
  23. package/dist/jsx-runtime-Cvu_ZYgL.js +0 -1448
  24. package/dist/jsx-runtime-Cvu_ZYgL.js.map +0 -1
  25. package/dist/jsx-runtime-DdmO3p0U.cjs +0 -1503
  26. package/dist/jsx-runtime-DdmO3p0U.cjs.map +0 -1
  27. package/dist/jsx-runtime.cjs.map +0 -1
  28. package/dist/jsx-runtime.js.map +0 -1
  29. package/dist/types-nAFMiWFw.d.ts +0 -168
  30. package/src/Renderer.ts +0 -184
  31. package/src/Runtime.tsx +0 -170
  32. package/src/components/Const.tsx +0 -72
  33. package/src/components/File.tsx +0 -186
  34. package/src/components/Function.tsx +0 -152
  35. package/src/components/Jsx.tsx +0 -34
  36. package/src/components/Root.tsx +0 -70
  37. package/src/components/Type.tsx +0 -66
  38. package/src/constants.ts +0 -28
  39. package/src/createRenderer.tsx +0 -93
  40. package/src/dom.ts +0 -105
  41. package/src/globals.ts +0 -34
  42. package/src/index.ts +0 -8
  43. package/src/jsx-dev-runtime.ts +0 -10
  44. package/src/jsx-namespace.d.ts +0 -52
  45. package/src/jsx-runtime.ts +0 -12
  46. package/src/types.ts +0 -207
  47. package/src/utils.ts +0 -267
@@ -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()
package/src/dom.ts DELETED
@@ -1,105 +0,0 @@
1
- import { TEXT_NODE_NAME } from './constants.ts'
2
- import type { DOMElement, DOMNode, DOMNodeAttribute, TextNode } from './types.ts'
3
-
4
- /**
5
- * Create a new, empty {@link DOMElement} with the given node name.
6
- * The element has no attributes, no children, and no parent.
7
- */
8
- export const createNode = (nodeName: string): DOMElement => {
9
- const node: DOMElement = {
10
- nodeName: nodeName as DOMElement['nodeName'],
11
- attributes: new Map(),
12
- childNodes: [],
13
- parentNode: undefined,
14
- }
15
-
16
- return node
17
- }
18
-
19
- /**
20
- * Append `childNode` as the last child of `node`.
21
- *
22
- * If `childNode` already has a parent, it is removed from that parent first
23
- * (matching standard DOM move semantics).
24
- * Text nodes (`nodeName === '#text'`) are silently ignored.
25
- */
26
- export const appendChildNode = (node: DOMNode, childNode: DOMElement | DOMNode): void => {
27
- if (childNode.parentNode) {
28
- removeChildNode(childNode.parentNode, childNode)
29
- }
30
-
31
- if (node.nodeName !== TEXT_NODE_NAME) {
32
- childNode.parentNode = node
33
- node.childNodes.push(childNode)
34
- }
35
- }
36
-
37
- /**
38
- * Insert `newChildNode` before `beforeChildNode` in `node`'s child list.
39
- *
40
- * If `newChildNode` already has a parent, it is removed from that parent first.
41
- * If `beforeChildNode` is not found, `newChildNode` is appended at the end.
42
- */
43
- export const insertBeforeNode = (node: DOMElement, newChildNode: DOMNode, beforeChildNode: DOMNode): void => {
44
- if (newChildNode.parentNode) {
45
- removeChildNode(newChildNode.parentNode, newChildNode)
46
- }
47
-
48
- newChildNode.parentNode = node
49
-
50
- const index = node.childNodes.indexOf(beforeChildNode)
51
- if (index >= 0) {
52
- node.childNodes.splice(index, 0, newChildNode)
53
-
54
- return
55
- }
56
-
57
- node.childNodes.push(newChildNode)
58
- }
59
-
60
- /**
61
- * Remove `removeNode` from `node`'s child list and clear its `parentNode` reference.
62
- * Does nothing if `removeNode` is not a direct child of `node`.
63
- */
64
- export const removeChildNode = (node: DOMElement, removeNode: DOMNode): void => {
65
- removeNode.parentNode = undefined
66
-
67
- const index = node.childNodes.indexOf(removeNode)
68
- if (index >= 0) {
69
- node.childNodes.splice(index, 1)
70
- }
71
- }
72
-
73
- /**
74
- * Set an attribute on `node`, storing it in the node's `attributes` map.
75
- */
76
- export const setAttribute = (node: DOMElement, key: string, value: DOMNodeAttribute): void => {
77
- node.attributes.set(key, value)
78
- }
79
-
80
- /**
81
- * Create a new {@link TextNode} with the given text value.
82
- */
83
- export const createTextNode = (text: string): TextNode => {
84
- const node: TextNode = {
85
- nodeName: TEXT_NODE_NAME,
86
- nodeValue: text,
87
- parentNode: undefined,
88
- }
89
-
90
- setTextNodeValue(node, text)
91
-
92
- return node
93
- }
94
-
95
- /**
96
- * Update the `nodeValue` of an existing {@link TextNode}.
97
- * Non-string values are coerced to strings via `String(text)`.
98
- */
99
- export const setTextNodeValue = (node: TextNode, text: string): void => {
100
- if (typeof text !== 'string') {
101
- text = String(text)
102
- }
103
-
104
- node.nodeValue = text
105
- }
package/src/globals.ts DELETED
@@ -1,34 +0,0 @@
1
- import type React from 'react'
2
- import type {
3
- KubbExportProps,
4
- KubbFileProps,
5
- KubbImportProps,
6
- KubbJsxProps,
7
- KubbReactElement,
8
- KubbReactNode,
9
- KubbSourceProps,
10
- KubbTextProps,
11
- LineBreakProps,
12
- } from './types.ts'
13
-
14
- declare global {
15
- namespace JSX {
16
- type Element = KubbReactElement
17
-
18
- interface ElementClass extends React.ComponentClass<any> {
19
- render(): KubbReactNode
20
- }
21
-
22
- interface IntrinsicElements {
23
- 'kubb-jsx': KubbJsxProps
24
- 'kubb-text': KubbTextProps
25
- 'kubb-file': KubbFileProps
26
- 'kubb-source': KubbSourceProps
27
- 'kubb-import': KubbImportProps
28
- 'kubb-export': KubbExportProps
29
- br: LineBreakProps
30
- indent: {}
31
- dedent: {}
32
- }
33
- }
34
- }
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export { createContext, inject, provide, unprovide } from '@internals/utils'
2
- export { Const } from './components/Const.tsx'
3
- export { File } from './components/File.tsx'
4
- export { Function } from './components/Function.tsx'
5
- export { Jsx } from './components/Jsx.tsx'
6
- export { Root } from './components/Root.tsx'
7
- export { Type } from './components/Type.tsx'
8
- export { createRenderer, jsxRenderer } from './createRenderer.tsx'
@@ -1,10 +0,0 @@
1
- import * as React from 'react/jsx-runtime'
2
- import type { KubbReactElement, KubbReactNode } from './types.ts'
3
-
4
- export const Fragment = React.Fragment
5
- export const jsxDEV = React.jsx
6
-
7
- export type * from './jsx-namespace.d.ts'
8
-
9
- export type JSXElement = KubbReactElement
10
- export type ReactNode = KubbReactNode
@@ -1,52 +0,0 @@
1
- import type React from 'react'
2
-
3
- import type {
4
- KubbArrowFunctionProps,
5
- KubbConstProps,
6
- KubbExportProps,
7
- KubbFileProps,
8
- KubbFunctionProps,
9
- KubbImportProps,
10
- KubbJsxProps,
11
- KubbReactElement,
12
- KubbReactNode,
13
- KubbSourceProps,
14
- KubbTextProps,
15
- KubbTypeProps,
16
- LineBreakProps,
17
- } from './types'
18
-
19
- export namespace JSX {
20
- type ElementType = React.JSX.ElementType
21
- type Element = KubbReactElement
22
-
23
- interface ElementClass extends React.JSX.ElementClass {
24
- render(): KubbReactNode
25
- }
26
- interface ElementAttributesProperty {
27
- props: {}
28
- }
29
-
30
- interface ElementChildrenAttribute {
31
- children: {}
32
- }
33
-
34
- interface IntrinsicElements extends React.JSX.IntrinsicElements {
35
- 'kubb-jsx': KubbJsxProps
36
- 'kubb-text': KubbTextProps
37
- 'kubb-file': KubbFileProps
38
- 'kubb-source': KubbSourceProps
39
- 'kubb-import': KubbImportProps
40
- 'kubb-export': KubbExportProps
41
- 'kubb-function': KubbFunctionProps
42
- 'kubb-arrow-function': KubbArrowFunctionProps
43
- 'kubb-const': KubbConstProps
44
- 'kubb-type': KubbTypeProps
45
- br: LineBreakProps
46
- indent: {}
47
- dedent: {}
48
- }
49
- type LibraryManagedAttributes<C, P> = React.JSX.LibraryManagedAttributes<C, P>
50
- interface IntrinsicClassAttributes<T> extends React.JSX.IntrinsicClassAttributes<T> {}
51
- interface IntrinsicElements extends React.JSX.IntrinsicElements {}
52
- }
@@ -1,12 +0,0 @@
1
- import * as React from 'react/jsx-runtime'
2
- import type { KubbReactElement, KubbReactNode } from './types.ts'
3
-
4
- export const Fragment = React.Fragment
5
- export const jsx = React.jsx
6
- export const jsxDEV = React.jsx
7
- export const jsxs = React.jsxs
8
-
9
- export type * from './jsx-namespace.d.ts'
10
-
11
- export type JSXElement = KubbReactElement
12
- export type ReactNode = KubbReactNode
package/src/types.ts DELETED
@@ -1,207 +0,0 @@
1
- import type { ArrowFunctionNode, ConstNode, ExportNode, FileNode, FunctionNode, ImportNode, SourceNode, TypeNode } from '@kubb/ast'
2
- import type React from 'react'
3
- import type { JSX, ReactNode } from 'react'
4
-
5
- /**
6
- * Unique identifier for a React element in lists or conditional renders.
7
- */
8
- export type Key = string | number | bigint
9
-
10
- /**
11
- * Custom element names recognized by the Kubb JSX renderer.
12
- * Each name maps to a corresponding AST node type in the generated code.
13
- */
14
- export type ElementNames =
15
- | 'br'
16
- | 'div'
17
- | 'indent'
18
- | 'dedent'
19
- | 'kubb-jsx'
20
- | 'kubb-text'
21
- | 'kubb-file'
22
- | 'kubb-source'
23
- | 'kubb-import'
24
- | 'kubb-export'
25
- | 'kubb-function'
26
- | 'kubb-arrow-function'
27
- | 'kubb-const'
28
- | 'kubb-type'
29
- | 'kubb-root'
30
- | 'kubb-app'
31
-
32
- type Node = {
33
- parentNode: DOMElement | undefined
34
- internal_static?: boolean
35
- }
36
-
37
- /**
38
- * Allowed attribute value types for DOM elements.
39
- */
40
- export type DOMNodeAttribute = boolean | string | number | Record<string, unknown> | Array<unknown>
41
-
42
- type TextName = '#text'
43
-
44
- /**
45
- * Leaf DOM node containing raw text.
46
- */
47
- export type TextNode = {
48
- nodeName: TextName
49
- nodeValue: string
50
- } & Node
51
-
52
- /**
53
- * Virtual DOM node — either a text node or a named element.
54
- */
55
- export type DOMNode<T = { nodeName: NodeNames }> = T extends {
56
- nodeName: infer U
57
- }
58
- ? U extends '#text'
59
- ? TextNode
60
- : DOMElement
61
- : never
62
-
63
- type OutputTransformer = (s: string, index: number) => string
64
-
65
- /**
66
- * Named element in the Kubb virtual DOM tree.
67
- * Stores attributes, child nodes, and lifecycle callbacks for rendering.
68
- */
69
- export type DOMElement = {
70
- nodeName: ElementNames
71
- /**
72
- * Key/value attributes passed as JSX props to this element.
73
- */
74
- attributes: Map<string, DOMNodeAttribute>
75
- /**
76
- * Ordered list of child nodes attached to this element.
77
- */
78
- childNodes: DOMNode[]
79
- internal_transform?: OutputTransformer
80
-
81
- // Internal properties
82
- isStaticDirty?: boolean
83
- staticNode?: DOMElement
84
- onComputeLayout?: () => void
85
- onRender?: () => void
86
- onImmediateRender?: () => void
87
- } & Node
88
-
89
- type NodeNames = ElementNames | TextName
90
-
91
- /**
92
- * React node type for Kubb JSX components.
93
- */
94
- export type KubbReactNode = ReactNode
95
-
96
- /**
97
- * React element type returned by Kubb JSX components.
98
- */
99
- export type KubbReactElement = JSX.Element
100
-
101
- /**
102
- * Props for the `<kubb-jsx>` element.
103
- * Embeds a raw JSX string verbatim in generated output.
104
- */
105
- export type KubbJsxProps = {
106
- children?: string
107
- }
108
-
109
- /**
110
- * Props for the `<kubb-text>` element.
111
- * Wraps React children as plain text in the output.
112
- */
113
- export type KubbTextProps = {
114
- children?: KubbReactNode
115
- }
116
-
117
- /**
118
- * Props for the `<kubb-file>` element.
119
- * Represents a generated file.
120
- */
121
- export type KubbFileProps = {
122
- id?: string
123
- children?: KubbReactNode
124
- baseName: string
125
- path: string
126
- override?: boolean
127
- meta?: FileNode['meta']
128
- }
129
-
130
- /**
131
- * Props for the `<kubb-source>` element.
132
- * Marks a block of source text associated with a file.
133
- */
134
- export type KubbSourceProps = Omit<SourceNode, 'kind'> & {
135
- children?: KubbReactNode
136
- }
137
-
138
- /**
139
- * Props for the `<kubb-import>` element.
140
- * Declares an import statement in the generated file.
141
- */
142
- export type KubbImportProps = Omit<ImportNode, 'kind'> & {}
143
-
144
- /**
145
- * Props for the `<kubb-export>` element.
146
- * Declares an export statement in the generated file.
147
- */
148
- export type KubbExportProps = Omit<ExportNode, 'kind'> & {}
149
-
150
- /**
151
- * Props for the `<kubb-function>` element.
152
- * Generates a function declaration.
153
- */
154
- export type KubbFunctionProps = Omit<FunctionNode, 'kind'> & {
155
- children?: KubbReactNode
156
- }
157
-
158
- /**
159
- * Props for the `<kubb-arrow-function>` element.
160
- * Generates an arrow function declaration.
161
- */
162
- export type KubbArrowFunctionProps = Omit<ArrowFunctionNode, 'kind'> & {
163
- children?: KubbReactNode
164
- }
165
-
166
- /**
167
- * Props for the `<kubb-const>` element.
168
- * Generates a constant declaration.
169
- */
170
- export type KubbConstProps = Omit<ConstNode, 'kind'> & {
171
- children?: KubbReactNode
172
- }
173
-
174
- /**
175
- * Props for the `<kubb-type>` element.
176
- * Generates a TypeScript type alias declaration.
177
- */
178
- export type KubbTypeProps = Omit<TypeNode, 'kind'> & {
179
- children?: KubbReactNode
180
- }
181
-
182
- /**
183
- * Props for the HTML `<br>` element.
184
- */
185
- export type LineBreakProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLBRElement>, HTMLBRElement>
186
-
187
- /**
188
- * JSDoc comment block to attach to a generated declaration.
189
- * Each string in `comments` becomes one line inside the `/** … *\/` block.
190
- *
191
- * @example
192
- * ```ts
193
- * { comments: ['@description A pet object.', '@deprecated Use PetV2 instead.'] }
194
- * // Emits:
195
- * // /**
196
- * // * @description A pet object.
197
- * // * @deprecated Use PetV2 instead.
198
- * // *\/
199
- * ```
200
- */
201
- export type JSDoc = {
202
- /**
203
- * Lines to emit inside the JSDoc block, in source order.
204
- * Use standard JSDoc tags such as `@description`, `@deprecated`, `@see`, etc.
205
- */
206
- comments: Array<string>
207
- }