@kubb/ast 5.0.0-alpha.9 → 5.0.0-beta.75

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.
@@ -0,0 +1,304 @@
1
+ import type { BaseNode } from './base.ts'
2
+
3
+ /**
4
+ * JSDoc documentation metadata attached to code declarations.
5
+ */
6
+ export type JSDocNode = {
7
+ /**
8
+ * JSDoc comment lines. `undefined` entries are filtered out during rendering.
9
+ * @example ['@description A pet resource', '@deprecated']
10
+ */
11
+ comments?: Array<string | undefined>
12
+ }
13
+
14
+ /**
15
+ * AST node representing a TypeScript `const` declaration.
16
+ *
17
+ * Mirrors the props of the `Const` component from `@kubb/renderer-jsx`.
18
+ * The `children` prop of the component is represented as `nodes`.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * createConst({ name: 'pet', export: true, asConst: true })
23
+ * // export const pet = ... as const
24
+ * ```
25
+ */
26
+ export type ConstNode = BaseNode & {
27
+ /**
28
+ * Node kind.
29
+ */
30
+ kind: 'Const'
31
+ /**
32
+ * Name of the constant declaration.
33
+ */
34
+ name: string
35
+ /**
36
+ * Whether the declaration should be exported.
37
+ * @default false
38
+ */
39
+ export?: boolean
40
+ /**
41
+ * Optional explicit type annotation.
42
+ * @example 'Pet'
43
+ */
44
+ type?: string
45
+ /**
46
+ * JSDoc documentation metadata.
47
+ */
48
+ JSDoc?: JSDocNode
49
+ /**
50
+ * Whether to append `as const` to the declaration.
51
+ * @default false
52
+ */
53
+ asConst?: boolean
54
+ /**
55
+ * Child nodes representing the value of the constant (children of the `Const` component).
56
+ * Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
57
+ */
58
+ nodes?: Array<CodeNode>
59
+ }
60
+
61
+ /**
62
+ * AST node representing a TypeScript `type` alias declaration.
63
+ *
64
+ * Mirrors the props of the `Type` component from `@kubb/renderer-jsx`.
65
+ * The `children` prop of the component is represented as `nodes`.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * createType({ name: 'Pet', export: true })
70
+ * // export type Pet = ...
71
+ * ```
72
+ */
73
+ export type TypeNode = BaseNode & {
74
+ /**
75
+ * Node kind.
76
+ */
77
+ kind: 'Type'
78
+ /**
79
+ * Name of the type alias.
80
+ */
81
+ name: string
82
+ /**
83
+ * Whether the declaration should be exported.
84
+ * @default false
85
+ */
86
+ export?: boolean
87
+ /**
88
+ * JSDoc documentation metadata.
89
+ */
90
+ JSDoc?: JSDocNode
91
+ /**
92
+ * Child nodes representing the type body (children of the `Type` component).
93
+ * Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
94
+ */
95
+ nodes?: Array<CodeNode>
96
+ }
97
+
98
+ /**
99
+ * Convenience alias for {@link TypeNode}.
100
+ * @deprecated Use `TypeNode` directly.
101
+ */
102
+ export type TypeDeclarationNode = TypeNode
103
+
104
+ /**
105
+ * AST node representing a TypeScript `function` declaration.
106
+ *
107
+ * Mirrors the props of the `Function` component from `@kubb/renderer-jsx`.
108
+ * The `children` prop of the component is represented as `nodes`.
109
+ *
110
+ * @example
111
+ * ```ts
112
+ * createFunctionDeclaration({ name: 'getPet', export: true, async: true, returnType: 'Pet' })
113
+ * // export async function getPet(): Promise<Pet> { ... }
114
+ * ```
115
+ */
116
+ export type FunctionNode = BaseNode & {
117
+ /**
118
+ * Node kind.
119
+ */
120
+ kind: 'Function'
121
+ /**
122
+ * Name of the function.
123
+ */
124
+ name: string
125
+ /**
126
+ * Whether the function is a default export.
127
+ * @default false
128
+ */
129
+ default?: boolean
130
+ /**
131
+ * Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
132
+ */
133
+ params?: string
134
+ /**
135
+ * Whether the function should be exported.
136
+ * @default false
137
+ */
138
+ export?: boolean
139
+ /**
140
+ * Whether the function is async. When `true`, the return type is wrapped in `Promise<>`.
141
+ * @default false
142
+ */
143
+ async?: boolean
144
+ /**
145
+ * TypeScript generic type parameters.
146
+ * @example ['T', 'U extends string']
147
+ */
148
+ generics?: string | string[]
149
+ /**
150
+ * Return type annotation.
151
+ * @example 'Pet'
152
+ */
153
+ returnType?: string
154
+ /**
155
+ * JSDoc documentation metadata.
156
+ */
157
+ JSDoc?: JSDocNode
158
+ /**
159
+ * Child nodes representing the function body (children of the `Function` component).
160
+ * Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
161
+ */
162
+ nodes?: Array<CodeNode>
163
+ }
164
+
165
+ /**
166
+ * AST node representing a TypeScript arrow function (`const name = () => { ... }`).
167
+ *
168
+ * Mirrors the props of the `Function.Arrow` component from `@kubb/renderer-jsx`.
169
+ * The `children` prop of the component is represented as `nodes`.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * createArrowFunctionDeclaration({ name: 'getPet', export: true, singleLine: true })
174
+ * // export const getPet = () => ...
175
+ * ```
176
+ */
177
+ export type ArrowFunctionNode = BaseNode & {
178
+ /**
179
+ * Node kind.
180
+ */
181
+ kind: 'ArrowFunction'
182
+ /**
183
+ * Name of the arrow function (used as the `const` variable name).
184
+ */
185
+ name: string
186
+ /**
187
+ * Whether the function is a default export.
188
+ * @default false
189
+ */
190
+ default?: boolean
191
+ /**
192
+ * Function parameter list rendered as a string (e.g. from `FunctionParams.toConstructor()`).
193
+ */
194
+ params?: string
195
+ /**
196
+ * Whether the arrow function should be exported.
197
+ * @default false
198
+ */
199
+ export?: boolean
200
+ /**
201
+ * Whether the arrow function is async. When `true`, the return type is wrapped in `Promise<>`.
202
+ * @default false
203
+ */
204
+ async?: boolean
205
+ /**
206
+ * TypeScript generic type parameters.
207
+ * @example ['T', 'U extends string']
208
+ */
209
+ generics?: string | string[]
210
+ /**
211
+ * Return type annotation.
212
+ * @example 'Pet'
213
+ */
214
+ returnType?: string
215
+ /**
216
+ * JSDoc documentation metadata.
217
+ */
218
+ JSDoc?: JSDocNode
219
+ /**
220
+ * Render the arrow function body as a single-line expression.
221
+ * @default false
222
+ */
223
+ singleLine?: boolean
224
+ /**
225
+ * Child nodes representing the function body (children of the `Function.Arrow` component).
226
+ * Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
227
+ */
228
+ nodes?: Array<CodeNode>
229
+ }
230
+
231
+ /**
232
+ * AST node representing a raw text/string fragment in the source output.
233
+ *
234
+ * Used instead of bare `string` values so that all entries in `nodes` arrays
235
+ * are typed `CodeNode` objects rather than a mixed `CodeNode | string` union.
236
+ *
237
+ * @example
238
+ * ```ts
239
+ * createText('return fetch(id)')
240
+ * // { kind: 'Text', value: 'return fetch(id)' }
241
+ * ```
242
+ */
243
+ export type TextNode = BaseNode & {
244
+ /**
245
+ * Node kind.
246
+ */
247
+ kind: 'Text'
248
+ /**
249
+ * The raw string content.
250
+ */
251
+ value: string
252
+ }
253
+
254
+ /**
255
+ * AST node representing a line break in the source output.
256
+ *
257
+ * Corresponds to `<br/>` in JSX components. When printed, produces an empty
258
+ * string that — joined with `\n` by `printNodes` — creates a blank line
259
+ * between surrounding code nodes.
260
+ *
261
+ * @example
262
+ * ```ts
263
+ * createBreak()
264
+ * // { kind: 'Break' }
265
+ * // prints as '' → blank line when surrounded by other nodes
266
+ * ```
267
+ */
268
+ export type BreakNode = BaseNode & {
269
+ /**
270
+ * Node kind.
271
+ */
272
+ kind: 'Break'
273
+ }
274
+
275
+ /**
276
+ * AST node representing a raw JSX fragment in the source output.
277
+ *
278
+ * Mirrors the `Jsx` component from `@kubb/renderer-jsx`. Use this to embed raw
279
+ * JSX/TSX markup (including fragments `<>…</>`) directly in generated code.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * createJsx('<>\n <a href={href}>Open</a>\n</>')
284
+ * // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
285
+ * ```
286
+ */
287
+ export type JsxNode = BaseNode & {
288
+ /**
289
+ * Node kind.
290
+ */
291
+ kind: 'Jsx'
292
+ /**
293
+ * The raw JSX string content.
294
+ */
295
+ value: string
296
+ }
297
+
298
+ /**
299
+ * Union of all code-generation AST nodes.
300
+ *
301
+ * These nodes mirror the JSX components from `@kubb/renderer-jsx` and are used as
302
+ * structured children in {@link SourceNode.nodes}.
303
+ */
304
+ export type CodeNode = ConstNode | TypeNode | FunctionNode | ArrowFunctionNode | TextNode | BreakNode | JsxNode
@@ -0,0 +1,230 @@
1
+ import type { BaseNode } from './base.ts'
2
+ import type { CodeNode } from './code.ts'
3
+
4
+ /**
5
+ * Supported file extensions.
6
+ */
7
+ export type Extname = '.ts' | '.js' | '.tsx' | '.json' | `.${string}`
8
+
9
+ type ImportName = string | Array<string | { propertyName: string; name?: string }>
10
+
11
+ /**
12
+ * Represents a language-agnostic import/dependency declaration.
13
+ *
14
+ * @example Named import (TypeScript: `import { useState } from 'react'`)
15
+ * ```ts
16
+ * createImport({ name: ['useState'], path: 'react' })
17
+ * ```
18
+ *
19
+ * @example Default import (TypeScript: `import React from 'react'`)
20
+ * ```ts
21
+ * createImport({ name: 'React', path: 'react' })
22
+ * ```
23
+ *
24
+ * @example Type-only import (TypeScript: `import type { FC } from 'react'`)
25
+ * ```ts
26
+ * createImport({ name: ['FC'], path: 'react', isTypeOnly: true })
27
+ * ```
28
+ *
29
+ * @example Namespace import (TypeScript: `import * as React from 'react'`)
30
+ * ```ts
31
+ * createImport({ name: 'React', path: 'react', isNameSpace: true })
32
+ * ```
33
+ */
34
+ export type ImportNode = BaseNode & {
35
+ kind: 'Import'
36
+ /**
37
+ * Import name(s) to be used.
38
+ * @example ['useState']
39
+ * @example 'React'
40
+ */
41
+ name: ImportName
42
+ /**
43
+ * Path for the import.
44
+ * @example '@kubb/core'
45
+ */
46
+ path: string
47
+ /**
48
+ * Add type-only import prefix.
49
+ * - `true` generates `import type { Type } from './path'`
50
+ * - `false` generates `import { Type } from './path'`
51
+ * @default false
52
+ */
53
+ isTypeOnly?: boolean
54
+ /**
55
+ * Import entire module as namespace.
56
+ * - `true` generates `import * as Name from './path'`
57
+ * - `false` generates standard import
58
+ * @default false
59
+ */
60
+ isNameSpace?: boolean
61
+ /**
62
+ * When set, the import path is resolved relative to this root.
63
+ */
64
+ root?: string
65
+ }
66
+
67
+ /**
68
+ * Represents a language-agnostic export/public API declaration.
69
+ *
70
+ * @example Named export (TypeScript: `export { Pets } from './Pets'`)
71
+ * ```ts
72
+ * createExport({ name: ['Pets'], path: './Pets' })
73
+ * ```
74
+ *
75
+ * @example Type-only export (TypeScript: `export type { Pet } from './Pet'`)
76
+ * ```ts
77
+ * createExport({ name: ['Pet'], path: './Pet', isTypeOnly: true })
78
+ * ```
79
+ *
80
+ * @example Wildcard export (TypeScript: `export * from './utils'`)
81
+ * ```ts
82
+ * createExport({ path: './utils' })
83
+ * ```
84
+ *
85
+ * @example Namespace alias (TypeScript: `export * as utils from './utils'`)
86
+ * ```ts
87
+ * createExport({ name: 'utils', path: './utils', asAlias: true })
88
+ * ```
89
+ */
90
+ export type ExportNode = BaseNode & {
91
+ kind: 'Export'
92
+ /**
93
+ * Export name(s) to be used. When omitted, generates a wildcard export.
94
+ * @example ['useState']
95
+ * @example 'React'
96
+ */
97
+ name?: string | Array<string>
98
+ /**
99
+ * Path for the export.
100
+ * @example '@kubb/core'
101
+ */
102
+ path: string
103
+ /**
104
+ * Add type-only export prefix.
105
+ * - `true` generates `export type { Type } from './path'`
106
+ * - `false` generates `export { Type } from './path'`
107
+ * @default false
108
+ */
109
+ isTypeOnly?: boolean
110
+ /**
111
+ * Export as an aliased namespace.
112
+ * - `true` generates `export * as aliasName from './path'`
113
+ * - `false` generates a standard export
114
+ * @default false
115
+ */
116
+ asAlias?: boolean
117
+ }
118
+
119
+ /**
120
+ * Represents a fragment of source code within a file.
121
+ *
122
+ * @example Named exportable source
123
+ * ```ts
124
+ * createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true, isIndexable: true })
125
+ * ```
126
+ *
127
+ * @example Inline unnamed code block
128
+ * ```ts
129
+ * createSource({ nodes: [createText('const x = 1')] })
130
+ * ```
131
+ */
132
+ export type SourceNode = BaseNode & {
133
+ kind: 'Source'
134
+ /**
135
+ * Optional name identifying this source (used for deduplication and barrel generation).
136
+ */
137
+ name?: string
138
+ /**
139
+ * Mark this source as a type-only export.
140
+ * @default false
141
+ */
142
+ isTypeOnly?: boolean
143
+ /**
144
+ * Include `export` keyword in the generated source.
145
+ * @default false
146
+ */
147
+ isExportable?: boolean
148
+ /**
149
+ * Include this source in barrel/index file generation.
150
+ * @default false
151
+ */
152
+ isIndexable?: boolean
153
+ /**
154
+ * Structured child nodes representing the content of this source fragment, in DOM order.
155
+ * Each entry is a {@link CodeNode}; use {@link TextNode} for raw string content.
156
+ */
157
+ nodes?: Array<CodeNode>
158
+ }
159
+
160
+ /**
161
+ * Represents a fully resolved file in the AST.
162
+ *
163
+ * Created via `createFile()`, which computes the `id`, `name`, and `extname` from the input
164
+ * and deduplicates `imports`, `exports`, and `sources`.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const file = createFile({
169
+ * baseName: 'petStore.ts',
170
+ * path: 'src/models/petStore.ts',
171
+ * sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })],
172
+ * imports: [createImport({ name: ['z'], path: 'zod' })],
173
+ * exports: [createExport({ name: ['Pet'], path: './petStore' })],
174
+ * })
175
+ * // file.id = SHA256 hash of the path
176
+ * // file.name = 'petStore'
177
+ * // file.extname = '.ts'
178
+ * ```
179
+ */
180
+ export type FileNode<TMeta extends object = object> = BaseNode & {
181
+ kind: 'File'
182
+ /**
183
+ * Unique identifier derived from a SHA256 hash of the file path.
184
+ * @default hash
185
+ */
186
+ id: string
187
+ /**
188
+ * File name without extension, derived from `baseName`.
189
+ * @link https://nodejs.org/api/path.html#pathformatpathobject
190
+ */
191
+ name: string
192
+ /**
193
+ * File base name, including extension.
194
+ * Based on UNIX basename: `${name}${extname}`
195
+ * @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
196
+ */
197
+ baseName: `${string}.${string}`
198
+ /**
199
+ * Full qualified path to the file.
200
+ */
201
+ path: string
202
+ /**
203
+ * File extension extracted from `baseName`.
204
+ */
205
+ extname: Extname
206
+ /**
207
+ * Deduplicated list of source code fragments.
208
+ */
209
+ sources: Array<SourceNode>
210
+ /**
211
+ * Deduplicated list of import declarations.
212
+ */
213
+ imports: Array<ImportNode>
214
+ /**
215
+ * Deduplicated list of export declarations.
216
+ */
217
+ exports: Array<ExportNode>
218
+ /**
219
+ * Optional metadata attached to this file (used by plugins for barrel generation etc.).
220
+ */
221
+ meta?: TMeta
222
+ /**
223
+ * Optional banner prepended to the generated file content.
224
+ */
225
+ banner?: string
226
+ /**
227
+ * Optional footer appended to the generated file content.
228
+ */
229
+ footer?: string
230
+ }