@kubb/ast 5.0.0-beta.7 → 5.0.0-beta.71
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 +17 -10
- package/README.md +53 -27
- package/dist/defineMacro-5Dvct8k_.cjs +114 -0
- package/dist/defineMacro-5Dvct8k_.cjs.map +1 -0
- package/dist/defineMacro-BXpTwp0y.d.ts +466 -0
- package/dist/defineMacro-VfsvblGi.js +98 -0
- package/dist/defineMacro-VfsvblGi.js.map +1 -0
- package/dist/index-DJEyS5y6.d.ts +2428 -0
- package/dist/index.cjs +198 -2239
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +98 -3400
- package/dist/index.js +149 -2160
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +130 -0
- package/dist/macros.cjs.map +1 -0
- package/dist/macros.d.ts +61 -0
- package/dist/macros.js +128 -0
- package/dist/macros.js.map +1 -0
- package/dist/operationParams-BaY12i2I.d.ts +148 -0
- package/dist/refs-D18OeCgb.js +117 -0
- package/dist/refs-D18OeCgb.js.map +1 -0
- package/dist/refs-DuP3_Leg.cjs +157 -0
- package/dist/refs-DuP3_Leg.cjs.map +1 -0
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/dist/types-BsP1SK9j.d.ts +244 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.js +1 -0
- package/dist/utils.cjs +803 -0
- package/dist/utils.cjs.map +1 -0
- package/dist/utils.d.ts +354 -0
- package/dist/utils.js +777 -0
- package/dist/utils.js.map +1 -0
- package/dist/visitor-CQdSiClY.cjs +1749 -0
- package/dist/visitor-CQdSiClY.cjs.map +1 -0
- package/dist/visitor-dcVC5TOW.js +1313 -0
- package/dist/visitor-dcVC5TOW.js.map +1 -0
- package/package.json +17 -5
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -46
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -915
- package/src/visitor.ts +0 -592
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
+
import { C as ParameterNode, Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, o as InputNode, rt as ContentNode, t as Node, y as RequestBodyNode } from "./index-DJEyS5y6.js";
|
|
3
|
+
|
|
4
|
+
//#region src/constants.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Traversal depth for AST visitor utilities.
|
|
7
|
+
*
|
|
8
|
+
* - `'shallow'` visits only the immediate node, skipping children.
|
|
9
|
+
* - `'deep'` recursively visits all descendant nodes.
|
|
10
|
+
*/
|
|
11
|
+
type VisitorDepth = 'shallow' | 'deep';
|
|
12
|
+
/**
|
|
13
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
14
|
+
*
|
|
15
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
16
|
+
*/
|
|
17
|
+
declare const schemaTypes: {
|
|
18
|
+
/**
|
|
19
|
+
* Text value.
|
|
20
|
+
*/
|
|
21
|
+
readonly string: "string";
|
|
22
|
+
/**
|
|
23
|
+
* Floating-point number (`float`, `double`).
|
|
24
|
+
*/
|
|
25
|
+
readonly number: "number";
|
|
26
|
+
/**
|
|
27
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
28
|
+
*/
|
|
29
|
+
readonly integer: "integer";
|
|
30
|
+
/**
|
|
31
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
32
|
+
*/
|
|
33
|
+
readonly bigint: "bigint";
|
|
34
|
+
/**
|
|
35
|
+
* Boolean value.
|
|
36
|
+
*/
|
|
37
|
+
readonly boolean: "boolean";
|
|
38
|
+
/**
|
|
39
|
+
* Explicit null value.
|
|
40
|
+
*/
|
|
41
|
+
readonly null: "null";
|
|
42
|
+
/**
|
|
43
|
+
* Any value (no type restriction).
|
|
44
|
+
*/
|
|
45
|
+
readonly any: "any";
|
|
46
|
+
/**
|
|
47
|
+
* Unknown value (must be narrowed before usage).
|
|
48
|
+
*/
|
|
49
|
+
readonly unknown: "unknown";
|
|
50
|
+
/**
|
|
51
|
+
* No return value (`void`).
|
|
52
|
+
*/
|
|
53
|
+
readonly void: "void";
|
|
54
|
+
/**
|
|
55
|
+
* Object with named properties.
|
|
56
|
+
*/
|
|
57
|
+
readonly object: "object";
|
|
58
|
+
/**
|
|
59
|
+
* Sequential list of items.
|
|
60
|
+
*/
|
|
61
|
+
readonly array: "array";
|
|
62
|
+
/**
|
|
63
|
+
* Fixed-length list with position-specific items.
|
|
64
|
+
*/
|
|
65
|
+
readonly tuple: "tuple";
|
|
66
|
+
/**
|
|
67
|
+
* "One of" multiple schema members.
|
|
68
|
+
*/
|
|
69
|
+
readonly union: "union";
|
|
70
|
+
/**
|
|
71
|
+
* "All of" multiple schema members.
|
|
72
|
+
*/
|
|
73
|
+
readonly intersection: "intersection";
|
|
74
|
+
/**
|
|
75
|
+
* Enum schema.
|
|
76
|
+
*/
|
|
77
|
+
readonly enum: "enum";
|
|
78
|
+
/**
|
|
79
|
+
* Reference to another schema.
|
|
80
|
+
*/
|
|
81
|
+
readonly ref: "ref";
|
|
82
|
+
/**
|
|
83
|
+
* Calendar date (for example `2026-03-24`).
|
|
84
|
+
*/
|
|
85
|
+
readonly date: "date";
|
|
86
|
+
/**
|
|
87
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
88
|
+
*/
|
|
89
|
+
readonly datetime: "datetime";
|
|
90
|
+
/**
|
|
91
|
+
* Time-only value (for example `09:00:00`).
|
|
92
|
+
*/
|
|
93
|
+
readonly time: "time";
|
|
94
|
+
/**
|
|
95
|
+
* UUID value.
|
|
96
|
+
*/
|
|
97
|
+
readonly uuid: "uuid";
|
|
98
|
+
/**
|
|
99
|
+
* Email address value.
|
|
100
|
+
*/
|
|
101
|
+
readonly email: "email";
|
|
102
|
+
/**
|
|
103
|
+
* URL value.
|
|
104
|
+
*/
|
|
105
|
+
readonly url: "url";
|
|
106
|
+
/**
|
|
107
|
+
* IPv4 address value.
|
|
108
|
+
*/
|
|
109
|
+
readonly ipv4: "ipv4";
|
|
110
|
+
/**
|
|
111
|
+
* IPv6 address value.
|
|
112
|
+
*/
|
|
113
|
+
readonly ipv6: "ipv6";
|
|
114
|
+
/**
|
|
115
|
+
* Binary/blob value.
|
|
116
|
+
*/
|
|
117
|
+
readonly blob: "blob";
|
|
118
|
+
/**
|
|
119
|
+
* Impossible value (`never`).
|
|
120
|
+
*/
|
|
121
|
+
readonly never: "never";
|
|
122
|
+
};
|
|
123
|
+
//#endregion
|
|
124
|
+
//#region src/visitor.d.ts
|
|
125
|
+
/**
|
|
126
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
127
|
+
*
|
|
128
|
+
* `ParentOf` uses this map to find parent types.
|
|
129
|
+
*/
|
|
130
|
+
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
131
|
+
/**
|
|
132
|
+
* Resolves the parent node type for a given AST node type.
|
|
133
|
+
*
|
|
134
|
+
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```ts
|
|
138
|
+
* type InputParent = ParentOf<InputNode>
|
|
139
|
+
* // undefined
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* ```ts
|
|
144
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
145
|
+
* // SchemaNode
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
151
|
+
* // InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
|
|
155
|
+
/**
|
|
156
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
157
|
+
* `parent` is typed from the current node type.
|
|
158
|
+
*
|
|
159
|
+
* @example
|
|
160
|
+
* ```ts
|
|
161
|
+
* const visitor: Visitor = {
|
|
162
|
+
* schema(node, { parent }) {
|
|
163
|
+
* // parent type is narrowed by node kind
|
|
164
|
+
* },
|
|
165
|
+
* }
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
type VisitorContext<T extends Node = Node> = {
|
|
169
|
+
/**
|
|
170
|
+
* Parent node of the currently visited node.
|
|
171
|
+
* For `InputNode`, this is `undefined`.
|
|
172
|
+
*/
|
|
173
|
+
parent?: ParentOf<T>;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
177
|
+
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
178
|
+
* to leave it untouched.
|
|
179
|
+
*
|
|
180
|
+
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
181
|
+
* rewrites the AST before printing.
|
|
182
|
+
*
|
|
183
|
+
* @example Prefix every operationId
|
|
184
|
+
* ```ts
|
|
185
|
+
* const visitor: Visitor = {
|
|
186
|
+
* operation(node) {
|
|
187
|
+
* return { ...node, operationId: `api_${node.operationId}` }
|
|
188
|
+
* },
|
|
189
|
+
* }
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @example Strip schema descriptions
|
|
193
|
+
* ```ts
|
|
194
|
+
* const visitor: Visitor = {
|
|
195
|
+
* schema(node) {
|
|
196
|
+
* return { ...node, description: undefined }
|
|
197
|
+
* },
|
|
198
|
+
* }
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
type Visitor = {
|
|
202
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
|
|
203
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
|
|
204
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
|
|
205
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
|
|
206
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
|
|
207
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
|
|
208
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* A visitor callback result that may be sync or async.
|
|
212
|
+
*/
|
|
213
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
214
|
+
/**
|
|
215
|
+
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
216
|
+
*
|
|
217
|
+
* @example
|
|
218
|
+
* ```ts
|
|
219
|
+
* const visitor: AsyncVisitor = {
|
|
220
|
+
* async operation(node) {
|
|
221
|
+
* await Promise.resolve(node.operationId)
|
|
222
|
+
* },
|
|
223
|
+
* }
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
type AsyncVisitor = {
|
|
227
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
|
|
228
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
|
|
229
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
|
|
230
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
|
|
231
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
|
|
232
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
|
|
233
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
|
|
234
|
+
};
|
|
235
|
+
/**
|
|
236
|
+
* Visitor used by `collect`.
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```ts
|
|
240
|
+
* const visitor: CollectVisitor<string> = {
|
|
241
|
+
* operation(node) {
|
|
242
|
+
* return node.operationId
|
|
243
|
+
* },
|
|
244
|
+
* }
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
type CollectVisitor<T> = {
|
|
248
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
|
|
249
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
|
|
250
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
|
|
251
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
|
|
252
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
|
|
253
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
|
|
254
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
|
|
255
|
+
};
|
|
256
|
+
/**
|
|
257
|
+
* Options for `transform`.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```ts
|
|
261
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
262
|
+
* ```
|
|
263
|
+
*
|
|
264
|
+
* @example
|
|
265
|
+
* ```ts
|
|
266
|
+
* // Only transform the current node, not nested children
|
|
267
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
268
|
+
* ```
|
|
269
|
+
*/
|
|
270
|
+
type TransformOptions = Visitor & {
|
|
271
|
+
/**
|
|
272
|
+
* Traversal depth.
|
|
273
|
+
* @default 'deep'
|
|
274
|
+
*/
|
|
275
|
+
depth?: VisitorDepth;
|
|
276
|
+
/**
|
|
277
|
+
* Internal parent override used during recursion.
|
|
278
|
+
*/
|
|
279
|
+
parent?: Node;
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* Options for `walk`.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```ts
|
|
286
|
+
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
287
|
+
* ```
|
|
288
|
+
*/
|
|
289
|
+
type WalkOptions = AsyncVisitor & {
|
|
290
|
+
/**
|
|
291
|
+
* Traversal depth.
|
|
292
|
+
* @default 'deep'
|
|
293
|
+
*/
|
|
294
|
+
depth?: VisitorDepth;
|
|
295
|
+
/**
|
|
296
|
+
* Maximum number of sibling nodes visited concurrently.
|
|
297
|
+
* @default 30
|
|
298
|
+
*/
|
|
299
|
+
concurrency?: number;
|
|
300
|
+
};
|
|
301
|
+
/**
|
|
302
|
+
* Options for `collect`.
|
|
303
|
+
*
|
|
304
|
+
* @example
|
|
305
|
+
* ```ts
|
|
306
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
307
|
+
* ```
|
|
308
|
+
*/
|
|
309
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
310
|
+
/**
|
|
311
|
+
* Traversal depth.
|
|
312
|
+
* @default 'deep'
|
|
313
|
+
*/
|
|
314
|
+
depth?: VisitorDepth;
|
|
315
|
+
/**
|
|
316
|
+
* Internal parent override used during recursion.
|
|
317
|
+
*/
|
|
318
|
+
parent?: Node;
|
|
319
|
+
};
|
|
320
|
+
/**
|
|
321
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
322
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
323
|
+
*
|
|
324
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
325
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
326
|
+
* work. Lower values reduce memory pressure.
|
|
327
|
+
*
|
|
328
|
+
* @example Log every operation
|
|
329
|
+
* ```ts
|
|
330
|
+
* await walk(root, {
|
|
331
|
+
* operation(node) {
|
|
332
|
+
* console.log(node.operationId)
|
|
333
|
+
* },
|
|
334
|
+
* })
|
|
335
|
+
* ```
|
|
336
|
+
*
|
|
337
|
+
* @example Only visit the root node
|
|
338
|
+
* ```ts
|
|
339
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
343
|
+
/**
|
|
344
|
+
* Synchronous depth-first transform. Each visitor callback can return a
|
|
345
|
+
* replacement node. Returning `undefined` keeps the original.
|
|
346
|
+
*
|
|
347
|
+
* The original tree is never mutated, a new tree is returned. Pass
|
|
348
|
+
* `depth: 'shallow'` to skip recursion into children.
|
|
349
|
+
*
|
|
350
|
+
* @example Prefix every operationId
|
|
351
|
+
* ```ts
|
|
352
|
+
* const next = transform(root, {
|
|
353
|
+
* operation(node) {
|
|
354
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
355
|
+
* },
|
|
356
|
+
* })
|
|
357
|
+
* ```
|
|
358
|
+
*
|
|
359
|
+
* @example Replace only the root node
|
|
360
|
+
* ```ts
|
|
361
|
+
* const next = transform(root, {
|
|
362
|
+
* depth: 'shallow',
|
|
363
|
+
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
364
|
+
* })
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
368
|
+
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
369
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
370
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
371
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
372
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
373
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
374
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
375
|
+
/**
|
|
376
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
377
|
+
* callbacks return into an array.
|
|
378
|
+
*
|
|
379
|
+
* @example Collect every operationId
|
|
380
|
+
* ```ts
|
|
381
|
+
* const ids = collect<string>(root, {
|
|
382
|
+
* operation(node) {
|
|
383
|
+
* return node.operationId
|
|
384
|
+
* },
|
|
385
|
+
* })
|
|
386
|
+
* ```
|
|
387
|
+
*/
|
|
388
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/defineMacro.d.ts
|
|
391
|
+
/**
|
|
392
|
+
* Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,
|
|
393
|
+
* and `undefined` keeps declaration order.
|
|
394
|
+
*/
|
|
395
|
+
type Enforce = 'pre' | 'post';
|
|
396
|
+
/**
|
|
397
|
+
* A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a
|
|
398
|
+
* {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an
|
|
399
|
+
* optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter
|
|
400
|
+
* and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).
|
|
401
|
+
*/
|
|
402
|
+
type Macro = Visitor & {
|
|
403
|
+
/**
|
|
404
|
+
* Macro identifier used to tell macros apart, for example `'simplify-union'`.
|
|
405
|
+
*/
|
|
406
|
+
name: string;
|
|
407
|
+
/**
|
|
408
|
+
* Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.
|
|
409
|
+
* Ordering within a bucket follows list order.
|
|
410
|
+
*/
|
|
411
|
+
enforce?: Enforce;
|
|
412
|
+
/**
|
|
413
|
+
* Gate checked against the current node before any callback runs. When it returns `false`
|
|
414
|
+
* the macro is skipped for that node.
|
|
415
|
+
*/
|
|
416
|
+
when?: (node: Node) => boolean;
|
|
417
|
+
};
|
|
418
|
+
/**
|
|
419
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
420
|
+
* Adds no runtime behavior.
|
|
421
|
+
*
|
|
422
|
+
* @example
|
|
423
|
+
* ```ts
|
|
424
|
+
* const macroUntagged = defineMacro({
|
|
425
|
+
* name: 'untagged',
|
|
426
|
+
* operation(node) {
|
|
427
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
428
|
+
* },
|
|
429
|
+
* })
|
|
430
|
+
* ```
|
|
431
|
+
*/
|
|
432
|
+
declare function defineMacro(macro: Macro): Macro;
|
|
433
|
+
/**
|
|
434
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
435
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
436
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
437
|
+
* has no names, ordering, or composition.
|
|
438
|
+
*
|
|
439
|
+
* @example
|
|
440
|
+
* ```ts
|
|
441
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
442
|
+
* const next = transform(root, visitor)
|
|
443
|
+
* ```
|
|
444
|
+
*/
|
|
445
|
+
declare function composeMacros(macros: ReadonlyArray<Macro>): Visitor;
|
|
446
|
+
/**
|
|
447
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
448
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
449
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
450
|
+
*
|
|
451
|
+
* @example
|
|
452
|
+
* ```ts
|
|
453
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
454
|
+
* ```
|
|
455
|
+
*
|
|
456
|
+
* @example Apply to the root node only
|
|
457
|
+
* ```ts
|
|
458
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
459
|
+
* ```
|
|
460
|
+
*/
|
|
461
|
+
declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: {
|
|
462
|
+
depth?: VisitorDepth;
|
|
463
|
+
}): TNode;
|
|
464
|
+
//#endregion
|
|
465
|
+
export { defineMacro as a, VisitorContext as c, walk as d, schemaTypes as f, composeMacros as i, collect as l, Macro as n, ParentOf as o, applyMacros as r, Visitor as s, Enforce as t, transform as u };
|
|
466
|
+
//# sourceMappingURL=defineMacro-BXpTwp0y.d.ts.map
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
+
import { r as transform, st as visitorKeys } from "./visitor-dcVC5TOW.js";
|
|
3
|
+
//#region src/defineMacro.ts
|
|
4
|
+
/**
|
|
5
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
6
|
+
* list keeps its authored order.
|
|
7
|
+
*/
|
|
8
|
+
function enforceWeight(enforce) {
|
|
9
|
+
if (enforce === "pre") return 0;
|
|
10
|
+
if (enforce === "post") return 2;
|
|
11
|
+
return 1;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
15
|
+
* Adds no runtime behavior.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* const macroUntagged = defineMacro({
|
|
20
|
+
* name: 'untagged',
|
|
21
|
+
* operation(node) {
|
|
22
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
23
|
+
* },
|
|
24
|
+
* })
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
function defineMacro(macro) {
|
|
28
|
+
return macro;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
32
|
+
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
33
|
+
* original reference (structural sharing).
|
|
34
|
+
*/
|
|
35
|
+
function chain({ macros, key, node, context }) {
|
|
36
|
+
let current = node;
|
|
37
|
+
for (const macro of macros) {
|
|
38
|
+
const callback = macro[key];
|
|
39
|
+
if (!callback) continue;
|
|
40
|
+
if (macro.when && !macro.when(current)) continue;
|
|
41
|
+
const next = callback(current, context);
|
|
42
|
+
if (next != null) current = next;
|
|
43
|
+
}
|
|
44
|
+
return current === node ? void 0 : current;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
48
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
49
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
50
|
+
* has no names, ordering, or composition.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
55
|
+
* const next = transform(root, visitor)
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function composeMacros(macros) {
|
|
59
|
+
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
60
|
+
const visitor = {};
|
|
61
|
+
for (const key of visitorKeys) {
|
|
62
|
+
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
63
|
+
const callback = (node, context) => chain({
|
|
64
|
+
macros: ordered,
|
|
65
|
+
key,
|
|
66
|
+
node,
|
|
67
|
+
context
|
|
68
|
+
});
|
|
69
|
+
visitor[key] = callback;
|
|
70
|
+
}
|
|
71
|
+
return visitor;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
75
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
76
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* @example Apply to the root node only
|
|
84
|
+
* ```ts
|
|
85
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
86
|
+
* ```
|
|
87
|
+
*/
|
|
88
|
+
function applyMacros(root, macros, options) {
|
|
89
|
+
if (macros.length === 0) return root;
|
|
90
|
+
return transform(root, {
|
|
91
|
+
...composeMacros(macros),
|
|
92
|
+
...options
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
//#endregion
|
|
96
|
+
export { composeMacros as n, defineMacro as r, applyMacros as t };
|
|
97
|
+
|
|
98
|
+
//# sourceMappingURL=defineMacro-VfsvblGi.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defineMacro-VfsvblGi.js","names":[],"sources":["../src/defineMacro.ts"],"sourcesContent":["import type { VisitorDepth } from './constants.ts'\nimport type { VisitorKey } from './defineNode.ts'\nimport { visitorKeys } from './defineNode.ts'\nimport type { Node } from './nodes/index.ts'\nimport type { Visitor, VisitorContext } from './visitor.ts'\nimport { transform } from './visitor.ts'\n\n/**\n * Ordering hint shared by macros and plugins. `pre` runs before unmarked items, `post` after,\n * and `undefined` keeps declaration order.\n */\nexport type Enforce = 'pre' | 'post'\n\n/**\n * Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain\n * list keeps its authored order.\n */\nfunction enforceWeight(enforce?: Enforce): number {\n if (enforce === 'pre') return 0\n if (enforce === 'post') return 2\n return 1\n}\n\n/**\n * A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a\n * {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an\n * optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter\n * and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).\n */\nexport type Macro = Visitor & {\n /**\n * Macro identifier used to tell macros apart, for example `'simplify-union'`.\n */\n name: string\n /**\n * Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.\n * Ordering within a bucket follows list order.\n */\n enforce?: Enforce\n /**\n * Gate checked against the current node before any callback runs. When it returns `false`\n * the macro is skipped for that node.\n */\n when?: (node: Node) => boolean\n}\n\n/**\n * Types a macro for inference and a single construction site, mirroring `definePlugin`.\n * Adds no runtime behavior.\n *\n * @example\n * ```ts\n * const macroUntagged = defineMacro({\n * name: 'untagged',\n * operation(node) {\n * return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }\n * },\n * })\n * ```\n */\nexport function defineMacro(macro: Macro): Macro {\n return macro\n}\n\ntype MacroCallback = (node: Node, context: VisitorContext) => Node | null | undefined\n\ntype ChainProps = {\n macros: ReadonlyArray<Macro>\n key: VisitorKey\n node: Node\n context: VisitorContext\n}\n\n/**\n * Runs every macro's callback for one node kind in order, chaining the result so each macro sees\n * the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the\n * original reference (structural sharing).\n */\nfunction chain({ macros, key, node, context }: ChainProps): Node | undefined {\n let current = node\n\n for (const macro of macros) {\n const callback = macro[key] as MacroCallback | undefined\n if (!callback) continue\n if (macro.when && !macro.when(current)) continue\n\n const next = callback(current, context)\n if (next != null) current = next\n }\n\n return current === node ? undefined : current\n}\n\n/**\n * Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin\n * transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied\n * sequentially per node so later macros see earlier output. This differs from a plain visitor, which\n * has no names, ordering, or composition.\n *\n * @example\n * ```ts\n * const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])\n * const next = transform(root, visitor)\n * ```\n */\nexport function composeMacros(macros: ReadonlyArray<Macro>): Visitor {\n const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce))\n\n const visitor: Visitor = {}\n for (const key of visitorKeys) {\n if (!ordered.some((macro) => typeof macro[key] === 'function')) continue\n\n const callback = (node: Node, context: VisitorContext) => chain({ macros: ordered, key, node, context })\n ;(visitor as Record<VisitorKey, MacroCallback>)[key] = callback\n }\n\n return visitor\n}\n\n/**\n * Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s\n * structural sharing, so an empty or no-op macro list returns the same reference. Pass\n * `depth: 'shallow'` to rewrite the root node only.\n *\n * @example\n * ```ts\n * const next = applyMacros(root, [macroIntegerToString])\n * ```\n *\n * @example Apply to the root node only\n * ```ts\n * const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })\n * ```\n */\nexport function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: { depth?: VisitorDepth }): TNode {\n if (macros.length === 0) return root\n\n return transform(root, { ...composeMacros(macros), ...options }) as TNode\n}\n"],"mappings":";;;;;;;AAiBA,SAAS,cAAc,SAA2B;CAChD,IAAI,YAAY,OAAO,OAAO;CAC9B,IAAI,YAAY,QAAQ,OAAO;CAC/B,OAAO;AACT;;;;;;;;;;;;;;;AAuCA,SAAgB,YAAY,OAAqB;CAC/C,OAAO;AACT;;;;;;AAgBA,SAAS,MAAM,EAAE,QAAQ,KAAK,MAAM,WAAyC;CAC3E,IAAI,UAAU;CAEd,KAAK,MAAM,SAAS,QAAQ;EAC1B,MAAM,WAAW,MAAM;EACvB,IAAI,CAAC,UAAU;EACf,IAAI,MAAM,QAAQ,CAAC,MAAM,KAAK,OAAO,GAAG;EAExC,MAAM,OAAO,SAAS,SAAS,OAAO;EACtC,IAAI,QAAQ,MAAM,UAAU;CAC9B;CAEA,OAAO,YAAY,OAAO,KAAA,IAAY;AACxC;;;;;;;;;;;;;AAcA,SAAgB,cAAc,QAAuC;CACnE,MAAM,UAAU,CAAC,GAAG,MAAM,CAAC,CAAC,MAAM,GAAG,MAAM,cAAc,EAAE,OAAO,IAAI,cAAc,EAAE,OAAO,CAAC;CAE9F,MAAM,UAAmB,CAAC;CAC1B,KAAK,MAAM,OAAO,aAAa;EAC7B,IAAI,CAAC,QAAQ,MAAM,UAAU,OAAO,MAAM,SAAS,UAAU,GAAG;EAEhE,MAAM,YAAY,MAAY,YAA4B,MAAM;GAAE,QAAQ;GAAS;GAAK;GAAM;EAAQ,CAAC;EACtG,QAA+C,OAAO;CACzD;CAEA,OAAO;AACT;;;;;;;;;;;;;;;;AAiBA,SAAgB,YAAgC,MAAa,QAA8B,SAA2C;CACpI,IAAI,OAAO,WAAW,GAAG,OAAO;CAEhC,OAAO,UAAU,MAAM;EAAE,GAAG,cAAc,MAAM;EAAG,GAAG;CAAQ,CAAC;AACjE"}
|