@kubb/ast 5.0.0-beta.63 → 5.0.0-beta.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{defineMacro-Shz8f6SG.js → defineMacro-B76LsJwO.js} +4 -4
- package/dist/{defineMacro-Shz8f6SG.js.map → defineMacro-B76LsJwO.js.map} +1 -1
- package/dist/{defineMacro-BATi7xoC.d.ts → defineMacro-BQpu6Ags.d.ts} +3 -3
- package/dist/factory-B_qPwA1b.d.ts +27 -0
- package/dist/factory.d.ts +3 -27
- package/dist/factory.js +2 -2
- package/dist/{index-B9cc8MBS.d.ts → index-CeJAFegf.d.ts} +4 -4
- package/dist/index.cjs +55 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +32 -29
- package/dist/index.js +55 -9
- package/dist/index.js.map +1 -1
- package/dist/macros.d.ts +2 -2
- package/dist/macros.js +4 -4
- package/dist/{operationParams-k5CKwSWZ.d.ts → operationParams-DCY3q4C7.d.ts} +3 -3
- package/dist/{refs-BjNDuCBD.js → refs-Dx6U5LoE.js} +3 -3
- package/dist/{refs-BjNDuCBD.js.map → refs-Dx6U5LoE.js.map} +1 -1
- package/dist/schema-BkvrrOAr.cjs.map +1 -1
- package/dist/{schema-Cbnxmz4b.js → schema-YNbOtTCM.js} +2 -2
- package/dist/{schema-Cbnxmz4b.js.map → schema-YNbOtTCM.js.map} +1 -1
- package/dist/{types-BB_xgRJ3.d.ts → types-B6thixAv.d.ts} +8 -8
- package/dist/types.d.ts +5 -5
- package/dist/{utils-DaXkewb1.js → utils-BJi0y-xg.js} +4 -4
- package/dist/utils-BJi0y-xg.js.map +1 -0
- package/dist/utils-CEepwqmb.cjs.map +1 -1
- package/dist/utils.d.ts +3 -3
- package/dist/utils.js +3 -3
- package/package.json +1 -2
- package/dist/utils-DaXkewb1.js.map +0 -1
- package/src/constants.ts +0 -150
- package/src/defineMacro.ts +0 -139
- package/src/defineNode.ts +0 -102
- package/src/definePrinter.ts +0 -258
- package/src/dialect.ts +0 -86
- package/src/factory.ts +0 -44
- package/src/guards.ts +0 -28
- package/src/index.ts +0 -13
- package/src/infer.ts +0 -132
- package/src/macros/index.ts +0 -3
- package/src/macros/macroDiscriminatorEnum.ts +0 -50
- package/src/macros/macroEnumName.ts +0 -33
- package/src/macros/macroSimplifyUnion.ts +0 -60
- package/src/nodes/base.ts +0 -49
- package/src/nodes/code.ts +0 -355
- package/src/nodes/content.ts +0 -51
- package/src/nodes/file.ts +0 -380
- package/src/nodes/function.ts +0 -283
- package/src/nodes/index.ts +0 -93
- package/src/nodes/input.ts +0 -144
- package/src/nodes/operation.ts +0 -168
- package/src/nodes/output.ts +0 -49
- package/src/nodes/parameter.ts +0 -71
- package/src/nodes/property.ts +0 -67
- package/src/nodes/requestBody.ts +0 -54
- package/src/nodes/response.ts +0 -173
- package/src/nodes/schema.ts +0 -732
- package/src/optionality.ts +0 -15
- package/src/registry.ts +0 -75
- package/src/signature.ts +0 -207
- package/src/types.ts +0 -8
- package/src/utils/codegen.ts +0 -103
- package/src/utils/extractStringsFromNodes.ts +0 -35
- package/src/utils/fileMerge.ts +0 -183
- package/src/utils/index.ts +0 -11
- package/src/utils/operationParams.ts +0 -353
- package/src/utils/refs.ts +0 -134
- package/src/utils/schemaGraph.ts +0 -177
- package/src/utils/schemaMerge.ts +0 -34
- package/src/utils/schemaTraversal.ts +0 -86
- package/src/utils/strings.ts +0 -139
- package/src/visitor.ts +0 -519
- /package/dist/{chunk-CNktS9qV.js → rolldown-runtime-CNktS9qV.js} +0 -0
package/src/visitor.ts
DELETED
|
@@ -1,519 +0,0 @@
|
|
|
1
|
-
import type { VisitorDepth } from './constants.ts'
|
|
2
|
-
import { visitorDepths, WALK_CONCURRENCY } from './constants.ts'
|
|
3
|
-
import type { NodeDef } from './defineNode.ts'
|
|
4
|
-
import type {
|
|
5
|
-
ContentNode,
|
|
6
|
-
InputNode,
|
|
7
|
-
Node,
|
|
8
|
-
NodeKind,
|
|
9
|
-
OperationNode,
|
|
10
|
-
OutputNode,
|
|
11
|
-
ParameterNode,
|
|
12
|
-
PropertyNode,
|
|
13
|
-
RequestBodyNode,
|
|
14
|
-
ResponseNode,
|
|
15
|
-
SchemaNode,
|
|
16
|
-
} from './nodes/index.ts'
|
|
17
|
-
import { nodeDefs } from './registry.ts'
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
21
|
-
* Derived from each definition's `children`.
|
|
22
|
-
*/
|
|
23
|
-
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => (def.children ? [[def.kind, def.children] as const] : []))) as Partial<
|
|
24
|
-
Record<NodeKind, ReadonlyArray<string>>
|
|
25
|
-
>
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
29
|
-
* definition's `visitorKey`.
|
|
30
|
-
*/
|
|
31
|
-
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => (def.visitorKey ? [[def.kind, def.visitorKey] as const] : []))) as Partial<
|
|
32
|
-
Record<NodeKind, NonNullable<NodeDef['visitorKey']>>
|
|
33
|
-
>
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Creates a small async concurrency limiter.
|
|
37
|
-
*
|
|
38
|
-
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
39
|
-
*
|
|
40
|
-
* @example
|
|
41
|
-
* ```ts
|
|
42
|
-
* const limit = createLimit(2)
|
|
43
|
-
* for (const task of [taskA, taskB, taskC]) {
|
|
44
|
-
* await limit(() => task())
|
|
45
|
-
* }
|
|
46
|
-
* // only 2 tasks run at the same time
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
function createLimit(concurrency: number) {
|
|
50
|
-
let active = 0
|
|
51
|
-
const queue: Array<() => void> = []
|
|
52
|
-
|
|
53
|
-
function next() {
|
|
54
|
-
if (active < concurrency && queue.length > 0) {
|
|
55
|
-
active++
|
|
56
|
-
queue.shift()!()
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
return function limit<T>(fn: () => Promise<T> | T): Promise<T> {
|
|
61
|
-
return new Promise<T>((resolve, reject) => {
|
|
62
|
-
queue.push(() => {
|
|
63
|
-
Promise.resolve(fn())
|
|
64
|
-
.then(resolve, reject)
|
|
65
|
-
.finally(() => {
|
|
66
|
-
active--
|
|
67
|
-
next()
|
|
68
|
-
})
|
|
69
|
-
})
|
|
70
|
-
next()
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
type LimitFn = ReturnType<typeof createLimit>
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
79
|
-
*
|
|
80
|
-
* `ParentOf` uses this map to find parent types.
|
|
81
|
-
*/
|
|
82
|
-
type ParentNodeMap = [
|
|
83
|
-
[InputNode, undefined],
|
|
84
|
-
[OutputNode, undefined],
|
|
85
|
-
[OperationNode, InputNode],
|
|
86
|
-
[RequestBodyNode, OperationNode],
|
|
87
|
-
[ContentNode, RequestBodyNode | ResponseNode],
|
|
88
|
-
[SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode],
|
|
89
|
-
[PropertyNode, SchemaNode],
|
|
90
|
-
[ParameterNode, OperationNode],
|
|
91
|
-
[ResponseNode, OperationNode],
|
|
92
|
-
]
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Resolves the parent node type for a given AST node type.
|
|
96
|
-
*
|
|
97
|
-
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
98
|
-
*
|
|
99
|
-
* @example
|
|
100
|
-
* ```ts
|
|
101
|
-
* type InputParent = ParentOf<InputNode>
|
|
102
|
-
* // undefined
|
|
103
|
-
* ```
|
|
104
|
-
*
|
|
105
|
-
* @example
|
|
106
|
-
* ```ts
|
|
107
|
-
* type PropertyParent = ParentOf<PropertyNode>
|
|
108
|
-
* // SchemaNode
|
|
109
|
-
* ```
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```ts
|
|
113
|
-
* type SchemaParent = ParentOf<SchemaNode>
|
|
114
|
-
* // InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode
|
|
115
|
-
* ```
|
|
116
|
-
*/
|
|
117
|
-
export type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [
|
|
118
|
-
infer TEntry extends [Node, unknown],
|
|
119
|
-
...infer TRest extends ReadonlyArray<[Node, unknown]>,
|
|
120
|
-
]
|
|
121
|
-
? T extends TEntry[0]
|
|
122
|
-
? TEntry[1]
|
|
123
|
-
: ParentOf<T, TRest>
|
|
124
|
-
: Node
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Traversal context passed as the second argument to every visitor callback.
|
|
128
|
-
* `parent` is typed from the current node type.
|
|
129
|
-
*
|
|
130
|
-
* @example
|
|
131
|
-
* ```ts
|
|
132
|
-
* const visitor: Visitor = {
|
|
133
|
-
* schema(node, { parent }) {
|
|
134
|
-
* // parent type is narrowed by node kind
|
|
135
|
-
* },
|
|
136
|
-
* }
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
|
-
export type VisitorContext<T extends Node = Node> = {
|
|
140
|
-
/**
|
|
141
|
-
* Parent node of the currently visited node.
|
|
142
|
-
* For `InputNode`, this is `undefined`.
|
|
143
|
-
*/
|
|
144
|
-
parent?: ParentOf<T>
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
149
|
-
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
150
|
-
* to leave it untouched.
|
|
151
|
-
*
|
|
152
|
-
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
153
|
-
* rewrites the AST before printing.
|
|
154
|
-
*
|
|
155
|
-
* @example Prefix every operationId
|
|
156
|
-
* ```ts
|
|
157
|
-
* const visitor: Visitor = {
|
|
158
|
-
* operation(node) {
|
|
159
|
-
* return { ...node, operationId: `api_${node.operationId}` }
|
|
160
|
-
* },
|
|
161
|
-
* }
|
|
162
|
-
* ```
|
|
163
|
-
*
|
|
164
|
-
* @example Strip schema descriptions
|
|
165
|
-
* ```ts
|
|
166
|
-
* const visitor: Visitor = {
|
|
167
|
-
* schema(node) {
|
|
168
|
-
* return { ...node, description: undefined }
|
|
169
|
-
* },
|
|
170
|
-
* }
|
|
171
|
-
* ```
|
|
172
|
-
*/
|
|
173
|
-
export type Visitor = {
|
|
174
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode
|
|
175
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode
|
|
176
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode
|
|
177
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode
|
|
178
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode
|
|
179
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode
|
|
180
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* A visitor callback result that may be sync or async.
|
|
185
|
-
*/
|
|
186
|
-
type MaybePromise<T> = T | Promise<T>
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
190
|
-
*
|
|
191
|
-
* @example
|
|
192
|
-
* ```ts
|
|
193
|
-
* const visitor: AsyncVisitor = {
|
|
194
|
-
* async operation(node) {
|
|
195
|
-
* await Promise.resolve(node.operationId)
|
|
196
|
-
* },
|
|
197
|
-
* }
|
|
198
|
-
* ```
|
|
199
|
-
*/
|
|
200
|
-
type AsyncVisitor = {
|
|
201
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>
|
|
202
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>
|
|
203
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>
|
|
204
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>
|
|
205
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>
|
|
206
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>
|
|
207
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
/**
|
|
211
|
-
* Visitor used by `collect`.
|
|
212
|
-
*
|
|
213
|
-
* @example
|
|
214
|
-
* ```ts
|
|
215
|
-
* const visitor: CollectVisitor<string> = {
|
|
216
|
-
* operation(node) {
|
|
217
|
-
* return node.operationId
|
|
218
|
-
* },
|
|
219
|
-
* }
|
|
220
|
-
* ```
|
|
221
|
-
*/
|
|
222
|
-
type CollectVisitor<T> = {
|
|
223
|
-
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined
|
|
224
|
-
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined
|
|
225
|
-
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined
|
|
226
|
-
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined
|
|
227
|
-
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined
|
|
228
|
-
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined
|
|
229
|
-
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Options for `transform`.
|
|
234
|
-
*
|
|
235
|
-
* @example
|
|
236
|
-
* ```ts
|
|
237
|
-
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
238
|
-
* ```
|
|
239
|
-
*
|
|
240
|
-
* @example
|
|
241
|
-
* ```ts
|
|
242
|
-
* // Only transform the current node, not nested children
|
|
243
|
-
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
export type TransformOptions = Visitor & {
|
|
247
|
-
/**
|
|
248
|
-
* Traversal depth.
|
|
249
|
-
* @default 'deep'
|
|
250
|
-
*/
|
|
251
|
-
depth?: VisitorDepth
|
|
252
|
-
/**
|
|
253
|
-
* Internal parent override used during recursion.
|
|
254
|
-
*/
|
|
255
|
-
parent?: Node
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Options for `walk`.
|
|
260
|
-
*
|
|
261
|
-
* @example
|
|
262
|
-
* ```ts
|
|
263
|
-
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
264
|
-
* ```
|
|
265
|
-
*/
|
|
266
|
-
export type WalkOptions = AsyncVisitor & {
|
|
267
|
-
/**
|
|
268
|
-
* Traversal depth.
|
|
269
|
-
* @default 'deep'
|
|
270
|
-
*/
|
|
271
|
-
depth?: VisitorDepth
|
|
272
|
-
/**
|
|
273
|
-
* Maximum number of sibling nodes visited concurrently.
|
|
274
|
-
* @default 30
|
|
275
|
-
*/
|
|
276
|
-
concurrency?: number
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* Options for `collect`.
|
|
281
|
-
*
|
|
282
|
-
* @example
|
|
283
|
-
* ```ts
|
|
284
|
-
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
285
|
-
* ```
|
|
286
|
-
*/
|
|
287
|
-
export type CollectOptions<T> = CollectVisitor<T> & {
|
|
288
|
-
/**
|
|
289
|
-
* Traversal depth.
|
|
290
|
-
* @default 'deep'
|
|
291
|
-
*/
|
|
292
|
-
depth?: VisitorDepth
|
|
293
|
-
/**
|
|
294
|
-
* Internal parent override used during recursion.
|
|
295
|
-
*/
|
|
296
|
-
parent?: Node
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
const visitorKeysByKind = VISITOR_KEYS as Record<string, ReadonlyArray<string> | undefined>
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
303
|
-
*/
|
|
304
|
-
function isNode(value: unknown): value is Node {
|
|
305
|
-
return typeof value === 'object' && value !== null && 'kind' in value
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
310
|
-
*
|
|
311
|
-
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
312
|
-
*
|
|
313
|
-
* @example
|
|
314
|
-
* ```ts
|
|
315
|
-
* const children = getChildren(operationNode, true)
|
|
316
|
-
* // returns parameters, the request body, and responses
|
|
317
|
-
* ```
|
|
318
|
-
*/
|
|
319
|
-
function* getChildren(node: Node, recurse: boolean): Generator<Node, void, undefined> {
|
|
320
|
-
if (node.kind === 'Schema' && !recurse) return
|
|
321
|
-
|
|
322
|
-
const keys = visitorKeysByKind[node.kind]
|
|
323
|
-
if (!keys) return
|
|
324
|
-
|
|
325
|
-
const record = node as unknown as Record<string, unknown>
|
|
326
|
-
for (const key of keys) {
|
|
327
|
-
const value = record[key]
|
|
328
|
-
if (Array.isArray(value)) {
|
|
329
|
-
for (const item of value) if (isNode(item)) yield item
|
|
330
|
-
} else if (isNode(value)) {
|
|
331
|
-
yield value
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
338
|
-
* context. The result is a replacement node, a collected value, or `undefined`
|
|
339
|
-
* when no callback is registered for the kind.
|
|
340
|
-
*
|
|
341
|
-
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
|
|
342
|
-
* in one place. `TResult` is the caller's expected return: the same node type
|
|
343
|
-
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
|
|
344
|
-
*/
|
|
345
|
-
function applyVisitor<TResult>(node: Node, visitor: Visitor | AsyncVisitor | CollectVisitor<unknown>, parent: Node | undefined): TResult | null | undefined {
|
|
346
|
-
const key = VISITOR_KEY_BY_KIND[node.kind]
|
|
347
|
-
if (!key) return undefined
|
|
348
|
-
|
|
349
|
-
const fn = visitor[key] as ((node: Node, context: VisitorContext) => TResult | null | undefined) | undefined
|
|
350
|
-
|
|
351
|
-
return fn?.(node, { parent })
|
|
352
|
-
}
|
|
353
|
-
|
|
354
|
-
/**
|
|
355
|
-
* Async depth-first traversal for side effects. Visitor return values are
|
|
356
|
-
* ignored. Use `transform` when you want to rewrite nodes.
|
|
357
|
-
*
|
|
358
|
-
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
359
|
-
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
360
|
-
* work. Lower values reduce memory pressure.
|
|
361
|
-
*
|
|
362
|
-
* @example Log every operation
|
|
363
|
-
* ```ts
|
|
364
|
-
* await walk(root, {
|
|
365
|
-
* operation(node) {
|
|
366
|
-
* console.log(node.operationId)
|
|
367
|
-
* },
|
|
368
|
-
* })
|
|
369
|
-
* ```
|
|
370
|
-
*
|
|
371
|
-
* @example Only visit the root node
|
|
372
|
-
* ```ts
|
|
373
|
-
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
374
|
-
* ```
|
|
375
|
-
*/
|
|
376
|
-
export async function walk(node: Node, options: WalkOptions): Promise<void> {
|
|
377
|
-
const recurse = (options.depth ?? visitorDepths.deep) === visitorDepths.deep
|
|
378
|
-
const limit = createLimit(options.concurrency ?? WALK_CONCURRENCY)
|
|
379
|
-
|
|
380
|
-
return _walk(node, options, recurse, limit, undefined)
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
async function _walk(node: Node, visitor: AsyncVisitor, recurse: boolean, limit: LimitFn, parent: Node | undefined): Promise<void> {
|
|
384
|
-
await limit(() => applyVisitor(node, visitor, parent))
|
|
385
|
-
|
|
386
|
-
// Visit siblings concurrently and let the shared `limit` cap how many callbacks
|
|
387
|
-
// run at once. Awaiting each child sequentially here would serialize the whole
|
|
388
|
-
// traversal and make `concurrency` inert. Every visitor callback would run one
|
|
389
|
-
// at a time regardless of the limit.
|
|
390
|
-
const children = Array.from(getChildren(node, recurse))
|
|
391
|
-
if (children.length === 0) return
|
|
392
|
-
|
|
393
|
-
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)))
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* Synchronous depth-first transform. Each visitor callback can return a
|
|
398
|
-
* replacement node. Returning `undefined` keeps the original.
|
|
399
|
-
*
|
|
400
|
-
* The original tree is never mutated, a new tree is returned. Pass
|
|
401
|
-
* `depth: 'shallow'` to skip recursion into children.
|
|
402
|
-
*
|
|
403
|
-
* @example Prefix every operationId
|
|
404
|
-
* ```ts
|
|
405
|
-
* const next = transform(root, {
|
|
406
|
-
* operation(node) {
|
|
407
|
-
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
408
|
-
* },
|
|
409
|
-
* })
|
|
410
|
-
* ```
|
|
411
|
-
*
|
|
412
|
-
* @example Replace only the root node
|
|
413
|
-
* ```ts
|
|
414
|
-
* const next = transform(root, {
|
|
415
|
-
* depth: 'shallow',
|
|
416
|
-
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
417
|
-
* })
|
|
418
|
-
* ```
|
|
419
|
-
*/
|
|
420
|
-
export function transform(node: InputNode, options: TransformOptions): InputNode
|
|
421
|
-
export function transform(node: OutputNode, options: TransformOptions): OutputNode
|
|
422
|
-
export function transform(node: OperationNode, options: TransformOptions): OperationNode
|
|
423
|
-
export function transform(node: SchemaNode, options: TransformOptions): SchemaNode
|
|
424
|
-
export function transform(node: PropertyNode, options: TransformOptions): PropertyNode
|
|
425
|
-
export function transform(node: ParameterNode, options: TransformOptions): ParameterNode
|
|
426
|
-
export function transform(node: ResponseNode, options: TransformOptions): ResponseNode
|
|
427
|
-
export function transform(node: Node, options: TransformOptions): Node
|
|
428
|
-
export function transform(node: Node, options: TransformOptions): Node {
|
|
429
|
-
const { depth, parent, ...visitor } = options
|
|
430
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep
|
|
431
|
-
|
|
432
|
-
const visited = applyVisitor<Node>(node, visitor, parent) ?? node
|
|
433
|
-
const rebuilt = transformChildren(visited, options, recurse)
|
|
434
|
-
|
|
435
|
-
// Structural sharing: when the visitor and child rebuild both left this node
|
|
436
|
-
// untouched, return the original reference so callers can detect "nothing
|
|
437
|
-
// changed" by identity and ancestors can avoid reallocating.
|
|
438
|
-
return rebuilt
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
443
|
-
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
444
|
-
* `Schema` children are skipped in shallow mode.
|
|
445
|
-
*/
|
|
446
|
-
function transformChildren(node: Node, options: TransformOptions, recurse: boolean): Node {
|
|
447
|
-
if (node.kind === 'Schema' && !recurse) return node
|
|
448
|
-
|
|
449
|
-
const keys = visitorKeysByKind[node.kind]
|
|
450
|
-
if (!keys) return node
|
|
451
|
-
|
|
452
|
-
const record = node as unknown as Record<string, unknown>
|
|
453
|
-
const childOptions = { ...options, parent: node }
|
|
454
|
-
let updates: Record<string, unknown> | undefined
|
|
455
|
-
|
|
456
|
-
for (const key of keys) {
|
|
457
|
-
if (!(key in record)) continue
|
|
458
|
-
const value = record[key]
|
|
459
|
-
if (Array.isArray(value)) {
|
|
460
|
-
let changed = false
|
|
461
|
-
const mapped = value.map((item) => {
|
|
462
|
-
if (!isNode(item)) return item
|
|
463
|
-
const next = transform(item, childOptions)
|
|
464
|
-
if (next !== item) changed = true
|
|
465
|
-
return next
|
|
466
|
-
})
|
|
467
|
-
if (changed) (updates ??= {})[key] = mapped
|
|
468
|
-
} else if (isNode(value)) {
|
|
469
|
-
const next = transform(value, childOptions)
|
|
470
|
-
if (next !== value) (updates ??= {})[key] = next
|
|
471
|
-
}
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return updates ? ({ ...node, ...updates } as Node) : node
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
478
|
-
* the visitor callbacks. Use `collect` for the eager array form.
|
|
479
|
-
*
|
|
480
|
-
* @example Collect every operationId
|
|
481
|
-
* ```ts
|
|
482
|
-
* const ids: string[] = []
|
|
483
|
-
* for (const id of collectLazy<string>(root, {
|
|
484
|
-
* operation(node) {
|
|
485
|
-
* return node.operationId
|
|
486
|
-
* },
|
|
487
|
-
* })) {
|
|
488
|
-
* ids.push(id)
|
|
489
|
-
* }
|
|
490
|
-
* ```
|
|
491
|
-
*/
|
|
492
|
-
export function* collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined> {
|
|
493
|
-
const { depth, parent, ...visitor } = options
|
|
494
|
-
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep
|
|
495
|
-
|
|
496
|
-
const v = applyVisitor<T>(node, visitor, parent)
|
|
497
|
-
if (v != null) yield v
|
|
498
|
-
|
|
499
|
-
for (const child of getChildren(node, recurse)) {
|
|
500
|
-
yield* collectLazy(child, { ...options, parent: node })
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
/**
|
|
505
|
-
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
506
|
-
* callbacks return into an array.
|
|
507
|
-
*
|
|
508
|
-
* @example Collect every operationId
|
|
509
|
-
* ```ts
|
|
510
|
-
* const ids = collect<string>(root, {
|
|
511
|
-
* operation(node) {
|
|
512
|
-
* return node.operationId
|
|
513
|
-
* },
|
|
514
|
-
* })
|
|
515
|
-
* ```
|
|
516
|
-
*/
|
|
517
|
-
export function collect<T>(node: Node, options: CollectOptions<T>): Array<T> {
|
|
518
|
-
return Array.from(collectLazy(node, options))
|
|
519
|
-
}
|
|
File without changes
|