@kubb/ast 5.0.0-beta.43 → 5.0.0-beta.45
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 +15 -467
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -18
- package/dist/index.js +3 -426
- package/dist/index.js.map +1 -1
- package/dist/{types-CC46hQUP.d.ts → types-BaaNZbSi.d.ts} +5 -56
- package/dist/types.d.ts +2 -2
- package/dist/utils-BIcKgbbc.js +626 -0
- package/dist/utils-BIcKgbbc.js.map +1 -0
- package/dist/utils-CMRZrT-w.cjs +794 -0
- package/dist/utils-CMRZrT-w.cjs.map +1 -0
- package/dist/utils.cjs +18 -0
- package/dist/utils.d.ts +205 -0
- package/dist/utils.js +2 -0
- package/package.json +5 -1
- package/src/factory.ts +1 -1
- package/src/index.ts +2 -6
- package/src/infer.ts +2 -2
- package/src/nodes/code.ts +0 -6
- package/src/nodes/file.ts +1 -1
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +4 -10
- package/src/nodes/operation.ts +2 -2
- package/src/nodes/schema.ts +6 -6
- package/src/printer.ts +2 -2
- package/src/resolvers.ts +1 -15
- package/src/signature.ts +1 -1
- package/src/transformers.ts +1 -1
- package/src/types.ts +3 -20
- package/src/{utils.ts → utils/ast.ts} +7 -70
- package/src/utils/index.ts +295 -0
- package/src/visitor.ts +2 -2
- package/src/refs.ts +0 -13
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
import "./chunk-C0LytTxp.js";
|
|
2
|
+
//#region src/constants.ts
|
|
3
|
+
const visitorDepths = {
|
|
4
|
+
shallow: "shallow",
|
|
5
|
+
deep: "deep"
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
9
|
+
*
|
|
10
|
+
* These values serve as stable discriminators across the AST (e.g., `schema.type === schemaTypes.object`).
|
|
11
|
+
* Grouped by category: primitives (`string`, `number`, `boolean`), structural types (`object`, `array`, `union`),
|
|
12
|
+
* and format-specific types (`date`, `uuid`, `email`). Use `isScalarPrimitive()` to check for scalar types.
|
|
13
|
+
*/
|
|
14
|
+
const schemaTypes = {
|
|
15
|
+
/**
|
|
16
|
+
* Text value.
|
|
17
|
+
*/
|
|
18
|
+
string: "string",
|
|
19
|
+
/**
|
|
20
|
+
* Floating-point number (`float`, `double`).
|
|
21
|
+
*/
|
|
22
|
+
number: "number",
|
|
23
|
+
/**
|
|
24
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
25
|
+
*/
|
|
26
|
+
integer: "integer",
|
|
27
|
+
/**
|
|
28
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
29
|
+
*/
|
|
30
|
+
bigint: "bigint",
|
|
31
|
+
/**
|
|
32
|
+
* Boolean value
|
|
33
|
+
*/
|
|
34
|
+
boolean: "boolean",
|
|
35
|
+
/**
|
|
36
|
+
* Explicit null value.
|
|
37
|
+
*/
|
|
38
|
+
null: "null",
|
|
39
|
+
/**
|
|
40
|
+
* Any value (no type restriction).
|
|
41
|
+
*/
|
|
42
|
+
any: "any",
|
|
43
|
+
/**
|
|
44
|
+
* Unknown value (must be narrowed before usage).
|
|
45
|
+
*/
|
|
46
|
+
unknown: "unknown",
|
|
47
|
+
/**
|
|
48
|
+
* No return value (`void`).
|
|
49
|
+
*/
|
|
50
|
+
void: "void",
|
|
51
|
+
/**
|
|
52
|
+
* Object with named properties.
|
|
53
|
+
*/
|
|
54
|
+
object: "object",
|
|
55
|
+
/**
|
|
56
|
+
* Sequential list of items.
|
|
57
|
+
*/
|
|
58
|
+
array: "array",
|
|
59
|
+
/**
|
|
60
|
+
* Fixed-length list with position-specific items.
|
|
61
|
+
*/
|
|
62
|
+
tuple: "tuple",
|
|
63
|
+
/**
|
|
64
|
+
* "One of" multiple schema members.
|
|
65
|
+
*/
|
|
66
|
+
union: "union",
|
|
67
|
+
/**
|
|
68
|
+
* "All of" multiple schema members.
|
|
69
|
+
*/
|
|
70
|
+
intersection: "intersection",
|
|
71
|
+
/**
|
|
72
|
+
* Enum schema.
|
|
73
|
+
*/
|
|
74
|
+
enum: "enum",
|
|
75
|
+
/**
|
|
76
|
+
* Reference to another schema.
|
|
77
|
+
*/
|
|
78
|
+
ref: "ref",
|
|
79
|
+
/**
|
|
80
|
+
* Calendar date (for example `2026-03-24`).
|
|
81
|
+
*/
|
|
82
|
+
date: "date",
|
|
83
|
+
/**
|
|
84
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
85
|
+
*/
|
|
86
|
+
datetime: "datetime",
|
|
87
|
+
/**
|
|
88
|
+
* Time-only value (for example `09:00:00`).
|
|
89
|
+
*/
|
|
90
|
+
time: "time",
|
|
91
|
+
/**
|
|
92
|
+
* UUID value.
|
|
93
|
+
*/
|
|
94
|
+
uuid: "uuid",
|
|
95
|
+
/**
|
|
96
|
+
* Email address value.
|
|
97
|
+
*/
|
|
98
|
+
email: "email",
|
|
99
|
+
/**
|
|
100
|
+
* URL value.
|
|
101
|
+
*/
|
|
102
|
+
url: "url",
|
|
103
|
+
/**
|
|
104
|
+
* IPv4 address value.
|
|
105
|
+
*/
|
|
106
|
+
ipv4: "ipv4",
|
|
107
|
+
/**
|
|
108
|
+
* IPv6 address value.
|
|
109
|
+
*/
|
|
110
|
+
ipv6: "ipv6",
|
|
111
|
+
/**
|
|
112
|
+
* Binary/blob value.
|
|
113
|
+
*/
|
|
114
|
+
blob: "blob",
|
|
115
|
+
/**
|
|
116
|
+
* Impossible value (`never`).
|
|
117
|
+
*/
|
|
118
|
+
never: "never"
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Scalar primitive schema types used for union simplification and type narrowing.
|
|
122
|
+
*
|
|
123
|
+
* Use `isScalarPrimitive()` to safely check whether a type is a scalar primitive.
|
|
124
|
+
*/
|
|
125
|
+
const SCALAR_PRIMITIVE_TYPES = new Set([
|
|
126
|
+
"string",
|
|
127
|
+
"number",
|
|
128
|
+
"integer",
|
|
129
|
+
"bigint",
|
|
130
|
+
"boolean"
|
|
131
|
+
]);
|
|
132
|
+
/**
|
|
133
|
+
* Type guard that returns `true` when `type` is a scalar primitive schema type.
|
|
134
|
+
*
|
|
135
|
+
* Use this to check if a schema type can be directly assigned without wrapping (e.g., `string | number | boolean`).
|
|
136
|
+
*/
|
|
137
|
+
function isScalarPrimitive(type) {
|
|
138
|
+
return SCALAR_PRIMITIVE_TYPES.has(type);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* HTTP method identifiers used by operation nodes.
|
|
142
|
+
*
|
|
143
|
+
* Includes all standard HTTP methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS, TRACE).
|
|
144
|
+
*/
|
|
145
|
+
const httpMethods = {
|
|
146
|
+
get: "GET",
|
|
147
|
+
post: "POST",
|
|
148
|
+
put: "PUT",
|
|
149
|
+
patch: "PATCH",
|
|
150
|
+
delete: "DELETE",
|
|
151
|
+
head: "HEAD",
|
|
152
|
+
options: "OPTIONS",
|
|
153
|
+
trace: "TRACE"
|
|
154
|
+
};
|
|
155
|
+
/**
|
|
156
|
+
* One indentation level, derived from {@link INDENT_SIZE}.
|
|
157
|
+
*/
|
|
158
|
+
const INDENT = Array.from({ length: 2 }, () => " ").join("");
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region ../../internals/utils/src/casing.ts
|
|
161
|
+
/**
|
|
162
|
+
* Shared implementation for camelCase and PascalCase conversion.
|
|
163
|
+
* Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
|
|
164
|
+
* and capitalizes each word according to `pascal`.
|
|
165
|
+
*
|
|
166
|
+
* When `pascal` is `true` the first word is also capitalized (PascalCase), otherwise only subsequent words are.
|
|
167
|
+
*/
|
|
168
|
+
function toCamelOrPascal(text, pascal) {
|
|
169
|
+
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) => {
|
|
170
|
+
if (word.length > 1 && word === word.toUpperCase()) return word;
|
|
171
|
+
if (i === 0 && !pascal) return word.charAt(0).toLowerCase() + word.slice(1);
|
|
172
|
+
return word.charAt(0).toUpperCase() + word.slice(1);
|
|
173
|
+
}).join("").replace(/[^a-zA-Z0-9]/g, "");
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Splits `text` on `.` and applies `transformPart` to each segment.
|
|
177
|
+
* The last segment receives `isLast = true`, all earlier segments receive `false`.
|
|
178
|
+
* Segments are joined with `/` to form a file path.
|
|
179
|
+
*
|
|
180
|
+
* Only splits on dots followed by a letter so that version numbers
|
|
181
|
+
* embedded in operationIds (e.g. `v2025.0`) are kept intact.
|
|
182
|
+
*
|
|
183
|
+
* Empty segments are filtered before joining. They arise when the text starts with
|
|
184
|
+
* a dot followed immediately by a letter (e.g. `..Schema` splits into `['..', 'Schema']`
|
|
185
|
+
* and `'..'` transforms to an empty string). Without this filter the join would produce
|
|
186
|
+
* a leading `/`, which `path.resolve` would interpret as an absolute path, allowing
|
|
187
|
+
* generated files to escape the configured output directory.
|
|
188
|
+
*/
|
|
189
|
+
function applyToFileParts(text, transformPart) {
|
|
190
|
+
const parts = text.split(/\.(?=[a-zA-Z])/);
|
|
191
|
+
return parts.map((part, i) => transformPart(part, i === parts.length - 1)).filter(Boolean).join("/");
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Converts `text` to camelCase.
|
|
195
|
+
* When `isFile` is `true`, dot-separated segments are each cased independently and joined with `/`.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* camelCase('hello-world') // 'helloWorld'
|
|
199
|
+
* camelCase('pet.petId', { isFile: true }) // 'pet/petId'
|
|
200
|
+
*/
|
|
201
|
+
function camelCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
202
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => camelCase(part, isLast ? {
|
|
203
|
+
prefix,
|
|
204
|
+
suffix
|
|
205
|
+
} : {}));
|
|
206
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, false);
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* Converts `text` to PascalCase.
|
|
210
|
+
* When `isFile` is `true`, the last dot-separated segment is PascalCased and earlier segments are camelCased.
|
|
211
|
+
*
|
|
212
|
+
* @example
|
|
213
|
+
* pascalCase('hello-world') // 'HelloWorld'
|
|
214
|
+
* pascalCase('pet.petId', { isFile: true }) // 'pet/PetId'
|
|
215
|
+
*/
|
|
216
|
+
function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
|
|
217
|
+
if (isFile) return applyToFileParts(text, (part, isLast) => isLast ? pascalCase(part, {
|
|
218
|
+
prefix,
|
|
219
|
+
suffix
|
|
220
|
+
}) : camelCase(part));
|
|
221
|
+
return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region ../../internals/utils/src/reserved.ts
|
|
225
|
+
/**
|
|
226
|
+
* JavaScript and Java reserved words.
|
|
227
|
+
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
228
|
+
*/
|
|
229
|
+
const reservedWords = new Set([
|
|
230
|
+
"abstract",
|
|
231
|
+
"arguments",
|
|
232
|
+
"boolean",
|
|
233
|
+
"break",
|
|
234
|
+
"byte",
|
|
235
|
+
"case",
|
|
236
|
+
"catch",
|
|
237
|
+
"char",
|
|
238
|
+
"class",
|
|
239
|
+
"const",
|
|
240
|
+
"continue",
|
|
241
|
+
"debugger",
|
|
242
|
+
"default",
|
|
243
|
+
"delete",
|
|
244
|
+
"do",
|
|
245
|
+
"double",
|
|
246
|
+
"else",
|
|
247
|
+
"enum",
|
|
248
|
+
"eval",
|
|
249
|
+
"export",
|
|
250
|
+
"extends",
|
|
251
|
+
"false",
|
|
252
|
+
"final",
|
|
253
|
+
"finally",
|
|
254
|
+
"float",
|
|
255
|
+
"for",
|
|
256
|
+
"function",
|
|
257
|
+
"goto",
|
|
258
|
+
"if",
|
|
259
|
+
"implements",
|
|
260
|
+
"import",
|
|
261
|
+
"in",
|
|
262
|
+
"instanceof",
|
|
263
|
+
"int",
|
|
264
|
+
"interface",
|
|
265
|
+
"let",
|
|
266
|
+
"long",
|
|
267
|
+
"native",
|
|
268
|
+
"new",
|
|
269
|
+
"null",
|
|
270
|
+
"package",
|
|
271
|
+
"private",
|
|
272
|
+
"protected",
|
|
273
|
+
"public",
|
|
274
|
+
"return",
|
|
275
|
+
"short",
|
|
276
|
+
"static",
|
|
277
|
+
"super",
|
|
278
|
+
"switch",
|
|
279
|
+
"synchronized",
|
|
280
|
+
"this",
|
|
281
|
+
"throw",
|
|
282
|
+
"throws",
|
|
283
|
+
"transient",
|
|
284
|
+
"true",
|
|
285
|
+
"try",
|
|
286
|
+
"typeof",
|
|
287
|
+
"var",
|
|
288
|
+
"void",
|
|
289
|
+
"volatile",
|
|
290
|
+
"while",
|
|
291
|
+
"with",
|
|
292
|
+
"yield",
|
|
293
|
+
"Array",
|
|
294
|
+
"Date",
|
|
295
|
+
"hasOwnProperty",
|
|
296
|
+
"Infinity",
|
|
297
|
+
"isFinite",
|
|
298
|
+
"isNaN",
|
|
299
|
+
"isPrototypeOf",
|
|
300
|
+
"length",
|
|
301
|
+
"Math",
|
|
302
|
+
"name",
|
|
303
|
+
"NaN",
|
|
304
|
+
"Number",
|
|
305
|
+
"Object",
|
|
306
|
+
"prototype",
|
|
307
|
+
"String",
|
|
308
|
+
"toString",
|
|
309
|
+
"undefined",
|
|
310
|
+
"valueOf"
|
|
311
|
+
]);
|
|
312
|
+
/**
|
|
313
|
+
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
314
|
+
*
|
|
315
|
+
* @example
|
|
316
|
+
* ```ts
|
|
317
|
+
* isValidVarName('status') // true
|
|
318
|
+
* isValidVarName('class') // false (reserved word)
|
|
319
|
+
* isValidVarName('42foo') // false (starts with digit)
|
|
320
|
+
* ```
|
|
321
|
+
*/
|
|
322
|
+
function isValidVarName(name) {
|
|
323
|
+
if (!name || reservedWords.has(name)) return false;
|
|
324
|
+
return isIdentifier(name);
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns `name` when it is already a valid JavaScript variable name, otherwise prefixes it with `_`
|
|
328
|
+
* so the result can be used as an identifier. Useful for sanitizing schema names or operation IDs
|
|
329
|
+
* that start with a digit (`409`, `504AccountCancel`) or collide with a reserved word.
|
|
330
|
+
*
|
|
331
|
+
* @example
|
|
332
|
+
* ```ts
|
|
333
|
+
* ensureValidVarName('409') // '_409'
|
|
334
|
+
* ensureValidVarName('Pet') // 'Pet'
|
|
335
|
+
* ensureValidVarName('class') // '_class'
|
|
336
|
+
* ```
|
|
337
|
+
*/
|
|
338
|
+
function ensureValidVarName(name) {
|
|
339
|
+
if (!name || isValidVarName(name)) return name;
|
|
340
|
+
return `_${name}`;
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
344
|
+
*
|
|
345
|
+
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
346
|
+
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
347
|
+
* deciding whether an object key needs quoting.
|
|
348
|
+
*
|
|
349
|
+
* @example
|
|
350
|
+
* ```ts
|
|
351
|
+
* isIdentifier('name') // true
|
|
352
|
+
* isIdentifier('x-total')// false
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
355
|
+
function isIdentifier(name) {
|
|
356
|
+
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
357
|
+
}
|
|
358
|
+
//#endregion
|
|
359
|
+
//#region ../../internals/utils/src/string.ts
|
|
360
|
+
/**
|
|
361
|
+
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
|
|
362
|
+
* any backslash or single quote in the content.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* ```ts
|
|
366
|
+
* singleQuote('foo') // "'foo'"
|
|
367
|
+
* singleQuote("o'clock") // "'o\\'clock'"
|
|
368
|
+
* ```
|
|
369
|
+
*/
|
|
370
|
+
function singleQuote(value) {
|
|
371
|
+
if (value === void 0 || value === null) return "''";
|
|
372
|
+
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
373
|
+
}
|
|
374
|
+
/**
|
|
375
|
+
* Strips the file extension from a path or file name.
|
|
376
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
377
|
+
*
|
|
378
|
+
* @example
|
|
379
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
380
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
381
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
382
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
383
|
+
*/
|
|
384
|
+
function trimExtName(text) {
|
|
385
|
+
const dotIndex = text.lastIndexOf(".");
|
|
386
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
387
|
+
return text;
|
|
388
|
+
}
|
|
389
|
+
//#endregion
|
|
390
|
+
//#region src/utils/index.ts
|
|
391
|
+
/**
|
|
392
|
+
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
393
|
+
* Returns the string unchanged when no balanced quote pair is found.
|
|
394
|
+
*
|
|
395
|
+
* @example
|
|
396
|
+
* ```ts
|
|
397
|
+
* trimQuotes('"hello"') // 'hello'
|
|
398
|
+
* trimQuotes('hello') // 'hello'
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
function trimQuotes(text) {
|
|
402
|
+
if (text.length >= 2) {
|
|
403
|
+
const first = text[0];
|
|
404
|
+
const last = text[text.length - 1];
|
|
405
|
+
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
|
|
406
|
+
}
|
|
407
|
+
return text;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Serializes a primitive value to a single-quoted string literal, stripping any surrounding quote
|
|
411
|
+
* characters first. Escaping comes from `JSON.stringify`, then the quote style switches to single
|
|
412
|
+
* quotes so generated code matches the repo style without a formatter.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```ts
|
|
416
|
+
* stringify('hello') // "'hello'"
|
|
417
|
+
* stringify('"hello"') // "'hello'"
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
function stringify(value) {
|
|
421
|
+
if (value === void 0 || value === null) return "''";
|
|
422
|
+
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
|
|
426
|
+
* and the Unicode line terminators U+2028 and U+2029.
|
|
427
|
+
*
|
|
428
|
+
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
429
|
+
*
|
|
430
|
+
* @example
|
|
431
|
+
* ```ts
|
|
432
|
+
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
|
|
433
|
+
* ```
|
|
434
|
+
*/
|
|
435
|
+
function jsStringEscape(input) {
|
|
436
|
+
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
|
|
437
|
+
switch (character) {
|
|
438
|
+
case "\"":
|
|
439
|
+
case "'":
|
|
440
|
+
case "\\": return `\\${character}`;
|
|
441
|
+
case "\n": return "\\n";
|
|
442
|
+
case "\r": return "\\r";
|
|
443
|
+
case "\u2028": return "\\u2028";
|
|
444
|
+
case "\u2029": return "\\u2029";
|
|
445
|
+
default: return "";
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
451
|
+
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
|
|
452
|
+
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
453
|
+
*
|
|
454
|
+
* @example
|
|
455
|
+
* ```ts
|
|
456
|
+
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
|
|
457
|
+
* toRegExpString('^(?im)foo', null) // '/^foo/im'
|
|
458
|
+
* ```
|
|
459
|
+
*/
|
|
460
|
+
function toRegExpString(text, func = "RegExp") {
|
|
461
|
+
const raw = trimQuotes(text);
|
|
462
|
+
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
|
|
463
|
+
const replacementTarget = match?.[1] ?? "";
|
|
464
|
+
const matchedFlags = match?.[2];
|
|
465
|
+
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
|
|
466
|
+
const { source, flags } = new RegExp(cleaned, matchedFlags);
|
|
467
|
+
if (func === null) return `/${source}/${flags}`;
|
|
468
|
+
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
|
|
472
|
+
* objects recurse with fixed indentation, so the result drops straight into an object literal
|
|
473
|
+
* without re-parsing.
|
|
474
|
+
*
|
|
475
|
+
* @example
|
|
476
|
+
* ```ts
|
|
477
|
+
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
|
|
478
|
+
* // 'foo: bar,\nnested: {\n a: 1\n }'
|
|
479
|
+
* ```
|
|
480
|
+
*/
|
|
481
|
+
function stringifyObject(value) {
|
|
482
|
+
return Object.entries(value).map(([key, val]) => {
|
|
483
|
+
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
|
|
484
|
+
return `${key}: ${val}`;
|
|
485
|
+
}).filter(Boolean).join(",\n");
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
|
|
489
|
+
* `accessor`. Returns `null` for an empty path.
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* ```ts
|
|
493
|
+
* getNestedAccessor('pagination.next.id', 'lastPage')
|
|
494
|
+
* // "lastPage?.['pagination']?.['next']?.['id']"
|
|
495
|
+
* ```
|
|
496
|
+
*/
|
|
497
|
+
function getNestedAccessor(param, accessor) {
|
|
498
|
+
const parts = Array.isArray(param) ? param : param.split(".");
|
|
499
|
+
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
|
|
500
|
+
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* Builds a JSDoc comment block from an array of lines, returning `fallback` when there are no
|
|
504
|
+
* comments so callers always get a usable string.
|
|
505
|
+
*
|
|
506
|
+
* @example
|
|
507
|
+
* ```ts
|
|
508
|
+
* buildJSDoc(['@type string', '@example hello'])
|
|
509
|
+
* // '/**\n * @type string\n * @example hello\n *\/\n '
|
|
510
|
+
* ```
|
|
511
|
+
*/
|
|
512
|
+
function buildJSDoc(comments, options = {}) {
|
|
513
|
+
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
|
|
514
|
+
if (comments.length === 0) return fallback;
|
|
515
|
+
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
|
|
519
|
+
*/
|
|
520
|
+
function indentLines(text) {
|
|
521
|
+
if (!text) return "";
|
|
522
|
+
return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
|
|
526
|
+
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
|
|
527
|
+
*
|
|
528
|
+
* @example
|
|
529
|
+
* ```ts
|
|
530
|
+
* objectKey('name') // 'name'
|
|
531
|
+
* objectKey('x-total') // "'x-total'"
|
|
532
|
+
* ```
|
|
533
|
+
*/
|
|
534
|
+
function objectKey(name) {
|
|
535
|
+
return isIdentifier(name) ? name : singleQuote(name);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
|
|
539
|
+
* level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
|
|
540
|
+
* so callers never re-parse the generated code. A trailing comma is added per entry to match the
|
|
541
|
+
* formatter's multi-line style.
|
|
542
|
+
*
|
|
543
|
+
* @example
|
|
544
|
+
* ```ts
|
|
545
|
+
* buildObject(['id: z.number()', 'name: z.string()'])
|
|
546
|
+
* // '{\n id: z.number(),\n name: z.string(),\n}'
|
|
547
|
+
* ```
|
|
548
|
+
*/
|
|
549
|
+
function buildObject(entries) {
|
|
550
|
+
if (entries.length === 0) return "{}";
|
|
551
|
+
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
|
|
555
|
+
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
|
|
556
|
+
* one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
|
|
557
|
+
* `z.array([…])`, and similar member lists so objects inside them nest correctly.
|
|
558
|
+
*
|
|
559
|
+
* @example
|
|
560
|
+
* ```ts
|
|
561
|
+
* buildList(['z.string()', 'z.number()'])
|
|
562
|
+
* // '[z.string(), z.number()]'
|
|
563
|
+
* ```
|
|
564
|
+
*/
|
|
565
|
+
function buildList(items, brackets = ["[", "]"]) {
|
|
566
|
+
const [open, close] = brackets;
|
|
567
|
+
if (items.length === 0) return `${open}${close}`;
|
|
568
|
+
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
|
|
569
|
+
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
|
|
570
|
+
}
|
|
571
|
+
/**
|
|
572
|
+
* Returns the last path segment of a reference string.
|
|
573
|
+
*
|
|
574
|
+
* @example
|
|
575
|
+
* ```ts
|
|
576
|
+
* extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
577
|
+
* ```
|
|
578
|
+
*/
|
|
579
|
+
function extractRefName(ref) {
|
|
580
|
+
return ref.split("/").at(-1) ?? ref;
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Builds a PascalCase child schema name by joining a parent name and property name.
|
|
584
|
+
* Returns `null` when there is no parent to nest under.
|
|
585
|
+
*
|
|
586
|
+
* @example
|
|
587
|
+
* ```ts
|
|
588
|
+
* childName('Order', 'shipping_address') // 'OrderShippingAddress'
|
|
589
|
+
* childName(undefined, 'params') // null
|
|
590
|
+
* ```
|
|
591
|
+
*/
|
|
592
|
+
function childName(parentName, propName) {
|
|
593
|
+
return parentName ? pascalCase([parentName, propName].join(" ")) : null;
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Builds a PascalCase enum name from the parent name, property name, and a suffix, skipping any
|
|
597
|
+
* empty parts.
|
|
598
|
+
*
|
|
599
|
+
* @example
|
|
600
|
+
* ```ts
|
|
601
|
+
* enumPropName('Order', 'status', 'enum') // 'OrderStatusEnum'
|
|
602
|
+
* ```
|
|
603
|
+
*/
|
|
604
|
+
function enumPropName(parentName, propName, enumSuffix) {
|
|
605
|
+
return pascalCase([
|
|
606
|
+
parentName,
|
|
607
|
+
propName,
|
|
608
|
+
enumSuffix
|
|
609
|
+
].filter(Boolean).join(" "));
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Returns the discriminator key whose mapping value matches `ref`, or `null` when there is no match.
|
|
613
|
+
*
|
|
614
|
+
* @example
|
|
615
|
+
* ```ts
|
|
616
|
+
* findDiscriminator({ dog: '#/components/schemas/Dog' }, '#/components/schemas/Dog') // 'dog'
|
|
617
|
+
* ```
|
|
618
|
+
*/
|
|
619
|
+
function findDiscriminator(mapping, ref) {
|
|
620
|
+
if (!mapping || !ref) return null;
|
|
621
|
+
return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
|
|
622
|
+
}
|
|
623
|
+
//#endregion
|
|
624
|
+
export { visitorDepths as S, isValidVarName as _, enumPropName as a, isScalarPrimitive as b, getNestedAccessor as c, stringify as d, stringifyObject as f, ensureValidVarName as g, trimExtName as h, childName as i, jsStringEscape as l, trimQuotes as m, buildList as n, extractRefName as o, toRegExpString as p, buildObject as r, findDiscriminator as s, buildJSDoc as t, objectKey as u, camelCase as v, schemaTypes as x, httpMethods as y };
|
|
625
|
+
|
|
626
|
+
//# sourceMappingURL=utils-BIcKgbbc.js.map
|