@kubb/renderer-jsx 5.0.0-alpha.34 → 5.0.0-alpha.35

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/utils.ts CHANGED
@@ -1,12 +1,25 @@
1
- import { createArrowFunction, createBreak, createConst, createFunction, createJsx, createSource, createText, createType } from '@kubb/ast'
2
- import type { ArrowFunctionNode, CodeNode, ExportNode, FileNode, ImportNode, JSDocNode, SourceNode } from '@kubb/ast/types'
3
- import { nodeNames } from './dom.ts'
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'
4
15
  import type { DOMElement, DOMNode, ElementNames } from './types.ts'
5
16
 
6
17
  /**
7
18
  * Collect the text and nested AST-node children of a single kubb-* element.
8
- * `#text` children become raw strings; nested kubb-function/const/type children
9
- * become their respective {@link CodeNode}s. Other DOM elements are skipped.
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.
10
23
  */
11
24
  function collectChildNodes(element: DOMElement): Array<CodeNode> {
12
25
  const result: Array<CodeNode> = []
@@ -16,7 +29,7 @@ function collectChildNodes(element: DOMElement): Array<CodeNode> {
16
29
  continue
17
30
  }
18
31
 
19
- if (child.nodeName === '#text') {
32
+ if (child.nodeName === TEXT_NODE_NAME) {
20
33
  const text = (child as DOMNode<{ nodeName: '#text' }>).nodeValue
21
34
  if (text && text.trim().length > 0) {
22
35
  result.push(createText(text))
@@ -84,7 +97,7 @@ function collectChildNodes(element: DOMElement): Array<CodeNode> {
84
97
  )
85
98
  } else if (child.nodeName === 'kubb-jsx') {
86
99
  const textChild = child.childNodes[0]
87
- const value = textChild?.nodeName === '#text' ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
100
+ const value = textChild?.nodeName === TEXT_NODE_NAME ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
88
101
  if (value) {
89
102
  result.push(createJsx(value))
90
103
  }
@@ -94,7 +107,18 @@ function collectChildNodes(element: DOMElement): Array<CodeNode> {
94
107
  return result
95
108
  }
96
109
 
97
- export function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>): Set<SourceNode> {
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> {
98
122
  const ignoreSet = new Set(ignores)
99
123
  const sources = new Set<SourceNode>()
100
124
 
@@ -104,99 +128,24 @@ export function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>
104
128
  continue
105
129
  }
106
130
 
107
- if (child.nodeName !== '#text' && ignoreSet.has(child.nodeName)) {
131
+ if (child.nodeName !== TEXT_NODE_NAME && ignoreSet.has(child.nodeName)) {
108
132
  continue
109
133
  }
110
134
 
111
135
  if (child.nodeName === 'kubb-source') {
112
- // Collect children in DOM order: text strings and kubb-* elements interleaved
113
- const orderedNodes: Array<CodeNode> = []
114
- for (const c of child.childNodes) {
115
- if (!c) continue
116
-
117
- if (c.nodeName === '#text') {
118
- const text = (c as DOMNode<{ nodeName: '#text' }>).nodeValue
119
- if (text && text.trim().length > 0) {
120
- orderedNodes.push(createText(text))
121
- }
122
- } else if (c.nodeName === 'br') {
123
- orderedNodes.push(createBreak())
124
- } else if (c.nodeName === 'kubb-function') {
125
- const attrs = c.attributes
126
- orderedNodes.push(
127
- createFunction({
128
- name: attrs.get('name') as string,
129
- params: attrs.get('params') as string | undefined,
130
- export: attrs.get('export') as boolean | undefined,
131
- default: attrs.get('default') as boolean | undefined,
132
- async: attrs.get('async') as boolean | undefined,
133
- generics: attrs.get('generics') as string | undefined,
134
- returnType: attrs.get('returnType') as string | undefined,
135
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
136
- nodes: collectChildNodes(c),
137
- }),
138
- )
139
- } else if (c.nodeName === 'kubb-arrow-function') {
140
- const attrs = c.attributes
141
- orderedNodes.push(
142
- createArrowFunction({
143
- name: attrs.get('name') as string,
144
- params: attrs.get('params') as string | undefined,
145
- export: attrs.get('export') as boolean | undefined,
146
- default: attrs.get('default') as boolean | undefined,
147
- async: attrs.get('async') as boolean | undefined,
148
- generics: attrs.get('generics') as string | undefined,
149
- returnType: attrs.get('returnType') as string | undefined,
150
- singleLine: attrs.get('singleLine') as boolean | undefined,
151
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
152
- nodes: collectChildNodes(c),
153
- } as Omit<ArrowFunctionNode, 'kind'>),
154
- )
155
- } else if (c.nodeName === 'kubb-const') {
156
- const attrs = c.attributes
157
- orderedNodes.push(
158
- createConst({
159
- name: attrs.get('name') as string,
160
- type: attrs.get('type') as string | undefined,
161
- export: attrs.get('export') as boolean | undefined,
162
- asConst: attrs.get('asConst') as boolean | undefined,
163
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
164
- nodes: collectChildNodes(c),
165
- }),
166
- )
167
- } else if (c.nodeName === 'kubb-type') {
168
- const attrs = c.attributes
169
- orderedNodes.push(
170
- createType({
171
- name: attrs.get('name') as string,
172
- export: attrs.get('export') as boolean | undefined,
173
- JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
174
- nodes: collectChildNodes(c),
175
- }),
176
- )
177
- } else if (c.nodeName === 'kubb-jsx') {
178
- const textChild = c.childNodes[0]
179
- const value = textChild?.nodeName === '#text' ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
180
- if (value) {
181
- orderedNodes.push(createJsx(value))
182
- }
183
- }
184
- }
185
-
186
136
  const source = createSource({
187
137
  name: child.attributes.get('name')?.toString(),
188
138
  isTypeOnly: (child.attributes.get('isTypeOnly') ?? false) as boolean,
189
139
  isExportable: (child.attributes.get('isExportable') ?? false) as boolean,
190
140
  isIndexable: (child.attributes.get('isIndexable') ?? false) as boolean,
191
- nodes: orderedNodes,
192
- // value is no longer set separately — all content is in nodes
141
+ nodes: collectChildNodes(child),
193
142
  })
194
143
 
195
144
  sources.add(source)
196
145
  continue
197
146
  }
198
147
 
199
- if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
148
+ if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
200
149
  walk(child)
201
150
  }
202
151
  }
@@ -206,7 +155,10 @@ export function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>
206
155
  return sources
207
156
  }
208
157
 
209
- export function squashExportNodes(node: DOMElement): Set<ExportNode> {
158
+ /**
159
+ * Traverse `node` and collect all `<kubb-export>` elements into a `Set<ExportNode>`.
160
+ */
161
+ function squashExportNodes(node: DOMElement): Set<ExportNode> {
210
162
  const exports = new Set<ExportNode>()
211
163
 
212
164
  const walk = (current: DOMElement): void => {
@@ -215,17 +167,19 @@ export function squashExportNodes(node: DOMElement): Set<ExportNode> {
215
167
  continue
216
168
  }
217
169
 
218
- if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
170
+ if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
219
171
  walk(child)
220
172
  }
221
173
 
222
174
  if (child.nodeName === 'kubb-export') {
223
- exports.add({
224
- name: child.attributes.get('name'),
225
- path: child.attributes.get('path'),
226
- isTypeOnly: child.attributes.get('isTypeOnly') ?? false,
227
- asAlias: child.attributes.get('asAlias') ?? false,
228
- } as ExportNode)
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
+ )
229
183
  }
230
184
  }
231
185
  }
@@ -234,7 +188,10 @@ export function squashExportNodes(node: DOMElement): Set<ExportNode> {
234
188
  return exports
235
189
  }
236
190
 
237
- export function squashImportNodes(node: DOMElement): Set<ImportNode> {
191
+ /**
192
+ * Traverse `node` and collect all `<kubb-import>` elements into a `Set<ImportNode>`.
193
+ */
194
+ function squashImportNodes(node: DOMElement): Set<ImportNode> {
238
195
  const imports = new Set<ImportNode>()
239
196
 
240
197
  const walk = (current: DOMElement): void => {
@@ -243,18 +200,20 @@ export function squashImportNodes(node: DOMElement): Set<ImportNode> {
243
200
  continue
244
201
  }
245
202
 
246
- if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
203
+ if (child.nodeName !== TEXT_NODE_NAME && nodeNames.has(child.nodeName)) {
247
204
  walk(child)
248
205
  }
249
206
 
250
207
  if (child.nodeName === 'kubb-import') {
251
- imports.add({
252
- name: child.attributes.get('name'),
253
- path: child.attributes.get('path'),
254
- root: child.attributes.get('root'),
255
- isTypeOnly: child.attributes.get('isTypeOnly') ?? false,
256
- isNameSpace: child.attributes.get('isNameSpace') ?? false,
257
- } as ImportNode)
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
+ )
258
217
  }
259
218
  }
260
219
  }
@@ -263,17 +222,24 @@ export function squashImportNodes(node: DOMElement): Set<ImportNode> {
263
222
  return imports
264
223
  }
265
224
 
266
- export async function processFiles(node: DOMElement): Promise<Array<FileNode>> {
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> {
267
233
  const collected: Array<FileNode> = []
268
234
 
269
- async function walk(current: DOMElement) {
235
+ function walk(current: DOMElement) {
270
236
  for (const child of current.childNodes) {
271
237
  if (!child) {
272
238
  continue
273
239
  }
274
240
 
275
- if (child.nodeName !== '#text' && child.nodeName !== 'kubb-file' && nodeNames.has(child.nodeName)) {
276
- await walk(child)
241
+ if (child.nodeName !== TEXT_NODE_NAME && child.nodeName !== 'kubb-file' && nodeNames.has(child.nodeName)) {
242
+ walk(child)
277
243
  }
278
244
 
279
245
  if (child.nodeName === 'kubb-file') {
@@ -295,7 +261,7 @@ export async function processFiles(node: DOMElement): Promise<Array<FileNode>> {
295
261
  }
296
262
  }
297
263
 
298
- await walk(node)
264
+ walk(node)
299
265
 
300
266
  return collected
301
267
  }
@@ -1,119 +0,0 @@
1
- import { n as __name } from "./chunk-CErwXX-a.js";
2
- import { ArrowFunctionNode, ConstNode, ExportNode, FileNode, FunctionNode, ImportNode, SourceNode, TypeNode } from "@kubb/ast/types";
3
- import React, { JSX, ReactNode } from "react";
4
-
5
- //#region ../../internals/utils/src/context.d.ts
6
- /**
7
- * Context type that carries type information about its value
8
- * This is a branded symbol type that enables type-safe context usage
9
- */
10
- type Context<T> = symbol & {
11
- readonly __type: T;
12
- };
13
- /**
14
- * Provides a value to descendant components (Vue 3 style)
15
- *
16
- * @example
17
- * ```ts
18
- * const ThemeKey = Symbol('theme')
19
- * provide(ThemeKey, { color: 'blue' })
20
- * ```
21
- */
22
- declare function provide<T>(key: symbol | Context<T>, value: T): void;
23
- /**
24
- * Injects a value provided by an ancestor component (Vue 3 style)
25
- *
26
- * @example
27
- * ```ts
28
- * const theme = inject(ThemeKey, { color: 'default' })
29
- * ```
30
- */
31
- declare function inject<T>(key: symbol | Context<T>, defaultValue?: T): T;
32
- /**
33
- * Removes a provided value from the context stack (for cleanup)
34
- * @internal
35
- */
36
- declare function unprovide<T>(key: symbol | Context<T>): void;
37
- /**
38
- * Creates a context key with a default value (React-style compatibility)
39
- *
40
- * @example
41
- * ```ts
42
- * const ThemeContext = createContext({ color: 'blue' })
43
- * // ThemeContext is now typed as Context<{ color: string }>
44
- * const theme = useContext(ThemeContext) // theme is { color: string }
45
- * ```
46
- */
47
- declare function createContext<T>(defaultValue: T): Context<T>;
48
- //#endregion
49
- //#region src/types.d.ts
50
- type Key = string | number | bigint;
51
- type ElementNames = 'br' | 'div' | 'indent' | 'dedent' | 'kubb-jsx' | 'kubb-text' | 'kubb-file' | 'kubb-source' | 'kubb-import' | 'kubb-export' | 'kubb-function' | 'kubb-arrow-function' | 'kubb-const' | 'kubb-type' | 'kubb-root' | 'kubb-app';
52
- type Node = {
53
- parentNode: DOMElement | undefined;
54
- internal_static?: boolean;
55
- };
56
- type DOMNodeAttribute = boolean | string | number | Record<string, unknown>;
57
- type TextName = '#text';
58
- type TextNode = {
59
- nodeName: TextName;
60
- nodeValue: string;
61
- } & Node;
62
- type DOMNode<T = {
63
- nodeName: NodeNames;
64
- }> = T extends {
65
- nodeName: infer U;
66
- } ? U extends '#text' ? TextNode : DOMElement : never;
67
- type OutputTransformer = (s: string, index: number) => string;
68
- type DOMElement = {
69
- nodeName: ElementNames;
70
- attributes: Map<string, DOMNodeAttribute>;
71
- childNodes: DOMNode[];
72
- internal_transform?: OutputTransformer;
73
- isStaticDirty?: boolean;
74
- staticNode?: DOMElement;
75
- onComputeLayout?: () => void;
76
- onRender?: () => void;
77
- onImmediateRender?: () => void;
78
- } & Node;
79
- type NodeNames = ElementNames | TextName;
80
- type KubbReactNode = ReactNode;
81
- type KubbReactElement = JSX.Element;
82
- type KubbJsxProps = {
83
- children?: string;
84
- };
85
- type KubbTextProps = {
86
- children?: KubbReactNode;
87
- };
88
- type KubbFileProps = {
89
- id?: string;
90
- children?: KubbReactNode;
91
- baseName: string;
92
- path: string;
93
- override?: boolean;
94
- meta?: FileNode['meta'];
95
- };
96
- type KubbSourceProps = Omit<SourceNode, 'kind'> & {
97
- children?: KubbReactNode;
98
- };
99
- type KubbImportProps = Omit<ImportNode, 'kind'> & {};
100
- type KubbExportProps = Omit<ExportNode, 'kind'> & {};
101
- type KubbFunctionProps = Omit<FunctionNode, 'kind'> & {
102
- children?: KubbReactNode;
103
- };
104
- type KubbArrowFunctionProps = Omit<ArrowFunctionNode, 'kind'> & {
105
- children?: KubbReactNode;
106
- };
107
- type KubbConstProps = Omit<ConstNode, 'kind'> & {
108
- children?: KubbReactNode;
109
- };
110
- type KubbTypeProps = Omit<TypeNode, 'kind'> & {
111
- children?: KubbReactNode;
112
- };
113
- type LineBreakProps = React.DetailedHTMLProps<React.HTMLAttributes<HTMLBRElement>, HTMLBRElement>;
114
- type JSDoc = {
115
- comments: string[];
116
- };
117
- //#endregion
118
- export { inject as C, createContext as S, unprovide as T, KubbTextProps as _, JSDoc as a, TextNode as b, KubbConstProps as c, KubbFunctionProps as d, KubbImportProps as f, KubbSourceProps as g, KubbReactNode as h, ElementNames as i, KubbExportProps as l, KubbReactElement as m, DOMNode as n, Key as o, KubbJsxProps as p, DOMNodeAttribute as r, KubbArrowFunctionProps as s, DOMElement as t, KubbFileProps as u, KubbTypeProps as v, provide as w, Context as x, LineBreakProps as y };
119
- //# sourceMappingURL=types-C7FD9BLg.d.ts.map