@kubb/renderer-jsx 5.0.0-beta.63 → 5.0.0-beta.65

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.
@@ -1,187 +0,0 @@
1
- import type { ExportNode, ImportNode, SourceNode } from '@kubb/ast'
2
- import type { Key, KubbReactElement, KubbReactNode } from '../types.ts'
3
-
4
- type BasePropsWithBaseName = {
5
- /**
6
- * Base file name including extension, derived from the input path.
7
- * Based on UNIX basename convention: `${name}${extname}`.
8
- *
9
- * @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
10
- *
11
- * @example
12
- * `baseName: 'petStore.ts'`
13
- */
14
- baseName: `${string}.${string}`
15
- /**
16
- * Fully qualified path to the generated file.
17
- *
18
- * @example
19
- * `path: 'src/models/petStore.ts'`
20
- */
21
- path: string
22
- }
23
-
24
- type BasePropsWithoutBaseName = {
25
- baseName?: never
26
- /**
27
- * Fully qualified path to the generated file.
28
- * Optional when `baseName` is omitted, the component renders its children inline.
29
- */
30
- path?: string | null
31
- }
32
-
33
- type BaseProps = BasePropsWithBaseName | BasePropsWithoutBaseName
34
-
35
- type Props<TMeta> = BaseProps & {
36
- key?: Key
37
- /**
38
- * Arbitrary metadata attached to the file node for plugins to read.
39
- */
40
- meta?: TMeta | null
41
- /**
42
- * Text prepended to the generated file content before any source blocks.
43
- * Accepts `null` so `resolver.resolveBanner()` results can be passed directly.
44
- */
45
- banner?: string | null
46
- /**
47
- * Text appended to the generated file content after all source blocks.
48
- * Accepts `null` so `resolver.resolveFooter()` results can be passed directly.
49
- */
50
- footer?: string | null
51
- /**
52
- * Child nodes rendered as the content of this file (source blocks, imports, exports).
53
- */
54
- children?: KubbReactNode
55
- }
56
-
57
- /**
58
- * Declares a generated file entry to be collected by the renderer.
59
- *
60
- * When both `baseName` and `path` are provided, the component registers a new
61
- * `FileNode` and passes its children through as source blocks.
62
- * When either is omitted the children are rendered inline without creating a file entry.
63
- *
64
- * @example Basic file with a source block
65
- * ```tsx
66
- * <File baseName="petStore.ts" path="src/models/petStore.ts">
67
- * <File.Source name="Pet" isExportable isIndexable>
68
- * {`export type Pet = { id: number; name: string }`}
69
- * </File.Source>
70
- * </File>
71
- * ```
72
- */
73
- export function File<TMeta extends object = object>({ children, ...props }: Props<TMeta>): KubbReactElement {
74
- const { baseName, path } = props
75
-
76
- if (!baseName || !path) {
77
- return <>{children}</>
78
- }
79
-
80
- return <kubb-file {...props}>{children}</kubb-file>
81
- }
82
-
83
- File.displayName = 'File'
84
-
85
- type FileSourceProps = Omit<SourceNode, 'kind' | 'value'> & {
86
- key?: Key
87
- /**
88
- * Child nodes rendered as the source content of this block.
89
- */
90
- children?: KubbReactNode
91
- }
92
-
93
- /**
94
- * Marks a block of source text to be associated with the enclosing {@link File}.
95
- *
96
- * Children are treated as the source string. `isExportable` prepends the `export` keyword,
97
- * `isIndexable` includes the source in barrel/index generation, and `name` keys deduplication.
98
- *
99
- * @example Exportable, indexable source block
100
- * ```tsx
101
- * <File.Source name="Pet" isExportable isIndexable>
102
- * {`export type Pet = { id: number; name: string }`}
103
- * </File.Source>
104
- * ```
105
- *
106
- * @example Type-only source block
107
- * ```tsx
108
- * <File.Source name="PetId" isTypeOnly isExportable>
109
- * {`export type PetId = string`}
110
- * </File.Source>
111
- * ```
112
- */
113
- function FileSource({ children, ...props }: FileSourceProps): KubbReactElement {
114
- const { name, isExportable, isIndexable, isTypeOnly } = props
115
-
116
- return (
117
- <kubb-source name={name} isTypeOnly={isTypeOnly} isExportable={isExportable} isIndexable={isIndexable}>
118
- {children}
119
- </kubb-source>
120
- )
121
- }
122
-
123
- FileSource.displayName = 'FileSource'
124
-
125
- type FileExportProps = Omit<ExportNode, 'kind'> & { key?: Key }
126
-
127
- /**
128
- * Declares an export entry for the enclosing {@link File}.
129
- *
130
- * The export is collected by the renderer and emitted at the top of the generated file.
131
- *
132
- * @example Named export
133
- * ```tsx
134
- * <File.Export name={['Pet']} path="./models/petStore" />
135
- * // export { Pet } from './models/petStore'
136
- * ```
137
- *
138
- * @example Type-only wildcard export
139
- * ```tsx
140
- * <File.Export path="./models/petStore" isTypeOnly />
141
- * // export type * from './models/petStore'
142
- * ```
143
- */
144
- function FileExport(props: FileExportProps): KubbReactElement {
145
- const { name, path, isTypeOnly, asAlias } = props
146
-
147
- return <kubb-export name={name} path={path} isTypeOnly={isTypeOnly} asAlias={asAlias} />
148
- }
149
-
150
- FileExport.displayName = 'FileExport'
151
-
152
- type FileImportProps = Omit<ImportNode, 'kind'> & { key?: Key }
153
-
154
- /**
155
- * Declares an import entry for the enclosing {@link File}.
156
- *
157
- * The import is collected by the renderer and emitted at the top of the generated file.
158
- *
159
- * @example Named import
160
- * ```tsx
161
- * <File.Import name={['useState']} path="react" />
162
- * // import { useState } from 'react'
163
- * ```
164
- *
165
- * @example Type-only import
166
- * ```tsx
167
- * <File.Import name={['Pet']} path="./models" isTypeOnly />
168
- * // import type { Pet } from './models'
169
- * ```
170
- *
171
- * @example Namespace import
172
- * ```tsx
173
- * <File.Import name="z" path="zod" isNameSpace />
174
- * // import * as z from 'zod'
175
- * ```
176
- */
177
- function FileImport(props: FileImportProps): KubbReactElement {
178
- const { name, root, path, isTypeOnly, isNameSpace } = props
179
-
180
- return <kubb-import name={name} root={root} path={path} isNameSpace={isNameSpace} isTypeOnly={isTypeOnly} />
181
- }
182
-
183
- FileImport.displayName = 'FileImport'
184
-
185
- File.Export = FileExport
186
- File.Import = FileImport
187
- File.Source = FileSource
@@ -1,37 +0,0 @@
1
- import { stringify } from 'yaml'
2
- import type { Key, KubbReactElement } from '../types.ts'
3
-
4
- type Props = {
5
- key?: Key
6
- /**
7
- * Plain object serialized as YAML between `---` fences.
8
- *
9
- * @example
10
- * `{ title: 'Pets', layout: 'doc' }`
11
- */
12
- data: Record<string, unknown>
13
- }
14
-
15
- /**
16
- * Emits a YAML frontmatter envelope at the top of a generated markdown file.
17
- *
18
- * Renders a `<File.Source>` block containing `---\n<yaml>\n---`. Place it as
19
- * the first child of `<File>` so it appears at the top of the output. Pair with
20
- * `parserMd` to write `.md` files whose frontmatter downstream tooling can read.
21
- *
22
- * @example Page frontmatter at the top of a generated markdown file
23
- * ```tsx
24
- * <File baseName="pets.md" path="src/pets.md">
25
- * <Frontmatter data={{ title: 'Pets', layout: 'doc' }} />
26
- * <File.Source>
27
- * {'# Pets\n\nList of pets.'}
28
- * </File.Source>
29
- * </File>
30
- * ```
31
- */
32
- export function Frontmatter({ data }: Props): KubbReactElement {
33
- const envelope = `---\n${stringify(data).trimEnd()}\n---`
34
- return <kubb-source name="frontmatter">{envelope}</kubb-source>
35
- }
36
-
37
- Frontmatter.displayName = 'Frontmatter'
@@ -1,148 +0,0 @@
1
- import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
2
-
3
- type Props = {
4
- key?: Key
5
- /**
6
- * Identifier of the generated function declaration.
7
- *
8
- * @example
9
- * `name: 'getPet'`
10
- */
11
- name: string
12
- /**
13
- * Emit `default` after the `export` keyword, making this the module's default export.
14
- * Requires `export` to also be `true`.
15
- */
16
- default?: boolean | null
17
- /**
18
- * Parameter list written verbatim between the function's parentheses.
19
- *
20
- * @example
21
- * `params: 'petId: string, options?: RequestOptions'`
22
- */
23
- params?: string | null
24
- /**
25
- * Emit the `export` keyword before the function declaration.
26
- * - `true` generates `export function name(…) { … }`
27
- * - `false` generates `function name(…) { … }`
28
- */
29
- export?: boolean | null
30
- /**
31
- * Emit the `async` keyword, making this an async function.
32
- * The return type is automatically wrapped in `Promise<returnType>` when both
33
- * `async` and `returnType` are set.
34
- */
35
- async?: boolean | null
36
- /**
37
- * TypeScript generic type parameters written verbatim between `<` and `>`.
38
- * Pass an array to emit multiple parameters separated by commas.
39
- *
40
- * @example Single generic
41
- * `generics: 'TData'`
42
- *
43
- * @example Multiple generics
44
- * `generics: ['TData', 'TError = unknown']`
45
- */
46
- generics?: string | Array<string> | null
47
- /**
48
- * TypeScript return type annotation written verbatim after `:`.
49
- * When `async` is `true`, the value is automatically wrapped in `Promise<…>`.
50
- *
51
- * @example
52
- * `returnType: 'Pet'`
53
- */
54
- returnType?: string | null
55
- /**
56
- * JSDoc block to prepend to the function declaration.
57
- * Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
58
- */
59
- JSDoc?: JSDoc | null
60
- /**
61
- * Child nodes rendered as the body of the function.
62
- */
63
- children?: KubbReactNode
64
- }
65
-
66
- /**
67
- * Generates a TypeScript function declaration.
68
- *
69
- * @example Async exported function with generics
70
- * ```tsx
71
- * <Function export async name="getPet" generics={['TData = Pet']} params="petId: string" returnType="TData">
72
- * {`return client.get(\`/pets/\${petId}\`)`}
73
- * </Function>
74
- * // export async function getPet<TData = Pet>(petId: string): Promise<TData> {
75
- * // return client.get(`/pets/${petId}`)
76
- * // }
77
- * ```
78
- */
79
- export function Function({ children, ...props }: Props): KubbReactElement {
80
- const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc } = props
81
-
82
- // Normalize generics array to comma-separated string for DOM attribute storage
83
- const genericsString = Array.isArray(generics) ? generics.join(', ').trim() : generics
84
-
85
- return (
86
- <kubb-function
87
- name={name}
88
- params={params}
89
- export={canExport}
90
- default={isDefault}
91
- async={isAsync}
92
- generics={genericsString}
93
- returnType={returnType}
94
- JSDoc={JSDoc}
95
- >
96
- {children}
97
- </kubb-function>
98
- )
99
- }
100
-
101
- Function.displayName = 'Function'
102
-
103
- type ArrowFunctionProps = Props & {
104
- /**
105
- * Render the arrow function as a single-line expression (no braces around the body).
106
- * - `true` generates `const name = (…) => expression`
107
- * - `false` generates `const name = (…) => { … }`
108
- */
109
- singleLine?: boolean | null
110
- }
111
-
112
- /**
113
- * Generates an arrow function expression assigned to a `const`.
114
- * Supports the same flags as {@link Function}.
115
- * Use `singleLine` to render the body as a concise expression rather than a block.
116
- *
117
- * @example Single-line arrow function
118
- * ```tsx
119
- * <Function.Arrow export name="double" params="n: number" returnType="number" singleLine>
120
- * {`n * 2`}
121
- * </Function.Arrow>
122
- * // export const double = (n: number): number => n * 2
123
- * ```
124
- */
125
- function ArrowFunction({ children, ...props }: ArrowFunctionProps) {
126
- const { name, default: isDefault, export: canExport, async, generics, params, returnType, JSDoc, singleLine } = props
127
-
128
- const genericsString = Array.isArray(generics) ? generics.join(', ').trim() : generics
129
-
130
- return (
131
- <kubb-arrow-function
132
- name={name}
133
- params={params}
134
- export={canExport}
135
- default={isDefault}
136
- async={async}
137
- generics={genericsString}
138
- returnType={returnType}
139
- singleLine={singleLine}
140
- JSDoc={JSDoc}
141
- >
142
- {children}
143
- </kubb-arrow-function>
144
- )
145
- }
146
-
147
- ArrowFunction.displayName = 'ArrowFunction'
148
- Function.Arrow = ArrowFunction
@@ -1,34 +0,0 @@
1
- import type { Key, KubbReactElement } from '../types.ts'
2
-
3
- type Level = 1 | 2 | 3 | 4 | 5 | 6
4
-
5
- type Props = {
6
- key?: Key
7
- /**
8
- * Heading depth, `1` through `6`. Matches the number of `#` characters
9
- * prefixed to the heading text.
10
- */
11
- level: Level
12
- /**
13
- * Heading text. Inline markdown (links, emphasis) is passed through verbatim.
14
- */
15
- children: string
16
- }
17
-
18
- /**
19
- * Renders an ATX-style markdown heading.
20
- *
21
- * Emits a `<File.Source>` block containing `${'#'.repeat(level)} ${children}`.
22
- * Use inside a `<File>` rendered by `parserMd`.
23
- *
24
- * @example
25
- * ```tsx
26
- * <Heading level={2}>Installation</Heading>
27
- * // ## Installation
28
- * ```
29
- */
30
- export function Heading({ level, children }: Props): KubbReactElement {
31
- return <kubb-source name="heading">{`${'#'.repeat(level)} ${children}`}</kubb-source>
32
- }
33
-
34
- Heading.displayName = 'Heading'
@@ -1,35 +0,0 @@
1
- import type { KubbReactElement } from '../types.ts'
2
-
3
- type Props = {
4
- /**
5
- * Raw JSX string embedded verbatim in the generated code, including
6
- * fragments (`<>…</>`).
7
- *
8
- * @example
9
- * ```tsx
10
- * <Jsx>{'<>\n <a href={href}>Open</a>\n</>'}</Jsx>
11
- * ```
12
- */
13
- children?: string
14
- }
15
-
16
- /**
17
- * Embeds a raw JSX string verbatim in the generated source code.
18
- *
19
- * Use this component to include JSX markup (including fragments `<>…</>`) in the
20
- * body of a generated function or component. The `children` prop must be a plain
21
- * string. Write expression attributes that reference runtime values as template
22
- * literals.
23
- *
24
- * @example
25
- * ```tsx
26
- * <Function name="MyComponent" export>
27
- * <Jsx>{'return (\n <>\n <div>Hello</div>\n </>\n)'}</Jsx>
28
- * </Function>
29
- * ```
30
- */
31
- export function Jsx({ children }: Props): KubbReactElement {
32
- return <kubb-jsx>{children}</kubb-jsx>
33
- }
34
-
35
- Jsx.displayName = 'Jsx'
@@ -1,40 +0,0 @@
1
- import type { Key, KubbReactElement } from '../types.ts'
2
-
3
- type Props = {
4
- key?: Key
5
- /**
6
- * When `true`, emits a numbered list (`1. …`). When `false` or omitted,
7
- * emits a bullet list (`- …`).
8
- *
9
- * @default false
10
- */
11
- ordered?: boolean | null
12
- /**
13
- * One entry per line. Inline markdown is passed through verbatim.
14
- */
15
- items: ReadonlyArray<string>
16
- }
17
-
18
- /**
19
- * Renders a markdown list.
20
- *
21
- * Emits a `<File.Source>` block containing one entry per line, prefixed with
22
- * `1.` / `2.` … when `ordered`, or `-` otherwise.
23
- *
24
- * @example
25
- * ```tsx
26
- * <List items={['Add the parser', 'Render the page']} />
27
- * // - Add the parser
28
- * // - Render the page
29
- *
30
- * <List ordered items={['First', 'Second']} />
31
- * // 1. First
32
- * // 2. Second
33
- * ```
34
- */
35
- export function List({ ordered, items }: Props): KubbReactElement {
36
- const body = items.map((item, index) => `${ordered ? `${index + 1}.` : '-'} ${item}`).join('\n')
37
- return <kubb-source name="list">{body}</kubb-source>
38
- }
39
-
40
- List.displayName = 'List'
@@ -1,28 +0,0 @@
1
- import type { Key, KubbReactElement } from '../types.ts'
2
-
3
- type Props = {
4
- key?: Key
5
- /**
6
- * Paragraph text. Inline markdown (links, emphasis, code spans) is passed
7
- * through verbatim.
8
- */
9
- children: string
10
- }
11
-
12
- /**
13
- * Renders a markdown paragraph.
14
- *
15
- * Emits a `<File.Source>` block containing the text as-is. Paragraphs are
16
- * separated from surrounding blocks by blank lines via the parser's source
17
- * joining.
18
- *
19
- * @example
20
- * ```tsx
21
- * <Paragraph>{'A pet object with `id` and `name` fields.'}</Paragraph>
22
- * ```
23
- */
24
- export function Paragraph({ children }: Props): KubbReactElement {
25
- return <kubb-source name="paragraph">{children}</kubb-source>
26
- }
27
-
28
- Paragraph.displayName = 'Paragraph'
@@ -1,65 +0,0 @@
1
- import type { JSDoc, Key, KubbReactElement, KubbReactNode } from '../types.ts'
2
-
3
- type TypeProps = {
4
- key?: Key
5
- /**
6
- * Identifier of the generated type alias.
7
- * Must start with an uppercase letter to follow TypeScript naming conventions.
8
- *
9
- * @example
10
- * `name: 'Pet'`
11
- */
12
- name: string
13
- /**
14
- * Emit the `export` keyword before the type alias declaration.
15
- * - `true` generates `export type Name = …`
16
- * - `false` generates `type Name = …`
17
- */
18
- export?: boolean | null
19
- /**
20
- * JSDoc block to prepend to the type alias declaration.
21
- * Each entry in `comments` becomes one line inside the emitted `/** … *\/` block.
22
- */
23
- JSDoc?: JSDoc | null
24
- /**
25
- * Child nodes rendered as the type expression on the right-hand side of the alias.
26
- */
27
- children?: KubbReactNode
28
- }
29
-
30
- /**
31
- * Generates a TypeScript type alias declaration.
32
- *
33
- * Throws if `name` does not start with an uppercase letter. TypeScript type aliases
34
- * should follow PascalCase naming conventions.
35
- *
36
- * @example Simple exported type alias
37
- * ```tsx
38
- * <Type export name="PetId">
39
- * {`string | number`}
40
- * </Type>
41
- * // export type PetId = string | number
42
- * ```
43
- *
44
- * @example Type alias with JSDoc
45
- * ```tsx
46
- * <Type export name="Pet" JSDoc={{ comments: ['@description A pet in the store.'] }}>
47
- * {`{ id: number; name: string }`}
48
- * </Type>
49
- * ```
50
- */
51
- export function Type({ children, ...props }: TypeProps): KubbReactElement {
52
- const { name, export: canExport, JSDoc } = props
53
-
54
- if (name.charAt(0).toUpperCase() !== name.charAt(0)) {
55
- throw new Error('Name should start with a capital letter(see TypeScript types)')
56
- }
57
-
58
- return (
59
- <kubb-type name={name} export={canExport} JSDoc={JSDoc}>
60
- {children}
61
- </kubb-type>
62
- )
63
- }
64
-
65
- Type.displayName = 'Type'
package/src/constants.ts DELETED
@@ -1,9 +0,0 @@
1
- export const KUBB_FILE = 'kubb-file' as const
2
- export const KUBB_SOURCE = 'kubb-source' as const
3
- export const KUBB_EXPORT = 'kubb-export' as const
4
- export const KUBB_IMPORT = 'kubb-import' as const
5
- export const KUBB_FUNCTION = 'kubb-function' as const
6
- export const KUBB_ARROW_FUNCTION = 'kubb-arrow-function' as const
7
- export const KUBB_CONST = 'kubb-const' as const
8
- export const KUBB_TYPE = 'kubb-type' as const
9
- export const KUBB_JSX = 'kubb-jsx' as const
@@ -1,56 +0,0 @@
1
- import type { FileNode } from '@kubb/ast'
2
- import { SyncRuntime } from './SyncRuntime.tsx'
3
- import type { KubbReactElement } from './types.ts'
4
-
5
- /**
6
- * Factory for a renderer that walks the JSX tree in a single recursive pass,
7
- * with no React reconciler or scheduler. Pass it as the `renderer` property on
8
- * `defineGenerator`. Kubb core calls the factory once per render cycle and stays
9
- * generic, with no hard dependency on `@kubb/renderer-jsx`.
10
- *
11
- * Every component must be a pure function. Hooks, suspense, and class
12
- * components are not supported. The returned renderer also exposes `stream()`
13
- * for incremental file emission.
14
- *
15
- * @example Wire up a JSX generator
16
- * ```tsx
17
- * import { defineGenerator } from '@kubb/core'
18
- * import { jsxRenderer } from '@kubb/renderer-jsx'
19
- *
20
- * export const myGenerator = defineGenerator<PluginTs>({
21
- * name: 'types',
22
- * renderer: jsxRenderer,
23
- * schema(node, ctx) {
24
- * return (
25
- * <File baseName="output.ts" path={`${ctx.root}/output.ts`}>
26
- * <Type node={node} resolver={ctx.resolver} />
27
- * </File>
28
- * )
29
- * },
30
- * })
31
- * ```
32
- *
33
- * @example Stream files as they are produced
34
- * ```tsx
35
- * const renderer = jsxRenderer()
36
- * for (const file of renderer.stream(element)) {
37
- * await writeFile(file.path, file.sources[0])
38
- * }
39
- * ```
40
- */
41
- export const jsxRenderer = () => {
42
- const runtime = new SyncRuntime()
43
-
44
- return {
45
- async render(element: KubbReactElement): Promise<void> {
46
- runtime.render(element)
47
- },
48
- get files() {
49
- return runtime.nodes
50
- },
51
- stream(element: KubbReactElement): Generator<FileNode> {
52
- return runtime.stream(element)
53
- },
54
- [Symbol.dispose]() {},
55
- }
56
- }
package/src/globals.ts DELETED
@@ -1,42 +0,0 @@
1
- import type {
2
- KubbArrowFunctionProps,
3
- KubbConstProps,
4
- KubbExportProps,
5
- KubbFileProps,
6
- KubbFunctionProps,
7
- KubbImportProps,
8
- KubbJsxProps,
9
- KubbReactElement,
10
- KubbReactNode,
11
- KubbSourceProps,
12
- KubbTypeProps,
13
- Key,
14
- LineBreakProps,
15
- } from './types.ts'
16
-
17
- declare global {
18
- namespace JSX {
19
- type Element = KubbReactElement
20
-
21
- interface ElementClass {
22
- render(): KubbReactNode
23
- }
24
-
25
- interface IntrinsicAttributes {
26
- key?: Key | null
27
- }
28
-
29
- interface IntrinsicElements {
30
- 'kubb-jsx': KubbJsxProps
31
- 'kubb-file': KubbFileProps
32
- 'kubb-source': KubbSourceProps
33
- 'kubb-import': KubbImportProps
34
- 'kubb-export': KubbExportProps
35
- 'kubb-function': KubbFunctionProps
36
- 'kubb-arrow-function': KubbArrowFunctionProps
37
- 'kubb-const': KubbConstProps
38
- 'kubb-type': KubbTypeProps
39
- br: LineBreakProps
40
- }
41
- }
42
- }