@kubb/ast 5.0.0-beta.85 → 5.0.0-beta.86
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 +0 -357
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +2 -171
- package/dist/index.js +1 -346
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -31,102 +31,6 @@ declare function isHttpOperationNode(node: OperationNode): node is HttpOperation
|
|
|
31
31
|
*/
|
|
32
32
|
declare function optionality(schema: SchemaNode, required: boolean): SchemaNode;
|
|
33
33
|
//#endregion
|
|
34
|
-
//#region ../../internals/utils/src/reserved.d.ts
|
|
35
|
-
/**
|
|
36
|
-
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* isValidVarName('status') // true
|
|
41
|
-
* isValidVarName('class') // false (reserved word)
|
|
42
|
-
* isValidVarName('42foo') // false (starts with digit)
|
|
43
|
-
* ```
|
|
44
|
-
*/
|
|
45
|
-
declare function isValidVarName(name: string): boolean;
|
|
46
|
-
//#endregion
|
|
47
|
-
//#region src/utils/codegen.d.ts
|
|
48
|
-
/**
|
|
49
|
-
* Builds a JSDoc comment block from an array of lines. Returns `fallback` when there are no
|
|
50
|
-
* comments.
|
|
51
|
-
*
|
|
52
|
-
* @example
|
|
53
|
-
* ```ts
|
|
54
|
-
* buildJSDoc(['@type string', '@example hello'])
|
|
55
|
-
* // '/**\n * @type string\n * @example hello\n *\/\n '
|
|
56
|
-
* ```
|
|
57
|
-
*/
|
|
58
|
-
declare function buildJSDoc(comments: Array<string>, options?: {
|
|
59
|
-
/**
|
|
60
|
-
* String used to indent each comment line.
|
|
61
|
-
* @default ' * '
|
|
62
|
-
*/
|
|
63
|
-
indent?: string;
|
|
64
|
-
/**
|
|
65
|
-
* String appended after the closing tag.
|
|
66
|
-
* @default '\n '
|
|
67
|
-
*/
|
|
68
|
-
suffix?: string;
|
|
69
|
-
/**
|
|
70
|
-
* Returned as-is when `comments` is empty.
|
|
71
|
-
* @default ' '
|
|
72
|
-
*/
|
|
73
|
-
fallback?: string;
|
|
74
|
-
}): string;
|
|
75
|
-
/**
|
|
76
|
-
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
|
|
77
|
-
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
|
|
78
|
-
*
|
|
79
|
-
* @example
|
|
80
|
-
* ```ts
|
|
81
|
-
* objectKey('name') // 'name'
|
|
82
|
-
* objectKey('x-total') // "'x-total'"
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
declare function objectKey(name: string): string;
|
|
86
|
-
/**
|
|
87
|
-
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
|
|
88
|
-
* level and closing the brace at column zero. Entries that are themselves multi-line objects indent
|
|
89
|
-
* cumulatively. Each entry ends with a trailing comma to match the formatter's multi-line style.
|
|
90
|
-
*
|
|
91
|
-
* @example
|
|
92
|
-
* ```ts
|
|
93
|
-
* buildObject(['id: z.number()', 'name: z.string()'])
|
|
94
|
-
* // '{\n id: z.number(),\n name: z.string(),\n}'
|
|
95
|
-
* ```
|
|
96
|
-
*/
|
|
97
|
-
declare function buildObject(entries: Array<string>): string;
|
|
98
|
-
/**
|
|
99
|
-
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
|
|
100
|
-
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
|
|
101
|
-
* one level with a trailing comma and the closing bracket at column zero. Used for member lists such
|
|
102
|
-
* as `z.union([…])` and `z.array([…])`.
|
|
103
|
-
*
|
|
104
|
-
* @example
|
|
105
|
-
* ```ts
|
|
106
|
-
* buildList(['z.string()', 'z.number()'])
|
|
107
|
-
* // '[z.string(), z.number()]'
|
|
108
|
-
* ```
|
|
109
|
-
*/
|
|
110
|
-
declare function buildList(items: Array<string>, brackets?: [open: string, close: string]): string;
|
|
111
|
-
/**
|
|
112
|
-
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
|
|
113
|
-
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
|
|
114
|
-
* of a recursive schema until first access.
|
|
115
|
-
*
|
|
116
|
-
* @example
|
|
117
|
-
* ```ts
|
|
118
|
-
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
|
|
119
|
-
* // "get parent() { return z.lazy(() => Pet) }"
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
declare function lazyGetter({
|
|
123
|
-
name,
|
|
124
|
-
body
|
|
125
|
-
}: {
|
|
126
|
-
name: string;
|
|
127
|
-
body: string;
|
|
128
|
-
}): string;
|
|
129
|
-
//#endregion
|
|
130
34
|
//#region src/utils/combineFileMembers.d.ts
|
|
131
35
|
/**
|
|
132
36
|
* Deduplicates `SourceNode` objects by `name + isExportable + isTypeOnly`, keeping the first of each
|
|
@@ -328,79 +232,6 @@ declare function mapSchemaMembers<TOutput>(node: UnionSchemaNode | IntersectionS
|
|
|
328
232
|
*/
|
|
329
233
|
declare function mapSchemaItems<TOutput>(node: ArraySchemaNode, transform: SchemaTransform<TOutput>): Array<MappedSchema<TOutput>>;
|
|
330
234
|
//#endregion
|
|
331
|
-
//#region src/utils/strings.d.ts
|
|
332
|
-
/**
|
|
333
|
-
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
334
|
-
* Returns the string unchanged when no balanced quote pair is found.
|
|
335
|
-
*
|
|
336
|
-
* @example
|
|
337
|
-
* ```ts
|
|
338
|
-
* trimQuotes('"hello"') // 'hello'
|
|
339
|
-
* trimQuotes('hello') // 'hello'
|
|
340
|
-
* ```
|
|
341
|
-
*/
|
|
342
|
-
declare function trimQuotes(text: string): string;
|
|
343
|
-
/**
|
|
344
|
-
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
|
|
345
|
-
*
|
|
346
|
-
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
|
|
347
|
-
* code matches the repo style without a formatter.
|
|
348
|
-
*
|
|
349
|
-
* @example
|
|
350
|
-
* ```ts
|
|
351
|
-
* stringify('hello') // "'hello'"
|
|
352
|
-
* stringify('"hello"') // "'hello'"
|
|
353
|
-
* ```
|
|
354
|
-
*/
|
|
355
|
-
declare function stringify(value: string | number | boolean | undefined): string;
|
|
356
|
-
/**
|
|
357
|
-
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
|
|
358
|
-
* and the Unicode line terminators U+2028 and U+2029.
|
|
359
|
-
*
|
|
360
|
-
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
361
|
-
*
|
|
362
|
-
* @example
|
|
363
|
-
* ```ts
|
|
364
|
-
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
|
|
365
|
-
* ```
|
|
366
|
-
*/
|
|
367
|
-
declare function jsStringEscape(input: unknown): string;
|
|
368
|
-
/**
|
|
369
|
-
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
370
|
-
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
|
|
371
|
-
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
372
|
-
*
|
|
373
|
-
* @example
|
|
374
|
-
* ```ts
|
|
375
|
-
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
|
|
376
|
-
* toRegExpString('^(?im)foo', null) // '/^foo/im'
|
|
377
|
-
* ```
|
|
378
|
-
*/
|
|
379
|
-
declare function toRegExpString(text: string, func?: string | null): string;
|
|
380
|
-
/**
|
|
381
|
-
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
|
|
382
|
-
* objects recurse with fixed indentation, so the result drops straight into an object literal
|
|
383
|
-
* without re-parsing.
|
|
384
|
-
*
|
|
385
|
-
* @example
|
|
386
|
-
* ```ts
|
|
387
|
-
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
|
|
388
|
-
* // 'foo: bar,\nnested: {\n a: 1\n }'
|
|
389
|
-
* ```
|
|
390
|
-
*/
|
|
391
|
-
declare function stringifyObject(value: Record<string, unknown>): string;
|
|
392
|
-
/**
|
|
393
|
-
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
|
|
394
|
-
* `accessor`. Returns `null` for an empty path.
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```ts
|
|
398
|
-
* getNestedAccessor('pagination.next.id', 'lastPage')
|
|
399
|
-
* // "lastPage?.['pagination']?.['next']?.['id']"
|
|
400
|
-
* ```
|
|
401
|
-
*/
|
|
402
|
-
declare function getNestedAccessor(param: string | Array<string>, accessor: string): string | null;
|
|
403
|
-
//#endregion
|
|
404
235
|
//#region src/macros/macroDiscriminatorEnum.d.ts
|
|
405
236
|
type Props$1 = {
|
|
406
237
|
propertyName: string;
|
|
@@ -551,8 +382,8 @@ declare const nodeDefs: (NodeDef<InputNode, Partial<Omit<InputNode, "kind">>> |
|
|
|
551
382
|
type: "ipv6";
|
|
552
383
|
}) | ScalarSchemaNode, "kind">> | NodeDef<PropertyNode, UserPropertyNode> | NodeDef<ParameterNode, Pick<ParameterNode, "schema" | "name" | "in"> & Partial<Omit<ParameterNode, "schema" | "kind" | "name" | "in">>> | NodeDef<ConstNode, Omit<ConstNode, "kind">> | NodeDef<TypeNode, Omit<TypeNode, "kind">> | NodeDef<FunctionNode, Omit<FunctionNode, "kind">> | NodeDef<ArrowFunctionNode, Omit<ArrowFunctionNode, "kind">> | NodeDef<TextNode, string> | NodeDef<BreakNode, void> | NodeDef<JsxNode, string> | NodeDef<ImportNode, Omit<ImportNode, "kind">> | NodeDef<ExportNode, Omit<ExportNode, "kind">> | NodeDef<SourceNode, Omit<SourceNode, "kind">> | NodeDef<FileNode<object>, Omit<FileNode<object>, "kind">>)[];
|
|
553
384
|
declare namespace exports_d_exports {
|
|
554
|
-
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, Dialect, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeDef, NodeKind, NumberSchemaNode, ObjectSchemaNode, OperationNode, OutputNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, arrowFunctionDef, breakDef,
|
|
385
|
+
export { ArraySchemaNode, ArrowFunctionNode, BreakNode, CodeNode, ConstNode, ContentNode, DateSchemaNode, DatetimeSchemaNode, Dialect, DistributiveOmit, Enforce, EnumSchemaNode, ExportNode, FileNode, FunctionNode, GenericOperationNode, HttpMethod, HttpOperationNode, ImportNode, InferSchemaNode, InputMeta, InputNode, IntersectionSchemaNode, JSDocNode, JsxNode, Macro, Node, NodeDef, NodeKind, NumberSchemaNode, ObjectSchemaNode, OperationNode, OutputNode, ParameterLocation, ParameterNode, ParameterStyle, ParentOf, ParserOptions, PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, PropertyNode, RefSchemaNode, RequestBodyNode, ResponseNode, ScalarSchemaNode, ScalarSchemaType, SchemaDialect, SchemaNode, SchemaNodeByType, SchemaType, SourceNode, StatusCode, StringSchemaNode, TextNode, TimeSchemaNode, TypeNode, UnionSchemaNode, UrlSchemaNode, UserFileNode, Visitor, VisitorContext, applyMacros, arrowFunctionDef, breakDef, childName, collect, collectUsedSchemaNames, combineExports, combineImports, combineSources, composeMacros, constDef, containsCircularRef, contentDef, createPrinter, defineDialect, defineMacro, defineNode, enumPropName, exportDef, extractRefName, extractStringsFromNodes, factory_d_exports as factory, fileDef, findCircularSchemas, functionDef, importDef, inputDef, isHttpOperationNode, isStringType, jsxDef, macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, mergeAdjacentObjectsLazy, narrowSchema, nodeDefs, operationDef, optionality, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, sourceDef, syncSchemaRef, textDef, transform, typeDef, walk };
|
|
555
386
|
}
|
|
556
387
|
//#endregion
|
|
557
|
-
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateSchemaNode, type DatetimeSchemaNode, type Dialect, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type OutputNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyMacros, arrowFunctionDef, exports_d_exports as ast, breakDef,
|
|
388
|
+
export { type ArraySchemaNode, type ArrowFunctionNode, type BreakNode, type CodeNode, type ConstNode, type ContentNode, type DateSchemaNode, type DatetimeSchemaNode, type Dialect, type DistributiveOmit, type Enforce, type EnumSchemaNode, type ExportNode, type FileNode, type FunctionNode, type GenericOperationNode, type HttpMethod, type HttpOperationNode, type ImportNode, type InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type JSDocNode, type JsxNode, type Macro, type Node, type NodeDef, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, type OutputNode, type ParameterLocation, type ParameterNode, type ParameterStyle, type ParentOf, type ParserOptions, type PrimitiveSchemaType, type Printer, type PrinterFactoryOptions, type PrinterPartial, type PropertyNode, type RefSchemaNode, type RequestBodyNode, type ResponseNode, type ScalarSchemaNode, type ScalarSchemaType, type SchemaDialect, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, type UserFileNode, type Visitor, type VisitorContext, applyMacros, arrowFunctionDef, exports_d_exports as ast, breakDef, childName, collect, collectUsedSchemaNames, combineExports, combineImports, combineSources, composeMacros, constDef, containsCircularRef, contentDef, createPrinter, defineDialect, defineMacro, defineNode, enumPropName, exportDef, extractRefName, extractStringsFromNodes, factory_d_exports as factory, fileDef, findCircularSchemas, functionDef, importDef, inputDef, isHttpOperationNode, isStringType, jsxDef, macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, mergeAdjacentObjectsLazy, narrowSchema, nodeDefs, operationDef, optionality, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, sourceDef, syncSchemaRef, textDef, transform, typeDef, walk };
|
|
558
389
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -438,141 +438,6 @@ function memoize(store, factory) {
|
|
|
438
438
|
};
|
|
439
439
|
}
|
|
440
440
|
//#endregion
|
|
441
|
-
//#region ../../internals/utils/src/reserved.ts
|
|
442
|
-
/**
|
|
443
|
-
* JavaScript and Java reserved words.
|
|
444
|
-
* @link https://github.com/jonschlinkert/reserved/blob/master/index.js
|
|
445
|
-
*/
|
|
446
|
-
const reservedWords = /* @__PURE__ */ new Set([
|
|
447
|
-
"abstract",
|
|
448
|
-
"arguments",
|
|
449
|
-
"boolean",
|
|
450
|
-
"break",
|
|
451
|
-
"byte",
|
|
452
|
-
"case",
|
|
453
|
-
"catch",
|
|
454
|
-
"char",
|
|
455
|
-
"class",
|
|
456
|
-
"const",
|
|
457
|
-
"continue",
|
|
458
|
-
"debugger",
|
|
459
|
-
"default",
|
|
460
|
-
"delete",
|
|
461
|
-
"do",
|
|
462
|
-
"double",
|
|
463
|
-
"else",
|
|
464
|
-
"enum",
|
|
465
|
-
"eval",
|
|
466
|
-
"export",
|
|
467
|
-
"extends",
|
|
468
|
-
"false",
|
|
469
|
-
"final",
|
|
470
|
-
"finally",
|
|
471
|
-
"float",
|
|
472
|
-
"for",
|
|
473
|
-
"function",
|
|
474
|
-
"goto",
|
|
475
|
-
"if",
|
|
476
|
-
"implements",
|
|
477
|
-
"import",
|
|
478
|
-
"in",
|
|
479
|
-
"instanceof",
|
|
480
|
-
"int",
|
|
481
|
-
"interface",
|
|
482
|
-
"let",
|
|
483
|
-
"long",
|
|
484
|
-
"native",
|
|
485
|
-
"new",
|
|
486
|
-
"null",
|
|
487
|
-
"package",
|
|
488
|
-
"private",
|
|
489
|
-
"protected",
|
|
490
|
-
"public",
|
|
491
|
-
"return",
|
|
492
|
-
"short",
|
|
493
|
-
"static",
|
|
494
|
-
"super",
|
|
495
|
-
"switch",
|
|
496
|
-
"synchronized",
|
|
497
|
-
"this",
|
|
498
|
-
"throw",
|
|
499
|
-
"throws",
|
|
500
|
-
"transient",
|
|
501
|
-
"true",
|
|
502
|
-
"try",
|
|
503
|
-
"typeof",
|
|
504
|
-
"var",
|
|
505
|
-
"void",
|
|
506
|
-
"volatile",
|
|
507
|
-
"while",
|
|
508
|
-
"with",
|
|
509
|
-
"yield",
|
|
510
|
-
"Array",
|
|
511
|
-
"Date",
|
|
512
|
-
"hasOwnProperty",
|
|
513
|
-
"Infinity",
|
|
514
|
-
"isFinite",
|
|
515
|
-
"isNaN",
|
|
516
|
-
"isPrototypeOf",
|
|
517
|
-
"length",
|
|
518
|
-
"Math",
|
|
519
|
-
"name",
|
|
520
|
-
"NaN",
|
|
521
|
-
"Number",
|
|
522
|
-
"Object",
|
|
523
|
-
"prototype",
|
|
524
|
-
"String",
|
|
525
|
-
"toString",
|
|
526
|
-
"undefined",
|
|
527
|
-
"valueOf"
|
|
528
|
-
]);
|
|
529
|
-
/**
|
|
530
|
-
* Returns `true` when `name` is a syntactically valid JavaScript variable name.
|
|
531
|
-
*
|
|
532
|
-
* @example
|
|
533
|
-
* ```ts
|
|
534
|
-
* isValidVarName('status') // true
|
|
535
|
-
* isValidVarName('class') // false (reserved word)
|
|
536
|
-
* isValidVarName('42foo') // false (starts with digit)
|
|
537
|
-
* ```
|
|
538
|
-
*/
|
|
539
|
-
function isValidVarName(name) {
|
|
540
|
-
if (!name || reservedWords.has(name)) return false;
|
|
541
|
-
return isIdentifier(name);
|
|
542
|
-
}
|
|
543
|
-
/**
|
|
544
|
-
* Returns `true` when `name` is syntactically a valid identifier, ignoring reserved words.
|
|
545
|
-
*
|
|
546
|
-
* Reserved words and globals (`class`, `name`, `Date`, …) are valid as bare object-literal keys
|
|
547
|
-
* even though they are not valid variable names, so use this (not {@link isValidVarName}) when
|
|
548
|
-
* deciding whether an object key needs quoting.
|
|
549
|
-
*
|
|
550
|
-
* @example
|
|
551
|
-
* ```ts
|
|
552
|
-
* isIdentifier('name') // true
|
|
553
|
-
* isIdentifier('x-total')// false
|
|
554
|
-
* ```
|
|
555
|
-
*/
|
|
556
|
-
function isIdentifier(name) {
|
|
557
|
-
return /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(name);
|
|
558
|
-
}
|
|
559
|
-
//#endregion
|
|
560
|
-
//#region ../../internals/utils/src/string.ts
|
|
561
|
-
/**
|
|
562
|
-
* Wraps a value in single quotes for emitting a single-quoted JavaScript string literal, escaping
|
|
563
|
-
* any backslash or single quote in the content.
|
|
564
|
-
*
|
|
565
|
-
* @example
|
|
566
|
-
* ```ts
|
|
567
|
-
* singleQuote('foo') // "'foo'"
|
|
568
|
-
* singleQuote("o'clock") // "'o\\'clock'"
|
|
569
|
-
* ```
|
|
570
|
-
*/
|
|
571
|
-
function singleQuote(value) {
|
|
572
|
-
if (value === void 0 || value === null) return "''";
|
|
573
|
-
return `'${String(value).replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
574
|
-
}
|
|
575
|
-
//#endregion
|
|
576
441
|
//#region src/utils/extractStringsFromNodes.ts
|
|
577
442
|
/**
|
|
578
443
|
* Extracts all string content from a `CodeNode` tree recursively.
|
|
@@ -1552,90 +1417,6 @@ function createPrinter(build) {
|
|
|
1552
1417
|
};
|
|
1553
1418
|
}
|
|
1554
1419
|
//#endregion
|
|
1555
|
-
//#region src/utils/codegen.ts
|
|
1556
|
-
/**
|
|
1557
|
-
* Builds a JSDoc comment block from an array of lines. Returns `fallback` when there are no
|
|
1558
|
-
* comments.
|
|
1559
|
-
*
|
|
1560
|
-
* @example
|
|
1561
|
-
* ```ts
|
|
1562
|
-
* buildJSDoc(['@type string', '@example hello'])
|
|
1563
|
-
* // '/**\n * @type string\n * @example hello\n *\/\n '
|
|
1564
|
-
* ```
|
|
1565
|
-
*/
|
|
1566
|
-
function buildJSDoc(comments, options = {}) {
|
|
1567
|
-
const { indent = " * ", suffix = "\n ", fallback = " " } = options;
|
|
1568
|
-
if (comments.length === 0) return fallback;
|
|
1569
|
-
return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
|
|
1570
|
-
}
|
|
1571
|
-
/**
|
|
1572
|
-
* Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
|
|
1573
|
-
*/
|
|
1574
|
-
function indentLines(text) {
|
|
1575
|
-
if (!text) return "";
|
|
1576
|
-
return text.split("\n").map((line) => line.trim() ? ` ${line}` : "").join("\n");
|
|
1577
|
-
}
|
|
1578
|
-
/**
|
|
1579
|
-
* Renders an object key, quoting it with single quotes only when it is not a valid identifier.
|
|
1580
|
-
* Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
|
|
1581
|
-
*
|
|
1582
|
-
* @example
|
|
1583
|
-
* ```ts
|
|
1584
|
-
* objectKey('name') // 'name'
|
|
1585
|
-
* objectKey('x-total') // "'x-total'"
|
|
1586
|
-
* ```
|
|
1587
|
-
*/
|
|
1588
|
-
function objectKey(name) {
|
|
1589
|
-
return isIdentifier(name) ? name : singleQuote(name);
|
|
1590
|
-
}
|
|
1591
|
-
/**
|
|
1592
|
-
* Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
|
|
1593
|
-
* level and closing the brace at column zero. Entries that are themselves multi-line objects indent
|
|
1594
|
-
* cumulatively. Each entry ends with a trailing comma to match the formatter's multi-line style.
|
|
1595
|
-
*
|
|
1596
|
-
* @example
|
|
1597
|
-
* ```ts
|
|
1598
|
-
* buildObject(['id: z.number()', 'name: z.string()'])
|
|
1599
|
-
* // '{\n id: z.number(),\n name: z.string(),\n}'
|
|
1600
|
-
* ```
|
|
1601
|
-
*/
|
|
1602
|
-
function buildObject(entries) {
|
|
1603
|
-
if (entries.length === 0) return "{}";
|
|
1604
|
-
return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
|
|
1605
|
-
}
|
|
1606
|
-
/**
|
|
1607
|
-
* Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
|
|
1608
|
-
* one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
|
|
1609
|
-
* one level with a trailing comma and the closing bracket at column zero. Used for member lists such
|
|
1610
|
-
* as `z.union([…])` and `z.array([…])`.
|
|
1611
|
-
*
|
|
1612
|
-
* @example
|
|
1613
|
-
* ```ts
|
|
1614
|
-
* buildList(['z.string()', 'z.number()'])
|
|
1615
|
-
* // '[z.string(), z.number()]'
|
|
1616
|
-
* ```
|
|
1617
|
-
*/
|
|
1618
|
-
function buildList(items, brackets = ["[", "]"]) {
|
|
1619
|
-
const [open, close] = brackets;
|
|
1620
|
-
if (items.length === 0) return `${open}${close}`;
|
|
1621
|
-
if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
|
|
1622
|
-
return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
|
|
1623
|
-
}
|
|
1624
|
-
/**
|
|
1625
|
-
* Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
|
|
1626
|
-
* is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
|
|
1627
|
-
* of a recursive schema until first access.
|
|
1628
|
-
*
|
|
1629
|
-
* @example
|
|
1630
|
-
* ```ts
|
|
1631
|
-
* lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
|
|
1632
|
-
* // "get parent() { return z.lazy(() => Pet) }"
|
|
1633
|
-
* ```
|
|
1634
|
-
*/
|
|
1635
|
-
function lazyGetter({ name, body }) {
|
|
1636
|
-
return `get ${objectKey(name)}() { return ${body} }`;
|
|
1637
|
-
}
|
|
1638
|
-
//#endregion
|
|
1639
1420
|
//#region src/utils/mergeAdjacentSchemas.ts
|
|
1640
1421
|
/**
|
|
1641
1422
|
* Merges a run of adjacent anonymous object members into one. Named or non-object members break the
|
|
@@ -1942,120 +1723,6 @@ function mapSchemaItems(node, transform) {
|
|
|
1942
1723
|
}));
|
|
1943
1724
|
}
|
|
1944
1725
|
//#endregion
|
|
1945
|
-
//#region src/utils/strings.ts
|
|
1946
|
-
/**
|
|
1947
|
-
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
1948
|
-
* Returns the string unchanged when no balanced quote pair is found.
|
|
1949
|
-
*
|
|
1950
|
-
* @example
|
|
1951
|
-
* ```ts
|
|
1952
|
-
* trimQuotes('"hello"') // 'hello'
|
|
1953
|
-
* trimQuotes('hello') // 'hello'
|
|
1954
|
-
* ```
|
|
1955
|
-
*/
|
|
1956
|
-
function trimQuotes(text) {
|
|
1957
|
-
if (text.length >= 2) {
|
|
1958
|
-
const first = text[0];
|
|
1959
|
-
const last = text[text.length - 1];
|
|
1960
|
-
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
|
|
1961
|
-
}
|
|
1962
|
-
return text;
|
|
1963
|
-
}
|
|
1964
|
-
/**
|
|
1965
|
-
* Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
|
|
1966
|
-
*
|
|
1967
|
-
* Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
|
|
1968
|
-
* code matches the repo style without a formatter.
|
|
1969
|
-
*
|
|
1970
|
-
* @example
|
|
1971
|
-
* ```ts
|
|
1972
|
-
* stringify('hello') // "'hello'"
|
|
1973
|
-
* stringify('"hello"') // "'hello'"
|
|
1974
|
-
* ```
|
|
1975
|
-
*/
|
|
1976
|
-
function stringify(value) {
|
|
1977
|
-
if (value === void 0 || value === null) return "''";
|
|
1978
|
-
return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
|
|
1979
|
-
}
|
|
1980
|
-
/**
|
|
1981
|
-
* Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
|
|
1982
|
-
* and the Unicode line terminators U+2028 and U+2029.
|
|
1983
|
-
*
|
|
1984
|
-
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
1985
|
-
*
|
|
1986
|
-
* @example
|
|
1987
|
-
* ```ts
|
|
1988
|
-
* jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
|
|
1989
|
-
* ```
|
|
1990
|
-
*/
|
|
1991
|
-
function jsStringEscape(input) {
|
|
1992
|
-
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
|
|
1993
|
-
switch (character) {
|
|
1994
|
-
case "\"":
|
|
1995
|
-
case "'":
|
|
1996
|
-
case "\\": return `\\${character}`;
|
|
1997
|
-
case "\n": return "\\n";
|
|
1998
|
-
case "\r": return "\\r";
|
|
1999
|
-
case "\u2028": return "\\u2028";
|
|
2000
|
-
case "\u2029": return "\\u2029";
|
|
2001
|
-
default: return "";
|
|
2002
|
-
}
|
|
2003
|
-
});
|
|
2004
|
-
}
|
|
2005
|
-
/**
|
|
2006
|
-
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
2007
|
-
* Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
|
|
2008
|
-
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
2009
|
-
*
|
|
2010
|
-
* @example
|
|
2011
|
-
* ```ts
|
|
2012
|
-
* toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
|
|
2013
|
-
* toRegExpString('^(?im)foo', null) // '/^foo/im'
|
|
2014
|
-
* ```
|
|
2015
|
-
*/
|
|
2016
|
-
function toRegExpString(text, func = "RegExp") {
|
|
2017
|
-
const raw = trimQuotes(text);
|
|
2018
|
-
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
|
|
2019
|
-
const replacementTarget = match?.[1] ?? "";
|
|
2020
|
-
const matchedFlags = match?.[2];
|
|
2021
|
-
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
|
|
2022
|
-
const { source, flags } = new RegExp(cleaned, matchedFlags);
|
|
2023
|
-
if (func === null) return `/${source}/${flags}`;
|
|
2024
|
-
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
|
|
2025
|
-
}
|
|
2026
|
-
/**
|
|
2027
|
-
* Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
|
|
2028
|
-
* objects recurse with fixed indentation, so the result drops straight into an object literal
|
|
2029
|
-
* without re-parsing.
|
|
2030
|
-
*
|
|
2031
|
-
* @example
|
|
2032
|
-
* ```ts
|
|
2033
|
-
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
|
|
2034
|
-
* // 'foo: bar,\nnested: {\n a: 1\n }'
|
|
2035
|
-
* ```
|
|
2036
|
-
*/
|
|
2037
|
-
function stringifyObject(value) {
|
|
2038
|
-
return Object.entries(value).map(([key, val]) => {
|
|
2039
|
-
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
|
|
2040
|
-
return `${key}: ${val}`;
|
|
2041
|
-
}).filter(Boolean).join(",\n");
|
|
2042
|
-
}
|
|
2043
|
-
/**
|
|
2044
|
-
* Renders a dotted path or string array as an optional-chaining accessor expression rooted at
|
|
2045
|
-
* `accessor`. Returns `null` for an empty path.
|
|
2046
|
-
*
|
|
2047
|
-
* @example
|
|
2048
|
-
* ```ts
|
|
2049
|
-
* getNestedAccessor('pagination.next.id', 'lastPage')
|
|
2050
|
-
* // "lastPage?.['pagination']?.['next']?.['id']"
|
|
2051
|
-
* ```
|
|
2052
|
-
*/
|
|
2053
|
-
function getNestedAccessor(param, accessor) {
|
|
2054
|
-
const parts = Array.isArray(param) ? param : param.split(".");
|
|
2055
|
-
if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
|
|
2056
|
-
return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
|
|
2057
|
-
}
|
|
2058
|
-
//#endregion
|
|
2059
1726
|
//#region src/macros/macroDiscriminatorEnum.ts
|
|
2060
1727
|
/**
|
|
2061
1728
|
* Builds a macro that replaces a discriminator property's schema with a string enum of the given
|
|
@@ -2230,9 +1897,6 @@ var exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
2230
1897
|
applyMacros: () => applyMacros,
|
|
2231
1898
|
arrowFunctionDef: () => arrowFunctionDef,
|
|
2232
1899
|
breakDef: () => breakDef,
|
|
2233
|
-
buildJSDoc: () => buildJSDoc,
|
|
2234
|
-
buildList: () => buildList,
|
|
2235
|
-
buildObject: () => buildObject,
|
|
2236
1900
|
childName: () => childName,
|
|
2237
1901
|
collect: () => collect,
|
|
2238
1902
|
collectUsedSchemaNames: () => collectUsedSchemaNames,
|
|
@@ -2255,15 +1919,11 @@ var exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
2255
1919
|
fileDef: () => fileDef,
|
|
2256
1920
|
findCircularSchemas: () => findCircularSchemas,
|
|
2257
1921
|
functionDef: () => functionDef,
|
|
2258
|
-
getNestedAccessor: () => getNestedAccessor,
|
|
2259
1922
|
importDef: () => importDef,
|
|
2260
1923
|
inputDef: () => inputDef,
|
|
2261
1924
|
isHttpOperationNode: () => isHttpOperationNode,
|
|
2262
1925
|
isStringType: () => isStringType,
|
|
2263
|
-
isValidVarName: () => isValidVarName,
|
|
2264
|
-
jsStringEscape: () => jsStringEscape,
|
|
2265
1926
|
jsxDef: () => jsxDef,
|
|
2266
|
-
lazyGetter: () => lazyGetter,
|
|
2267
1927
|
macroDiscriminatorEnum: () => macroDiscriminatorEnum,
|
|
2268
1928
|
macroEnumName: () => macroEnumName,
|
|
2269
1929
|
macroSimplifyUnion: () => macroSimplifyUnion,
|
|
@@ -2273,7 +1933,6 @@ var exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
2273
1933
|
mergeAdjacentObjectsLazy: () => mergeAdjacentObjectsLazy,
|
|
2274
1934
|
narrowSchema: () => narrowSchema,
|
|
2275
1935
|
nodeDefs: () => nodeDefs,
|
|
2276
|
-
objectKey: () => objectKey,
|
|
2277
1936
|
operationDef: () => operationDef,
|
|
2278
1937
|
optionality: () => optionality,
|
|
2279
1938
|
outputDef: () => outputDef,
|
|
@@ -2284,17 +1943,13 @@ var exports_exports = /* @__PURE__ */ __exportAll({
|
|
|
2284
1943
|
schemaDef: () => schemaDef,
|
|
2285
1944
|
schemaTypes: () => schemaTypes,
|
|
2286
1945
|
sourceDef: () => sourceDef,
|
|
2287
|
-
stringify: () => stringify,
|
|
2288
|
-
stringifyObject: () => stringifyObject,
|
|
2289
1946
|
syncSchemaRef: () => syncSchemaRef,
|
|
2290
1947
|
textDef: () => textDef,
|
|
2291
|
-
toRegExpString: () => toRegExpString,
|
|
2292
1948
|
transform: () => transform,
|
|
2293
|
-
trimQuotes: () => trimQuotes,
|
|
2294
1949
|
typeDef: () => typeDef,
|
|
2295
1950
|
walk: () => walk
|
|
2296
1951
|
});
|
|
2297
1952
|
//#endregion
|
|
2298
|
-
export { applyMacros, arrowFunctionDef, exports_exports as ast, breakDef,
|
|
1953
|
+
export { applyMacros, arrowFunctionDef, exports_exports as ast, breakDef, childName, collect, collectUsedSchemaNames, combineExports, combineImports, combineSources, composeMacros, constDef, containsCircularRef, contentDef, createPrinter, defineDialect, defineMacro, defineNode, enumPropName, exportDef, extractRefName, extractStringsFromNodes, factory_exports as factory, fileDef, findCircularSchemas, functionDef, importDef, inputDef, isHttpOperationNode, isStringType, jsxDef, macroDiscriminatorEnum, macroEnumName, macroSimplifyUnion, mapSchemaItems, mapSchemaMembers, mapSchemaProperties, mergeAdjacentObjectsLazy, narrowSchema, nodeDefs, operationDef, optionality, outputDef, parameterDef, propertyDef, requestBodyDef, responseDef, schemaDef, schemaTypes, sourceDef, syncSchemaRef, textDef, transform, typeDef, walk };
|
|
2299
1954
|
|
|
2300
1955
|
//# sourceMappingURL=index.js.map
|