@kubb/ast 5.0.0-beta.6 → 5.0.0-beta.61
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 +51 -27
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/defineMacro-BLIR6k-j.d.ts +475 -0
- package/dist/defineMacro-BTXvS8nI.js +106 -0
- package/dist/defineMacro-BTXvS8nI.js.map +1 -0
- package/dist/defineMacro-Bv9R_9a2.cjs +123 -0
- package/dist/defineMacro-Bv9R_9a2.cjs.map +1 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js +29 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js.map +1 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs +34 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs.map +1 -0
- package/dist/extractStringsFromNodes-p4mX1TQD.d.ts +14 -0
- package/dist/factory-CZNOGI-N.js +283 -0
- package/dist/factory-CZNOGI-N.js.map +1 -0
- package/dist/factory-DG1CVkEb.cjs +300 -0
- package/dist/factory-DG1CVkEb.cjs.map +1 -0
- package/dist/factory.cjs +29 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index-BzjwdK2M.d.ts +2433 -0
- package/dist/index.cjs +444 -2180
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -3408
- package/dist/index.js +395 -2101
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +117 -0
- package/dist/macros.cjs.map +1 -0
- package/dist/macros.d.ts +59 -0
- package/dist/macros.js +115 -0
- package/dist/macros.js.map +1 -0
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-KUdWiDWw.js +658 -0
- package/dist/response-KUdWiDWw.js.map +1 -0
- package/dist/response-hnSw2NKE.cjs +1027 -0
- package/dist/response-hnSw2NKE.cjs.map +1 -0
- package/dist/types-DyDzizSf.d.ts +364 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/dist/utils-BLJwyza-.cjs +912 -0
- package/dist/utils-BLJwyza-.cjs.map +1 -0
- package/dist/utils-CF_-Pn_c.js +770 -0
- package/dist/utils-CF_-Pn_c.js.map +1 -0
- package/dist/utils.cjs +36 -0
- package/dist/utils.d.ts +358 -0
- package/dist/utils.js +4 -0
- package/dist/visitor-DJ6ZEJvq.js +548 -0
- package/dist/visitor-DJ6ZEJvq.js.map +1 -0
- package/dist/visitor-DpKZ9Tk0.cjs +654 -0
- package/dist/visitor-DpKZ9Tk0.cjs.map +1 -0
- package/package.json +21 -6
- package/src/constants.ts +19 -64
- package/src/dedupe.ts +239 -0
- package/src/defineMacro.ts +132 -0
- package/src/dialect.ts +53 -0
- package/src/factory.ts +67 -678
- package/src/guards.ts +10 -92
- package/src/index.ts +13 -44
- package/src/infer.ts +16 -14
- package/src/macros/index.ts +3 -0
- package/src/macros/macroDiscriminatorEnum.ts +44 -0
- package/src/macros/macroEnumName.ts +25 -0
- package/src/macros/macroSimplifyUnion.ts +50 -0
- package/src/mocks.ts +7 -127
- package/src/node.ts +128 -0
- package/src/nodes/base.ts +5 -12
- package/src/nodes/code.ts +165 -74
- package/src/nodes/content.ts +56 -0
- package/src/nodes/file.ts +97 -36
- package/src/nodes/function.ts +216 -156
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +23 -15
- package/src/nodes/input.ts +140 -0
- package/src/nodes/operation.ts +122 -68
- package/src/nodes/output.ts +23 -0
- package/src/nodes/parameter.ts +33 -3
- package/src/nodes/property.ts +36 -3
- package/src/nodes/requestBody.ts +61 -0
- package/src/nodes/response.ts +58 -13
- package/src/nodes/schema.ts +93 -17
- package/src/printer.ts +48 -42
- package/src/registry.ts +75 -0
- package/src/signature.ts +207 -0
- package/src/types.ts +8 -68
- package/src/utils/codegen.ts +104 -0
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +138 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/schemaMerge.ts +34 -0
- package/src/utils/schemaTraversal.ts +86 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +227 -289
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/nodes/root.ts +0 -64
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/utils.ts +0 -915
|
@@ -0,0 +1,654 @@
|
|
|
1
|
+
const require_response = require("./response-hnSw2NKE.cjs");
|
|
2
|
+
//#region src/constants.ts
|
|
3
|
+
const visitorDepths = {
|
|
4
|
+
shallow: "shallow",
|
|
5
|
+
deep: "deep"
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
9
|
+
*
|
|
10
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
11
|
+
* Call `isScalarPrimitive()` to check for the scalar types.
|
|
12
|
+
*/
|
|
13
|
+
const schemaTypes = {
|
|
14
|
+
/**
|
|
15
|
+
* Text value.
|
|
16
|
+
*/
|
|
17
|
+
string: "string",
|
|
18
|
+
/**
|
|
19
|
+
* Floating-point number (`float`, `double`).
|
|
20
|
+
*/
|
|
21
|
+
number: "number",
|
|
22
|
+
/**
|
|
23
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
24
|
+
*/
|
|
25
|
+
integer: "integer",
|
|
26
|
+
/**
|
|
27
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
28
|
+
*/
|
|
29
|
+
bigint: "bigint",
|
|
30
|
+
/**
|
|
31
|
+
* Boolean value.
|
|
32
|
+
*/
|
|
33
|
+
boolean: "boolean",
|
|
34
|
+
/**
|
|
35
|
+
* Explicit null value.
|
|
36
|
+
*/
|
|
37
|
+
null: "null",
|
|
38
|
+
/**
|
|
39
|
+
* Any value (no type restriction).
|
|
40
|
+
*/
|
|
41
|
+
any: "any",
|
|
42
|
+
/**
|
|
43
|
+
* Unknown value (must be narrowed before usage).
|
|
44
|
+
*/
|
|
45
|
+
unknown: "unknown",
|
|
46
|
+
/**
|
|
47
|
+
* No return value (`void`).
|
|
48
|
+
*/
|
|
49
|
+
void: "void",
|
|
50
|
+
/**
|
|
51
|
+
* Object with named properties.
|
|
52
|
+
*/
|
|
53
|
+
object: "object",
|
|
54
|
+
/**
|
|
55
|
+
* Sequential list of items.
|
|
56
|
+
*/
|
|
57
|
+
array: "array",
|
|
58
|
+
/**
|
|
59
|
+
* Fixed-length list with position-specific items.
|
|
60
|
+
*/
|
|
61
|
+
tuple: "tuple",
|
|
62
|
+
/**
|
|
63
|
+
* "One of" multiple schema members.
|
|
64
|
+
*/
|
|
65
|
+
union: "union",
|
|
66
|
+
/**
|
|
67
|
+
* "All of" multiple schema members.
|
|
68
|
+
*/
|
|
69
|
+
intersection: "intersection",
|
|
70
|
+
/**
|
|
71
|
+
* Enum schema.
|
|
72
|
+
*/
|
|
73
|
+
enum: "enum",
|
|
74
|
+
/**
|
|
75
|
+
* Reference to another schema.
|
|
76
|
+
*/
|
|
77
|
+
ref: "ref",
|
|
78
|
+
/**
|
|
79
|
+
* Calendar date (for example `2026-03-24`).
|
|
80
|
+
*/
|
|
81
|
+
date: "date",
|
|
82
|
+
/**
|
|
83
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
84
|
+
*/
|
|
85
|
+
datetime: "datetime",
|
|
86
|
+
/**
|
|
87
|
+
* Time-only value (for example `09:00:00`).
|
|
88
|
+
*/
|
|
89
|
+
time: "time",
|
|
90
|
+
/**
|
|
91
|
+
* UUID value.
|
|
92
|
+
*/
|
|
93
|
+
uuid: "uuid",
|
|
94
|
+
/**
|
|
95
|
+
* Email address value.
|
|
96
|
+
*/
|
|
97
|
+
email: "email",
|
|
98
|
+
/**
|
|
99
|
+
* URL value.
|
|
100
|
+
*/
|
|
101
|
+
url: "url",
|
|
102
|
+
/**
|
|
103
|
+
* IPv4 address value.
|
|
104
|
+
*/
|
|
105
|
+
ipv4: "ipv4",
|
|
106
|
+
/**
|
|
107
|
+
* IPv6 address value.
|
|
108
|
+
*/
|
|
109
|
+
ipv6: "ipv6",
|
|
110
|
+
/**
|
|
111
|
+
* Binary/blob value.
|
|
112
|
+
*/
|
|
113
|
+
blob: "blob",
|
|
114
|
+
/**
|
|
115
|
+
* Impossible value (`never`).
|
|
116
|
+
*/
|
|
117
|
+
never: "never"
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
121
|
+
*/
|
|
122
|
+
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
123
|
+
"string",
|
|
124
|
+
"number",
|
|
125
|
+
"integer",
|
|
126
|
+
"bigint",
|
|
127
|
+
"boolean"
|
|
128
|
+
]);
|
|
129
|
+
/**
|
|
130
|
+
* Returns `true` when `type` is a scalar primitive that can be assigned without wrapping
|
|
131
|
+
* (for example `string | number | boolean`).
|
|
132
|
+
*/
|
|
133
|
+
function isScalarPrimitive(type) {
|
|
134
|
+
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* HTTP method identifiers used by operation nodes.
|
|
138
|
+
*/
|
|
139
|
+
const httpMethods = {
|
|
140
|
+
get: "GET",
|
|
141
|
+
post: "POST",
|
|
142
|
+
put: "PUT",
|
|
143
|
+
patch: "PATCH",
|
|
144
|
+
delete: "DELETE",
|
|
145
|
+
head: "HEAD",
|
|
146
|
+
options: "OPTIONS",
|
|
147
|
+
trace: "TRACE"
|
|
148
|
+
};
|
|
149
|
+
/**
|
|
150
|
+
* One indentation level, derived from {@link INDENT_SIZE}.
|
|
151
|
+
*/
|
|
152
|
+
const INDENT = Array.from({ length: 2 }, () => " ").join("");
|
|
153
|
+
//#endregion
|
|
154
|
+
//#region src/guards.ts
|
|
155
|
+
/**
|
|
156
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* const schema = createSchema({ type: 'string' })
|
|
161
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
162
|
+
* ```
|
|
163
|
+
*/
|
|
164
|
+
function narrowSchema(node, type) {
|
|
165
|
+
return node?.type === type ? node : null;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* ```ts
|
|
172
|
+
* if (isHttpOperationNode(node)) {
|
|
173
|
+
* console.log(node.method, node.path)
|
|
174
|
+
* }
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function isHttpOperationNode(node) {
|
|
178
|
+
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
179
|
+
}
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/utils/refs.ts
|
|
182
|
+
const plainStringTypes = new Set([
|
|
183
|
+
"string",
|
|
184
|
+
"uuid",
|
|
185
|
+
"email",
|
|
186
|
+
"url",
|
|
187
|
+
"datetime"
|
|
188
|
+
]);
|
|
189
|
+
/**
|
|
190
|
+
* Returns the last path segment of a reference string.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
function extractRefName(ref) {
|
|
198
|
+
return ref.split("/").at(-1) ?? ref;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
|
|
202
|
+
*
|
|
203
|
+
* Returns `null` for non-ref nodes or when no name resolves.
|
|
204
|
+
*
|
|
205
|
+
* @example
|
|
206
|
+
* ```ts
|
|
207
|
+
* resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' })
|
|
208
|
+
* // => 'Pet'
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
function resolveRefName(node) {
|
|
212
|
+
if (!node || node.type !== "ref") return null;
|
|
213
|
+
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
|
|
214
|
+
return node.name ?? node.schema?.name ?? null;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Builds a PascalCase child schema name by joining a parent name and property name.
|
|
218
|
+
* Returns `null` when there is no parent to nest under.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* ```ts
|
|
222
|
+
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
|
|
223
|
+
* childName(undefined, 'params') // null
|
|
224
|
+
* ```
|
|
225
|
+
*/
|
|
226
|
+
function childName(parentName, propName) {
|
|
227
|
+
return parentName ? require_response.pascalCase([parentName, propName].join(" ")) : null;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
|
|
231
|
+
* empty parts.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
|
|
236
|
+
* ```
|
|
237
|
+
*/
|
|
238
|
+
function enumPropName(parentName, propName, enumSuffix) {
|
|
239
|
+
return require_response.pascalCase([
|
|
240
|
+
parentName,
|
|
241
|
+
propName,
|
|
242
|
+
enumSuffix
|
|
243
|
+
].filter(Boolean).join(" "));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
247
|
+
*
|
|
248
|
+
* Usage-site fields (`description`, `readOnly`, `nullable`, `deprecated`) on the ref node override
|
|
249
|
+
* the same fields in the resolved `node.schema`. Non-ref nodes are returned unchanged.
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* ```ts
|
|
253
|
+
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
254
|
+
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
255
|
+
* ```
|
|
256
|
+
*/
|
|
257
|
+
function syncSchemaRef(node) {
|
|
258
|
+
const ref = narrowSchema(node, "ref");
|
|
259
|
+
if (!ref) return node;
|
|
260
|
+
if (!ref.schema) return node;
|
|
261
|
+
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
262
|
+
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
263
|
+
return require_response.createSchema({
|
|
264
|
+
...ref.schema,
|
|
265
|
+
...definedOverrides
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
270
|
+
*
|
|
271
|
+
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
272
|
+
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
273
|
+
*/
|
|
274
|
+
function isStringType(node) {
|
|
275
|
+
if (plainStringTypes.has(node.type)) return true;
|
|
276
|
+
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
|
|
277
|
+
if (temporal) return temporal.representation !== "date";
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Derives a {@link ParamGroupType} for a query or header group from the resolver.
|
|
282
|
+
*
|
|
283
|
+
* Returns `null` when there is no resolver, no params, or the group name equals the
|
|
284
|
+
* individual param name (so there is no real group to emit).
|
|
285
|
+
*/
|
|
286
|
+
function resolveGroupType({ node, params, group, resolver }) {
|
|
287
|
+
if (!resolver || !params.length) return null;
|
|
288
|
+
const firstParam = params[0];
|
|
289
|
+
const groupName = (group === "query" ? resolver.resolveQueryParamsName : resolver.resolveHeaderParamsName).call(resolver, node, firstParam);
|
|
290
|
+
if (groupName === resolver.resolveParamName(node, firstParam)) return null;
|
|
291
|
+
return {
|
|
292
|
+
type: groupName,
|
|
293
|
+
optional: params.every((p) => !p.required)
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
//#endregion
|
|
297
|
+
//#region src/registry.ts
|
|
298
|
+
/**
|
|
299
|
+
* Every node definition. Adding a node means adding its `defineNode` to one
|
|
300
|
+
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
|
|
301
|
+
*/
|
|
302
|
+
const nodeDefs = [
|
|
303
|
+
require_response.inputDef,
|
|
304
|
+
require_response.outputDef,
|
|
305
|
+
require_response.operationDef,
|
|
306
|
+
require_response.requestBodyDef,
|
|
307
|
+
require_response.contentDef,
|
|
308
|
+
require_response.responseDef,
|
|
309
|
+
require_response.schemaDef,
|
|
310
|
+
require_response.propertyDef,
|
|
311
|
+
require_response.parameterDef,
|
|
312
|
+
require_response.functionParameterDef,
|
|
313
|
+
require_response.functionParametersDef,
|
|
314
|
+
require_response.typeLiteralDef,
|
|
315
|
+
require_response.indexedAccessTypeDef,
|
|
316
|
+
require_response.objectBindingPatternDef,
|
|
317
|
+
require_response.constDef,
|
|
318
|
+
require_response.typeDef,
|
|
319
|
+
require_response.functionDef,
|
|
320
|
+
require_response.arrowFunctionDef,
|
|
321
|
+
require_response.textDef,
|
|
322
|
+
require_response.breakDef,
|
|
323
|
+
require_response.jsxDef,
|
|
324
|
+
require_response.importDef,
|
|
325
|
+
require_response.exportDef,
|
|
326
|
+
require_response.sourceDef,
|
|
327
|
+
require_response.fileDef
|
|
328
|
+
];
|
|
329
|
+
//#endregion
|
|
330
|
+
//#region src/visitor.ts
|
|
331
|
+
/**
|
|
332
|
+
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
333
|
+
* Derived from each definition's `children`.
|
|
334
|
+
*/
|
|
335
|
+
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
|
|
336
|
+
/**
|
|
337
|
+
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
338
|
+
* definition's `visitorKey`.
|
|
339
|
+
*/
|
|
340
|
+
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
|
|
341
|
+
/**
|
|
342
|
+
* Per-kind builders rerun after children are rebuilt. Derived from each
|
|
343
|
+
* definition's `rebuild` flag.
|
|
344
|
+
*/
|
|
345
|
+
const nodeRebuilders = Object.fromEntries(nodeDefs.flatMap((def) => def.rebuild ? [[def.kind, def.create]] : []));
|
|
346
|
+
/**
|
|
347
|
+
* Creates a small async concurrency limiter.
|
|
348
|
+
*
|
|
349
|
+
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
350
|
+
*
|
|
351
|
+
* @example
|
|
352
|
+
* ```ts
|
|
353
|
+
* const limit = createLimit(2)
|
|
354
|
+
* for (const task of [taskA, taskB, taskC]) {
|
|
355
|
+
* await limit(() => task())
|
|
356
|
+
* }
|
|
357
|
+
* // only 2 tasks run at the same time
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
function createLimit(concurrency) {
|
|
361
|
+
let active = 0;
|
|
362
|
+
const queue = [];
|
|
363
|
+
function next() {
|
|
364
|
+
if (active < concurrency && queue.length > 0) {
|
|
365
|
+
active++;
|
|
366
|
+
queue.shift()();
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return function limit(fn) {
|
|
370
|
+
return new Promise((resolve, reject) => {
|
|
371
|
+
queue.push(() => {
|
|
372
|
+
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
373
|
+
active--;
|
|
374
|
+
next();
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
next();
|
|
378
|
+
});
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
const visitorKeysByKind = VISITOR_KEYS;
|
|
382
|
+
/**
|
|
383
|
+
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
384
|
+
*/
|
|
385
|
+
function isNode(value) {
|
|
386
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
390
|
+
*
|
|
391
|
+
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```ts
|
|
395
|
+
* const children = getChildren(operationNode, true)
|
|
396
|
+
* // returns parameters, the request body, and responses
|
|
397
|
+
* ```
|
|
398
|
+
*/
|
|
399
|
+
function* getChildren(node, recurse) {
|
|
400
|
+
if (node.kind === "Schema" && !recurse) return;
|
|
401
|
+
const keys = visitorKeysByKind[node.kind];
|
|
402
|
+
if (!keys) return;
|
|
403
|
+
const record = node;
|
|
404
|
+
for (const key of keys) {
|
|
405
|
+
const value = record[key];
|
|
406
|
+
if (Array.isArray(value)) {
|
|
407
|
+
for (const item of value) if (isNode(item)) yield item;
|
|
408
|
+
} else if (isNode(value)) yield value;
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
413
|
+
* context. The result is a replacement node, a collected value, or `undefined`
|
|
414
|
+
* when no callback is registered for the kind.
|
|
415
|
+
*
|
|
416
|
+
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
|
|
417
|
+
* in one place. `TResult` is the caller's expected return: the same node type
|
|
418
|
+
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
|
|
419
|
+
*/
|
|
420
|
+
function applyVisitor(node, visitor, parent) {
|
|
421
|
+
const key = VISITOR_KEY_BY_KIND[node.kind];
|
|
422
|
+
if (!key) return void 0;
|
|
423
|
+
const fn = visitor[key];
|
|
424
|
+
return fn?.(node, { parent });
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
428
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
429
|
+
*
|
|
430
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
431
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
432
|
+
* work. Lower values reduce memory pressure.
|
|
433
|
+
*
|
|
434
|
+
* @example Log every operation
|
|
435
|
+
* ```ts
|
|
436
|
+
* await walk(root, {
|
|
437
|
+
* operation(node) {
|
|
438
|
+
* console.log(node.operationId)
|
|
439
|
+
* },
|
|
440
|
+
* })
|
|
441
|
+
* ```
|
|
442
|
+
*
|
|
443
|
+
* @example Only visit the root node
|
|
444
|
+
* ```ts
|
|
445
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
446
|
+
* ```
|
|
447
|
+
*/
|
|
448
|
+
async function walk(node, options) {
|
|
449
|
+
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
450
|
+
}
|
|
451
|
+
async function _walk(node, visitor, recurse, limit, parent) {
|
|
452
|
+
await limit(() => applyVisitor(node, visitor, parent));
|
|
453
|
+
const children = Array.from(getChildren(node, recurse));
|
|
454
|
+
if (children.length === 0) return;
|
|
455
|
+
await Promise.all(children.map((child) => _walk(child, visitor, recurse, limit, node)));
|
|
456
|
+
}
|
|
457
|
+
function transform(node, options) {
|
|
458
|
+
const { depth, parent, ...visitor } = options;
|
|
459
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
460
|
+
const rebuilt = transformChildren(applyVisitor(node, visitor, parent) ?? node, options, recurse);
|
|
461
|
+
if (rebuilt === node) return node;
|
|
462
|
+
const rebuild = nodeRebuilders[rebuilt.kind];
|
|
463
|
+
return rebuild ? rebuild(rebuilt) : rebuilt;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
467
|
+
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
468
|
+
* `Schema` children are skipped in shallow mode.
|
|
469
|
+
*/
|
|
470
|
+
function transformChildren(node, options, recurse) {
|
|
471
|
+
if (node.kind === "Schema" && !recurse) return node;
|
|
472
|
+
const keys = visitorKeysByKind[node.kind];
|
|
473
|
+
if (!keys) return node;
|
|
474
|
+
const record = node;
|
|
475
|
+
const childOptions = {
|
|
476
|
+
...options,
|
|
477
|
+
parent: node
|
|
478
|
+
};
|
|
479
|
+
let updates;
|
|
480
|
+
for (const key of keys) {
|
|
481
|
+
if (!(key in record)) continue;
|
|
482
|
+
const value = record[key];
|
|
483
|
+
if (Array.isArray(value)) {
|
|
484
|
+
let changed = false;
|
|
485
|
+
const mapped = value.map((item) => {
|
|
486
|
+
if (!isNode(item)) return item;
|
|
487
|
+
const next = transform(item, childOptions);
|
|
488
|
+
if (next !== item) changed = true;
|
|
489
|
+
return next;
|
|
490
|
+
});
|
|
491
|
+
if (changed) (updates ??= {})[key] = mapped;
|
|
492
|
+
} else if (isNode(value)) {
|
|
493
|
+
const next = transform(value, childOptions);
|
|
494
|
+
if (next !== value) (updates ??= {})[key] = next;
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
return updates ? {
|
|
498
|
+
...node,
|
|
499
|
+
...updates
|
|
500
|
+
} : node;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
504
|
+
* the visitor callbacks. Use `collect` for the eager array form.
|
|
505
|
+
*
|
|
506
|
+
* @example Collect every operationId
|
|
507
|
+
* ```ts
|
|
508
|
+
* const ids: string[] = []
|
|
509
|
+
* for (const id of collectLazy<string>(root, {
|
|
510
|
+
* operation(node) {
|
|
511
|
+
* return node.operationId
|
|
512
|
+
* },
|
|
513
|
+
* })) {
|
|
514
|
+
* ids.push(id)
|
|
515
|
+
* }
|
|
516
|
+
* ```
|
|
517
|
+
*/
|
|
518
|
+
function* collectLazy(node, options) {
|
|
519
|
+
const { depth, parent, ...visitor } = options;
|
|
520
|
+
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
521
|
+
const v = applyVisitor(node, visitor, parent);
|
|
522
|
+
if (v != null) yield v;
|
|
523
|
+
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
|
|
524
|
+
...options,
|
|
525
|
+
parent: node
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
/**
|
|
529
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
530
|
+
* callbacks return into an array.
|
|
531
|
+
*
|
|
532
|
+
* @example Collect every operationId
|
|
533
|
+
* ```ts
|
|
534
|
+
* const ids = collect<string>(root, {
|
|
535
|
+
* operation(node) {
|
|
536
|
+
* return node.operationId
|
|
537
|
+
* },
|
|
538
|
+
* })
|
|
539
|
+
* ```
|
|
540
|
+
*/
|
|
541
|
+
function collect(node, options) {
|
|
542
|
+
return Array.from(collectLazy(node, options));
|
|
543
|
+
}
|
|
544
|
+
//#endregion
|
|
545
|
+
Object.defineProperty(exports, "INDENT", {
|
|
546
|
+
enumerable: true,
|
|
547
|
+
get: function() {
|
|
548
|
+
return INDENT;
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
Object.defineProperty(exports, "childName", {
|
|
552
|
+
enumerable: true,
|
|
553
|
+
get: function() {
|
|
554
|
+
return childName;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
Object.defineProperty(exports, "collect", {
|
|
558
|
+
enumerable: true,
|
|
559
|
+
get: function() {
|
|
560
|
+
return collect;
|
|
561
|
+
}
|
|
562
|
+
});
|
|
563
|
+
Object.defineProperty(exports, "collectLazy", {
|
|
564
|
+
enumerable: true,
|
|
565
|
+
get: function() {
|
|
566
|
+
return collectLazy;
|
|
567
|
+
}
|
|
568
|
+
});
|
|
569
|
+
Object.defineProperty(exports, "enumPropName", {
|
|
570
|
+
enumerable: true,
|
|
571
|
+
get: function() {
|
|
572
|
+
return enumPropName;
|
|
573
|
+
}
|
|
574
|
+
});
|
|
575
|
+
Object.defineProperty(exports, "extractRefName", {
|
|
576
|
+
enumerable: true,
|
|
577
|
+
get: function() {
|
|
578
|
+
return extractRefName;
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
Object.defineProperty(exports, "httpMethods", {
|
|
582
|
+
enumerable: true,
|
|
583
|
+
get: function() {
|
|
584
|
+
return httpMethods;
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
Object.defineProperty(exports, "isHttpOperationNode", {
|
|
588
|
+
enumerable: true,
|
|
589
|
+
get: function() {
|
|
590
|
+
return isHttpOperationNode;
|
|
591
|
+
}
|
|
592
|
+
});
|
|
593
|
+
Object.defineProperty(exports, "isScalarPrimitive", {
|
|
594
|
+
enumerable: true,
|
|
595
|
+
get: function() {
|
|
596
|
+
return isScalarPrimitive;
|
|
597
|
+
}
|
|
598
|
+
});
|
|
599
|
+
Object.defineProperty(exports, "isStringType", {
|
|
600
|
+
enumerable: true,
|
|
601
|
+
get: function() {
|
|
602
|
+
return isStringType;
|
|
603
|
+
}
|
|
604
|
+
});
|
|
605
|
+
Object.defineProperty(exports, "narrowSchema", {
|
|
606
|
+
enumerable: true,
|
|
607
|
+
get: function() {
|
|
608
|
+
return narrowSchema;
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
Object.defineProperty(exports, "nodeDefs", {
|
|
612
|
+
enumerable: true,
|
|
613
|
+
get: function() {
|
|
614
|
+
return nodeDefs;
|
|
615
|
+
}
|
|
616
|
+
});
|
|
617
|
+
Object.defineProperty(exports, "resolveGroupType", {
|
|
618
|
+
enumerable: true,
|
|
619
|
+
get: function() {
|
|
620
|
+
return resolveGroupType;
|
|
621
|
+
}
|
|
622
|
+
});
|
|
623
|
+
Object.defineProperty(exports, "resolveRefName", {
|
|
624
|
+
enumerable: true,
|
|
625
|
+
get: function() {
|
|
626
|
+
return resolveRefName;
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
Object.defineProperty(exports, "schemaTypes", {
|
|
630
|
+
enumerable: true,
|
|
631
|
+
get: function() {
|
|
632
|
+
return schemaTypes;
|
|
633
|
+
}
|
|
634
|
+
});
|
|
635
|
+
Object.defineProperty(exports, "syncSchemaRef", {
|
|
636
|
+
enumerable: true,
|
|
637
|
+
get: function() {
|
|
638
|
+
return syncSchemaRef;
|
|
639
|
+
}
|
|
640
|
+
});
|
|
641
|
+
Object.defineProperty(exports, "transform", {
|
|
642
|
+
enumerable: true,
|
|
643
|
+
get: function() {
|
|
644
|
+
return transform;
|
|
645
|
+
}
|
|
646
|
+
});
|
|
647
|
+
Object.defineProperty(exports, "walk", {
|
|
648
|
+
enumerable: true,
|
|
649
|
+
get: function() {
|
|
650
|
+
return walk;
|
|
651
|
+
}
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
//# sourceMappingURL=visitor-DpKZ9Tk0.cjs.map
|