@kubb/ast 5.0.0-beta.80 → 5.0.0-beta.81
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 -9
- 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
package/dist/index.cjs
CHANGED
|
@@ -1,27 +1,1524 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __exportAll = (all, no_symbols) => {
|
|
10
|
+
let target = {};
|
|
11
|
+
for (var name in all) __defProp(target, name, {
|
|
12
|
+
get: all[name],
|
|
13
|
+
enumerable: true
|
|
14
|
+
});
|
|
15
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
16
|
+
return target;
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
20
|
+
key = keys[i];
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
22
|
+
get: ((k) => from[k]).bind(null, key),
|
|
23
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return to;
|
|
27
|
+
};
|
|
28
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
29
|
+
value: mod,
|
|
30
|
+
enumerable: true
|
|
31
|
+
}) : target, mod));
|
|
32
|
+
//#endregion
|
|
33
|
+
let node_crypto = require("node:crypto");
|
|
34
|
+
let node_path = require("node:path");
|
|
35
|
+
node_path = __toESM(node_path, 1);
|
|
36
|
+
//#region src/constants.ts
|
|
37
|
+
const visitorDepths = {
|
|
38
|
+
shallow: "shallow",
|
|
39
|
+
deep: "deep"
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
43
|
+
*
|
|
44
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
45
|
+
*/
|
|
46
|
+
const schemaTypes = {
|
|
47
|
+
/**
|
|
48
|
+
* Text value.
|
|
49
|
+
*/
|
|
50
|
+
string: "string",
|
|
51
|
+
/**
|
|
52
|
+
* Floating-point number (`float`, `double`).
|
|
53
|
+
*/
|
|
54
|
+
number: "number",
|
|
55
|
+
/**
|
|
56
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
57
|
+
*/
|
|
58
|
+
integer: "integer",
|
|
59
|
+
/**
|
|
60
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
61
|
+
*/
|
|
62
|
+
bigint: "bigint",
|
|
63
|
+
/**
|
|
64
|
+
* Boolean value.
|
|
65
|
+
*/
|
|
66
|
+
boolean: "boolean",
|
|
67
|
+
/**
|
|
68
|
+
* Explicit null value.
|
|
69
|
+
*/
|
|
70
|
+
null: "null",
|
|
71
|
+
/**
|
|
72
|
+
* Any value (no type restriction).
|
|
73
|
+
*/
|
|
74
|
+
any: "any",
|
|
75
|
+
/**
|
|
76
|
+
* Unknown value (must be narrowed before usage).
|
|
77
|
+
*/
|
|
78
|
+
unknown: "unknown",
|
|
79
|
+
/**
|
|
80
|
+
* No return value (`void`).
|
|
81
|
+
*/
|
|
82
|
+
void: "void",
|
|
83
|
+
/**
|
|
84
|
+
* Object with named properties.
|
|
85
|
+
*/
|
|
86
|
+
object: "object",
|
|
87
|
+
/**
|
|
88
|
+
* Sequential list of items.
|
|
89
|
+
*/
|
|
90
|
+
array: "array",
|
|
91
|
+
/**
|
|
92
|
+
* Fixed-length list with position-specific items.
|
|
93
|
+
*/
|
|
94
|
+
tuple: "tuple",
|
|
95
|
+
/**
|
|
96
|
+
* "One of" multiple schema members.
|
|
97
|
+
*/
|
|
98
|
+
union: "union",
|
|
99
|
+
/**
|
|
100
|
+
* "All of" multiple schema members.
|
|
101
|
+
*/
|
|
102
|
+
intersection: "intersection",
|
|
103
|
+
/**
|
|
104
|
+
* Enum schema.
|
|
105
|
+
*/
|
|
106
|
+
enum: "enum",
|
|
107
|
+
/**
|
|
108
|
+
* Reference to another schema.
|
|
109
|
+
*/
|
|
110
|
+
ref: "ref",
|
|
111
|
+
/**
|
|
112
|
+
* Calendar date (for example `2026-03-24`).
|
|
113
|
+
*/
|
|
114
|
+
date: "date",
|
|
115
|
+
/**
|
|
116
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
117
|
+
*/
|
|
118
|
+
datetime: "datetime",
|
|
119
|
+
/**
|
|
120
|
+
* Time-only value (for example `09:00:00`).
|
|
121
|
+
*/
|
|
122
|
+
time: "time",
|
|
123
|
+
/**
|
|
124
|
+
* UUID value.
|
|
125
|
+
*/
|
|
126
|
+
uuid: "uuid",
|
|
127
|
+
/**
|
|
128
|
+
* Email address value.
|
|
129
|
+
*/
|
|
130
|
+
email: "email",
|
|
131
|
+
/**
|
|
132
|
+
* URL value.
|
|
133
|
+
*/
|
|
134
|
+
url: "url",
|
|
135
|
+
/**
|
|
136
|
+
* IPv4 address value.
|
|
137
|
+
*/
|
|
138
|
+
ipv4: "ipv4",
|
|
139
|
+
/**
|
|
140
|
+
* IPv6 address value.
|
|
141
|
+
*/
|
|
142
|
+
ipv6: "ipv6",
|
|
143
|
+
/**
|
|
144
|
+
* Binary/blob value.
|
|
145
|
+
*/
|
|
146
|
+
blob: "blob",
|
|
147
|
+
/**
|
|
148
|
+
* Impossible value (`never`).
|
|
149
|
+
*/
|
|
150
|
+
never: "never"
|
|
151
|
+
};
|
|
152
|
+
//#endregion
|
|
4
153
|
//#region src/defineDialect.ts
|
|
5
154
|
/**
|
|
6
|
-
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
|
|
7
|
-
* dialect's type for inference.
|
|
155
|
+
* Types a {@link Dialect} for an adapter. Adds no runtime behavior and only pins the
|
|
156
|
+
* dialect's type for inference.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* export const oasDialect = defineDialect({
|
|
161
|
+
* name: 'oas',
|
|
162
|
+
* schema: {
|
|
163
|
+
* isNullable,
|
|
164
|
+
* isReference,
|
|
165
|
+
* isDiscriminator,
|
|
166
|
+
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
167
|
+
* resolveRef,
|
|
168
|
+
* },
|
|
169
|
+
* })
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
function defineDialect(dialect) {
|
|
173
|
+
return dialect;
|
|
174
|
+
}
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/guards.ts
|
|
177
|
+
/**
|
|
178
|
+
* Narrows a `SchemaNode` to the variant that matches `type`.
|
|
179
|
+
*
|
|
180
|
+
* @example
|
|
181
|
+
* ```ts
|
|
182
|
+
* const schema = createSchema({ type: 'string' })
|
|
183
|
+
* const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
function narrowSchema(node, type) {
|
|
187
|
+
return node?.type === type ? node : null;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Narrows an `OperationNode` to an `HttpOperationNode` so `method` and `path` are present.
|
|
191
|
+
*
|
|
192
|
+
* @example
|
|
193
|
+
* ```ts
|
|
194
|
+
* if (isHttpOperationNode(node)) {
|
|
195
|
+
* console.log(node.method, node.path)
|
|
196
|
+
* }
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
function isHttpOperationNode(node) {
|
|
200
|
+
return node.protocol === "http" || node.method !== void 0 && node.path !== void 0;
|
|
201
|
+
}
|
|
202
|
+
//#endregion
|
|
203
|
+
//#region src/defineNode.ts
|
|
204
|
+
/**
|
|
205
|
+
* Visitor callback names, one per traversable node kind, in traversal order.
|
|
206
|
+
* Kept in sync with the keys of `Visitor` in `visitor.ts`.
|
|
207
|
+
*/
|
|
208
|
+
const visitorKeys = [
|
|
209
|
+
"input",
|
|
210
|
+
"output",
|
|
211
|
+
"operation",
|
|
212
|
+
"schema",
|
|
213
|
+
"property",
|
|
214
|
+
"parameter",
|
|
215
|
+
"response"
|
|
216
|
+
];
|
|
217
|
+
/**
|
|
218
|
+
* Builds a type guard that matches nodes of the given `kind`.
|
|
219
|
+
*/
|
|
220
|
+
function isKind(kind) {
|
|
221
|
+
return (node) => node?.kind === kind;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Defines a node once and derives its `create` builder, `is` guard, and traversal
|
|
225
|
+
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
|
|
226
|
+
* `kind`, so node construction lives in one place without scattered `as` casts.
|
|
227
|
+
*
|
|
228
|
+
* @example Simple node
|
|
229
|
+
* ```ts
|
|
230
|
+
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
|
|
231
|
+
* const createImport = importDef.create
|
|
232
|
+
* ```
|
|
233
|
+
*
|
|
234
|
+
* @example Node with a build hook
|
|
235
|
+
* ```ts
|
|
236
|
+
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
|
|
237
|
+
* kind: 'Property',
|
|
238
|
+
* build: (props) => ({ ...props, required: props.required ?? false }),
|
|
239
|
+
* children: ['schema'],
|
|
240
|
+
* visitorKey: 'property',
|
|
241
|
+
* })
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
function defineNode(config) {
|
|
245
|
+
const { kind, defaults, build, children, visitorKey } = config;
|
|
246
|
+
function create(input) {
|
|
247
|
+
const base = build ? build(input) : input;
|
|
248
|
+
const node = {
|
|
249
|
+
kind,
|
|
250
|
+
...defaults,
|
|
251
|
+
...base
|
|
252
|
+
};
|
|
253
|
+
node.kind = kind;
|
|
254
|
+
return node;
|
|
255
|
+
}
|
|
256
|
+
return {
|
|
257
|
+
kind,
|
|
258
|
+
create,
|
|
259
|
+
is: isKind(kind),
|
|
260
|
+
children,
|
|
261
|
+
visitorKey
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
//#endregion
|
|
265
|
+
//#region src/nodes/code.ts
|
|
266
|
+
/**
|
|
267
|
+
* Definition for the {@link ConstNode}.
|
|
268
|
+
*/
|
|
269
|
+
const constDef = defineNode({ kind: "Const" });
|
|
270
|
+
/**
|
|
271
|
+
* Definition for the {@link TypeNode}.
|
|
272
|
+
*/
|
|
273
|
+
const typeDef = defineNode({ kind: "Type" });
|
|
274
|
+
/**
|
|
275
|
+
* Definition for the {@link FunctionNode}.
|
|
276
|
+
*/
|
|
277
|
+
const functionDef = defineNode({ kind: "Function" });
|
|
278
|
+
/**
|
|
279
|
+
* Definition for the {@link ArrowFunctionNode}.
|
|
280
|
+
*/
|
|
281
|
+
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
|
|
282
|
+
/**
|
|
283
|
+
* Definition for the {@link TextNode}.
|
|
284
|
+
*/
|
|
285
|
+
const textDef = defineNode({
|
|
286
|
+
kind: "Text",
|
|
287
|
+
build: (value) => ({ value })
|
|
288
|
+
});
|
|
289
|
+
/**
|
|
290
|
+
* Definition for the {@link BreakNode}.
|
|
291
|
+
*/
|
|
292
|
+
const breakDef = defineNode({
|
|
293
|
+
kind: "Break",
|
|
294
|
+
build: () => ({})
|
|
295
|
+
});
|
|
296
|
+
/**
|
|
297
|
+
* Definition for the {@link JsxNode}.
|
|
298
|
+
*/
|
|
299
|
+
const jsxDef = defineNode({
|
|
300
|
+
kind: "Jsx",
|
|
301
|
+
build: (value) => ({ value })
|
|
302
|
+
});
|
|
303
|
+
/**
|
|
304
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
305
|
+
*
|
|
306
|
+
* @example Exported constant with type and `as const`
|
|
307
|
+
* ```ts
|
|
308
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
309
|
+
* // export const pets: Pet[] = ... as const
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
const createConst = constDef.create;
|
|
313
|
+
/**
|
|
314
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* ```ts
|
|
318
|
+
* createType({ name: 'Pet', export: true })
|
|
319
|
+
* // export type Pet = ...
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
const createType = typeDef.create;
|
|
323
|
+
/**
|
|
324
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```ts
|
|
328
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
329
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
const createFunction = functionDef.create;
|
|
333
|
+
/**
|
|
334
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
339
|
+
* // export const double = (n: number) => ...
|
|
340
|
+
* ```
|
|
341
|
+
*/
|
|
342
|
+
const createArrowFunction = arrowFunctionDef.create;
|
|
343
|
+
/**
|
|
344
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
345
|
+
*
|
|
346
|
+
* @example
|
|
347
|
+
* ```ts
|
|
348
|
+
* createText('return fetch(id)')
|
|
349
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
const createText = textDef.create;
|
|
353
|
+
/**
|
|
354
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
355
|
+
*
|
|
356
|
+
* @example
|
|
357
|
+
* ```ts
|
|
358
|
+
* createBreak()
|
|
359
|
+
* // { kind: 'Break' }
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
function createBreak() {
|
|
363
|
+
return breakDef.create();
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```ts
|
|
370
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
371
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
372
|
+
* ```
|
|
373
|
+
*/
|
|
374
|
+
const createJsx = jsxDef.create;
|
|
375
|
+
//#endregion
|
|
376
|
+
//#region src/nodes/content.ts
|
|
377
|
+
/**
|
|
378
|
+
* Definition for the {@link ContentNode}.
|
|
379
|
+
*/
|
|
380
|
+
const contentDef = defineNode({
|
|
381
|
+
kind: "Content",
|
|
382
|
+
children: ["schema"]
|
|
383
|
+
});
|
|
384
|
+
/**
|
|
385
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
386
|
+
*/
|
|
387
|
+
const createContent = contentDef.create;
|
|
388
|
+
//#endregion
|
|
389
|
+
//#region ../../internals/utils/src/casing.ts
|
|
390
|
+
/**
|
|
391
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
392
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
393
|
+
* and capitalizes each word according to `pascal`.
|
|
394
|
+
*
|
|
395
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
396
|
+
*/
|
|
397
|
+
function toCamelOrPascal(text, pascal) {
|
|
398
|
+
return text.trim().replace(/([a-z\d])([A-Z])/g, "$1 $2").replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2").replace(/(\d)([a-z])/g, "$1 $2").split(/[\s\-_./\\:]+/).filter(Boolean).map((word, i) => {
|
|
399
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
400
|
+
return (i === 0 && !pascal ? word.charAt(0).toLowerCase() : word.charAt(0).toUpperCase()) + word.slice(1);
|
|
401
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
402
|
+
}
|
|
403
|
+
/**
|
|
404
|
+
* Converts `text` to PascalCase.
|
|
405
|
+
*
|
|
406
|
+
* @example Word boundaries
|
|
407
|
+
* `pascalCase('hello-world') // 'HelloWorld'`
|
|
408
|
+
*
|
|
409
|
+
* @example With a suffix
|
|
410
|
+
* `pascalCase('tag', { suffix: 'schema' }) // 'TagSchema'`
|
|
411
|
+
*/
|
|
412
|
+
function pascalCase(text, { prefix = "", suffix = "" } = {}) {
|
|
413
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
414
|
+
}
|
|
415
|
+
//#endregion
|
|
416
|
+
//#region ../../internals/utils/src/fs.ts
|
|
417
|
+
/**
|
|
418
|
+
* Strips the file extension from a path or file name.
|
|
419
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
420
|
+
*
|
|
421
|
+
* @example
|
|
422
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
423
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
424
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
425
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
426
|
+
*/
|
|
427
|
+
function trimExtName(text) {
|
|
428
|
+
const dotIndex = text.lastIndexOf(".");
|
|
429
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
430
|
+
return text;
|
|
431
|
+
}
|
|
432
|
+
//#endregion
|
|
433
|
+
//#region ../../internals/utils/src/promise.ts
|
|
434
|
+
/**
|
|
435
|
+
* Wraps `factory` with a keyed cache backed by the provided store.
|
|
436
|
+
*
|
|
437
|
+
* Pass a `WeakMap` for object keys (results are GC-eligible when the key is
|
|
438
|
+
* collected) or a `Map` for primitive keys. For multi-argument functions,
|
|
439
|
+
* nest two `memoize` calls — the outer keyed by the first argument, the
|
|
440
|
+
* inner (created once per outer miss) keyed by the second.
|
|
441
|
+
*
|
|
442
|
+
* Because the cache is owned by the caller, it can be shared, inspected, or
|
|
443
|
+
* cleared independently of the memoized function.
|
|
444
|
+
*
|
|
445
|
+
* @example Single WeakMap key
|
|
446
|
+
* ```ts
|
|
447
|
+
* const cache = new WeakMap<SchemaNode, Set<string>>()
|
|
448
|
+
* const getRefs = memoize(cache, (node) => collectRefs(node))
|
|
449
|
+
* ```
|
|
450
|
+
*
|
|
451
|
+
* @example Single Map key (primitive)
|
|
452
|
+
* ```ts
|
|
453
|
+
* const cache = new Map<string, Resolver>()
|
|
454
|
+
* const getResolver = memoize(cache, (name) => buildResolver(name))
|
|
455
|
+
* ```
|
|
456
|
+
*
|
|
457
|
+
* @example Two-level (object + primitive)
|
|
458
|
+
* ```ts
|
|
459
|
+
* const outer = new WeakMap<Params[], Map<string, Params[]>>()
|
|
460
|
+
* const fn = memoize(outer, (params) => memoize(new Map(), (key) => transform(params, key)))
|
|
461
|
+
* fn(params)('camelcase')
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
function memoize(store, factory) {
|
|
465
|
+
return (key) => {
|
|
466
|
+
if (store.has(key)) return store.get(key);
|
|
467
|
+
const value = factory(key);
|
|
468
|
+
store.set(key, value);
|
|
469
|
+
return value;
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
474
|
+
/**
|
|
475
|
+
* JavaScript and Java reserved words.
|
|
476
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
477
|
+
*/
|
|
478
|
+
const reservedWords = /* @__PURE__ */ new Set([
|
|
479
|
+
"abstract",
|
|
480
|
+
"arguments",
|
|
481
|
+
"boolean",
|
|
482
|
+
"break",
|
|
483
|
+
"byte",
|
|
484
|
+
"case",
|
|
485
|
+
"catch",
|
|
486
|
+
"char",
|
|
487
|
+
"class",
|
|
488
|
+
"const",
|
|
489
|
+
"continue",
|
|
490
|
+
"debugger",
|
|
491
|
+
"default",
|
|
492
|
+
"delete",
|
|
493
|
+
"do",
|
|
494
|
+
"double",
|
|
495
|
+
"else",
|
|
496
|
+
"enum",
|
|
497
|
+
"eval",
|
|
498
|
+
"export",
|
|
499
|
+
"extends",
|
|
500
|
+
"false",
|
|
501
|
+
"final",
|
|
502
|
+
"finally",
|
|
503
|
+
"float",
|
|
504
|
+
"for",
|
|
505
|
+
"function",
|
|
506
|
+
"goto",
|
|
507
|
+
"if",
|
|
508
|
+
"implements",
|
|
509
|
+
"import",
|
|
510
|
+
"in",
|
|
511
|
+
"instanceof",
|
|
512
|
+
"int",
|
|
513
|
+
"interface",
|
|
514
|
+
"let",
|
|
515
|
+
"long",
|
|
516
|
+
"native",
|
|
517
|
+
"new",
|
|
518
|
+
"null",
|
|
519
|
+
"package",
|
|
520
|
+
"private",
|
|
521
|
+
"protected",
|
|
522
|
+
"public",
|
|
523
|
+
"return",
|
|
524
|
+
"short",
|
|
525
|
+
"static",
|
|
526
|
+
"super",
|
|
527
|
+
"switch",
|
|
528
|
+
"synchronized",
|
|
529
|
+
"this",
|
|
530
|
+
"throw",
|
|
531
|
+
"throws",
|
|
532
|
+
"transient",
|
|
533
|
+
"true",
|
|
534
|
+
"try",
|
|
535
|
+
"typeof",
|
|
536
|
+
"var",
|
|
537
|
+
"void",
|
|
538
|
+
"volatile",
|
|
539
|
+
"while",
|
|
540
|
+
"with",
|
|
541
|
+
"yield",
|
|
542
|
+
"Array",
|
|
543
|
+
"Date",
|
|
544
|
+
"hasOwnProperty",
|
|
545
|
+
"Infinity",
|
|
546
|
+
"isFinite",
|
|
547
|
+
"isNaN",
|
|
548
|
+
"isPrototypeOf",
|
|
549
|
+
"length",
|
|
550
|
+
"Math",
|
|
551
|
+
"name",
|
|
552
|
+
"NaN",
|
|
553
|
+
"Number",
|
|
554
|
+
"Object",
|
|
555
|
+
"prototype",
|
|
556
|
+
"String",
|
|
557
|
+
"toString",
|
|
558
|
+
"undefined",
|
|
559
|
+
"valueOf"
|
|
560
|
+
]);
|
|
561
|
+
/**
|
|
562
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
563
|
+
*
|
|
564
|
+
* @example
|
|
565
|
+
* ```ts
|
|
566
|
+
* isValidVarName('status') // true
|
|
567
|
+
* isValidVarName('class') // false (reserved word)
|
|
568
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
569
|
+
* ```
|
|
570
|
+
*/
|
|
571
|
+
function isValidVarName(name) {
|
|
572
|
+
if (!name || reservedWords.has(name)) return false;
|
|
573
|
+
return isIdentifier(name);
|
|
574
|
+
}
|
|
575
|
+
/**
|
|
576
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
577
|
+
*
|
|
578
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
579
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
580
|
+
* deciding whether an object key needs quoting.
|
|
581
|
+
*
|
|
582
|
+
* @example
|
|
583
|
+
* ```ts
|
|
584
|
+
* isIdentifier('name') // true
|
|
585
|
+
* isIdentifier('x-total')// false
|
|
586
|
+
* ```
|
|
587
|
+
*/
|
|
588
|
+
function isIdentifier(name) {
|
|
589
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
590
|
+
}
|
|
591
|
+
//#endregion
|
|
592
|
+
//#region ../../internals/utils/src/string.ts
|
|
593
|
+
/**
|
|
594
|
+
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
|
|
595
|
+
* any backslash or single quote in the content.
|
|
596
|
+
*
|
|
597
|
+
* @example
|
|
598
|
+
* ```ts
|
|
599
|
+
* singleQuote('foo') // "'foo'"
|
|
600
|
+
* singleQuote("o'clock") // "'o\\'clock'"
|
|
601
|
+
* ```
|
|
602
|
+
*/
|
|
603
|
+
function singleQuote(value) {
|
|
604
|
+
if (value === void 0 || value === null) return "''";
|
|
605
|
+
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
606
|
+
}
|
|
607
|
+
//#endregion
|
|
608
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
609
|
+
/**
|
|
610
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
611
|
+
*
|
|
612
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
613
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
614
|
+
*/
|
|
615
|
+
function extractStringsFromNodes(nodes) {
|
|
616
|
+
if (!nodes?.length) return "";
|
|
617
|
+
const collected = [];
|
|
618
|
+
for (const node of nodes) {
|
|
619
|
+
if (typeof node === "string") {
|
|
620
|
+
if (node) collected.push(node);
|
|
621
|
+
continue;
|
|
622
|
+
}
|
|
623
|
+
if (node.kind === "Text") {
|
|
624
|
+
if (node.value) collected.push(node.value);
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
if (node.kind === "Break") continue;
|
|
628
|
+
if (node.kind === "Jsx") {
|
|
629
|
+
if (node.value) collected.push(node.value);
|
|
630
|
+
continue;
|
|
631
|
+
}
|
|
632
|
+
const parts = [];
|
|
633
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
634
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
635
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
636
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
637
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
638
|
+
if (nested) parts.push(nested);
|
|
639
|
+
if (parts.length) collected.push(parts.join("\n"));
|
|
640
|
+
}
|
|
641
|
+
return collected.join("\n");
|
|
642
|
+
}
|
|
643
|
+
//#endregion
|
|
644
|
+
//#region src/utils/fileMerge.ts
|
|
645
|
+
function sourceKey(source) {
|
|
646
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
647
|
+
}
|
|
648
|
+
function pathTypeKey(path, isTypeOnly) {
|
|
649
|
+
return `${path}:${isTypeOnly ?? false}`;
|
|
650
|
+
}
|
|
651
|
+
function exportKey(path, name, isTypeOnly, asAlias) {
|
|
652
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
653
|
+
}
|
|
654
|
+
function importKey(path, name, isTypeOnly) {
|
|
655
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Computes a multi-level sort key for exports and imports:
|
|
659
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
660
|
+
*/
|
|
661
|
+
function sortKey(node) {
|
|
662
|
+
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
663
|
+
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
664
|
+
const hasName = node.name != null ? "1" : "0";
|
|
665
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
666
|
+
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
667
|
+
}
|
|
668
|
+
/**
|
|
669
|
+
* Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each
|
|
670
|
+
* key. Unnamed sources fall back to their extracted node strings as the name part of the key. Returns
|
|
671
|
+
* the deduplicated array in original order.
|
|
672
|
+
*/
|
|
673
|
+
function combineSources(sources) {
|
|
674
|
+
const seen = /* @__PURE__ */ new Map();
|
|
675
|
+
for (const source of sources) {
|
|
676
|
+
const key = sourceKey(source);
|
|
677
|
+
if (!seen.has(key)) seen.set(key, source);
|
|
678
|
+
}
|
|
679
|
+
return [...seen.values()];
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
|
|
683
|
+
*
|
|
684
|
+
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
|
|
685
|
+
*/
|
|
686
|
+
function mergeNameArrays(existing, incoming) {
|
|
687
|
+
const merged = new Set(existing);
|
|
688
|
+
for (const name of incoming) merged.add(name);
|
|
689
|
+
return [...merged];
|
|
690
|
+
}
|
|
691
|
+
/**
|
|
692
|
+
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
693
|
+
*
|
|
694
|
+
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
695
|
+
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
696
|
+
*/
|
|
697
|
+
function combineExports(exports) {
|
|
698
|
+
const result = [];
|
|
699
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
700
|
+
const seen = /* @__PURE__ */ new Set();
|
|
701
|
+
const keyed = exports.map((node) => ({
|
|
702
|
+
node,
|
|
703
|
+
key: sortKey(node)
|
|
704
|
+
}));
|
|
705
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
706
|
+
for (const { node: curr } of keyed) {
|
|
707
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
708
|
+
if (Array.isArray(name)) {
|
|
709
|
+
if (!name.length) continue;
|
|
710
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
711
|
+
const existing = namedByPath.get(key);
|
|
712
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
713
|
+
else {
|
|
714
|
+
const newItem = {
|
|
715
|
+
...curr,
|
|
716
|
+
name: [...new Set(name)]
|
|
717
|
+
};
|
|
718
|
+
result.push(newItem);
|
|
719
|
+
namedByPath.set(key, newItem);
|
|
720
|
+
}
|
|
721
|
+
} else {
|
|
722
|
+
const key = exportKey(path, name, isTypeOnly, asAlias);
|
|
723
|
+
if (!seen.has(key)) {
|
|
724
|
+
result.push(curr);
|
|
725
|
+
seen.add(key);
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return result;
|
|
730
|
+
}
|
|
731
|
+
/**
|
|
732
|
+
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
733
|
+
*
|
|
734
|
+
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
735
|
+
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
736
|
+
*/
|
|
737
|
+
function combineImports(imports, exports, source) {
|
|
738
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
739
|
+
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
740
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
741
|
+
const canonicalizeName = (n) => {
|
|
742
|
+
if (typeof n === "string") return n;
|
|
743
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
744
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
745
|
+
return importNameMemo.get(key);
|
|
746
|
+
};
|
|
747
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
748
|
+
for (const node of imports) {
|
|
749
|
+
if (!Array.isArray(node.name)) continue;
|
|
750
|
+
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
|
|
751
|
+
}
|
|
752
|
+
const result = [];
|
|
753
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
754
|
+
const seen = /* @__PURE__ */ new Set();
|
|
755
|
+
const keyed = imports.map((node) => ({
|
|
756
|
+
node,
|
|
757
|
+
key: sortKey(node)
|
|
758
|
+
}));
|
|
759
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
760
|
+
for (const { node: curr } of keyed) {
|
|
761
|
+
if (curr.path === curr.root) continue;
|
|
762
|
+
const { path, isTypeOnly } = curr;
|
|
763
|
+
let { name } = curr;
|
|
764
|
+
if (Array.isArray(name)) {
|
|
765
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
766
|
+
if (!name.length) continue;
|
|
767
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
768
|
+
const existing = namedByPath.get(key);
|
|
769
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
770
|
+
else {
|
|
771
|
+
const newItem = {
|
|
772
|
+
...curr,
|
|
773
|
+
name
|
|
774
|
+
};
|
|
775
|
+
result.push(newItem);
|
|
776
|
+
namedByPath.set(key, newItem);
|
|
777
|
+
}
|
|
778
|
+
} else {
|
|
779
|
+
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
780
|
+
const key = importKey(path, name, isTypeOnly);
|
|
781
|
+
if (!seen.has(key)) {
|
|
782
|
+
result.push(curr);
|
|
783
|
+
seen.add(key);
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
return result;
|
|
788
|
+
}
|
|
789
|
+
//#endregion
|
|
790
|
+
//#region src/nodes/file.ts
|
|
791
|
+
/**
|
|
792
|
+
* Definition for the {@link ImportNode}.
|
|
793
|
+
*/
|
|
794
|
+
const importDef = defineNode({ kind: "Import" });
|
|
795
|
+
/**
|
|
796
|
+
* Definition for the {@link ExportNode}.
|
|
797
|
+
*/
|
|
798
|
+
const exportDef = defineNode({ kind: "Export" });
|
|
799
|
+
/**
|
|
800
|
+
* Definition for the {@link SourceNode}.
|
|
801
|
+
*/
|
|
802
|
+
const sourceDef = defineNode({ kind: "Source" });
|
|
803
|
+
/**
|
|
804
|
+
* Definition for the {@link FileNode}. The fully resolved builder lives in
|
|
805
|
+
* `createFile`, so this definition only supplies the guard.
|
|
806
|
+
*/
|
|
807
|
+
const fileDef = defineNode({ kind: "File" });
|
|
808
|
+
/**
|
|
809
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
810
|
+
*
|
|
811
|
+
* @example Named import
|
|
812
|
+
* ```ts
|
|
813
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
814
|
+
* // import { useState } from 'react'
|
|
815
|
+
* ```
|
|
816
|
+
*/
|
|
817
|
+
const createImport = importDef.create;
|
|
818
|
+
/**
|
|
819
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
820
|
+
*
|
|
821
|
+
* @example Named export
|
|
822
|
+
* ```ts
|
|
823
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
824
|
+
* // export { Pet } from './Pet'
|
|
825
|
+
* ```
|
|
826
|
+
*/
|
|
827
|
+
const createExport = exportDef.create;
|
|
828
|
+
/**
|
|
829
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
830
|
+
*
|
|
831
|
+
* @example
|
|
832
|
+
* ```ts
|
|
833
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
834
|
+
* ```
|
|
835
|
+
*/
|
|
836
|
+
const createSource = sourceDef.create;
|
|
837
|
+
/**
|
|
838
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
839
|
+
*
|
|
840
|
+
* Computes:
|
|
841
|
+
* - `id` SHA256 hash of the file path
|
|
842
|
+
* - `name` `baseName` without extension
|
|
843
|
+
* - `extname` extension extracted from `baseName`
|
|
844
|
+
*
|
|
845
|
+
* Deduplicates:
|
|
846
|
+
* - `sources` via `combineSources`
|
|
847
|
+
* - `exports` via `combineExports`
|
|
848
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
849
|
+
*
|
|
850
|
+
* @throws {Error} when `baseName` has no extension.
|
|
851
|
+
*
|
|
852
|
+
* @example
|
|
853
|
+
* ```ts
|
|
854
|
+
* const file = createFile({
|
|
855
|
+
* baseName: 'petStore.ts',
|
|
856
|
+
* path: 'src/models/petStore.ts',
|
|
857
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
858
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
859
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
860
|
+
* })
|
|
861
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
862
|
+
* // file.name = 'petStore'
|
|
863
|
+
* // file.extname = '.ts'
|
|
864
|
+
* ```
|
|
865
|
+
*
|
|
866
|
+
* @example Copy a real file into the output verbatim
|
|
867
|
+
* ```ts
|
|
868
|
+
* const file = createFile({
|
|
869
|
+
* baseName: 'client.ts',
|
|
870
|
+
* path: 'src/gen/client.ts',
|
|
871
|
+
* copy: '/abs/path/to/templates/client.ts',
|
|
872
|
+
* })
|
|
873
|
+
* ```
|
|
874
|
+
*/
|
|
875
|
+
function createFile(input) {
|
|
876
|
+
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
877
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
878
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
879
|
+
let resolvedImports = [];
|
|
880
|
+
if (input.imports?.length) {
|
|
881
|
+
const source = extractStringsFromNodes((input.sources ?? []).flatMap((item) => item.nodes ?? [])) || void 0;
|
|
882
|
+
const combinedImports = combineImports(input.imports, resolvedExports, source);
|
|
883
|
+
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
|
|
884
|
+
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
885
|
+
resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
|
|
886
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
887
|
+
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
|
|
888
|
+
if (!kept.length) return [];
|
|
889
|
+
return [kept.length === imp.name.length ? imp : {
|
|
890
|
+
...imp,
|
|
891
|
+
name: kept
|
|
892
|
+
}];
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
896
|
+
return {
|
|
897
|
+
kind: "File",
|
|
898
|
+
...input,
|
|
899
|
+
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
|
|
900
|
+
name: trimExtName(input.baseName),
|
|
901
|
+
extname,
|
|
902
|
+
imports: resolvedImports,
|
|
903
|
+
exports: resolvedExports,
|
|
904
|
+
sources: resolvedSources,
|
|
905
|
+
meta: input.meta ?? {}
|
|
906
|
+
};
|
|
907
|
+
}
|
|
908
|
+
//#endregion
|
|
909
|
+
//#region src/nodes/input.ts
|
|
910
|
+
/**
|
|
911
|
+
* Definition for the {@link InputNode}.
|
|
912
|
+
*/
|
|
913
|
+
const inputDef = defineNode({
|
|
914
|
+
kind: "Input",
|
|
915
|
+
defaults: {
|
|
916
|
+
schemas: [],
|
|
917
|
+
operations: [],
|
|
918
|
+
meta: {
|
|
919
|
+
circularNames: [],
|
|
920
|
+
enumNames: []
|
|
921
|
+
}
|
|
922
|
+
},
|
|
923
|
+
children: ["schemas", "operations"],
|
|
924
|
+
visitorKey: "input"
|
|
925
|
+
});
|
|
926
|
+
/**
|
|
927
|
+
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
|
|
928
|
+
* `operations` are `AsyncIterable` sources. Otherwise it builds the eager variant with array
|
|
929
|
+
* `schemas`/`operations`. Both variants get the defaulted `meta`.
|
|
930
|
+
*
|
|
931
|
+
* @example Eager
|
|
932
|
+
* ```ts
|
|
933
|
+
* const input = createInput()
|
|
934
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
935
|
+
* ```
|
|
936
|
+
*
|
|
937
|
+
* @example Streaming
|
|
938
|
+
* ```ts
|
|
939
|
+
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
|
|
940
|
+
* ```
|
|
941
|
+
*/
|
|
942
|
+
function createInput(options = {}) {
|
|
943
|
+
const { stream, ...overrides } = options;
|
|
944
|
+
if (stream) return {
|
|
945
|
+
kind: "Input",
|
|
946
|
+
meta: {
|
|
947
|
+
circularNames: [],
|
|
948
|
+
enumNames: []
|
|
949
|
+
},
|
|
950
|
+
...overrides
|
|
951
|
+
};
|
|
952
|
+
return inputDef.create(overrides);
|
|
953
|
+
}
|
|
954
|
+
//#endregion
|
|
955
|
+
//#region src/nodes/requestBody.ts
|
|
956
|
+
/**
|
|
957
|
+
* Definition for the {@link RequestBodyNode}. Content entries are built upfront with
|
|
958
|
+
* {@link createContent}, mirroring how `parameters` and `responses` take prebuilt nodes.
|
|
959
|
+
*/
|
|
960
|
+
const requestBodyDef = defineNode({
|
|
961
|
+
kind: "RequestBody",
|
|
962
|
+
children: ["content"]
|
|
963
|
+
});
|
|
964
|
+
/**
|
|
965
|
+
* Creates a `RequestBodyNode`.
|
|
966
|
+
*/
|
|
967
|
+
const createRequestBody = requestBodyDef.create;
|
|
968
|
+
//#endregion
|
|
969
|
+
//#region src/nodes/operation.ts
|
|
970
|
+
/**
|
|
971
|
+
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
|
|
972
|
+
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
|
|
973
|
+
* normalized into a `RequestBodyNode`.
|
|
974
|
+
*/
|
|
975
|
+
const operationDef = defineNode({
|
|
976
|
+
kind: "Operation",
|
|
977
|
+
build: (props) => {
|
|
978
|
+
const { requestBody, ...rest } = props;
|
|
979
|
+
const isHttp = rest.method !== void 0 && rest.path !== void 0;
|
|
980
|
+
return {
|
|
981
|
+
tags: [],
|
|
982
|
+
parameters: [],
|
|
983
|
+
responses: [],
|
|
984
|
+
...rest,
|
|
985
|
+
...isHttp ? { protocol: "http" } : {},
|
|
986
|
+
requestBody: requestBody ? createRequestBody(requestBody) : void 0
|
|
987
|
+
};
|
|
988
|
+
},
|
|
989
|
+
children: [
|
|
990
|
+
"parameters",
|
|
991
|
+
"requestBody",
|
|
992
|
+
"responses"
|
|
993
|
+
],
|
|
994
|
+
visitorKey: "operation"
|
|
995
|
+
});
|
|
996
|
+
function createOperation(props) {
|
|
997
|
+
return operationDef.create(props);
|
|
998
|
+
}
|
|
999
|
+
//#endregion
|
|
1000
|
+
//#region src/nodes/output.ts
|
|
1001
|
+
/**
|
|
1002
|
+
* Definition for the {@link OutputNode}.
|
|
1003
|
+
*/
|
|
1004
|
+
const outputDef = defineNode({
|
|
1005
|
+
kind: "Output",
|
|
1006
|
+
defaults: { files: [] },
|
|
1007
|
+
visitorKey: "output"
|
|
1008
|
+
});
|
|
1009
|
+
/**
|
|
1010
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
1011
|
+
*
|
|
1012
|
+
* @example
|
|
1013
|
+
* ```ts
|
|
1014
|
+
* const output = createOutput()
|
|
1015
|
+
* // { kind: 'Output', files: [] }
|
|
1016
|
+
* ```
|
|
1017
|
+
*/
|
|
1018
|
+
function createOutput(overrides = {}) {
|
|
1019
|
+
return outputDef.create(overrides);
|
|
1020
|
+
}
|
|
1021
|
+
//#endregion
|
|
1022
|
+
//#region src/optionality.ts
|
|
1023
|
+
/**
|
|
1024
|
+
* Generic JSON Schema optionality: a non-required field is optional, and a
|
|
1025
|
+
* non-required nullable field is nullish.
|
|
1026
|
+
*/
|
|
1027
|
+
function optionality(schema, required) {
|
|
1028
|
+
const nullable = schema.nullable ?? false;
|
|
1029
|
+
return {
|
|
1030
|
+
...schema,
|
|
1031
|
+
optional: !required && !nullable ? true : void 0,
|
|
1032
|
+
nullish: !required && nullable ? true : void 0
|
|
1033
|
+
};
|
|
1034
|
+
}
|
|
1035
|
+
//#endregion
|
|
1036
|
+
//#region src/nodes/parameter.ts
|
|
1037
|
+
/**
|
|
1038
|
+
* Definition for the {@link ParameterNode}. `required` defaults to `false`, and the schema's
|
|
1039
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
1040
|
+
*/
|
|
1041
|
+
const parameterDef = defineNode({
|
|
1042
|
+
kind: "Parameter",
|
|
1043
|
+
build: (props) => {
|
|
1044
|
+
const required = props.required ?? false;
|
|
1045
|
+
return {
|
|
1046
|
+
...props,
|
|
1047
|
+
required,
|
|
1048
|
+
schema: optionality(props.schema, required)
|
|
1049
|
+
};
|
|
1050
|
+
},
|
|
1051
|
+
children: ["schema"],
|
|
1052
|
+
visitorKey: "parameter"
|
|
1053
|
+
});
|
|
1054
|
+
/**
|
|
1055
|
+
* Creates a `ParameterNode`.
|
|
1056
|
+
*
|
|
1057
|
+
* @example
|
|
1058
|
+
* ```ts
|
|
1059
|
+
* const param = createParameter({
|
|
1060
|
+
* name: 'petId',
|
|
1061
|
+
* in: 'path',
|
|
1062
|
+
* required: true,
|
|
1063
|
+
* schema: createSchema({ type: 'string' }),
|
|
1064
|
+
* })
|
|
1065
|
+
* ```
|
|
1066
|
+
*/
|
|
1067
|
+
const createParameter = parameterDef.create;
|
|
1068
|
+
//#endregion
|
|
1069
|
+
//#region src/nodes/property.ts
|
|
1070
|
+
/**
|
|
1071
|
+
* Definition for the {@link PropertyNode}. `required` defaults to `false`, and the schema's
|
|
1072
|
+
* `optional`/`nullish` flags are derived from it through {@link optionality}.
|
|
1073
|
+
*/
|
|
1074
|
+
const propertyDef = defineNode({
|
|
1075
|
+
kind: "Property",
|
|
1076
|
+
build: (props) => {
|
|
1077
|
+
const required = props.required ?? false;
|
|
1078
|
+
return {
|
|
1079
|
+
...props,
|
|
1080
|
+
required,
|
|
1081
|
+
schema: optionality(props.schema, required)
|
|
1082
|
+
};
|
|
1083
|
+
},
|
|
1084
|
+
children: ["schema"],
|
|
1085
|
+
visitorKey: "property"
|
|
1086
|
+
});
|
|
1087
|
+
/**
|
|
1088
|
+
* Creates a `PropertyNode`.
|
|
1089
|
+
*
|
|
1090
|
+
* @example
|
|
1091
|
+
* ```ts
|
|
1092
|
+
* const property = createProperty({
|
|
1093
|
+
* name: 'status',
|
|
1094
|
+
* required: true,
|
|
1095
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
1096
|
+
* })
|
|
1097
|
+
* // required=true, no optional/nullish
|
|
1098
|
+
* ```
|
|
1099
|
+
*/
|
|
1100
|
+
const createProperty = propertyDef.create;
|
|
1101
|
+
//#endregion
|
|
1102
|
+
//#region src/nodes/response.ts
|
|
1103
|
+
/**
|
|
1104
|
+
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
|
|
1105
|
+
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
|
|
1106
|
+
*/
|
|
1107
|
+
const responseDef = defineNode({
|
|
1108
|
+
kind: "Response",
|
|
1109
|
+
build: (props) => {
|
|
1110
|
+
const { schema, mediaType, keysToOmit, content, ...rest } = props;
|
|
1111
|
+
const entries = content ?? (schema ? [createContent({
|
|
1112
|
+
contentType: mediaType ?? "application/json",
|
|
1113
|
+
schema,
|
|
1114
|
+
keysToOmit: keysToOmit ?? null
|
|
1115
|
+
})] : void 0);
|
|
1116
|
+
return {
|
|
1117
|
+
...rest,
|
|
1118
|
+
content: entries
|
|
1119
|
+
};
|
|
1120
|
+
},
|
|
1121
|
+
children: ["content"],
|
|
1122
|
+
visitorKey: "response"
|
|
1123
|
+
});
|
|
1124
|
+
/**
|
|
1125
|
+
* Creates a `ResponseNode`.
|
|
1126
|
+
*
|
|
1127
|
+
* @example
|
|
1128
|
+
* ```ts
|
|
1129
|
+
* const response = createResponse({
|
|
1130
|
+
* statusCode: '200',
|
|
1131
|
+
* content: [createContent({ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) })],
|
|
1132
|
+
* })
|
|
1133
|
+
* ```
|
|
1134
|
+
*/
|
|
1135
|
+
const createResponse = responseDef.create;
|
|
1136
|
+
//#endregion
|
|
1137
|
+
//#region src/nodes/schema.ts
|
|
1138
|
+
/**
|
|
1139
|
+
* Maps schema `type` to its underlying `primitive`.
|
|
1140
|
+
* Primitive types map to themselves and special string formats map to `'string'`.
|
|
1141
|
+
* Any type not listed here (such as `ref`, `enum`, `union`, `intersection`, `tuple`, `ipv4`, `ipv6`, `blob`) has no `primitive`.
|
|
1142
|
+
*/
|
|
1143
|
+
const TYPE_TO_PRIMITIVE = {
|
|
1144
|
+
string: "string",
|
|
1145
|
+
number: "number",
|
|
1146
|
+
integer: "integer",
|
|
1147
|
+
bigint: "bigint",
|
|
1148
|
+
boolean: "boolean",
|
|
1149
|
+
null: "null",
|
|
1150
|
+
any: "any",
|
|
1151
|
+
unknown: "unknown",
|
|
1152
|
+
void: "void",
|
|
1153
|
+
never: "never",
|
|
1154
|
+
object: "object",
|
|
1155
|
+
array: "array",
|
|
1156
|
+
date: "date",
|
|
1157
|
+
uuid: "string",
|
|
1158
|
+
email: "string",
|
|
1159
|
+
url: "string",
|
|
1160
|
+
datetime: "string",
|
|
1161
|
+
time: "string"
|
|
1162
|
+
};
|
|
1163
|
+
/**
|
|
1164
|
+
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
|
|
1165
|
+
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
|
|
1166
|
+
*/
|
|
1167
|
+
const schemaDef = defineNode({
|
|
1168
|
+
kind: "Schema",
|
|
1169
|
+
build: (props) => {
|
|
1170
|
+
if (props.type === "object") return {
|
|
1171
|
+
properties: [],
|
|
1172
|
+
primitive: "object",
|
|
1173
|
+
...props
|
|
1174
|
+
};
|
|
1175
|
+
return {
|
|
1176
|
+
primitive: TYPE_TO_PRIMITIVE[props.type],
|
|
1177
|
+
...props
|
|
1178
|
+
};
|
|
1179
|
+
},
|
|
1180
|
+
children: [
|
|
1181
|
+
"properties",
|
|
1182
|
+
"items",
|
|
1183
|
+
"members",
|
|
1184
|
+
"additionalProperties"
|
|
1185
|
+
],
|
|
1186
|
+
visitorKey: "schema"
|
|
1187
|
+
});
|
|
1188
|
+
function createSchema(props) {
|
|
1189
|
+
return schemaDef.create(props);
|
|
1190
|
+
}
|
|
1191
|
+
//#endregion
|
|
1192
|
+
//#region src/registry.ts
|
|
1193
|
+
/**
|
|
1194
|
+
* Every node definition. Adding a node means adding its `defineNode` to one
|
|
1195
|
+
* `nodes/*.ts` file and listing it here. The visitor tables in `visitor.ts` derive from it.
|
|
1196
|
+
*/
|
|
1197
|
+
const nodeDefs = [
|
|
1198
|
+
inputDef,
|
|
1199
|
+
outputDef,
|
|
1200
|
+
operationDef,
|
|
1201
|
+
requestBodyDef,
|
|
1202
|
+
contentDef,
|
|
1203
|
+
responseDef,
|
|
1204
|
+
schemaDef,
|
|
1205
|
+
propertyDef,
|
|
1206
|
+
parameterDef,
|
|
1207
|
+
constDef,
|
|
1208
|
+
typeDef,
|
|
1209
|
+
functionDef,
|
|
1210
|
+
arrowFunctionDef,
|
|
1211
|
+
textDef,
|
|
1212
|
+
breakDef,
|
|
1213
|
+
jsxDef,
|
|
1214
|
+
importDef,
|
|
1215
|
+
exportDef,
|
|
1216
|
+
sourceDef,
|
|
1217
|
+
fileDef
|
|
1218
|
+
];
|
|
1219
|
+
//#endregion
|
|
1220
|
+
//#region src/visitor.ts
|
|
1221
|
+
/**
|
|
1222
|
+
* Child node fields per node kind, in traversal order (Babel's `VISITOR_KEYS`).
|
|
1223
|
+
* Derived from each definition's `children`.
|
|
1224
|
+
*/
|
|
1225
|
+
const VISITOR_KEYS = Object.fromEntries(nodeDefs.flatMap((def) => def.children ? [[def.kind, def.children]] : []));
|
|
1226
|
+
/**
|
|
1227
|
+
* Maps a node kind to the matching visitor callback name. Derived from each
|
|
1228
|
+
* definition's `visitorKey`.
|
|
1229
|
+
*/
|
|
1230
|
+
const VISITOR_KEY_BY_KIND = Object.fromEntries(nodeDefs.flatMap((def) => def.visitorKey ? [[def.kind, def.visitorKey]] : []));
|
|
1231
|
+
/**
|
|
1232
|
+
* Creates a small async concurrency limiter.
|
|
1233
|
+
*
|
|
1234
|
+
* At most `concurrency` tasks are in flight at once. Extra tasks are queued.
|
|
1235
|
+
*
|
|
1236
|
+
* @example
|
|
1237
|
+
* ```ts
|
|
1238
|
+
* const limit = createLimit(2)
|
|
1239
|
+
* for (const task of [taskA, taskB, taskC]) {
|
|
1240
|
+
* await limit(() => task())
|
|
1241
|
+
* }
|
|
1242
|
+
* // only 2 tasks run at the same time
|
|
1243
|
+
* ```
|
|
1244
|
+
*/
|
|
1245
|
+
function createLimit(concurrency) {
|
|
1246
|
+
let active = 0;
|
|
1247
|
+
const queue = [];
|
|
1248
|
+
function next() {
|
|
1249
|
+
if (active < concurrency && queue.length > 0) {
|
|
1250
|
+
active++;
|
|
1251
|
+
queue.shift()();
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
return function limit(fn) {
|
|
1255
|
+
return new Promise((resolve, reject) => {
|
|
1256
|
+
queue.push(() => {
|
|
1257
|
+
Promise.resolve(fn()).then(resolve, reject).finally(() => {
|
|
1258
|
+
active--;
|
|
1259
|
+
next();
|
|
1260
|
+
});
|
|
1261
|
+
});
|
|
1262
|
+
next();
|
|
1263
|
+
});
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
const visitorKeysByKind = VISITOR_KEYS;
|
|
1267
|
+
/**
|
|
1268
|
+
* Returns `true` when `value` is an AST node (an object carrying a `kind`).
|
|
1269
|
+
*/
|
|
1270
|
+
function isNode(value) {
|
|
1271
|
+
return typeof value === "object" && value !== null && typeof value.kind === "string";
|
|
1272
|
+
}
|
|
1273
|
+
/**
|
|
1274
|
+
* Returns the immediate traversable children of `node` based on {@link VISITOR_KEYS}.
|
|
1275
|
+
*
|
|
1276
|
+
* `Schema` children are only included when `recurse` is `true`. Shallow mode skips them.
|
|
1277
|
+
*
|
|
1278
|
+
* @example
|
|
1279
|
+
* ```ts
|
|
1280
|
+
* const children = getChildren(operationNode, true)
|
|
1281
|
+
* // returns parameters, the request body, and responses
|
|
1282
|
+
* ```
|
|
1283
|
+
*/
|
|
1284
|
+
function* getChildren(node, recurse) {
|
|
1285
|
+
if (node.kind === "Schema" && !recurse) return;
|
|
1286
|
+
const keys = visitorKeysByKind[node.kind];
|
|
1287
|
+
if (!keys) return;
|
|
1288
|
+
const record = node;
|
|
1289
|
+
for (const key of keys) {
|
|
1290
|
+
const value = record[key];
|
|
1291
|
+
if (Array.isArray(value)) {
|
|
1292
|
+
for (const item of value) if (isNode(item)) yield item;
|
|
1293
|
+
} else if (isNode(value)) yield value;
|
|
1294
|
+
}
|
|
1295
|
+
}
|
|
1296
|
+
/**
|
|
1297
|
+
* Runs the visitor callback that matches `node.kind` with the traversal
|
|
1298
|
+
* context. The result is a replacement node, a collected value, or `undefined`
|
|
1299
|
+
* when no callback is registered for the kind.
|
|
1300
|
+
*
|
|
1301
|
+
* Shared by `walk`, `transform`, and `collectLazy` so node-kind dispatch lives
|
|
1302
|
+
* in one place. `TResult` is the caller's expected return: the same node type
|
|
1303
|
+
* for `transform`, the collected value type for `collectLazy`, ignored for `walk`.
|
|
1304
|
+
*/
|
|
1305
|
+
function applyVisitor(node, visitor, parent) {
|
|
1306
|
+
const key = VISITOR_KEY_BY_KIND[node.kind];
|
|
1307
|
+
if (!key) return void 0;
|
|
1308
|
+
const fn = visitor[key];
|
|
1309
|
+
return fn?.(node, { parent });
|
|
1310
|
+
}
|
|
1311
|
+
/**
|
|
1312
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
1313
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
1314
|
+
*
|
|
1315
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
1316
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
1317
|
+
* work. Lower values reduce memory pressure.
|
|
1318
|
+
*
|
|
1319
|
+
* @example Log every operation
|
|
1320
|
+
* ```ts
|
|
1321
|
+
* await walk(root, {
|
|
1322
|
+
* operation(node) {
|
|
1323
|
+
* console.log(node.operationId)
|
|
1324
|
+
* },
|
|
1325
|
+
* })
|
|
1326
|
+
* ```
|
|
1327
|
+
*
|
|
1328
|
+
* @example Only visit the root node
|
|
1329
|
+
* ```ts
|
|
1330
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
1331
|
+
* ```
|
|
1332
|
+
*/
|
|
1333
|
+
async function walk(node, options) {
|
|
1334
|
+
return _walk(node, options, (options.depth ?? visitorDepths.deep) === visitorDepths.deep, createLimit(options.concurrency ?? 30), void 0);
|
|
1335
|
+
}
|
|
1336
|
+
async function _walk(node, visitor, recurse, limit, parent) {
|
|
1337
|
+
await limit(() => applyVisitor(node, visitor, parent));
|
|
1338
|
+
let pending;
|
|
1339
|
+
for (const child of getChildren(node, recurse)) (pending ??= []).push(_walk(child, visitor, recurse, limit, node));
|
|
1340
|
+
if (pending) await Promise.all(pending);
|
|
1341
|
+
}
|
|
1342
|
+
function transform(node, options) {
|
|
1343
|
+
const { depth, parent, ...visitor } = options;
|
|
1344
|
+
return transformNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
|
|
1345
|
+
}
|
|
1346
|
+
/**
|
|
1347
|
+
* Visits a single node, then immutably rebuilds its children. Returns the original
|
|
1348
|
+
* reference when neither the visitor nor the child rebuild changed anything, so callers
|
|
1349
|
+
* can detect "nothing changed" by identity and ancestors avoid reallocating.
|
|
1350
|
+
*/
|
|
1351
|
+
function transformNode(node, visitor, recurse, parent) {
|
|
1352
|
+
return transformChildren(applyVisitor(node, visitor, parent) ?? node, visitor, recurse);
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Immutably rebuilds a node's children using {@link VISITOR_KEYS}, transforming
|
|
1356
|
+
* each child node and leaving non-node values (e.g. `additionalProperties: true`) intact.
|
|
1357
|
+
* `Schema` children are skipped in shallow mode.
|
|
1358
|
+
*/
|
|
1359
|
+
function transformChildren(node, visitor, recurse) {
|
|
1360
|
+
if (node.kind === "Schema" && !recurse) return node;
|
|
1361
|
+
const keys = visitorKeysByKind[node.kind];
|
|
1362
|
+
if (!keys) return node;
|
|
1363
|
+
const record = node;
|
|
1364
|
+
let updates;
|
|
1365
|
+
for (const key of keys) {
|
|
1366
|
+
if (!(key in record)) continue;
|
|
1367
|
+
const value = record[key];
|
|
1368
|
+
if (Array.isArray(value)) {
|
|
1369
|
+
let mapped;
|
|
1370
|
+
for (const [i, item] of value.entries()) {
|
|
1371
|
+
const next = isNode(item) ? transformNode(item, visitor, recurse, node) : item;
|
|
1372
|
+
if (mapped) {
|
|
1373
|
+
mapped.push(next);
|
|
1374
|
+
continue;
|
|
1375
|
+
}
|
|
1376
|
+
if (next !== item) mapped = [...value.slice(0, i), next];
|
|
1377
|
+
}
|
|
1378
|
+
if (mapped) (updates ??= {})[key] = mapped;
|
|
1379
|
+
} else if (isNode(value)) {
|
|
1380
|
+
const next = transformNode(value, visitor, recurse, node);
|
|
1381
|
+
if (next !== value) (updates ??= {})[key] = next;
|
|
1382
|
+
}
|
|
1383
|
+
}
|
|
1384
|
+
return updates ? {
|
|
1385
|
+
...node,
|
|
1386
|
+
...updates
|
|
1387
|
+
} : node;
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
1391
|
+
* the visitor callbacks. Use `collect` for the eager array form.
|
|
1392
|
+
*
|
|
1393
|
+
* @example Collect every operationId
|
|
1394
|
+
* ```ts
|
|
1395
|
+
* const ids: string[] = []
|
|
1396
|
+
* for (const id of collectLazy<string>(root, {
|
|
1397
|
+
* operation(node) {
|
|
1398
|
+
* return node.operationId
|
|
1399
|
+
* },
|
|
1400
|
+
* })) {
|
|
1401
|
+
* ids.push(id)
|
|
1402
|
+
* }
|
|
1403
|
+
* ```
|
|
1404
|
+
*/
|
|
1405
|
+
function* collectLazy(node, options) {
|
|
1406
|
+
const { depth, parent, ...visitor } = options;
|
|
1407
|
+
yield* collectNode(node, visitor, (depth ?? visitorDepths.deep) === visitorDepths.deep, parent);
|
|
1408
|
+
}
|
|
1409
|
+
function* collectNode(node, visitor, recurse, parent) {
|
|
1410
|
+
const v = applyVisitor(node, visitor, parent);
|
|
1411
|
+
if (v != null) yield v;
|
|
1412
|
+
for (const child of getChildren(node, recurse)) yield* collectNode(child, visitor, recurse, node);
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
1416
|
+
* callbacks return into an array.
|
|
1417
|
+
*
|
|
1418
|
+
* @example Collect every operationId
|
|
1419
|
+
* ```ts
|
|
1420
|
+
* const ids = collect<string>(root, {
|
|
1421
|
+
* operation(node) {
|
|
1422
|
+
* return node.operationId
|
|
1423
|
+
* },
|
|
1424
|
+
* })
|
|
1425
|
+
* ```
|
|
1426
|
+
*/
|
|
1427
|
+
function collect(node, options) {
|
|
1428
|
+
return Array.from(collectLazy(node, options));
|
|
1429
|
+
}
|
|
1430
|
+
//#endregion
|
|
1431
|
+
//#region src/defineMacro.ts
|
|
1432
|
+
/**
|
|
1433
|
+
* Sort weight for an `enforce` hint. `pre` sorts before unmarked items and `post` after, so a plain
|
|
1434
|
+
* list keeps its authored order.
|
|
1435
|
+
*/
|
|
1436
|
+
function enforceWeight(enforce) {
|
|
1437
|
+
if (enforce === "pre") return 0;
|
|
1438
|
+
if (enforce === "post") return 2;
|
|
1439
|
+
return 1;
|
|
1440
|
+
}
|
|
1441
|
+
/**
|
|
1442
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
1443
|
+
* Adds no runtime behavior.
|
|
8
1444
|
*
|
|
9
1445
|
* @example
|
|
10
1446
|
* ```ts
|
|
11
|
-
*
|
|
12
|
-
* name: '
|
|
13
|
-
*
|
|
14
|
-
*
|
|
15
|
-
* isReference,
|
|
16
|
-
* isDiscriminator,
|
|
17
|
-
* isBinary: (schema) => schema.type === 'string' && schema.contentMediaType === 'application/octet-stream',
|
|
18
|
-
* resolveRef,
|
|
1447
|
+
* const macroUntagged = defineMacro({
|
|
1448
|
+
* name: 'untagged',
|
|
1449
|
+
* operation(node) {
|
|
1450
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
19
1451
|
* },
|
|
20
1452
|
* })
|
|
21
1453
|
* ```
|
|
22
1454
|
*/
|
|
23
|
-
function
|
|
24
|
-
return
|
|
1455
|
+
function defineMacro(macro) {
|
|
1456
|
+
return macro;
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Runs every macro's callback for one node kind in order, chaining the result so each macro sees
|
|
1460
|
+
* the previous macro's output. Returns `undefined` when nothing changed, so `transform` keeps the
|
|
1461
|
+
* original reference (structural sharing).
|
|
1462
|
+
*/
|
|
1463
|
+
function chain({ macros, key, node, context }) {
|
|
1464
|
+
let current = node;
|
|
1465
|
+
for (const macro of macros) {
|
|
1466
|
+
const callback = macro[key];
|
|
1467
|
+
if (!callback) continue;
|
|
1468
|
+
if (macro.when && !macro.when(current)) continue;
|
|
1469
|
+
const next = callback(current, context);
|
|
1470
|
+
if (next != null) current = next;
|
|
1471
|
+
}
|
|
1472
|
+
return current === node ? void 0 : current;
|
|
1473
|
+
}
|
|
1474
|
+
/**
|
|
1475
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
1476
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
1477
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
1478
|
+
* has no names, ordering, or composition.
|
|
1479
|
+
*
|
|
1480
|
+
* @example
|
|
1481
|
+
* ```ts
|
|
1482
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
1483
|
+
* const next = transform(root, visitor)
|
|
1484
|
+
* ```
|
|
1485
|
+
*/
|
|
1486
|
+
function composeMacros(macros) {
|
|
1487
|
+
const ordered = [...macros].sort((a, b) => enforceWeight(a.enforce) - enforceWeight(b.enforce));
|
|
1488
|
+
const visitor = {};
|
|
1489
|
+
for (const key of visitorKeys) {
|
|
1490
|
+
if (!ordered.some((macro) => typeof macro[key] === "function")) continue;
|
|
1491
|
+
const callback = (node, context) => chain({
|
|
1492
|
+
macros: ordered,
|
|
1493
|
+
key,
|
|
1494
|
+
node,
|
|
1495
|
+
context
|
|
1496
|
+
});
|
|
1497
|
+
visitor[key] = callback;
|
|
1498
|
+
}
|
|
1499
|
+
return visitor;
|
|
1500
|
+
}
|
|
1501
|
+
/**
|
|
1502
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
1503
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
1504
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
1505
|
+
*
|
|
1506
|
+
* @example
|
|
1507
|
+
* ```ts
|
|
1508
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
1509
|
+
* ```
|
|
1510
|
+
*
|
|
1511
|
+
* @example Apply to the root node only
|
|
1512
|
+
* ```ts
|
|
1513
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
1514
|
+
* ```
|
|
1515
|
+
*/
|
|
1516
|
+
function applyMacros(root, macros, options) {
|
|
1517
|
+
if (macros.length === 0) return root;
|
|
1518
|
+
return transform(root, {
|
|
1519
|
+
...composeMacros(macros),
|
|
1520
|
+
...options
|
|
1521
|
+
});
|
|
25
1522
|
}
|
|
26
1523
|
//#endregion
|
|
27
1524
|
//#region src/createPrinter.ts
|
|
@@ -95,28 +1592,653 @@ function createPrinter(build) {
|
|
|
95
1592
|
};
|
|
96
1593
|
}
|
|
97
1594
|
//#endregion
|
|
1595
|
+
//#region src/utils/codegen.ts
|
|
1596
|
+
/**
|
|
1597
|
+
* Builds a JSDoc comment block from an array of lines. Returns `fallback` when there are no
|
|
1598
|
+
* comments.
|
|
1599
|
+
*
|
|
1600
|
+
* @example
|
|
1601
|
+
* ```ts
|
|
1602
|
+
* buildJSDoc(['@type string', '@example hello'])
|
|
1603
|
+
* // '/**\n * @type string\n * @example hello\n *\/\n '
|
|
1604
|
+
* ```
|
|
1605
|
+
*/
|
|
1606
|
+
function buildJSDoc(comments, options = {}) {
|
|
1607
|
+
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
|
|
1608
|
+
if (comments.length === 0) return fallback;
|
|
1609
|
+
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
|
|
1610
|
+
}
|
|
1611
|
+
/**
|
|
1612
|
+
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
|
|
1613
|
+
*/
|
|
1614
|
+
function indentLines(text) {
|
|
1615
|
+
if (!text) return "";
|
|
1616
|
+
return text.split("\n").map((line) => line.trim() ? ` ${line}` : "").join("\n");
|
|
1617
|
+
}
|
|
1618
|
+
/**
|
|
1619
|
+
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
|
|
1620
|
+
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
|
|
1621
|
+
*
|
|
1622
|
+
* @example
|
|
1623
|
+
* ```ts
|
|
1624
|
+
* objectKey('name') // 'name'
|
|
1625
|
+
* objectKey('x-total') // "'x-total'"
|
|
1626
|
+
* ```
|
|
1627
|
+
*/
|
|
1628
|
+
function objectKey(name) {
|
|
1629
|
+
return isIdentifier(name) ? name : singleQuote(name);
|
|
1630
|
+
}
|
|
1631
|
+
/**
|
|
1632
|
+
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
|
|
1633
|
+
* level and closing the brace at column zero. Entries that are themselves multi-line objects indent
|
|
1634
|
+
* cumulatively. Each entry ends with a trailing comma to match the formatter's multi-line style.
|
|
1635
|
+
*
|
|
1636
|
+
* @example
|
|
1637
|
+
* ```ts
|
|
1638
|
+
* buildObject(['id: z.number()', 'name: z.string()'])
|
|
1639
|
+
* // '{\n id: z.number(),\n name: z.string(),\n}'
|
|
1640
|
+
* ```
|
|
1641
|
+
*/
|
|
1642
|
+
function buildObject(entries) {
|
|
1643
|
+
if (entries.length === 0) return "{}";
|
|
1644
|
+
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
|
|
1645
|
+
}
|
|
1646
|
+
/**
|
|
1647
|
+
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
|
|
1648
|
+
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
|
|
1649
|
+
* one level with a trailing comma and the closing bracket at column zero. Used for member lists such
|
|
1650
|
+
* as `z.union([…])` and `z.array([…])`.
|
|
1651
|
+
*
|
|
1652
|
+
* @example
|
|
1653
|
+
* ```ts
|
|
1654
|
+
* buildList(['z.string()', 'z.number()'])
|
|
1655
|
+
* // '[z.string(), z.number()]'
|
|
1656
|
+
* ```
|
|
1657
|
+
*/
|
|
1658
|
+
function buildList(items, brackets = ["[", "]"]) {
|
|
1659
|
+
const [open, close] = brackets;
|
|
1660
|
+
if (items.length === 0) return `${open}${close}`;
|
|
1661
|
+
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
|
|
1662
|
+
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
|
|
1663
|
+
}
|
|
1664
|
+
//#endregion
|
|
1665
|
+
//#region src/utils/refs.ts
|
|
1666
|
+
const plainStringTypes = /* @__PURE__ */ new Set([
|
|
1667
|
+
"string",
|
|
1668
|
+
"uuid",
|
|
1669
|
+
"email",
|
|
1670
|
+
"url",
|
|
1671
|
+
"datetime"
|
|
1672
|
+
]);
|
|
1673
|
+
/**
|
|
1674
|
+
* Returns the last path segment of a reference string.
|
|
1675
|
+
*
|
|
1676
|
+
* @example
|
|
1677
|
+
* `extractRefName('#/components/schemas/Pet') // 'Pet'`
|
|
1678
|
+
*/
|
|
1679
|
+
function extractRefName(ref) {
|
|
1680
|
+
return ref.split("/").at(-1) ?? ref;
|
|
1681
|
+
}
|
|
1682
|
+
/**
|
|
1683
|
+
* Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls
|
|
1684
|
+
* back to `name` then nested `schema.name`.
|
|
1685
|
+
*
|
|
1686
|
+
* Returns `null` for non-ref nodes or when no name resolves.
|
|
1687
|
+
*
|
|
1688
|
+
* @example
|
|
1689
|
+
* `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`
|
|
1690
|
+
*/
|
|
1691
|
+
function resolveRefName(node) {
|
|
1692
|
+
if (!node || node.type !== "ref") return null;
|
|
1693
|
+
if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
|
|
1694
|
+
return node.name ?? node.schema?.name ?? null;
|
|
1695
|
+
}
|
|
1696
|
+
/**
|
|
1697
|
+
* Builds a PascalCase child schema name by joining a parent name and property name.
|
|
1698
|
+
* Returns `null` when there is no parent to nest under.
|
|
1699
|
+
*
|
|
1700
|
+
* @example Nested under a parent
|
|
1701
|
+
* `childName('Order', 'shipping_address') // 'OrderShippingAddress'`
|
|
1702
|
+
*
|
|
1703
|
+
* @example No parent
|
|
1704
|
+
* `childName(undefined, 'params') // null`
|
|
1705
|
+
*/
|
|
1706
|
+
function childName(parentName, propName) {
|
|
1707
|
+
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
1708
|
+
}
|
|
1709
|
+
/**
|
|
1710
|
+
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
|
|
1711
|
+
* empty parts.
|
|
1712
|
+
*
|
|
1713
|
+
* @example
|
|
1714
|
+
* `enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'`
|
|
1715
|
+
*/
|
|
1716
|
+
function enumPropName(parentName, propName, enumSuffix) {
|
|
1717
|
+
return pascalCase([
|
|
1718
|
+
parentName,
|
|
1719
|
+
propName,
|
|
1720
|
+
enumSuffix
|
|
1721
|
+
].filter(Boolean).join(" "));
|
|
1722
|
+
}
|
|
1723
|
+
/**
|
|
1724
|
+
* Merges a ref node with its resolved schema, giving usage-site fields precedence.
|
|
1725
|
+
*
|
|
1726
|
+
* Every field set on the ref node except `kind`, `type`, `name`, `ref`, and `schema` overrides the
|
|
1727
|
+
* same field in the resolved `node.schema` (for example `description`, `nullable`, `readOnly`,
|
|
1728
|
+
* `deprecated`). Fields left `undefined` on the ref do not shadow the resolved schema. Non-ref
|
|
1729
|
+
* nodes and refs without a resolved `schema` are returned unchanged.
|
|
1730
|
+
*
|
|
1731
|
+
* @example
|
|
1732
|
+
* ```ts
|
|
1733
|
+
* const ref = createSchema({ type: 'ref', ref: '#/components/schemas/Pet', description: 'A cute pet' })
|
|
1734
|
+
* const merged = syncSchemaRef(ref) // merges with resolved Pet schema
|
|
1735
|
+
* ```
|
|
1736
|
+
*/
|
|
1737
|
+
function syncSchemaRef(node) {
|
|
1738
|
+
const ref = narrowSchema(node, "ref");
|
|
1739
|
+
if (!ref) return node;
|
|
1740
|
+
if (!ref.schema) return node;
|
|
1741
|
+
const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = ref;
|
|
1742
|
+
const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, v]) => v !== void 0));
|
|
1743
|
+
return createSchema({
|
|
1744
|
+
...ref.schema,
|
|
1745
|
+
...definedOverrides
|
|
1746
|
+
});
|
|
1747
|
+
}
|
|
1748
|
+
/**
|
|
1749
|
+
* Type guard that returns `true` when a schema emits as a plain `string` type.
|
|
1750
|
+
*
|
|
1751
|
+
* Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
|
|
1752
|
+
* types, returns `true` only when `representation` is `'string'` rather than `'date'`.
|
|
1753
|
+
*/
|
|
1754
|
+
function isStringType(node) {
|
|
1755
|
+
if (plainStringTypes.has(node.type)) return true;
|
|
1756
|
+
const temporal = narrowSchema(node, "date") ?? narrowSchema(node, "time");
|
|
1757
|
+
if (temporal) return temporal.representation !== "date";
|
|
1758
|
+
return false;
|
|
1759
|
+
}
|
|
1760
|
+
//#endregion
|
|
1761
|
+
//#region src/utils/schemaMerge.ts
|
|
1762
|
+
/**
|
|
1763
|
+
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
|
|
1764
|
+
* run and pass through unchanged. The merge follows member order, so callers control which members
|
|
1765
|
+
* combine by where they place them in the sequence.
|
|
1766
|
+
*
|
|
1767
|
+
* @example
|
|
1768
|
+
* ```ts
|
|
1769
|
+
* const merged = [...mergeAdjacentObjectsLazy([objectA, objectB])]
|
|
1770
|
+
* ```
|
|
1771
|
+
*/
|
|
1772
|
+
function* mergeAdjacentObjectsLazy(members) {
|
|
1773
|
+
let acc;
|
|
1774
|
+
for (const member of members) {
|
|
1775
|
+
const objectMember = narrowSchema(member, "object");
|
|
1776
|
+
if (objectMember && !objectMember.name && acc !== void 0) {
|
|
1777
|
+
const accObject = narrowSchema(acc, "object");
|
|
1778
|
+
if (accObject && !accObject.name) {
|
|
1779
|
+
acc = createSchema({
|
|
1780
|
+
...accObject,
|
|
1781
|
+
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
|
|
1782
|
+
});
|
|
1783
|
+
continue;
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
if (acc !== void 0) yield acc;
|
|
1787
|
+
acc = member;
|
|
1788
|
+
}
|
|
1789
|
+
if (acc !== void 0) yield acc;
|
|
1790
|
+
}
|
|
1791
|
+
//#endregion
|
|
1792
|
+
//#region src/utils/strings.ts
|
|
1793
|
+
/**
|
|
1794
|
+
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
1795
|
+
* Returns the string unchanged when no balanced quote pair is found.
|
|
1796
|
+
*
|
|
1797
|
+
* @example
|
|
1798
|
+
* ```ts
|
|
1799
|
+
* trimQuotes('"hello"') // 'hello'
|
|
1800
|
+
* trimQuotes('hello') // 'hello'
|
|
1801
|
+
* ```
|
|
1802
|
+
*/
|
|
1803
|
+
function trimQuotes(text) {
|
|
1804
|
+
if (text.length >= 2) {
|
|
1805
|
+
const first = text[0];
|
|
1806
|
+
const last = text[text.length - 1];
|
|
1807
|
+
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
|
|
1808
|
+
}
|
|
1809
|
+
return text;
|
|
1810
|
+
}
|
|
1811
|
+
/**
|
|
1812
|
+
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
|
|
1813
|
+
*
|
|
1814
|
+
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
|
|
1815
|
+
* code matches the repo style without a formatter.
|
|
1816
|
+
*
|
|
1817
|
+
* @example
|
|
1818
|
+
* ```ts
|
|
1819
|
+
* stringify('hello') // "'hello'"
|
|
1820
|
+
* stringify('"hello"') // "'hello'"
|
|
1821
|
+
* ```
|
|
1822
|
+
*/
|
|
1823
|
+
function stringify(value) {
|
|
1824
|
+
if (value === void 0 || value === null) return "''";
|
|
1825
|
+
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
|
|
1826
|
+
}
|
|
1827
|
+
/**
|
|
1828
|
+
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
|
|
1829
|
+
* and the Unicode line terminators U+2028 and U+2029.
|
|
1830
|
+
*
|
|
1831
|
+
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
1832
|
+
*
|
|
1833
|
+
* @example
|
|
1834
|
+
* ```ts
|
|
1835
|
+
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
|
|
1836
|
+
* ```
|
|
1837
|
+
*/
|
|
1838
|
+
function jsStringEscape(input) {
|
|
1839
|
+
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
|
|
1840
|
+
switch (character) {
|
|
1841
|
+
case "\"":
|
|
1842
|
+
case "'":
|
|
1843
|
+
case "\\": return `\\${character}`;
|
|
1844
|
+
case "\n": return "\\n";
|
|
1845
|
+
case "\r": return "\\r";
|
|
1846
|
+
case "\u2028": return "\\u2028";
|
|
1847
|
+
case "\u2029": return "\\u2029";
|
|
1848
|
+
default: return "";
|
|
1849
|
+
}
|
|
1850
|
+
});
|
|
1851
|
+
}
|
|
1852
|
+
/**
|
|
1853
|
+
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
1854
|
+
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
|
|
1855
|
+
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
1856
|
+
*
|
|
1857
|
+
* @example
|
|
1858
|
+
* ```ts
|
|
1859
|
+
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
|
|
1860
|
+
* toRegExpString('^(?im)foo', null) // '/^foo/im'
|
|
1861
|
+
* ```
|
|
1862
|
+
*/
|
|
1863
|
+
function toRegExpString(text, func = "RegExp") {
|
|
1864
|
+
const raw = trimQuotes(text);
|
|
1865
|
+
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
|
|
1866
|
+
const replacementTarget = match?.[1] ?? "";
|
|
1867
|
+
const matchedFlags = match?.[2];
|
|
1868
|
+
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
|
|
1869
|
+
const { source, flags } = new RegExp(cleaned, matchedFlags);
|
|
1870
|
+
if (func === null) return `/${source}/${flags}`;
|
|
1871
|
+
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
|
|
1872
|
+
}
|
|
1873
|
+
/**
|
|
1874
|
+
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
|
|
1875
|
+
* objects recurse with fixed indentation, so the result drops straight into an object literal
|
|
1876
|
+
* without re-parsing.
|
|
1877
|
+
*
|
|
1878
|
+
* @example
|
|
1879
|
+
* ```ts
|
|
1880
|
+
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
|
|
1881
|
+
* // 'foo: bar,\nnested: {\n a: 1\n }'
|
|
1882
|
+
* ```
|
|
1883
|
+
*/
|
|
1884
|
+
function stringifyObject(value) {
|
|
1885
|
+
return Object.entries(value).map(([key, val]) => {
|
|
1886
|
+
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
|
|
1887
|
+
return `${key}: ${val}`;
|
|
1888
|
+
}).filter(Boolean).join(",\n");
|
|
1889
|
+
}
|
|
1890
|
+
/**
|
|
1891
|
+
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
|
|
1892
|
+
* `accessor`. Returns `null` for an empty path.
|
|
1893
|
+
*
|
|
1894
|
+
* @example
|
|
1895
|
+
* ```ts
|
|
1896
|
+
* getNestedAccessor('pagination.next.id', 'lastPage')
|
|
1897
|
+
* // "lastPage?.['pagination']?.['next']?.['id']"
|
|
1898
|
+
* ```
|
|
1899
|
+
*/
|
|
1900
|
+
function getNestedAccessor(param, accessor) {
|
|
1901
|
+
const parts = Array.isArray(param) ? param : param.split(".");
|
|
1902
|
+
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
|
|
1903
|
+
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
|
|
1904
|
+
}
|
|
1905
|
+
//#endregion
|
|
1906
|
+
//#region src/utils/schemaGraph.ts
|
|
1907
|
+
/**
|
|
1908
|
+
* Memoized inner pass that walks a single node and returns the names of every schema it references.
|
|
1909
|
+
*/
|
|
1910
|
+
const collectSchemaRefs = memoize(/* @__PURE__ */ new WeakMap(), (node) => {
|
|
1911
|
+
const refs = /* @__PURE__ */ new Set();
|
|
1912
|
+
collect(node, { schema(child) {
|
|
1913
|
+
if (child.type === "ref") {
|
|
1914
|
+
const name = resolveRefName(child);
|
|
1915
|
+
if (name) refs.add(name);
|
|
1916
|
+
}
|
|
1917
|
+
} });
|
|
1918
|
+
return refs;
|
|
1919
|
+
});
|
|
1920
|
+
/**
|
|
1921
|
+
* Collects the names of every ref found anywhere inside a node's own subtree.
|
|
1922
|
+
*
|
|
1923
|
+
* Each ref contributes its name only, so the schema it points to is never traversed here. Pass `out`
|
|
1924
|
+
* to accumulate names from several nodes into one set.
|
|
1925
|
+
*
|
|
1926
|
+
* @example Collect refs from a single schema
|
|
1927
|
+
* ```ts
|
|
1928
|
+
* const names = collectReferencedSchemaNames(petSchema)
|
|
1929
|
+
* // Set { 'Category', 'Tag' }
|
|
1930
|
+
* ```
|
|
1931
|
+
*
|
|
1932
|
+
* @example Accumulate refs from multiple schemas into one set
|
|
1933
|
+
* ```ts
|
|
1934
|
+
* const out = new Set<string>()
|
|
1935
|
+
* for (const schema of schemas) {
|
|
1936
|
+
* collectReferencedSchemaNames(schema, out)
|
|
1937
|
+
* }
|
|
1938
|
+
* ```
|
|
1939
|
+
*/
|
|
1940
|
+
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1941
|
+
if (!node) return out;
|
|
1942
|
+
for (const name of collectSchemaRefs(node)) out.add(name);
|
|
1943
|
+
return out;
|
|
1944
|
+
}
|
|
1945
|
+
/**
|
|
1946
|
+
* Memoized two-level cache keyed first on the operations array, then on the schemas array.
|
|
1947
|
+
*/
|
|
1948
|
+
const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
|
|
1949
|
+
function computeUsedSchemaNames(operations, schemas) {
|
|
1950
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
1951
|
+
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
1952
|
+
const result = /* @__PURE__ */ new Set();
|
|
1953
|
+
function visitSchema(schema) {
|
|
1954
|
+
const directRefs = collectReferencedSchemaNames(schema);
|
|
1955
|
+
for (const name of directRefs) if (!result.has(name)) {
|
|
1956
|
+
result.add(name);
|
|
1957
|
+
const namedSchema = schemaMap.get(name);
|
|
1958
|
+
if (namedSchema) visitSchema(namedSchema);
|
|
1959
|
+
}
|
|
1960
|
+
}
|
|
1961
|
+
for (const op of operations) for (const schema of collectLazy(op, {
|
|
1962
|
+
depth: "shallow",
|
|
1963
|
+
schema: (node) => node
|
|
1964
|
+
})) visitSchema(schema);
|
|
1965
|
+
return result;
|
|
1966
|
+
}
|
|
1967
|
+
/**
|
|
1968
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
1969
|
+
*
|
|
1970
|
+
* An operation uses a schema when its parameters, request body, or responses reference it, directly
|
|
1971
|
+
* or through other named schemas. Once a name is added to the result it is not revisited, so
|
|
1972
|
+
* reference cycles terminate.
|
|
1973
|
+
*
|
|
1974
|
+
* Pair it with `include` filters so schemas reachable only from excluded operations stay ungenerated.
|
|
1975
|
+
*
|
|
1976
|
+
* @example Only generate schemas referenced by included operations
|
|
1977
|
+
* ```ts
|
|
1978
|
+
* const includedOps = operations.filter((op) => resolver.resolveOptions(op, { options, include }) !== null)
|
|
1979
|
+
* const allowed = collectUsedSchemaNames(includedOps, schemas)
|
|
1980
|
+
*
|
|
1981
|
+
* for (const schema of schemas) {
|
|
1982
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
1983
|
+
* // generate schema
|
|
1984
|
+
* }
|
|
1985
|
+
* ```
|
|
1986
|
+
*/
|
|
1987
|
+
function collectUsedSchemaNames(operations, schemas) {
|
|
1988
|
+
return collectUsedSchemaNamesMemo(operations)(schemas);
|
|
1989
|
+
}
|
|
1990
|
+
const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
|
|
1991
|
+
const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
|
|
1992
|
+
const graph = /* @__PURE__ */ new Map();
|
|
1993
|
+
for (const schema of schemas) {
|
|
1994
|
+
if (!schema.name) continue;
|
|
1995
|
+
graph.set(schema.name, collectReferencedSchemaNames(schema));
|
|
1996
|
+
}
|
|
1997
|
+
const circular = /* @__PURE__ */ new Set();
|
|
1998
|
+
for (const start of graph.keys()) {
|
|
1999
|
+
const visited = /* @__PURE__ */ new Set();
|
|
2000
|
+
const stack = [...graph.get(start) ?? []];
|
|
2001
|
+
while (stack.length > 0) {
|
|
2002
|
+
const node = stack.pop();
|
|
2003
|
+
if (node === start) {
|
|
2004
|
+
circular.add(start);
|
|
2005
|
+
break;
|
|
2006
|
+
}
|
|
2007
|
+
if (visited.has(node)) continue;
|
|
2008
|
+
visited.add(node);
|
|
2009
|
+
const next = graph.get(node);
|
|
2010
|
+
if (next) for (const r of next) stack.push(r);
|
|
2011
|
+
}
|
|
2012
|
+
}
|
|
2013
|
+
return circular;
|
|
2014
|
+
});
|
|
2015
|
+
/**
|
|
2016
|
+
* Finds every schema that takes part in a circular dependency chain, including direct self-loops.
|
|
2017
|
+
*
|
|
2018
|
+
* Wrap the returned schema positions in a deferred construct (a lazy getter or `z.lazy(() => …)`) so
|
|
2019
|
+
* the generated code does not recurse forever. Refs are followed by name only, so the walk stays
|
|
2020
|
+
* linear in the size of the schema graph.
|
|
2021
|
+
*
|
|
2022
|
+
* @note Call this once on the full graph, then check individual schemas with `containsCircularRef()`.
|
|
2023
|
+
*/
|
|
2024
|
+
function findCircularSchemas(schemas) {
|
|
2025
|
+
if (schemas.length === 0) return EMPTY_CIRCULAR_SET;
|
|
2026
|
+
return findCircularSchemasMemo(schemas);
|
|
2027
|
+
}
|
|
2028
|
+
/**
|
|
2029
|
+
* Returns `true` when a schema, or anything nested inside it, references a circular schema.
|
|
2030
|
+
*
|
|
2031
|
+
* Pass `excludeName` to skip refs to a specific schema, which helps when self-references are handled
|
|
2032
|
+
* on their own. Pair it with `findCircularSchemas()` to decide where lazy wrappers go.
|
|
2033
|
+
*
|
|
2034
|
+
* @note Stops at the first matching circular ref.
|
|
2035
|
+
*/
|
|
2036
|
+
function containsCircularRef(node, { circularSchemas, excludeName }) {
|
|
2037
|
+
if (!node || circularSchemas.size === 0) return false;
|
|
2038
|
+
for (const _ of collectLazy(node, { schema(child) {
|
|
2039
|
+
if (child.type !== "ref") return null;
|
|
2040
|
+
const name = resolveRefName(child);
|
|
2041
|
+
return name && name !== excludeName && circularSchemas.has(name) ? true : null;
|
|
2042
|
+
} })) return true;
|
|
2043
|
+
return false;
|
|
2044
|
+
}
|
|
2045
|
+
//#endregion
|
|
2046
|
+
//#region src/utils/schemaTraversal.ts
|
|
2047
|
+
/**
|
|
2048
|
+
* Maps each property of an object schema to its transformed output. Pairs every result with the
|
|
2049
|
+
* original property so the printer keeps full control over modifiers, getters, and key syntax.
|
|
2050
|
+
*
|
|
2051
|
+
* @example
|
|
2052
|
+
* ```ts
|
|
2053
|
+
* const entries = mapSchemaProperties(node, (schema) => this.transform(schema))
|
|
2054
|
+
* // entries: [{ name: 'id', property, output: 'z.number()' }, ...]
|
|
2055
|
+
* ```
|
|
2056
|
+
*/
|
|
2057
|
+
function mapSchemaProperties(node, transform) {
|
|
2058
|
+
return node.properties.map((property) => ({
|
|
2059
|
+
name: property.name,
|
|
2060
|
+
property,
|
|
2061
|
+
output: transform(property.schema)
|
|
2062
|
+
}));
|
|
2063
|
+
}
|
|
2064
|
+
/**
|
|
2065
|
+
* Maps each member of a union or intersection schema to its transformed output, pairing every
|
|
2066
|
+
* result with the original member.
|
|
2067
|
+
*/
|
|
2068
|
+
function mapSchemaMembers(node, transform) {
|
|
2069
|
+
return (node.members ?? []).map((schema) => ({
|
|
2070
|
+
schema,
|
|
2071
|
+
output: transform(schema)
|
|
2072
|
+
}));
|
|
2073
|
+
}
|
|
2074
|
+
/**
|
|
2075
|
+
* Maps each item of an array or tuple schema to its transformed output, pairing every result with
|
|
2076
|
+
* the original item.
|
|
2077
|
+
*/
|
|
2078
|
+
function mapSchemaItems(node, transform) {
|
|
2079
|
+
return (node.items ?? []).map((schema) => ({
|
|
2080
|
+
schema,
|
|
2081
|
+
output: transform(schema)
|
|
2082
|
+
}));
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
|
|
2086
|
+
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
|
|
2087
|
+
* of a recursive schema until first access.
|
|
2088
|
+
*
|
|
2089
|
+
* @example
|
|
2090
|
+
* ```ts
|
|
2091
|
+
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
|
|
2092
|
+
* // "get parent() { return z.lazy(() => Pet) }"
|
|
2093
|
+
* ```
|
|
2094
|
+
*/
|
|
2095
|
+
function lazyGetter({ name, body }) {
|
|
2096
|
+
return `get ${objectKey(name)}() { return ${body} }`;
|
|
2097
|
+
}
|
|
2098
|
+
//#endregion
|
|
2099
|
+
//#region src/macros/macroDiscriminatorEnum.ts
|
|
2100
|
+
/**
|
|
2101
|
+
* Builds a macro that replaces a discriminator property's schema with a string enum of the given
|
|
2102
|
+
* values. Object schemas that lack the property are returned unchanged.
|
|
2103
|
+
*
|
|
2104
|
+
* @example
|
|
2105
|
+
* ```ts
|
|
2106
|
+
* const macro = macroDiscriminatorEnum({ propertyName: 'type', values: ['dog', 'cat'] })
|
|
2107
|
+
* const next = applyMacros(objectSchema, [macro], { depth: 'shallow' })
|
|
2108
|
+
* ```
|
|
2109
|
+
*/
|
|
2110
|
+
function macroDiscriminatorEnum({ propertyName, values, enumName }) {
|
|
2111
|
+
return defineMacro({
|
|
2112
|
+
name: "discriminator-enum",
|
|
2113
|
+
schema(node) {
|
|
2114
|
+
const objectNode = narrowSchema(node, "object");
|
|
2115
|
+
if (!objectNode?.properties?.length) return void 0;
|
|
2116
|
+
if (!objectNode.properties.some((prop) => prop.name === propertyName)) return void 0;
|
|
2117
|
+
return createSchema({
|
|
2118
|
+
...objectNode,
|
|
2119
|
+
properties: objectNode.properties.map((prop) => {
|
|
2120
|
+
if (prop.name !== propertyName) return prop;
|
|
2121
|
+
return createProperty({
|
|
2122
|
+
...prop,
|
|
2123
|
+
schema: createSchema({
|
|
2124
|
+
type: "enum",
|
|
2125
|
+
primitive: "string",
|
|
2126
|
+
enumValues: values,
|
|
2127
|
+
name: enumName,
|
|
2128
|
+
readOnly: prop.schema.readOnly,
|
|
2129
|
+
writeOnly: prop.schema.writeOnly
|
|
2130
|
+
})
|
|
2131
|
+
});
|
|
2132
|
+
})
|
|
2133
|
+
});
|
|
2134
|
+
}
|
|
2135
|
+
});
|
|
2136
|
+
}
|
|
2137
|
+
//#endregion
|
|
2138
|
+
//#region src/macros/macroEnumName.ts
|
|
2139
|
+
/**
|
|
2140
|
+
* Builds a macro that names an inline enum schema from its parent and property name. Boolean enums
|
|
2141
|
+
* are left anonymous. Non-enum nodes are returned unchanged.
|
|
2142
|
+
*
|
|
2143
|
+
* @example
|
|
2144
|
+
* ```ts
|
|
2145
|
+
* const macro = macroEnumName({ parentName: 'Pet', propName: 'status', enumSuffix: 'enum' })
|
|
2146
|
+
* const named = applyMacros(propSchema, [macro], { depth: 'shallow' })
|
|
2147
|
+
* ```
|
|
2148
|
+
*/
|
|
2149
|
+
function macroEnumName({ parentName, propName, enumSuffix }) {
|
|
2150
|
+
return defineMacro({
|
|
2151
|
+
name: "enum-name",
|
|
2152
|
+
schema(node) {
|
|
2153
|
+
const enumNode = narrowSchema(node, "enum");
|
|
2154
|
+
if (enumNode?.primitive === "boolean") return {
|
|
2155
|
+
...node,
|
|
2156
|
+
name: null
|
|
2157
|
+
};
|
|
2158
|
+
if (enumNode) return {
|
|
2159
|
+
...node,
|
|
2160
|
+
name: enumPropName(parentName, propName, enumSuffix)
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
});
|
|
2164
|
+
}
|
|
2165
|
+
//#endregion
|
|
2166
|
+
//#region src/macros/macroSimplifyUnion.ts
|
|
2167
|
+
/**
|
|
2168
|
+
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
2169
|
+
*/
|
|
2170
|
+
const SCALAR_PRIMITIVE_TYPES = /* @__PURE__ */ new Set([
|
|
2171
|
+
"string",
|
|
2172
|
+
"number",
|
|
2173
|
+
"integer",
|
|
2174
|
+
"bigint",
|
|
2175
|
+
"boolean"
|
|
2176
|
+
]);
|
|
2177
|
+
function isScalarPrimitive(type) {
|
|
2178
|
+
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Filters union members, dropping enum members that a broader scalar primitive already covers.
|
|
2182
|
+
*/
|
|
2183
|
+
function simplifyUnionMembers(members) {
|
|
2184
|
+
const scalarPrimitives = new Set(members.filter((member) => isScalarPrimitive(member.type)).map((m) => m.type));
|
|
2185
|
+
if (!scalarPrimitives.size) return members;
|
|
2186
|
+
return members.filter((member) => {
|
|
2187
|
+
const enumNode = narrowSchema(member, "enum");
|
|
2188
|
+
if (!enumNode) return true;
|
|
2189
|
+
const primitive = enumNode.primitive;
|
|
2190
|
+
if (!primitive) return true;
|
|
2191
|
+
if ((enumNode.namedEnumValues?.length ?? enumNode.enumValues?.length ?? 0) <= 1) return true;
|
|
2192
|
+
if (scalarPrimitives.has(primitive)) return false;
|
|
2193
|
+
if ((primitive === "integer" || primitive === "number") && (scalarPrimitives.has("integer") || scalarPrimitives.has("number"))) return false;
|
|
2194
|
+
return true;
|
|
2195
|
+
});
|
|
2196
|
+
}
|
|
2197
|
+
/**
|
|
2198
|
+
* Removes union members a broader scalar primitive already covers, such as a multi-value string enum
|
|
2199
|
+
* sitting next to a plain `string`. Single-value enums are kept.
|
|
2200
|
+
*
|
|
2201
|
+
* @example
|
|
2202
|
+
* ```ts
|
|
2203
|
+
* const next = applyMacros(unionSchema, [macroSimplifyUnion], { depth: 'shallow' })
|
|
2204
|
+
* ```
|
|
2205
|
+
*/
|
|
2206
|
+
const macroSimplifyUnion = defineMacro({
|
|
2207
|
+
name: "simplify-union",
|
|
2208
|
+
schema(node) {
|
|
2209
|
+
const unionNode = narrowSchema(node, "union");
|
|
2210
|
+
if (!unionNode?.members?.length) return void 0;
|
|
2211
|
+
const simplified = simplifyUnionMembers(unionNode.members);
|
|
2212
|
+
if (simplified.length === unionNode.members.length) return void 0;
|
|
2213
|
+
return {
|
|
2214
|
+
...unionNode,
|
|
2215
|
+
members: simplified
|
|
2216
|
+
};
|
|
2217
|
+
}
|
|
2218
|
+
});
|
|
2219
|
+
//#endregion
|
|
98
2220
|
//#region src/factory.ts
|
|
99
|
-
var factory_exports = /* @__PURE__ */
|
|
100
|
-
createArrowFunction: () =>
|
|
101
|
-
createBreak: () =>
|
|
102
|
-
createConst: () =>
|
|
103
|
-
createContent: () =>
|
|
104
|
-
createExport: () =>
|
|
105
|
-
createFile: () =>
|
|
106
|
-
createFunction: () =>
|
|
107
|
-
createImport: () =>
|
|
108
|
-
createInput: () =>
|
|
109
|
-
createJsx: () =>
|
|
110
|
-
createOperation: () =>
|
|
111
|
-
createOutput: () =>
|
|
112
|
-
createParameter: () =>
|
|
113
|
-
createProperty: () =>
|
|
114
|
-
createRequestBody: () =>
|
|
115
|
-
createResponse: () =>
|
|
116
|
-
createSchema: () =>
|
|
117
|
-
createSource: () =>
|
|
118
|
-
createText: () =>
|
|
119
|
-
createType: () =>
|
|
2221
|
+
var factory_exports = /* @__PURE__ */ __exportAll({
|
|
2222
|
+
createArrowFunction: () => createArrowFunction,
|
|
2223
|
+
createBreak: () => createBreak,
|
|
2224
|
+
createConst: () => createConst,
|
|
2225
|
+
createContent: () => createContent,
|
|
2226
|
+
createExport: () => createExport,
|
|
2227
|
+
createFile: () => createFile,
|
|
2228
|
+
createFunction: () => createFunction,
|
|
2229
|
+
createImport: () => createImport,
|
|
2230
|
+
createInput: () => createInput,
|
|
2231
|
+
createJsx: () => createJsx,
|
|
2232
|
+
createOperation: () => createOperation,
|
|
2233
|
+
createOutput: () => createOutput,
|
|
2234
|
+
createParameter: () => createParameter,
|
|
2235
|
+
createProperty: () => createProperty,
|
|
2236
|
+
createRequestBody: () => createRequestBody,
|
|
2237
|
+
createResponse: () => createResponse,
|
|
2238
|
+
createSchema: () => createSchema,
|
|
2239
|
+
createSource: () => createSource,
|
|
2240
|
+
createText: () => createText,
|
|
2241
|
+
createType: () => createType,
|
|
120
2242
|
update: () => update
|
|
121
2243
|
});
|
|
122
2244
|
/**
|
|
@@ -144,89 +2266,145 @@ function update(node, changes) {
|
|
|
144
2266
|
}
|
|
145
2267
|
//#endregion
|
|
146
2268
|
//#region src/exports.ts
|
|
147
|
-
var exports_exports = /* @__PURE__ */
|
|
148
|
-
applyMacros: () =>
|
|
149
|
-
arrowFunctionDef: () =>
|
|
150
|
-
breakDef: () =>
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
2269
|
+
var exports_exports = /* @__PURE__ */ __exportAll({
|
|
2270
|
+
applyMacros: () => applyMacros,
|
|
2271
|
+
arrowFunctionDef: () => arrowFunctionDef,
|
|
2272
|
+
breakDef: () => breakDef,
|
|
2273
|
+
buildJSDoc: () => buildJSDoc,
|
|
2274
|
+
buildList: () => buildList,
|
|
2275
|
+
buildObject: () => buildObject,
|
|
2276
|
+
childName: () => childName,
|
|
2277
|
+
collect: () => collect,
|
|
2278
|
+
collectUsedSchemaNames: () => collectUsedSchemaNames,
|
|
2279
|
+
composeMacros: () => composeMacros,
|
|
2280
|
+
constDef: () => constDef,
|
|
2281
|
+
containsCircularRef: () => containsCircularRef,
|
|
2282
|
+
contentDef: () => contentDef,
|
|
155
2283
|
createPrinter: () => createPrinter,
|
|
156
2284
|
defineDialect: () => defineDialect,
|
|
157
|
-
defineMacro: () =>
|
|
158
|
-
defineNode: () =>
|
|
159
|
-
|
|
2285
|
+
defineMacro: () => defineMacro,
|
|
2286
|
+
defineNode: () => defineNode,
|
|
2287
|
+
enumPropName: () => enumPropName,
|
|
2288
|
+
exportDef: () => exportDef,
|
|
2289
|
+
extractRefName: () => extractRefName,
|
|
2290
|
+
extractStringsFromNodes: () => extractStringsFromNodes,
|
|
160
2291
|
factory: () => factory_exports,
|
|
161
|
-
fileDef: () =>
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
2292
|
+
fileDef: () => fileDef,
|
|
2293
|
+
findCircularSchemas: () => findCircularSchemas,
|
|
2294
|
+
functionDef: () => functionDef,
|
|
2295
|
+
getNestedAccessor: () => getNestedAccessor,
|
|
2296
|
+
importDef: () => importDef,
|
|
2297
|
+
inputDef: () => inputDef,
|
|
2298
|
+
isHttpOperationNode: () => isHttpOperationNode,
|
|
2299
|
+
isStringType: () => isStringType,
|
|
2300
|
+
isValidVarName: () => isValidVarName,
|
|
2301
|
+
jsStringEscape: () => jsStringEscape,
|
|
2302
|
+
jsxDef: () => jsxDef,
|
|
2303
|
+
lazyGetter: () => lazyGetter,
|
|
2304
|
+
macroDiscriminatorEnum: () => macroDiscriminatorEnum,
|
|
2305
|
+
macroEnumName: () => macroEnumName,
|
|
2306
|
+
macroSimplifyUnion: () => macroSimplifyUnion,
|
|
2307
|
+
mapSchemaItems: () => mapSchemaItems,
|
|
2308
|
+
mapSchemaMembers: () => mapSchemaMembers,
|
|
2309
|
+
mapSchemaProperties: () => mapSchemaProperties,
|
|
2310
|
+
mergeAdjacentObjectsLazy: () => mergeAdjacentObjectsLazy,
|
|
2311
|
+
narrowSchema: () => narrowSchema,
|
|
2312
|
+
nodeDefs: () => nodeDefs,
|
|
2313
|
+
objectKey: () => objectKey,
|
|
2314
|
+
operationDef: () => operationDef,
|
|
2315
|
+
optionality: () => optionality,
|
|
2316
|
+
outputDef: () => outputDef,
|
|
2317
|
+
parameterDef: () => parameterDef,
|
|
2318
|
+
propertyDef: () => propertyDef,
|
|
2319
|
+
requestBodyDef: () => requestBodyDef,
|
|
2320
|
+
responseDef: () => responseDef,
|
|
2321
|
+
schemaDef: () => schemaDef,
|
|
2322
|
+
schemaTypes: () => schemaTypes,
|
|
2323
|
+
sourceDef: () => sourceDef,
|
|
2324
|
+
stringify: () => stringify,
|
|
2325
|
+
stringifyObject: () => stringifyObject,
|
|
2326
|
+
syncSchemaRef: () => syncSchemaRef,
|
|
2327
|
+
textDef: () => textDef,
|
|
2328
|
+
toRegExpString: () => toRegExpString,
|
|
2329
|
+
transform: () => transform,
|
|
2330
|
+
trimQuotes: () => trimQuotes,
|
|
2331
|
+
typeDef: () => typeDef,
|
|
2332
|
+
walk: () => walk
|
|
2333
|
+
});
|
|
2334
|
+
//#endregion
|
|
2335
|
+
exports.applyMacros = applyMacros;
|
|
2336
|
+
exports.arrowFunctionDef = arrowFunctionDef;
|
|
187
2337
|
Object.defineProperty(exports, "ast", {
|
|
188
2338
|
enumerable: true,
|
|
189
2339
|
get: function() {
|
|
190
2340
|
return exports_exports;
|
|
191
2341
|
}
|
|
192
2342
|
});
|
|
193
|
-
exports.breakDef =
|
|
194
|
-
exports.
|
|
195
|
-
exports.
|
|
196
|
-
exports.
|
|
197
|
-
exports.
|
|
2343
|
+
exports.breakDef = breakDef;
|
|
2344
|
+
exports.buildJSDoc = buildJSDoc;
|
|
2345
|
+
exports.buildList = buildList;
|
|
2346
|
+
exports.buildObject = buildObject;
|
|
2347
|
+
exports.childName = childName;
|
|
2348
|
+
exports.collect = collect;
|
|
2349
|
+
exports.collectUsedSchemaNames = collectUsedSchemaNames;
|
|
2350
|
+
exports.composeMacros = composeMacros;
|
|
2351
|
+
exports.constDef = constDef;
|
|
2352
|
+
exports.containsCircularRef = containsCircularRef;
|
|
2353
|
+
exports.contentDef = contentDef;
|
|
198
2354
|
exports.createPrinter = createPrinter;
|
|
199
2355
|
exports.defineDialect = defineDialect;
|
|
200
|
-
exports.defineMacro =
|
|
201
|
-
exports.defineNode =
|
|
202
|
-
exports.
|
|
2356
|
+
exports.defineMacro = defineMacro;
|
|
2357
|
+
exports.defineNode = defineNode;
|
|
2358
|
+
exports.enumPropName = enumPropName;
|
|
2359
|
+
exports.exportDef = exportDef;
|
|
2360
|
+
exports.extractRefName = extractRefName;
|
|
2361
|
+
exports.extractStringsFromNodes = extractStringsFromNodes;
|
|
203
2362
|
Object.defineProperty(exports, "factory", {
|
|
204
2363
|
enumerable: true,
|
|
205
2364
|
get: function() {
|
|
206
2365
|
return factory_exports;
|
|
207
2366
|
}
|
|
208
2367
|
});
|
|
209
|
-
exports.fileDef =
|
|
210
|
-
exports.
|
|
211
|
-
exports.
|
|
212
|
-
exports.
|
|
213
|
-
exports.
|
|
214
|
-
exports.
|
|
215
|
-
exports.
|
|
216
|
-
exports.
|
|
217
|
-
exports.
|
|
218
|
-
exports.
|
|
219
|
-
exports.
|
|
220
|
-
exports.
|
|
221
|
-
exports.
|
|
222
|
-
exports.
|
|
223
|
-
exports.
|
|
224
|
-
exports.
|
|
225
|
-
exports.
|
|
226
|
-
exports.
|
|
227
|
-
exports.
|
|
228
|
-
exports.
|
|
229
|
-
exports.
|
|
230
|
-
exports.
|
|
2368
|
+
exports.fileDef = fileDef;
|
|
2369
|
+
exports.findCircularSchemas = findCircularSchemas;
|
|
2370
|
+
exports.functionDef = functionDef;
|
|
2371
|
+
exports.getNestedAccessor = getNestedAccessor;
|
|
2372
|
+
exports.importDef = importDef;
|
|
2373
|
+
exports.inputDef = inputDef;
|
|
2374
|
+
exports.isHttpOperationNode = isHttpOperationNode;
|
|
2375
|
+
exports.isStringType = isStringType;
|
|
2376
|
+
exports.isValidVarName = isValidVarName;
|
|
2377
|
+
exports.jsStringEscape = jsStringEscape;
|
|
2378
|
+
exports.jsxDef = jsxDef;
|
|
2379
|
+
exports.lazyGetter = lazyGetter;
|
|
2380
|
+
exports.macroDiscriminatorEnum = macroDiscriminatorEnum;
|
|
2381
|
+
exports.macroEnumName = macroEnumName;
|
|
2382
|
+
exports.macroSimplifyUnion = macroSimplifyUnion;
|
|
2383
|
+
exports.mapSchemaItems = mapSchemaItems;
|
|
2384
|
+
exports.mapSchemaMembers = mapSchemaMembers;
|
|
2385
|
+
exports.mapSchemaProperties = mapSchemaProperties;
|
|
2386
|
+
exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
|
|
2387
|
+
exports.narrowSchema = narrowSchema;
|
|
2388
|
+
exports.nodeDefs = nodeDefs;
|
|
2389
|
+
exports.objectKey = objectKey;
|
|
2390
|
+
exports.operationDef = operationDef;
|
|
2391
|
+
exports.optionality = optionality;
|
|
2392
|
+
exports.outputDef = outputDef;
|
|
2393
|
+
exports.parameterDef = parameterDef;
|
|
2394
|
+
exports.propertyDef = propertyDef;
|
|
2395
|
+
exports.requestBodyDef = requestBodyDef;
|
|
2396
|
+
exports.responseDef = responseDef;
|
|
2397
|
+
exports.schemaDef = schemaDef;
|
|
2398
|
+
exports.schemaTypes = schemaTypes;
|
|
2399
|
+
exports.sourceDef = sourceDef;
|
|
2400
|
+
exports.stringify = stringify;
|
|
2401
|
+
exports.stringifyObject = stringifyObject;
|
|
2402
|
+
exports.syncSchemaRef = syncSchemaRef;
|
|
2403
|
+
exports.textDef = textDef;
|
|
2404
|
+
exports.toRegExpString = toRegExpString;
|
|
2405
|
+
exports.transform = transform;
|
|
2406
|
+
exports.trimQuotes = trimQuotes;
|
|
2407
|
+
exports.typeDef = typeDef;
|
|
2408
|
+
exports.walk = walk;
|
|
231
2409
|
|
|
232
2410
|
//# sourceMappingURL=index.cjs.map
|