@kubb/renderer-jsx 5.0.0-beta.42 → 5.0.0-beta.43

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 DELETED
@@ -1,246 +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 {
15
- KUBB_ARROW_FUNCTION,
16
- KUBB_CONST,
17
- KUBB_EXPORT,
18
- KUBB_FILE,
19
- KUBB_FUNCTION,
20
- KUBB_IMPORT,
21
- KUBB_JSX,
22
- KUBB_SOURCE,
23
- KUBB_TYPE,
24
- TEXT_NODE_NAME,
25
- nodeNames,
26
- } from './constants.ts'
27
- import type { DOMElement, DOMNode } from './types.ts'
28
-
29
- function toBool(val: unknown): boolean {
30
- return (val ?? false) as boolean
31
- }
32
-
33
- /**
34
- * Collect the text and nested AST-node children of a single kubb-* element.
35
- *
36
- * `#text` children become raw text nodes. Nested `kubb-function`, `kubb-const`,
37
- * `kubb-type`, and similar elements are converted into their respective {@link CodeNode}s.
38
- * Any unrecognized element names are silently skipped.
39
- */
40
- function collectCodeNodes(element: DOMElement): Array<CodeNode> {
41
- const result: Array<CodeNode> = []
42
-
43
- for (const child of element.childNodes) {
44
- if (!child) continue
45
-
46
- if (child.nodeName === TEXT_NODE_NAME) {
47
- const text = (child as DOMNode<{ nodeName: '#text' }>).nodeValue
48
- if (text && text.trim()) result.push(createText(text))
49
- continue
50
- }
51
-
52
- if (child.nodeName === 'br') {
53
- result.push(createBreak())
54
- continue
55
- }
56
-
57
- if (child.nodeName === KUBB_FUNCTION) {
58
- const attrs = child.attributes
59
- result.push(
60
- createFunction({
61
- name: attrs['name'] as string,
62
- params: attrs['params'] as string | null | undefined,
63
- export: attrs['export'] as boolean | null | undefined,
64
- default: attrs['default'] as boolean | null | undefined,
65
- async: attrs['async'] as boolean | null | undefined,
66
- generics: attrs['generics'] as string | Array<string> | null | undefined,
67
- returnType: attrs['returnType'] as string | null | undefined,
68
- JSDoc: attrs['JSDoc'] as JSDocNode | null | undefined,
69
- nodes: collectCodeNodes(child),
70
- }),
71
- )
72
- continue
73
- }
74
-
75
- if (child.nodeName === KUBB_ARROW_FUNCTION) {
76
- const attrs = child.attributes
77
- result.push(
78
- createArrowFunction({
79
- name: attrs['name'] as string,
80
- params: attrs['params'] as string | null | undefined,
81
- export: attrs['export'] as boolean | null | undefined,
82
- default: attrs['default'] as boolean | null | undefined,
83
- async: attrs['async'] as boolean | null | undefined,
84
- generics: attrs['generics'] as string | Array<string> | null | undefined,
85
- returnType: attrs['returnType'] as string | null | undefined,
86
- singleLine: attrs['singleLine'] as boolean | null | undefined,
87
- JSDoc: attrs['JSDoc'] as JSDocNode | null | undefined,
88
- nodes: collectCodeNodes(child),
89
- } as Omit<ArrowFunctionNode, 'kind'>),
90
- )
91
- continue
92
- }
93
-
94
- if (child.nodeName === KUBB_CONST) {
95
- const attrs = child.attributes
96
- result.push(
97
- createConst({
98
- name: attrs['name'] as string,
99
- type: attrs['type'] as string | null | undefined,
100
- export: attrs['export'] as boolean | null | undefined,
101
- asConst: attrs['asConst'] as boolean | null | undefined,
102
- JSDoc: attrs['JSDoc'] as JSDocNode | null | undefined,
103
- nodes: collectCodeNodes(child),
104
- }),
105
- )
106
- continue
107
- }
108
-
109
- if (child.nodeName === KUBB_TYPE) {
110
- const attrs = child.attributes
111
- result.push(
112
- createType({
113
- name: attrs['name'] as string,
114
- export: attrs['export'] as boolean | null | undefined,
115
- JSDoc: attrs['JSDoc'] as JSDocNode | null | undefined,
116
- nodes: collectCodeNodes(child),
117
- }),
118
- )
119
- continue
120
- }
121
-
122
- if (child.nodeName === KUBB_JSX) {
123
- const textChild = child.childNodes[0]
124
- const value = textChild?.nodeName === TEXT_NODE_NAME ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
125
- if (value) result.push(createJsx(value))
126
- continue
127
- }
128
- }
129
-
130
- return result
131
- }
132
-
133
- /**
134
- * Yields every {@link SourceNode}, {@link ExportNode}, and {@link ImportNode}
135
- * within a `<kubb-file>` subtree in a single tree walk.
136
- *
137
- * Import and export elements are leaf nodes. Once yielded, the walker does not
138
- * recurse into them, which also prevents source collection from descending into
139
- * their subtrees. Dispatch on `.kind` (`'Source'`, `'Export'`, `'Import'`) to
140
- * separate the results.
141
- */
142
- function* collectFileEntries(node: DOMElement): Generator<SourceNode | ExportNode | ImportNode> {
143
- for (const child of node.childNodes) {
144
- if (!child || child.nodeName === TEXT_NODE_NAME) continue
145
-
146
- if (child.nodeName === KUBB_SOURCE) {
147
- yield createSource({
148
- name: child.attributes['name']?.toString(),
149
- isTypeOnly: toBool(child.attributes['isTypeOnly']),
150
- isExportable: toBool(child.attributes['isExportable']),
151
- isIndexable: toBool(child.attributes['isIndexable']),
152
- nodes: collectCodeNodes(child),
153
- })
154
- continue
155
- }
156
-
157
- if (child.nodeName === KUBB_EXPORT) {
158
- yield createExport({
159
- name: child.attributes['name'] as ExportNode['name'],
160
- path: child.attributes['path'] as string,
161
- isTypeOnly: toBool(child.attributes['isTypeOnly']),
162
- asAlias: toBool(child.attributes['asAlias']),
163
- })
164
- continue
165
- }
166
-
167
- if (child.nodeName === KUBB_IMPORT) {
168
- yield createImport({
169
- name: child.attributes['name'] as ImportNode['name'],
170
- path: child.attributes['path'] as string,
171
- root: child.attributes['root'] as string | null | undefined,
172
- isTypeOnly: toBool(child.attributes['isTypeOnly']),
173
- isNameSpace: toBool(child.attributes['isNameSpace']),
174
- })
175
- continue
176
- }
177
-
178
- if (nodeNames.has(child.nodeName)) {
179
- yield* collectFileEntries(child)
180
- }
181
- }
182
- }
183
-
184
- /**
185
- * Runs a single {@link collectFileEntries} pass over a `<kubb-file>` DOM element
186
- * and assembles the result into a {@link FileNode}, bucketing each yielded
187
- * node by its `.kind`.
188
- */
189
- function createFileNode(child: DOMElement): FileNode {
190
- const sources: Array<SourceNode> = []
191
- const exports: Array<ExportNode> = []
192
- const imports: Array<ImportNode> = []
193
-
194
- for (const node of collectFileEntries(child)) {
195
- if (node.kind === 'Source') {
196
- sources.push(node)
197
- continue
198
- }
199
- if (node.kind === 'Export') {
200
- exports.push(node)
201
- continue
202
- }
203
- imports.push(node)
204
- }
205
-
206
- return {
207
- baseName: child.attributes['baseName'],
208
- path: child.attributes['path'],
209
- meta: child.attributes['meta'] || {},
210
- footer: child.attributes['footer'],
211
- banner: child.attributes['banner'],
212
- sources,
213
- exports,
214
- imports,
215
- } as FileNode
216
- }
217
-
218
- /**
219
- * Yields each {@link FileNode} as it is encountered during the tree walk,
220
- * without collecting into an intermediate array. Callers can begin processing
221
- * each file before the rest of the tree is traversed.
222
- */
223
- export function* streamFiles(node: DOMElement): Generator<FileNode> {
224
- for (const child of node.childNodes) {
225
- if (!child) continue
226
-
227
- if (child.nodeName !== TEXT_NODE_NAME && child.nodeName !== KUBB_FILE && nodeNames.has(child.nodeName)) {
228
- yield* streamFiles(child)
229
- }
230
-
231
- if (child.nodeName === KUBB_FILE && child.attributes['baseName'] !== undefined && child.attributes['path'] !== undefined) {
232
- yield createFileNode(child)
233
- }
234
- }
235
- }
236
-
237
- /**
238
- * Walk the virtual DOM tree rooted at `node` and convert every `<kubb-file>` element
239
- * into a {@link FileNode}, collecting its source blocks, imports, and exports.
240
- *
241
- * Returns the list of file nodes in document order. Nested files are supported;
242
- * the walker descends into non-file elements and recurses through them.
243
- */
244
- export function collectFiles(node: DOMElement): Array<FileNode> {
245
- return [...streamFiles(node)]
246
- }