@kubb/renderer-jsx 5.5.5 → 5.6.0

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/dist/index.cjs CHANGED
@@ -37,7 +37,6 @@ function Callout({ type, title, children }) {
37
37
  children: `${header}\n${quoted}`
38
38
  });
39
39
  }
40
- Callout.displayName = "Callout";
41
40
  //#endregion
42
41
  //#region src/components/js/Const.tsx
43
42
  /**
@@ -58,8 +57,7 @@ Callout.displayName = "Callout";
58
57
  * </Const>
59
58
  * ```
60
59
  */
61
- function Const({ children, ...props }) {
62
- const { name, export: canExport, type, JSDoc, asConst } = props;
60
+ function Const({ children, name, export: canExport, type, JSDoc, asConst }) {
63
61
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-const", {
64
62
  name,
65
63
  type,
@@ -69,7 +67,6 @@ function Const({ children, ...props }) {
69
67
  children
70
68
  });
71
69
  }
72
- Const.displayName = "Const";
73
70
  //#endregion
74
71
  //#region src/components/File.tsx
75
72
  /**
@@ -89,14 +86,12 @@ Const.displayName = "Const";
89
86
  * ```
90
87
  */
91
88
  function File({ children, ...props }) {
92
- const { baseName, path } = props;
93
- if (!baseName || !path) return /* @__PURE__ */ require_jsx_runtime.jsx(require_jsx_runtime.Fragment, { children });
89
+ if (!props.baseName || !props.path) return /* @__PURE__ */ require_jsx_runtime.jsx(require_jsx_runtime.Fragment, { children });
94
90
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-file", {
95
91
  ...props,
96
92
  children
97
93
  });
98
94
  }
99
- File.displayName = "File";
100
95
  /**
101
96
  * Marks a block of source text to be associated with the enclosing {@link File}.
102
97
  *
@@ -118,16 +113,11 @@ File.displayName = "File";
118
113
  * ```
119
114
  */
120
115
  function FileSource({ children, ...props }) {
121
- const { name, isExportable, isIndexable, isTypeOnly } = props;
122
116
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-source", {
123
- name,
124
- isTypeOnly,
125
- isExportable,
126
- isIndexable,
117
+ ...props,
127
118
  children
128
119
  });
129
120
  }
130
- FileSource.displayName = "FileSource";
131
121
  /**
132
122
  * Declares an export entry for the enclosing {@link File}.
133
123
  *
@@ -146,15 +136,8 @@ FileSource.displayName = "FileSource";
146
136
  * ```
147
137
  */
148
138
  function FileExport(props) {
149
- const { name, path, isTypeOnly, asAlias } = props;
150
- return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-export", {
151
- name,
152
- path,
153
- isTypeOnly,
154
- asAlias
155
- });
139
+ return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-export", { ...props });
156
140
  }
157
- FileExport.displayName = "FileExport";
158
141
  /**
159
142
  * Declares an import entry for the enclosing {@link File}.
160
143
  *
@@ -179,16 +162,8 @@ FileExport.displayName = "FileExport";
179
162
  * ```
180
163
  */
181
164
  function FileImport(props) {
182
- const { name, root, path, isTypeOnly, isNameSpace } = props;
183
- return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-import", {
184
- name,
185
- root,
186
- path,
187
- isNameSpace,
188
- isTypeOnly
189
- });
165
+ return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-import", { ...props });
190
166
  }
191
- FileImport.displayName = "FileImport";
192
167
  File.Export = FileExport;
193
168
  File.Import = FileImport;
194
169
  File.Source = FileSource;
@@ -218,7 +193,6 @@ function Frontmatter({ data }) {
218
193
  children: envelope
219
194
  });
220
195
  }
221
- Frontmatter.displayName = "Frontmatter";
222
196
  //#endregion
223
197
  //#region src/components/js/Function.tsx
224
198
  /**
@@ -234,8 +208,7 @@ Frontmatter.displayName = "Frontmatter";
234
208
  * // }
235
209
  * ```
236
210
  */
237
- function Function({ children, ...props }) {
238
- const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc } = props;
211
+ function Function({ children, name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc }) {
239
212
  const genericsString = Array.isArray(generics) ? generics.join(", ").trim() : generics;
240
213
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-function", {
241
214
  name,
@@ -249,7 +222,6 @@ function Function({ children, ...props }) {
249
222
  children
250
223
  });
251
224
  }
252
- Function.displayName = "Function";
253
225
  /**
254
226
  * Generates an arrow function expression assigned to a `const`.
255
227
  * Supports the same flags as {@link Function}.
@@ -263,8 +235,7 @@ Function.displayName = "Function";
263
235
  * // export const double = (n: number): number => n * 2
264
236
  * ```
265
237
  */
266
- function ArrowFunction({ children, ...props }) {
267
- const { name, default: isDefault, export: canExport, async, generics, params, returnType, JSDoc, singleLine } = props;
238
+ function ArrowFunction({ children, name, default: isDefault, export: canExport, async, generics, params, returnType, JSDoc, singleLine }) {
268
239
  const genericsString = Array.isArray(generics) ? generics.join(", ").trim() : generics;
269
240
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-arrow-function", {
270
241
  name,
@@ -279,7 +250,6 @@ function ArrowFunction({ children, ...props }) {
279
250
  children
280
251
  });
281
252
  }
282
- ArrowFunction.displayName = "ArrowFunction";
283
253
  Function.Arrow = ArrowFunction;
284
254
  //#endregion
285
255
  //#region src/components/md/Heading.tsx
@@ -301,7 +271,6 @@ function Heading({ level, children }) {
301
271
  children: `${"#".repeat(level)} ${children}`
302
272
  });
303
273
  }
304
- Heading.displayName = "Heading";
305
274
  //#endregion
306
275
  //#region src/components/jsx/Jsx.tsx
307
276
  /**
@@ -322,7 +291,6 @@ Heading.displayName = "Heading";
322
291
  function Jsx({ children }) {
323
292
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-jsx", { children });
324
293
  }
325
- Jsx.displayName = "Jsx";
326
294
  //#endregion
327
295
  //#region src/components/md/List.tsx
328
296
  /**
@@ -349,7 +317,6 @@ function List({ ordered, items }) {
349
317
  children: body
350
318
  });
351
319
  }
352
- List.displayName = "List";
353
320
  //#endregion
354
321
  //#region src/components/md/Paragraph.tsx
355
322
  /**
@@ -370,7 +337,6 @@ function Paragraph({ children }) {
370
337
  children
371
338
  });
372
339
  }
373
- Paragraph.displayName = "Paragraph";
374
340
  //#endregion
375
341
  //#region src/components/js/Type.tsx
376
342
  /**
@@ -394,8 +360,7 @@ Paragraph.displayName = "Paragraph";
394
360
  * </Type>
395
361
  * ```
396
362
  */
397
- function Type({ children, ...props }) {
398
- const { name, export: canExport, JSDoc } = props;
363
+ function Type({ children, name, export: canExport, JSDoc }) {
399
364
  if (name.charAt(0).toUpperCase() !== name.charAt(0)) throw new Error("Name should start with a capital letter(see TypeScript types)");
400
365
  return /* @__PURE__ */ require_jsx_runtime.jsx("kubb-type", {
401
366
  name,
@@ -404,15 +369,9 @@ function Type({ children, ...props }) {
404
369
  children
405
370
  });
406
371
  }
407
- Type.displayName = "Type";
408
372
  //#endregion
409
- //#region src/Runtime.tsx
410
- /**
411
- * Walks `element`, resolving arrays, Fragments, and function components, then
412
- * calls `onText` for primitive values and `onHost` for each host element.
413
- * Function components are called synchronously. Hooks and class components are
414
- * not supported.
415
- */
373
+ //#region src/jsxRenderer.ts
374
+ /** Walks `element` through arrays, Fragments and function components, calling `onText` and `onHost`. */
416
375
  function walkElement(element, onText, onHost) {
417
376
  if (element == null || typeof element === "boolean") return;
418
377
  if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") {
@@ -423,10 +382,9 @@ function walkElement(element, onText, onHost) {
423
382
  for (const child of element) walkElement(child, onText, onHost);
424
383
  return;
425
384
  }
426
- if (typeof element === "object" && "$$typeof" in element) {
427
- const el = element;
428
- const { type } = el;
429
- const props = el.props;
385
+ if (require_jsx_runtime.isKubbElement(element)) {
386
+ const { type } = element;
387
+ const props = element.props;
430
388
  if (type === require_jsx_runtime.Fragment) {
431
389
  walkElement(props["children"], onText, onHost);
432
390
  return;
@@ -435,81 +393,52 @@ function walkElement(element, onText, onHost) {
435
393
  walkElement(type(props), onText, onHost);
436
394
  return;
437
395
  }
438
- if (typeof type === "string") onHost(type, props);
396
+ if (typeof type === "string") onHost({
397
+ type,
398
+ props
399
+ });
439
400
  }
440
401
  }
441
- function collectCodeNodes(props) {
442
- const nodes = [];
443
- collectCode(props["children"], nodes);
444
- return nodes;
402
+ /** Returns the `children` prop of a host element, or `undefined` for tags without one. */
403
+ function childrenOf(host) {
404
+ return "children" in host.props ? host.props.children : void 0;
405
+ }
406
+ const codeFactories = {
407
+ "kubb-function": _kubb_kit.ast.factory.createFunction,
408
+ "kubb-arrow-function": _kubb_kit.ast.factory.createArrowFunction,
409
+ "kubb-const": _kubb_kit.ast.factory.createConst,
410
+ "kubb-type": _kubb_kit.ast.factory.createType
411
+ };
412
+ function isCodeHost(host) {
413
+ return host.type in codeFactories;
445
414
  }
446
- function collectCode(element, nodes) {
415
+ function createCodeNode(type, props) {
416
+ const { children, ...rest } = props;
417
+ return codeFactories[type]({
418
+ ...rest,
419
+ nodes: collectCodeNodes(children)
420
+ });
421
+ }
422
+ function collectCodeNodes(element) {
423
+ const nodes = [];
447
424
  walkElement(element, (text) => {
448
425
  if (text.trim()) nodes.push(_kubb_kit.ast.factory.createText(text));
449
- }, (type, props) => resolveCodeNode(type, props, nodes));
450
- }
451
- function resolveCodeNode(type, props, nodes) {
452
- if (type === "br") {
453
- nodes.push(_kubb_kit.ast.factory.createBreak());
454
- return;
455
- }
456
- if (type === "kubb-jsx") {
457
- let value = "";
458
- walkElement(props["children"], (t) => {
459
- value += t;
460
- }, () => {});
461
- if (value) nodes.push(_kubb_kit.ast.factory.createJsx(value));
462
- return;
463
- }
464
- if (type === "kubb-function") {
465
- nodes.push(_kubb_kit.ast.factory.createFunction({
466
- name: props["name"],
467
- params: props["params"],
468
- export: props["export"],
469
- default: props["default"],
470
- async: props["async"],
471
- generics: props["generics"],
472
- returnType: props["returnType"],
473
- JSDoc: props["JSDoc"],
474
- nodes: collectCodeNodes(props)
475
- }));
476
- return;
477
- }
478
- if (type === "kubb-arrow-function") {
479
- nodes.push(_kubb_kit.ast.factory.createArrowFunction({
480
- name: props["name"],
481
- params: props["params"],
482
- export: props["export"],
483
- default: props["default"],
484
- async: props["async"],
485
- generics: props["generics"],
486
- returnType: props["returnType"],
487
- singleLine: props["singleLine"],
488
- JSDoc: props["JSDoc"],
489
- nodes: collectCodeNodes(props)
490
- }));
491
- return;
492
- }
493
- if (type === "kubb-const") {
494
- nodes.push(_kubb_kit.ast.factory.createConst({
495
- name: props["name"],
496
- type: props["type"],
497
- export: props["export"],
498
- asConst: props["asConst"],
499
- JSDoc: props["JSDoc"],
500
- nodes: collectCodeNodes(props)
501
- }));
502
- return;
503
- }
504
- if (type === "kubb-type") {
505
- nodes.push(_kubb_kit.ast.factory.createType({
506
- name: props["name"],
507
- export: props["export"],
508
- JSDoc: props["JSDoc"],
509
- nodes: collectCodeNodes(props)
510
- }));
511
- return;
512
- }
426
+ }, (host) => {
427
+ if (host.type === "br") {
428
+ nodes.push(_kubb_kit.ast.factory.createBreak());
429
+ return;
430
+ }
431
+ if (host.type === "kubb-jsx") {
432
+ let value = "";
433
+ walkElement(host.props.children, (t) => {
434
+ value += t;
435
+ }, () => {});
436
+ if (value) nodes.push(_kubb_kit.ast.factory.createJsx(value));
437
+ return;
438
+ }
439
+ if (isCodeHost(host)) nodes.push(createCodeNode(host.type, host.props));
440
+ });
441
+ return nodes;
513
442
  }
514
443
  function collectFileChildren(element) {
515
444
  const sources = [];
@@ -517,37 +446,40 @@ function collectFileChildren(element) {
517
446
  const imports = [];
518
447
  walkElement(element, (text) => {
519
448
  if (text.trim()) throw new Error(`[jsx] '${text}' should be part of <File.Source> component when using the <File/> component`);
520
- }, (type, props) => {
521
- if (type === "kubb-source") {
449
+ }, (host) => {
450
+ if (host.type === "kubb-source") {
451
+ const { name, isTypeOnly, isExportable, isIndexable, children } = host.props;
522
452
  sources.push(_kubb_kit.ast.factory.createSource({
523
- name: props["name"]?.toString(),
524
- isTypeOnly: !!props["isTypeOnly"],
525
- isExportable: !!props["isExportable"],
526
- isIndexable: !!props["isIndexable"],
527
- nodes: collectCodeNodes(props)
453
+ name: name?.toString(),
454
+ isTypeOnly: !!isTypeOnly,
455
+ isExportable: !!isExportable,
456
+ isIndexable: !!isIndexable,
457
+ nodes: collectCodeNodes(children)
528
458
  }));
529
459
  return;
530
460
  }
531
- if (type === "kubb-export") {
461
+ if (host.type === "kubb-export") {
462
+ const { name, path, isTypeOnly, asAlias } = host.props;
532
463
  exports.push(_kubb_kit.ast.factory.createExport({
533
- name: props["name"],
534
- path: props["path"],
535
- isTypeOnly: !!props["isTypeOnly"],
536
- asAlias: !!props["asAlias"]
464
+ name,
465
+ path,
466
+ isTypeOnly: !!isTypeOnly,
467
+ asAlias: !!asAlias
537
468
  }));
538
469
  return;
539
470
  }
540
- if (type === "kubb-import") {
471
+ if (host.type === "kubb-import") {
472
+ const { name, path, root, isTypeOnly, isNameSpace } = host.props;
541
473
  imports.push(_kubb_kit.ast.factory.createImport({
542
- name: props["name"],
543
- path: props["path"],
544
- root: props["root"],
545
- isTypeOnly: !!props["isTypeOnly"],
546
- isNameSpace: !!props["isNameSpace"]
474
+ name,
475
+ path,
476
+ root,
477
+ isTypeOnly: !!isTypeOnly,
478
+ isNameSpace: !!isNameSpace
547
479
  }));
548
480
  return;
549
481
  }
550
- const nested = collectFileChildren(props["children"]);
482
+ const nested = collectFileChildren(childrenOf(host));
551
483
  sources.push(...nested.sources);
552
484
  exports.push(...nested.exports);
553
485
  imports.push(...nested.imports);
@@ -558,75 +490,26 @@ function collectFileChildren(element) {
558
490
  imports
559
491
  };
560
492
  }
561
- function* walkFiles(element) {
562
- if (element == null || typeof element === "boolean") return;
563
- if (typeof element === "string" || typeof element === "number" || typeof element === "bigint") return;
564
- if (Array.isArray(element)) {
565
- for (const child of element) yield* walkFiles(child);
566
- return;
567
- }
568
- if (typeof element === "object" && "$$typeof" in element) {
569
- const el = element;
570
- const { type } = el;
571
- const props = el.props;
572
- if (type === require_jsx_runtime.Fragment) {
573
- yield* walkFiles(props["children"]);
574
- return;
575
- }
576
- if (typeof type === "function") {
577
- yield* walkFiles(type(props));
578
- return;
579
- }
580
- if (typeof type === "string") {
581
- if (type === "kubb-file" && props["baseName"] !== void 0 && props["path"] !== void 0) {
582
- const { sources, exports, imports } = collectFileChildren(props["children"]);
583
- yield {
584
- baseName: props["baseName"],
585
- path: props["path"],
586
- meta: props["meta"] || {},
587
- footer: props["footer"],
588
- banner: props["banner"],
589
- copy: props["copy"],
590
- sources,
591
- exports,
592
- imports
593
- };
594
- } else yield* walkFiles(props["children"]);
595
- }
596
- }
493
+ function createFileNode(props) {
494
+ const { sources, exports, imports } = collectFileChildren(props.children);
495
+ return {
496
+ baseName: props.baseName,
497
+ path: props.path,
498
+ meta: props.meta || {},
499
+ footer: props.footer,
500
+ banner: props.banner,
501
+ copy: props.copy,
502
+ sources,
503
+ exports,
504
+ imports
505
+ };
597
506
  }
598
507
  /**
599
- * Synchronous JSX renderer that walks the element tree in a single pass,
600
- * producing {@link ast.FileNode} objects directly without an intermediate virtual
601
- * DOM. No React fiber, scheduler, or work loop is involved.
602
- *
603
- * All components must be pure functions. Hooks and class components are not
604
- * supported.
605
- */
606
- var Runtime = class {
607
- /**
608
- * Accumulated {@link ast.FileNode} results from every {@link render} call.
609
- */
610
- nodes = [];
611
- /**
612
- * Walks `element` synchronously, converts every `<kubb-file>` subtree into
613
- * a {@link ast.FileNode} with no intermediate virtual DOM, and appends the results
614
- * to {@link nodes}.
615
- */
616
- render(element) {
617
- for (const file of walkFiles(element)) this.nodes.push(file);
618
- }
619
- };
620
- //#endregion
621
- //#region src/jsxRenderer.tsx
622
- /**
623
508
  * Factory for a renderer that walks the JSX tree in a single recursive pass,
624
509
  * with no React reconciler or scheduler. Pass it as the `renderer` property on
625
510
  * `defineGenerator`. Kubb core calls the factory once per render cycle and stays
626
- * generic, with no hard dependency on `@kubb/renderer-jsx`.
627
- *
628
- * Every component must be a pure function. Hooks, suspense, and class
629
- * components are not supported.
511
+ * generic, with no hard dependency on `@kubb/renderer-jsx`. Every component must be a pure
512
+ * function. Hooks, suspense and class components are not supported.
630
513
  *
631
514
  * @example Wire up a JSX generator
632
515
  * ```tsx
@@ -647,13 +530,22 @@ var Runtime = class {
647
530
  * ```
648
531
  */
649
532
  const jsxRenderer = () => {
650
- const runtime = new Runtime();
533
+ const files = [];
534
+ function collectFiles(element) {
535
+ walkElement(element, () => {}, (host) => {
536
+ if (host.type === "kubb-file") {
537
+ files.push(createFileNode(host.props));
538
+ return;
539
+ }
540
+ collectFiles(childrenOf(host));
541
+ });
542
+ }
651
543
  return {
652
544
  async render(element) {
653
- runtime.render(element);
545
+ collectFiles(element);
654
546
  },
655
547
  get files() {
656
- return runtime.nodes;
548
+ return files;
657
549
  },
658
550
  [Symbol.dispose]() {}
659
551
  };