@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.
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
- }
package/src/utils.ts DELETED
@@ -1,267 +0,0 @@
1
- import type { ArrowFunctionNode, CodeNode, ExportNode, FileNode, ImportNode, JSDocNode, SourceNode } from '@kubb/ast'
2
- import {
3
- createArrowFunction,
4
- createBreak,
5
- createConst,
6
- createExport,
7
- createFunction,
8
- createImport,
9
- createJsx,
10
- createSource,
11
- createText,
12
- createType,
13
- } from '@kubb/ast'
14
- import { nodeNames, TEXT_NODE_NAME } from './constants.ts'
15
- import type { DOMElement, DOMNode, ElementNames } from './types.ts'
16
-
17
- /**
18
- * Collect the text and nested AST-node children of a single kubb-* element.
19
- *
20
- * `#text` children become raw {@link TextNode}s; nested `kubb-function`, `kubb-const`,
21
- * `kubb-type`, and similar elements are converted into their respective {@link CodeNode}s.
22
- * Any unrecognized DOM elements are silently skipped.
23
- */
24
- function collectChildNodes(element: DOMElement): Array<CodeNode> {
25
- const result: Array<CodeNode> = []
26
-
27
- for (const child of element.childNodes) {
28
- if (!child) {
29
- continue
30
- }
31
-
32
- if (child.nodeName === TEXT_NODE_NAME) {
33
- const text = (child as DOMNode<{ nodeName: '#text' }>).nodeValue
34
- if (text && text.trim().length > 0) {
35
- result.push(createText(text))
36
- }
37
- continue
38
- }
39
-
40
- if (child.nodeName === 'br') {
41
- result.push(createBreak())
42
- continue
43
- }
44
-
45
- if (child.nodeName === 'kubb-function') {
46
- const attrs = child.attributes
47
- result.push(
48
- createFunction({
49
- name: attrs.get('name') as string,
50
- params: attrs.get('params') as string | undefined,
51
- export: attrs.get('export') as boolean | undefined,
52
- default: attrs.get('default') as boolean | undefined,
53
- async: attrs.get('async') as boolean | undefined,
54
- generics: attrs.get('generics') as string | undefined,
55
- returnType: attrs.get('returnType') as string | undefined,
56
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
57
- nodes: collectChildNodes(child),
58
- }),
59
- )
60
- } else if (child.nodeName === 'kubb-arrow-function') {
61
- const attrs = child.attributes
62
- result.push(
63
- createArrowFunction({
64
- name: attrs.get('name') as string,
65
- params: attrs.get('params') as string | undefined,
66
- export: attrs.get('export') as boolean | undefined,
67
- default: attrs.get('default') as boolean | undefined,
68
- async: attrs.get('async') as boolean | undefined,
69
- generics: attrs.get('generics') as string | undefined,
70
- returnType: attrs.get('returnType') as string | undefined,
71
- singleLine: attrs.get('singleLine') as boolean | undefined,
72
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
73
- nodes: collectChildNodes(child),
74
- } as Omit<ArrowFunctionNode, 'kind'>),
75
- )
76
- } else if (child.nodeName === 'kubb-const') {
77
- const attrs = child.attributes
78
- result.push(
79
- createConst({
80
- name: attrs.get('name') as string,
81
- type: attrs.get('type') as string | undefined,
82
- export: attrs.get('export') as boolean | undefined,
83
- asConst: attrs.get('asConst') as boolean | undefined,
84
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
85
- nodes: collectChildNodes(child),
86
- }),
87
- )
88
- } else if (child.nodeName === 'kubb-type') {
89
- const attrs = child.attributes
90
- result.push(
91
- createType({
92
- name: attrs.get('name') as string,
93
- export: attrs.get('export') as boolean | undefined,
94
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
95
- nodes: collectChildNodes(child),
96
- }),
97
- )
98
- } else if (child.nodeName === 'kubb-jsx') {
99
- const textChild = child.childNodes[0]
100
- const value = textChild?.nodeName === TEXT_NODE_NAME ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
101
- if (value) {
102
- result.push(createJsx(value))
103
- }
104
- }
105
- }
106
-
107
- return result
108
- }
109
-
110
- /**
111
- * Traverse `node` and collect all `<kubb-source>` elements into a `Set<SourceNode>`.
112
- *
113
- * Elements whose `nodeName` is in `ignores` are skipped entirely (including their subtrees).
114
- * This is used to collect source blocks from a file node while excluding import/export subtrees.
115
- *
116
- * @example Collect sources while ignoring export and import elements
117
- * ```ts
118
- * const sources = squashSourceNodes(fileElement, ['kubb-export', 'kubb-import'])
119
- * ```
120
- */
121
- function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>): Set<SourceNode> {
122
- const ignoreSet = new Set(ignores)
123
- const sources = new Set<SourceNode>()
124
-
125
- const walk = (current: DOMElement): void => {
126
- for (const child of current.childNodes) {
127
- if (!child) {
128
- continue
129
- }
130
-
131
- if (child.nodeName !== TEXT_NODE_NAME && ignoreSet.has(child.nodeName)) {
132
- continue
133
- }
134
-
135
- if (child.nodeName === 'kubb-source') {
136
- const source = createSource({
137
- name: child.attributes.get('name')?.toString(),
138
- isTypeOnly: (child.attributes.get('isTypeOnly') ?? false) as boolean,
139
- isExportable: (child.attributes.get('isExportable') ?? false) as boolean,
140
- isIndexable: (child.attributes.get('isIndexable') ?? false) as boolean,
141
- nodes: collectChildNodes(child),
142
- })
143
-
144
- sources.add(source)
145
- continue
146
- }
147
-
148
- if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
149
- walk(child)
150
- }
151
- }
152
- }
153
-
154
- walk(node)
155
- return sources
156
- }
157
-
158
- /**
159
- * Traverse `node` and collect all `<kubb-export>` elements into a `Set<ExportNode>`.
160
- */
161
- function squashExportNodes(node: DOMElement): Set<ExportNode> {
162
- const exports = new Set<ExportNode>()
163
-
164
- const walk = (current: DOMElement): void => {
165
- for (const child of current.childNodes) {
166
- if (!child) {
167
- continue
168
- }
169
-
170
- if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
171
- walk(child)
172
- }
173
-
174
- if (child.nodeName === 'kubb-export') {
175
- exports.add(
176
- createExport({
177
- name: child.attributes.get('name') as ExportNode['name'],
178
- path: child.attributes.get('path') as string,
179
- isTypeOnly: (child.attributes.get('isTypeOnly') ?? false) as boolean,
180
- asAlias: (child.attributes.get('asAlias') ?? false) as boolean,
181
- }),
182
- )
183
- }
184
- }
185
- }
186
-
187
- walk(node)
188
- return exports
189
- }
190
-
191
- /**
192
- * Traverse `node` and collect all `<kubb-import>` elements into a `Set<ImportNode>`.
193
- */
194
- function squashImportNodes(node: DOMElement): Set<ImportNode> {
195
- const imports = new Set<ImportNode>()
196
-
197
- const walk = (current: DOMElement): void => {
198
- for (const child of current.childNodes) {
199
- if (!child) {
200
- continue
201
- }
202
-
203
- if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
204
- walk(child)
205
- }
206
-
207
- if (child.nodeName === 'kubb-import') {
208
- imports.add(
209
- createImport({
210
- name: child.attributes.get('name') as ImportNode['name'],
211
- path: child.attributes.get('path') as string,
212
- root: child.attributes.get('root') as string | undefined,
213
- isTypeOnly: (child.attributes.get('isTypeOnly') ?? false) as boolean,
214
- isNameSpace: (child.attributes.get('isNameSpace') ?? false) as boolean,
215
- }),
216
- )
217
- }
218
- }
219
- }
220
-
221
- walk(node)
222
- return imports
223
- }
224
-
225
- /**
226
- * Walk the virtual DOM tree rooted at `node` and convert every `<kubb-file>` element
227
- * into a {@link FileNode}, collecting its source blocks, imports, and exports.
228
- *
229
- * Returns the list of file nodes in document order. Nested files are supported —
230
- * the walker descends into non-file elements and recurses through them.
231
- */
232
- export function processFiles(node: DOMElement): Array<FileNode> {
233
- const collected: Array<FileNode> = []
234
-
235
- function walk(current: DOMElement) {
236
- for (const child of current.childNodes) {
237
- if (!child) {
238
- continue
239
- }
240
-
241
- if (child.nodeName !== TEXT_NODE_NAME && child.nodeName !== 'kubb-file' && nodeNames.has(child.nodeName)) {
242
- walk(child)
243
- }
244
-
245
- if (child.nodeName === 'kubb-file') {
246
- if (child.attributes.has('baseName') && child.attributes.has('path')) {
247
- const sources = squashSourceNodes(child, ['kubb-export', 'kubb-import'])
248
-
249
- collected.push({
250
- baseName: child.attributes.get('baseName'),
251
- path: child.attributes.get('path'),
252
- meta: child.attributes.get('meta') || {},
253
- footer: child.attributes.get('footer'),
254
- banner: child.attributes.get('banner'),
255
- sources: [...sources],
256
- exports: [...squashExportNodes(child)],
257
- imports: [...squashImportNodes(child)],
258
- } as FileNode)
259
- }
260
- }
261
- }
262
- }
263
-
264
- walk(node)
265
-
266
- return collected
267
- }