@kubb/ast 5.0.0-alpha.30 → 5.0.0-alpha.32
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 +1556 -1067
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +42 -3
- package/dist/index.js +1519 -1067
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +2 -2
- package/dist/{visitor-z-5U8NoF.d.ts → visitor-DysNCWvh.d.ts} +794 -61
- package/package.json +2 -2
- package/src/constants.ts +7 -1
- package/src/factory.ts +296 -24
- package/src/guards.ts +17 -4
- package/src/index.ts +23 -5
- package/src/mocks.ts +6 -6
- package/src/nodes/base.ts +11 -2
- package/src/nodes/code.ts +237 -0
- package/src/nodes/file.ts +235 -0
- package/src/nodes/function.ts +35 -17
- package/src/nodes/index.ts +32 -7
- package/src/nodes/output.ts +26 -0
- package/src/nodes/root.ts +9 -8
- package/src/refs.ts +5 -5
- package/src/types.ts +14 -2
- package/src/utils.ts +177 -19
- package/src/visitor.ts +52 -25
|
@@ -0,0 +1,237 @@
|
|
|
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/react-fabric`.
|
|
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 either a structured {@link CodeNode} or a raw string expression.
|
|
57
|
+
*/
|
|
58
|
+
nodes?: Array<CodeNode | string>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* AST node representing a TypeScript `type` alias declaration.
|
|
63
|
+
*
|
|
64
|
+
* Mirrors the props of the `Type` component from `@kubb/react-fabric`.
|
|
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 either a structured {@link CodeNode} or a raw string expression.
|
|
94
|
+
*/
|
|
95
|
+
nodes?: Array<CodeNode | string>
|
|
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/react-fabric`.
|
|
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 either a structured {@link CodeNode} or a raw string statement.
|
|
161
|
+
*/
|
|
162
|
+
nodes?: Array<CodeNode | string>
|
|
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/react-fabric`.
|
|
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 either a structured {@link CodeNode} or a raw string statement.
|
|
227
|
+
*/
|
|
228
|
+
nodes?: Array<CodeNode | string>
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Union of all code-generation AST nodes.
|
|
233
|
+
*
|
|
234
|
+
* These nodes mirror the JSX components from `@kubb/react-fabric` and are used as
|
|
235
|
+
* structured children in {@link SourceNode.nodes}.
|
|
236
|
+
*/
|
|
237
|
+
export type CodeNode = ConstNode | TypeNode | FunctionNode | ArrowFunctionNode
|
|
@@ -0,0 +1,235 @@
|
|
|
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', value: 'export type Pet = { id: number }', isExportable: true, isIndexable: true })
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @example Inline unnamed code block
|
|
128
|
+
* ```ts
|
|
129
|
+
* createSource({ value: '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
|
+
* The source code value.
|
|
140
|
+
*/
|
|
141
|
+
value?: string
|
|
142
|
+
/**
|
|
143
|
+
* Mark this source as a type-only export.
|
|
144
|
+
* @default false
|
|
145
|
+
*/
|
|
146
|
+
isTypeOnly?: boolean
|
|
147
|
+
/**
|
|
148
|
+
* Include `export` keyword in the generated source.
|
|
149
|
+
* @default false
|
|
150
|
+
*/
|
|
151
|
+
isExportable?: boolean
|
|
152
|
+
/**
|
|
153
|
+
* Include this source in barrel/index file generation.
|
|
154
|
+
* @default false
|
|
155
|
+
*/
|
|
156
|
+
isIndexable?: boolean
|
|
157
|
+
/**
|
|
158
|
+
* Structured child nodes representing the content of this source fragment.
|
|
159
|
+
* These correspond to the children of the `File.Source` component from `@kubb/react-fabric`
|
|
160
|
+
* (e.g. `ConstNode`, `TypeDeclarationNode`, `FunctionDeclarationNode`, `ArrowFunctionDeclarationNode`).
|
|
161
|
+
*/
|
|
162
|
+
nodes?: Array<CodeNode>
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Represents a fully resolved file in the AST.
|
|
167
|
+
*
|
|
168
|
+
* Created via `createFile()`, which computes the `id`, `name`, and `extname` from the input
|
|
169
|
+
* and deduplicates `imports`, `exports`, and `sources`.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* const file = createFile({
|
|
174
|
+
* baseName: 'petStore.ts',
|
|
175
|
+
* path: 'src/models/petStore.ts',
|
|
176
|
+
* sources: [createSource({ name: 'Pet', value: 'export type Pet = { id: number }', isExportable: true })],
|
|
177
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
178
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
179
|
+
* })
|
|
180
|
+
* // file.id = SHA256 hash of the path
|
|
181
|
+
* // file.name = 'petStore'
|
|
182
|
+
* // file.extname = '.ts'
|
|
183
|
+
* ```
|
|
184
|
+
*/
|
|
185
|
+
export type FileNode<TMeta extends object = object> = BaseNode & {
|
|
186
|
+
kind: 'File'
|
|
187
|
+
/**
|
|
188
|
+
* Unique identifier derived from a SHA256 hash of the file path.
|
|
189
|
+
* @default hash
|
|
190
|
+
*/
|
|
191
|
+
id: string
|
|
192
|
+
/**
|
|
193
|
+
* File name without extension, derived from `baseName`.
|
|
194
|
+
* @link https://nodejs.org/api/path.html#pathformatpathobject
|
|
195
|
+
*/
|
|
196
|
+
name: string
|
|
197
|
+
/**
|
|
198
|
+
* File base name, including extension.
|
|
199
|
+
* Based on UNIX basename: `${name}${extname}`
|
|
200
|
+
* @link https://nodejs.org/api/path.html#pathbasenamepath-suffix
|
|
201
|
+
*/
|
|
202
|
+
baseName: `${string}.${string}`
|
|
203
|
+
/**
|
|
204
|
+
* Full qualified path to the file.
|
|
205
|
+
*/
|
|
206
|
+
path: string
|
|
207
|
+
/**
|
|
208
|
+
* File extension extracted from `baseName`.
|
|
209
|
+
*/
|
|
210
|
+
extname: Extname
|
|
211
|
+
/**
|
|
212
|
+
* Deduplicated list of source code fragments.
|
|
213
|
+
*/
|
|
214
|
+
sources: Array<SourceNode>
|
|
215
|
+
/**
|
|
216
|
+
* Deduplicated list of import declarations.
|
|
217
|
+
*/
|
|
218
|
+
imports: Array<ImportNode>
|
|
219
|
+
/**
|
|
220
|
+
* Deduplicated list of export declarations.
|
|
221
|
+
*/
|
|
222
|
+
exports: Array<ExportNode>
|
|
223
|
+
/**
|
|
224
|
+
* Optional metadata attached to this file (used by plugins for barrel generation etc.).
|
|
225
|
+
*/
|
|
226
|
+
meta?: TMeta
|
|
227
|
+
/**
|
|
228
|
+
* Optional banner prepended to the generated file content.
|
|
229
|
+
*/
|
|
230
|
+
banner?: string
|
|
231
|
+
/**
|
|
232
|
+
* Optional footer appended to the generated file content.
|
|
233
|
+
*/
|
|
234
|
+
footer?: string
|
|
235
|
+
}
|
package/src/nodes/function.ts
CHANGED
|
@@ -1,19 +1,37 @@
|
|
|
1
1
|
import type { BaseNode } from './base.ts'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* AST node representing a language-agnostic type expression
|
|
5
|
-
* Each language printer renders the variant into its own syntax.
|
|
4
|
+
* AST node representing a language-agnostic type expression used as a function parameter
|
|
5
|
+
* type annotation. Each language printer renders the variant into its own syntax.
|
|
6
6
|
*
|
|
7
7
|
* - `struct` — an inline anonymous type grouping named fields.
|
|
8
|
-
* TypeScript renders as `{ petId: string; name?: string }
|
|
8
|
+
* TypeScript renders as `{ petId: string; name?: string }`.
|
|
9
9
|
* - `member` — a single named field accessed from a named group type.
|
|
10
|
-
* TypeScript renders as `PathParams['petId']
|
|
10
|
+
* TypeScript renders as `PathParams['petId']`.
|
|
11
|
+
*
|
|
12
|
+
* @example Reference variant
|
|
13
|
+
* ```ts
|
|
14
|
+
* createParamsType({ variant: 'reference', name: 'QueryParams' })
|
|
15
|
+
* // QueryParams
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* @example Struct variant
|
|
19
|
+
* ```ts
|
|
20
|
+
* createParamsType({ variant: 'struct', properties: [{ name: 'petId', optional: false, type: createParamsType({ variant: 'reference', name: 'string' }) }] })
|
|
21
|
+
* // { petId: string }
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* @example Member variant
|
|
25
|
+
* ```ts
|
|
26
|
+
* createParamsType({ variant: 'member', base: 'PathParams', key: 'petId' })
|
|
27
|
+
* // PathParams['petId']
|
|
28
|
+
* ```
|
|
11
29
|
*/
|
|
12
|
-
export type
|
|
30
|
+
export type ParamsTypeNode = BaseNode & {
|
|
13
31
|
/**
|
|
14
32
|
* Node kind.
|
|
15
33
|
*/
|
|
16
|
-
kind: '
|
|
34
|
+
kind: 'ParamsType'
|
|
17
35
|
} & (
|
|
18
36
|
| {
|
|
19
37
|
/**
|
|
@@ -35,7 +53,7 @@ export type TypeNode = BaseNode & {
|
|
|
35
53
|
/**
|
|
36
54
|
* Properties of the struct type.
|
|
37
55
|
*/
|
|
38
|
-
properties: Array<{ name: string; optional: boolean; type:
|
|
56
|
+
properties: Array<{ name: string; optional: boolean; type: ParamsTypeNode }>
|
|
39
57
|
}
|
|
40
58
|
| {
|
|
41
59
|
/**
|
|
@@ -79,19 +97,19 @@ export type FunctionParameterNode = BaseNode & {
|
|
|
79
97
|
*/
|
|
80
98
|
name: string
|
|
81
99
|
/**
|
|
82
|
-
* Type annotation as a structured {@link
|
|
100
|
+
* Type annotation as a structured {@link ParamsTypeNode}.
|
|
83
101
|
* Omit for untyped output.
|
|
84
102
|
*
|
|
85
103
|
* @example Reference type node
|
|
86
|
-
* `{ kind: '
|
|
104
|
+
* `{ kind: 'ParamsType', variant: 'reference', name: 'string' }` → `petId: string`
|
|
87
105
|
*
|
|
88
106
|
* @example Struct type node
|
|
89
|
-
* `{ kind: '
|
|
107
|
+
* `{ kind: 'ParamsType', variant: 'struct', properties: [...] }` → `{ key: Type; other?: OtherType }`
|
|
90
108
|
*
|
|
91
109
|
* @example Member type node
|
|
92
|
-
* `{ kind: '
|
|
110
|
+
* `{ kind: 'ParamsType', variant: 'member', base: 'PathParams', key: 'petId' }` → `PathParams['petId']`
|
|
93
111
|
*/
|
|
94
|
-
type?:
|
|
112
|
+
type?: ParamsTypeNode
|
|
95
113
|
/**
|
|
96
114
|
* When `true` the parameter is emitted as a rest parameter.
|
|
97
115
|
*
|
|
@@ -147,7 +165,7 @@ export type ParameterGroupNode = BaseNode & {
|
|
|
147
165
|
* Optional explicit type annotation for the whole group.
|
|
148
166
|
* When absent, printers auto-compute it from `properties`.
|
|
149
167
|
*/
|
|
150
|
-
type?:
|
|
168
|
+
type?: ParamsTypeNode
|
|
151
169
|
/**
|
|
152
170
|
* When `true`, `properties` are emitted as individual top-level parameters instead of
|
|
153
171
|
* being wrapped in a single grouped construct.
|
|
@@ -191,11 +209,11 @@ export type FunctionParametersNode = BaseNode & {
|
|
|
191
209
|
}
|
|
192
210
|
|
|
193
211
|
/**
|
|
194
|
-
*
|
|
212
|
+
* Union of all function-parameter AST node variants used by the function-parameter printer.
|
|
195
213
|
*/
|
|
196
|
-
export type
|
|
214
|
+
export type FunctionParamNode = FunctionParameterNode | ParameterGroupNode | FunctionParametersNode | ParamsTypeNode
|
|
197
215
|
|
|
198
216
|
/**
|
|
199
|
-
* Handler map keys — one per `
|
|
217
|
+
* Handler map keys — one per `FunctionParamNode` kind.
|
|
200
218
|
*/
|
|
201
|
-
export type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | '
|
|
219
|
+
export type FunctionNodeType = 'functionParameter' | 'parameterGroup' | 'functionParameters' | 'paramsType'
|
package/src/nodes/index.ts
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
|
-
import type { FunctionNode } from './
|
|
1
|
+
import type { ArrowFunctionNode, ConstNode, FunctionNode, TypeNode } from './code.ts'
|
|
2
|
+
import type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
|
|
3
|
+
import type { FunctionParamNode, ParamsTypeNode } from './function.ts'
|
|
2
4
|
import type { OperationNode } from './operation.ts'
|
|
5
|
+
import type { OutputNode } from './output.ts'
|
|
3
6
|
import type { ParameterNode } from './parameter.ts'
|
|
4
7
|
import type { PropertyNode } from './property.ts'
|
|
5
8
|
import type { ResponseNode } from './response.ts'
|
|
6
|
-
import type {
|
|
9
|
+
import type { InputNode } from './root.ts'
|
|
7
10
|
import type { SchemaNode } from './schema.ts'
|
|
8
11
|
|
|
9
12
|
export type { BaseNode, NodeKind } from './base.ts'
|
|
10
|
-
export type {
|
|
13
|
+
export type { ArrowFunctionNode, CodeNode, ConstNode, FunctionNode, JSDocNode, TypeDeclarationNode, TypeNode } from './code.ts'
|
|
14
|
+
export type { ExportNode, FileNode, ImportNode, SourceNode } from './file.ts'
|
|
15
|
+
export type { FunctionNodeType, FunctionParameterNode, FunctionParametersNode, FunctionParamNode, ParameterGroupNode, ParamsTypeNode } from './function.ts'
|
|
11
16
|
export type { HttpStatusCode, MediaType, StatusCode } from './http.ts'
|
|
12
17
|
export type { HttpMethod, OperationNode } from './operation.ts'
|
|
18
|
+
export type { OutputNode } from './output.ts'
|
|
13
19
|
export type { ParameterLocation, ParameterNode } from './parameter.ts'
|
|
14
20
|
export type { PropertyNode } from './property.ts'
|
|
15
21
|
export type { ResponseNode } from './response.ts'
|
|
16
|
-
export type {
|
|
22
|
+
export type { InputMeta, InputNode } from './root.ts'
|
|
17
23
|
export type {
|
|
18
24
|
ArraySchemaNode,
|
|
19
25
|
ComplexSchemaType,
|
|
@@ -50,12 +56,31 @@ export type {
|
|
|
50
56
|
* ```ts
|
|
51
57
|
* function getKind(node: Node): string {
|
|
52
58
|
* switch (node.kind) {
|
|
53
|
-
* case '
|
|
54
|
-
* return '
|
|
59
|
+
* case 'Input':
|
|
60
|
+
* return 'input'
|
|
61
|
+
* case 'Output':
|
|
62
|
+
* return 'output'
|
|
55
63
|
* default:
|
|
56
64
|
* return 'other'
|
|
57
65
|
* }
|
|
58
66
|
* }
|
|
59
67
|
* ```
|
|
60
68
|
*/
|
|
61
|
-
export type Node =
|
|
69
|
+
export type Node =
|
|
70
|
+
| InputNode
|
|
71
|
+
| OutputNode
|
|
72
|
+
| OperationNode
|
|
73
|
+
| SchemaNode
|
|
74
|
+
| PropertyNode
|
|
75
|
+
| ParameterNode
|
|
76
|
+
| ResponseNode
|
|
77
|
+
| FunctionParamNode
|
|
78
|
+
| FileNode
|
|
79
|
+
| ImportNode
|
|
80
|
+
| ExportNode
|
|
81
|
+
| SourceNode
|
|
82
|
+
| ConstNode
|
|
83
|
+
| TypeNode
|
|
84
|
+
| ParamsTypeNode
|
|
85
|
+
| FunctionNode
|
|
86
|
+
| ArrowFunctionNode
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { BaseNode } from './base.ts'
|
|
2
|
+
import type { FileNode } from './file.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Output AST node that groups all generated file output for one API document.
|
|
6
|
+
*
|
|
7
|
+
* Produced by generators and consumed by the build pipeline to write files.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* const output: OutputNode = {
|
|
12
|
+
* kind: 'Output',
|
|
13
|
+
* files: [],
|
|
14
|
+
* }
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export type OutputNode = BaseNode & {
|
|
18
|
+
/**
|
|
19
|
+
* Node kind.
|
|
20
|
+
*/
|
|
21
|
+
kind: 'Output'
|
|
22
|
+
/**
|
|
23
|
+
* Generated file nodes.
|
|
24
|
+
*/
|
|
25
|
+
files: Array<FileNode>
|
|
26
|
+
}
|