@kubb/renderer-jsx 5.0.0-alpha.33
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/LICENSE +14 -0
- package/dist/chunk-CErwXX-a.js +28 -0
- package/dist/index.cjs +18090 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.js +18075 -0
- package/dist/index.js.map +1 -0
- package/dist/jsx-dev-runtime.cjs +11 -0
- package/dist/jsx-dev-runtime.cjs.map +1 -0
- package/dist/jsx-dev-runtime.d.ts +14 -0
- package/dist/jsx-dev-runtime.js +10 -0
- package/dist/jsx-dev-runtime.js.map +1 -0
- package/dist/jsx-namespace-Cx1KMEbe.d.ts +39 -0
- package/dist/jsx-runtime-CeMde2cR.cjs +1503 -0
- package/dist/jsx-runtime-CeMde2cR.cjs.map +1 -0
- package/dist/jsx-runtime-Dmf9wTKR.js +1448 -0
- package/dist/jsx-runtime-Dmf9wTKR.js.map +1 -0
- package/dist/jsx-runtime.cjs +15 -0
- package/dist/jsx-runtime.cjs.map +1 -0
- package/dist/jsx-runtime.d.ts +16 -0
- package/dist/jsx-runtime.js +12 -0
- package/dist/jsx-runtime.js.map +1 -0
- package/dist/types-C7FD9BLg.d.ts +119 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +121 -0
- package/src/Renderer.ts +184 -0
- package/src/Runtime.tsx +171 -0
- package/src/components/Const.tsx +44 -0
- package/src/components/File.tsx +106 -0
- package/src/components/Function.tsx +107 -0
- package/src/components/Jsx.tsx +34 -0
- package/src/components/Root.tsx +64 -0
- package/src/components/Type.tsx +40 -0
- package/src/context/KubbContext.ts +15 -0
- package/src/context/OasContext.ts +9 -0
- package/src/createRenderer.tsx +32 -0
- package/src/dom.ts +91 -0
- package/src/globals.ts +34 -0
- package/src/index.ts +10 -0
- package/src/jsx-dev-runtime.ts +10 -0
- package/src/jsx-namespace.d.ts +53 -0
- package/src/jsx-runtime.ts +12 -0
- package/src/types.ts +113 -0
- package/src/utils.ts +301 -0
package/src/utils.ts
ADDED
|
@@ -0,0 +1,301 @@
|
|
|
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'
|
|
4
|
+
import type { DOMElement, DOMNode, ElementNames } from './types.ts'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 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.
|
|
10
|
+
*/
|
|
11
|
+
function collectChildNodes(element: DOMElement): Array<CodeNode> {
|
|
12
|
+
const result: Array<CodeNode> = []
|
|
13
|
+
|
|
14
|
+
for (const child of element.childNodes) {
|
|
15
|
+
if (!child) {
|
|
16
|
+
continue
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (child.nodeName === '#text') {
|
|
20
|
+
const text = (child as DOMNode<{ nodeName: '#text' }>).nodeValue
|
|
21
|
+
if (text && text.trim().length > 0) {
|
|
22
|
+
result.push(createText(text))
|
|
23
|
+
}
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if (child.nodeName === 'br') {
|
|
28
|
+
result.push(createBreak())
|
|
29
|
+
continue
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (child.nodeName === 'kubb-function') {
|
|
33
|
+
const attrs = child.attributes
|
|
34
|
+
result.push(
|
|
35
|
+
createFunction({
|
|
36
|
+
name: attrs.get('name') as string,
|
|
37
|
+
params: attrs.get('params') as string | undefined,
|
|
38
|
+
export: attrs.get('export') as boolean | undefined,
|
|
39
|
+
default: attrs.get('default') as boolean | undefined,
|
|
40
|
+
async: attrs.get('async') as boolean | undefined,
|
|
41
|
+
generics: attrs.get('generics') as string | undefined,
|
|
42
|
+
returnType: attrs.get('returnType') as string | undefined,
|
|
43
|
+
JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
|
|
44
|
+
nodes: collectChildNodes(child),
|
|
45
|
+
}),
|
|
46
|
+
)
|
|
47
|
+
} else if (child.nodeName === 'kubb-arrow-function') {
|
|
48
|
+
const attrs = child.attributes
|
|
49
|
+
result.push(
|
|
50
|
+
createArrowFunction({
|
|
51
|
+
name: attrs.get('name') as string,
|
|
52
|
+
params: attrs.get('params') as string | undefined,
|
|
53
|
+
export: attrs.get('export') as boolean | undefined,
|
|
54
|
+
default: attrs.get('default') as boolean | undefined,
|
|
55
|
+
async: attrs.get('async') as boolean | undefined,
|
|
56
|
+
generics: attrs.get('generics') as string | undefined,
|
|
57
|
+
returnType: attrs.get('returnType') as string | undefined,
|
|
58
|
+
singleLine: attrs.get('singleLine') as boolean | undefined,
|
|
59
|
+
JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
|
|
60
|
+
nodes: collectChildNodes(child),
|
|
61
|
+
} as Omit<ArrowFunctionNode, 'kind'>),
|
|
62
|
+
)
|
|
63
|
+
} else if (child.nodeName === 'kubb-const') {
|
|
64
|
+
const attrs = child.attributes
|
|
65
|
+
result.push(
|
|
66
|
+
createConst({
|
|
67
|
+
name: attrs.get('name') as string,
|
|
68
|
+
type: attrs.get('type') as string | undefined,
|
|
69
|
+
export: attrs.get('export') as boolean | undefined,
|
|
70
|
+
asConst: attrs.get('asConst') as boolean | undefined,
|
|
71
|
+
JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
|
|
72
|
+
nodes: collectChildNodes(child),
|
|
73
|
+
}),
|
|
74
|
+
)
|
|
75
|
+
} else if (child.nodeName === 'kubb-type') {
|
|
76
|
+
const attrs = child.attributes
|
|
77
|
+
result.push(
|
|
78
|
+
createType({
|
|
79
|
+
name: attrs.get('name') as string,
|
|
80
|
+
export: attrs.get('export') as boolean | undefined,
|
|
81
|
+
JSDoc: attrs.get('JSDoc') as JSDocNode | undefined,
|
|
82
|
+
nodes: collectChildNodes(child),
|
|
83
|
+
}),
|
|
84
|
+
)
|
|
85
|
+
} else if (child.nodeName === 'kubb-jsx') {
|
|
86
|
+
const textChild = child.childNodes[0]
|
|
87
|
+
const value = textChild?.nodeName === '#text' ? (textChild as DOMNode<{ nodeName: '#text' }>).nodeValue : ''
|
|
88
|
+
if (value) {
|
|
89
|
+
result.push(createJsx(value))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return result
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function squashSourceNodes(node: DOMElement, ignores: Array<ElementNames>): Set<SourceNode> {
|
|
98
|
+
const ignoreSet = new Set(ignores)
|
|
99
|
+
const sources = new Set<SourceNode>()
|
|
100
|
+
|
|
101
|
+
const walk = (current: DOMElement): void => {
|
|
102
|
+
for (const child of current.childNodes) {
|
|
103
|
+
if (!child) {
|
|
104
|
+
continue
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (child.nodeName !== '#text' && ignoreSet.has(child.nodeName)) {
|
|
108
|
+
continue
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
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
|
+
const source = createSource({
|
|
187
|
+
name: child.attributes.get('name')?.toString(),
|
|
188
|
+
isTypeOnly: (child.attributes.get('isTypeOnly') ?? false) as boolean,
|
|
189
|
+
isExportable: (child.attributes.get('isExportable') ?? false) as boolean,
|
|
190
|
+
isIndexable: (child.attributes.get('isIndexable') ?? false) as boolean,
|
|
191
|
+
nodes: orderedNodes,
|
|
192
|
+
// value is no longer set separately — all content is in nodes
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
sources.add(source)
|
|
196
|
+
continue
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
|
|
200
|
+
walk(child)
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
walk(node)
|
|
206
|
+
return sources
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function squashExportNodes(node: DOMElement): Set<ExportNode> {
|
|
210
|
+
const exports = new Set<ExportNode>()
|
|
211
|
+
|
|
212
|
+
const walk = (current: DOMElement): void => {
|
|
213
|
+
for (const child of current.childNodes) {
|
|
214
|
+
if (!child) {
|
|
215
|
+
continue
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
|
|
219
|
+
walk(child)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
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)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
walk(node)
|
|
234
|
+
return exports
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function squashImportNodes(node: DOMElement): Set<ImportNode> {
|
|
238
|
+
const imports = new Set<ImportNode>()
|
|
239
|
+
|
|
240
|
+
const walk = (current: DOMElement): void => {
|
|
241
|
+
for (const child of current.childNodes) {
|
|
242
|
+
if (!child) {
|
|
243
|
+
continue
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
if (child.nodeName !== '#text' && nodeNames.has(child.nodeName)) {
|
|
247
|
+
walk(child)
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
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)
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
walk(node)
|
|
263
|
+
return imports
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
export async function processFiles(node: DOMElement): Promise<Array<FileNode>> {
|
|
267
|
+
const collected: Array<FileNode> = []
|
|
268
|
+
|
|
269
|
+
async function walk(current: DOMElement) {
|
|
270
|
+
for (const child of current.childNodes) {
|
|
271
|
+
if (!child) {
|
|
272
|
+
continue
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (child.nodeName !== '#text' && child.nodeName !== 'kubb-file' && nodeNames.has(child.nodeName)) {
|
|
276
|
+
await walk(child)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (child.nodeName === 'kubb-file') {
|
|
280
|
+
if (child.attributes.has('baseName') && child.attributes.has('path')) {
|
|
281
|
+
const sources = squashSourceNodes(child, ['kubb-export', 'kubb-import'])
|
|
282
|
+
|
|
283
|
+
collected.push({
|
|
284
|
+
baseName: child.attributes.get('baseName'),
|
|
285
|
+
path: child.attributes.get('path'),
|
|
286
|
+
meta: child.attributes.get('meta') || {},
|
|
287
|
+
footer: child.attributes.get('footer'),
|
|
288
|
+
banner: child.attributes.get('banner'),
|
|
289
|
+
sources: [...sources],
|
|
290
|
+
exports: [...squashExportNodes(child)],
|
|
291
|
+
imports: [...squashImportNodes(child)],
|
|
292
|
+
} as FileNode)
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
await walk(node)
|
|
299
|
+
|
|
300
|
+
return collected
|
|
301
|
+
}
|