@kubb/ast 5.0.0-beta.80 → 5.0.0-beta.82
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 +2280 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +420 -18
- package/dist/index.js +2133 -15
- package/dist/index.js.map +1 -1
- package/dist/{index-Cu2zmNxv.d.ts → types-XcYJovdT.d.ts} +717 -2
- package/dist/types.d.ts +1 -3
- package/package.json +1 -12
- package/dist/defineMacro-CqX4m8Dq.js +0 -98
- package/dist/defineMacro-CqX4m8Dq.js.map +0 -1
- package/dist/defineMacro-DmGR7vfu.cjs +0 -114
- package/dist/defineMacro-DmGR7vfu.cjs.map +0 -1
- package/dist/defineMacro-DzsACbFo.d.ts +0 -466
- package/dist/macros.cjs +0 -130
- package/dist/macros.cjs.map +0 -1
- package/dist/macros.d.ts +0 -61
- package/dist/macros.js +0 -128
- package/dist/macros.js.map +0 -1
- package/dist/refs-Bg3rmujP.cjs +0 -135
- package/dist/refs-Bg3rmujP.cjs.map +0 -1
- package/dist/refs-BjBHwunY.js +0 -101
- package/dist/refs-BjBHwunY.js.map +0 -1
- package/dist/types-CWF7DV0f.d.ts +0 -259
- package/dist/utils.cjs +0 -613
- package/dist/utils.cjs.map +0 -1
- package/dist/utils.d.ts +0 -353
- package/dist/utils.js +0 -589
- package/dist/utils.js.map +0 -1
- package/dist/visitor-Dk0DNLfC.js +0 -1203
- package/dist/visitor-Dk0DNLfC.js.map +0 -1
- package/dist/visitor-h6dEM3vR.cjs +0 -1567
- package/dist/visitor-h6dEM3vR.cjs.map +0 -1
|
@@ -1,466 +0,0 @@
|
|
|
1
|
-
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { B as ContentNode, C as ParameterNode, et as SchemaNode, f as OperationNode, h as ResponseNode, lt as PropertyNode, n as OutputNode, o as InputNode, t as Node, y as RequestBodyNode } from "./index-Cu2zmNxv.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-DzsACbFo.d.ts.map
|
package/dist/macros.cjs
DELETED
|
@@ -1,130 +0,0 @@
|
|
|
1
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
const require_visitor = require("./visitor-h6dEM3vR.cjs");
|
|
3
|
-
const require_defineMacro = require("./defineMacro-DmGR7vfu.cjs");
|
|
4
|
-
const require_refs = require("./refs-Bg3rmujP.cjs");
|
|
5
|
-
//#region src/macros/macroDiscriminatorEnum.ts
|
|
6
|
-
/**
|
|
7
|
-
* Builds a macro that replaces a discriminator property's schema with a string enum of the given
|
|
8
|
-
* values. Object schemas that lack the property are returned unchanged.
|
|
9
|
-
*
|
|
10
|
-
* @example
|
|
11
|
-
* ```ts
|
|
12
|
-
* const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })
|
|
13
|
-
* const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })
|
|
14
|
-
* ```
|
|
15
|
-
*/
|
|
16
|
-
function macroDiscriminatorEnum({ propertyName, values, enumName }) {
|
|
17
|
-
return require_defineMacro.defineMacro({
|
|
18
|
-
name: "discriminator-enum",
|
|
19
|
-
schema(node) {
|
|
20
|
-
const objectNode = require_visitor.narrowSchema(node, "object");
|
|
21
|
-
if (!objectNode?.properties?.length) return void 0;
|
|
22
|
-
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return void 0;
|
|
23
|
-
return require_visitor.createSchema({
|
|
24
|
-
...objectNode,
|
|
25
|
-
properties: objectNode.properties.map((prop) => {
|
|
26
|
-
if (prop.name !== propertyName) return prop;
|
|
27
|
-
return require_visitor.createProperty({
|
|
28
|
-
...prop,
|
|
29
|
-
schema: require_visitor.createSchema({
|
|
30
|
-
type: "enum",
|
|
31
|
-
primitive: "string",
|
|
32
|
-
enumValues: values,
|
|
33
|
-
name: enumName,
|
|
34
|
-
readOnly: prop.schema.readOnly,
|
|
35
|
-
writeOnly: prop.schema.writeOnly
|
|
36
|
-
})
|
|
37
|
-
});
|
|
38
|
-
})
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region src/macros/macroEnumName.ts
|
|
45
|
-
/**
|
|
46
|
-
* Builds a macro that names an inline enum schema from its parent and property name. Boolean enums
|
|
47
|
-
* are left anonymous. Non-enum nodes are returned unchanged.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })
|
|
52
|
-
* const named = applyMacros(propSchema, [macro], { depth: 'shallow' })
|
|
53
|
-
* ```
|
|
54
|
-
*/
|
|
55
|
-
function macroEnumName({ parentName, propName, enumSuffix }) {
|
|
56
|
-
return require_defineMacro.defineMacro({
|
|
57
|
-
name: "enum-name",
|
|
58
|
-
schema(node) {
|
|
59
|
-
const enumNode = require_visitor.narrowSchema(node, "enum");
|
|
60
|
-
if (enumNode?.primitive === "boolean") return {
|
|
61
|
-
...node,
|
|
62
|
-
name: null
|
|
63
|
-
};
|
|
64
|
-
if (enumNode) return {
|
|
65
|
-
...node,
|
|
66
|
-
name: require_refs.enumPropName(parentName, propName, enumSuffix)
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
//#endregion
|
|
72
|
-
//#region src/macros/macroSimplifyUnion.ts
|
|
73
|
-
/**
|
|
74
|
-
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
75
|
-
*/
|
|
76
|
-
const SCALAR_PRIMITIVE_TYPES = /* @__PURE__ */ new Set([
|
|
77
|
-
"string",
|
|
78
|
-
"number",
|
|
79
|
-
"integer",
|
|
80
|
-
"bigint",
|
|
81
|
-
"boolean"
|
|
82
|
-
]);
|
|
83
|
-
function isScalarPrimitive(type) {
|
|
84
|
-
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Filters union members, dropping enum members that a broader scalar primitive already covers.
|
|
88
|
-
*/
|
|
89
|
-
function simplifyUnionMembers(members) {
|
|
90
|
-
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
91
|
-
if (!scalarPrimitives.size) return members;
|
|
92
|
-
return members.filter((member) => {
|
|
93
|
-
const enumNode = require_visitor.narrowSchema(member, "enum");
|
|
94
|
-
if (!enumNode) return true;
|
|
95
|
-
const primitive = enumNode.primitive;
|
|
96
|
-
if (!primitive) return true;
|
|
97
|
-
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
98
|
-
if (scalarPrimitives.has(primitive)) return false;
|
|
99
|
-
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
100
|
-
return true;
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
/**
|
|
104
|
-
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
|
|
105
|
-
* sitting next to a plain `string`. Single-value enums are kept.
|
|
106
|
-
*
|
|
107
|
-
* @example
|
|
108
|
-
* ```ts
|
|
109
|
-
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
|
|
110
|
-
* ```
|
|
111
|
-
*/
|
|
112
|
-
const macroSimplifyUnion = require_defineMacro.defineMacro({
|
|
113
|
-
name: "simplify-union",
|
|
114
|
-
schema(node) {
|
|
115
|
-
const unionNode = require_visitor.narrowSchema(node, "union");
|
|
116
|
-
if (!unionNode?.members?.length) return void 0;
|
|
117
|
-
const simplified = simplifyUnionMembers(unionNode.members);
|
|
118
|
-
if (simplified.length === unionNode.members.length) return void 0;
|
|
119
|
-
return {
|
|
120
|
-
...unionNode,
|
|
121
|
-
members: simplified
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
//#endregion
|
|
126
|
-
exports.macroDiscriminatorEnum = macroDiscriminatorEnum;
|
|
127
|
-
exports.macroEnumName = macroEnumName;
|
|
128
|
-
exports.macroSimplifyUnion = macroSimplifyUnion;
|
|
129
|
-
|
|
130
|
-
//# sourceMappingURL=macros.cjs.map
|
package/dist/macros.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"macros.cjs","names":["defineMacro","narrowSchema","createSchema","createProperty","defineMacro","narrowSchema","enumPropName","narrowSchema","defineMacro"],"sources":["../src/macros/macroDiscriminatorEnum.ts","../src/macros/macroEnumName.ts","../src/macros/macroSimplifyUnion.ts"],"sourcesContent":["import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { createProperty } from '../nodes/property.ts'\nimport { createSchema } from '../nodes/schema.ts'\n\ntype Props = {\n propertyName: string\n values: Array<string>\n enumName?: string\n}\n\n/**\n * Builds a macro that replaces a discriminator property's schema with a string enum of the given\n * values. Object schemas that lack the property are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })\n * const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroDiscriminatorEnum({ propertyName, values, enumName }: Props) {\n return defineMacro({\n name: 'discriminator-enum',\n schema(node) {\n const objectNode = narrowSchema(node, 'object')\n if (!objectNode?.properties?.length) return undefined\n if (!objectNode.properties.some((prop) => prop.name === propertyName)) return undefined\n\n return createSchema({\n ...objectNode,\n properties: objectNode.properties.map((prop) => {\n if (prop.name !== propertyName) return prop\n\n return createProperty({\n ...prop,\n schema: createSchema({\n type: 'enum',\n primitive: 'string',\n enumValues: values,\n name: enumName,\n readOnly: prop.schema.readOnly,\n writeOnly: prop.schema.writeOnly,\n }),\n })\n }),\n })\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport { enumPropName } from '../utils/refs.ts'\n\ntype Props = {\n parentName: string | null | undefined\n propName: string\n enumSuffix: string\n}\n\n/**\n * Builds a macro that names an inline enum schema from its parent and property name. Boolean enums\n * are left anonymous. Non-enum nodes are returned unchanged.\n *\n * @example\n * ```ts\n * const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })\n * const named = applyMacros(propSchema, [macro], { depth: 'shallow' })\n * ```\n */\nexport function macroEnumName({ parentName, propName, enumSuffix }: Props) {\n return defineMacro({\n name: 'enum-name',\n schema(node) {\n const enumNode = narrowSchema(node, 'enum')\n\n if (enumNode?.primitive === 'boolean') return { ...node, name: null }\n if (enumNode) return { ...node, name: enumPropName(parentName, propName, enumSuffix) }\n\n return undefined\n },\n })\n}\n","import { defineMacro } from '../defineMacro.ts'\nimport { narrowSchema } from '../guards.ts'\nimport type { SchemaNode } from '../nodes/schema.ts'\n\ntype ScalarPrimitive = 'string' | 'number' | 'integer' | 'bigint' | 'boolean'\n\n/**\n * Scalar primitive schema types used for union simplification and type narrowing.\n */\nconst SCALAR_PRIMITIVE_TYPES = new Set<ScalarPrimitive>(['string', 'number', 'integer', 'bigint', 'boolean'])\n\nfunction isScalarPrimitive(type: string): type is ScalarPrimitive {\n return SCALAR_PRIMITIVE_TYPES.has(type as ScalarPrimitive)\n}\n\n/**\n * Filters union members, dropping enum members that a broader scalar primitive already covers.\n */\nfunction simplifyUnionMembers(members: Array<SchemaNode>): Array<SchemaNode> {\n const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type))\n if (!scalarPrimitives.size) return members\n\n return members.filter((member) => {\n const enumNode = narrowSchema(member, 'enum')\n if (!enumNode) return true\n\n const primitive = enumNode.primitive\n if (!primitive) return true\n\n const enumValueCount = enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0\n if (enumValueCount <= 1) return true\n\n if (scalarPrimitives.has(primitive)) return false\n if ((primitive === 'integer' || primitive === 'number') && (scalarPrimitives.has('integer') || scalarPrimitives.has('number'))) return false\n\n return true\n })\n}\n\n/**\n * Removes union members a broader scalar primitive already covers, such as a multi-value string enum\n * sitting next to a plain `string`. Single-value enums are kept.\n *\n * @example\n * ```ts\n * const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })\n * ```\n */\nexport const macroSimplifyUnion = defineMacro({\n name: 'simplify-union',\n schema(node) {\n const unionNode = narrowSchema(node, 'union')\n if (!unionNode?.members?.length) return undefined\n\n const simplified = simplifyUnionMembers(unionNode.members)\n if (simplified.length === unionNode.members.length) return undefined\n\n return { ...unionNode, members: simplified }\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;AAqBA,SAAgB,uBAAuB,EAAE,cAAc,QAAQ,YAAmB;CAChF,OAAOA,oBAAAA,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,aAAaC,gBAAAA,aAAa,MAAM,QAAQ;GAC9C,IAAI,CAAC,YAAY,YAAY,QAAQ,OAAO,KAAA;GAC5C,IAAI,CAAC,WAAW,WAAW,MAAM,SAAS,KAAK,SAAS,YAAY,GAAG,OAAO,KAAA;GAE9E,OAAOC,gBAAAA,aAAa;IAClB,GAAG;IACH,YAAY,WAAW,WAAW,KAAK,SAAS;KAC9C,IAAI,KAAK,SAAS,cAAc,OAAO;KAEvC,OAAOC,gBAAAA,eAAe;MACpB,GAAG;MACH,QAAQD,gBAAAA,aAAa;OACnB,MAAM;OACN,WAAW;OACX,YAAY;OACZ,MAAM;OACN,UAAU,KAAK,OAAO;OACtB,WAAW,KAAK,OAAO;MACzB,CAAC;KACH,CAAC;IACH,CAAC;GACH,CAAC;EACH;CACF,CAAC;AACH;;;;;;;;;;;;;AC7BA,SAAgB,cAAc,EAAE,YAAY,UAAU,cAAqB;CACzE,OAAOE,oBAAAA,YAAY;EACjB,MAAM;EACN,OAAO,MAAM;GACX,MAAM,WAAWC,gBAAAA,aAAa,MAAM,MAAM;GAE1C,IAAI,UAAU,cAAc,WAAW,OAAO;IAAE,GAAG;IAAM,MAAM;GAAK;GACpE,IAAI,UAAU,OAAO;IAAE,GAAG;IAAM,MAAMC,aAAAA,aAAa,YAAY,UAAU,UAAU;GAAE;EAGvF;CACF,CAAC;AACH;;;;;;ACvBA,MAAM,yCAAyB,IAAI,IAAqB;CAAC;CAAU;CAAU;CAAW;CAAU;AAAS,CAAC;AAE5G,SAAS,kBAAkB,MAAuC;CAChE,OAAO,uBAAuB,IAAI,IAAuB;AAC3D;;;;AAKA,SAAS,qBAAqB,SAA+C;CAC3E,MAAM,mBAAmB,IAAI,IAAI,QAAQ,QAAQ,WAAW,kBAAkB,OAAO,IAAI,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,IAAI,CAAC;CAC9G,IAAI,CAAC,iBAAiB,MAAM,OAAO;CAEnC,OAAO,QAAQ,QAAQ,WAAW;EAChC,MAAM,WAAWC,gBAAAA,aAAa,QAAQ,MAAM;EAC5C,IAAI,CAAC,UAAU,OAAO;EAEtB,MAAM,YAAY,SAAS;EAC3B,IAAI,CAAC,WAAW,OAAO;EAGvB,KADuB,SAAS,iBAAiB,UAAU,SAAS,YAAY,UAAU,MACpE,GAAG,OAAO;EAEhC,IAAI,iBAAiB,IAAI,SAAS,GAAG,OAAO;EAC5C,KAAK,cAAc,aAAa,cAAc,cAAc,iBAAiB,IAAI,SAAS,KAAK,iBAAiB,IAAI,QAAQ,IAAI,OAAO;EAEvI,OAAO;CACT,CAAC;AACH;;;;;;;;;;AAWA,MAAa,qBAAqBC,oBAAAA,YAAY;CAC5C,MAAM;CACN,OAAO,MAAM;EACX,MAAM,YAAYD,gBAAAA,aAAa,MAAM,OAAO;EAC5C,IAAI,CAAC,WAAW,SAAS,QAAQ,OAAO,KAAA;EAExC,MAAM,aAAa,qBAAqB,UAAU,OAAO;EACzD,IAAI,WAAW,WAAW,UAAU,QAAQ,QAAQ,OAAO,KAAA;EAE3D,OAAO;GAAE,GAAG;GAAW,SAAS;EAAW;CAC7C;AACF,CAAC"}
|
package/dist/macros.d.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { n as __name } from "./rolldown-runtime-CNktS9qV.js";
|
|
2
|
-
import { n as Macro } from "./defineMacro-DzsACbFo.js";
|
|
3
|
-
|
|
4
|
-
//#region src/macros/macroDiscriminatorEnum.d.ts
|
|
5
|
-
type Props$1 = {
|
|
6
|
-
propertyName: string;
|
|
7
|
-
values: Array<string>;
|
|
8
|
-
enumName?: string;
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* Builds a macro that replaces a discriminator property's schema with a string enum of the given
|
|
12
|
-
* values. Object schemas that lack the property are returned unchanged.
|
|
13
|
-
*
|
|
14
|
-
* @example
|
|
15
|
-
* ```ts
|
|
16
|
-
* const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })
|
|
17
|
-
* const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
declare function macroDiscriminatorEnum({
|
|
21
|
-
propertyName,
|
|
22
|
-
values,
|
|
23
|
-
enumName
|
|
24
|
-
}: Props$1): Macro;
|
|
25
|
-
//#endregion
|
|
26
|
-
//#region src/macros/macroEnumName.d.ts
|
|
27
|
-
type Props = {
|
|
28
|
-
parentName: string | null | undefined;
|
|
29
|
-
propName: string;
|
|
30
|
-
enumSuffix: string;
|
|
31
|
-
};
|
|
32
|
-
/**
|
|
33
|
-
* Builds a macro that names an inline enum schema from its parent and property name. Boolean enums
|
|
34
|
-
* are left anonymous. Non-enum nodes are returned unchanged.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```ts
|
|
38
|
-
* const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })
|
|
39
|
-
* const named = applyMacros(propSchema, [macro], { depth: 'shallow' })
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare function macroEnumName({
|
|
43
|
-
parentName,
|
|
44
|
-
propName,
|
|
45
|
-
enumSuffix
|
|
46
|
-
}: Props): Macro;
|
|
47
|
-
//#endregion
|
|
48
|
-
//#region src/macros/macroSimplifyUnion.d.ts
|
|
49
|
-
/**
|
|
50
|
-
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
|
|
51
|
-
* sitting next to a plain `string`. Single-value enums are kept.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```ts
|
|
55
|
-
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
declare const macroSimplifyUnion: Macro;
|
|
59
|
-
//#endregion
|
|
60
|
-
export { macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion };
|
|
61
|
-
//# sourceMappingURL=macros.d.ts.map
|