@kubb/ast 5.0.0-beta.85 → 5.0.0-beta.87

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 CHANGED
@@ -470,141 +470,6 @@ function memoize(store, factory) {
470
470
  };
471
471
  }
472
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
473
  //#region src/utils/extractStringsFromNodes.ts
609
474
  /**
610
475
  * Extracts all string content from a `CodeNode` tree recursively.
@@ -1584,90 +1449,6 @@ function createPrinter(build) {
1584
1449
  };
1585
1450
  }
1586
1451
  //#endregion
1587
- //#region src/utils/codegen.ts
1588
- /**
1589
- * Builds a JSDoc comment block from an array of lines. Returns `fallback` when there are no
1590
- * comments.
1591
- *
1592
- * @example
1593
- * ```ts
1594
- * buildJSDoc(['@type string', '@example hello'])
1595
- * // '/**\n * @type string\n * @example hello\n *\/\n '
1596
- * ```
1597
- */
1598
- function buildJSDoc(comments, options = {}) {
1599
- const { indent = " * ", suffix = "\n ", fallback = " " } = options;
1600
- if (comments.length === 0) return fallback;
1601
- return `/**\n${comments.map((c) => `${indent}${c}`).join("\n")}\n */${suffix}`;
1602
- }
1603
- /**
1604
- * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
1605
- */
1606
- function indentLines(text) {
1607
- if (!text) return "";
1608
- return text.split("\n").map((line) => line.trim() ? ` ${line}` : "").join("\n");
1609
- }
1610
- /**
1611
- * Renders an object key, quoting it with single quotes only when it is not a valid identifier.
1612
- * Reserved words and globals (`name`, `class`, …) are valid bare keys and stay unquoted.
1613
- *
1614
- * @example
1615
- * ```ts
1616
- * objectKey('name') // 'name'
1617
- * objectKey('x-total') // "'x-total'"
1618
- * ```
1619
- */
1620
- function objectKey(name) {
1621
- return isIdentifier(name) ? name : singleQuote(name);
1622
- }
1623
- /**
1624
- * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
1625
- * level and closing the brace at column zero. Entries that are themselves multi-line objects indent
1626
- * cumulatively. Each entry ends with a trailing comma to match the formatter's multi-line style.
1627
- *
1628
- * @example
1629
- * ```ts
1630
- * buildObject(['id: z.number()', 'name: z.string()'])
1631
- * // '{\n id: z.number(),\n name: z.string(),\n}'
1632
- * ```
1633
- */
1634
- function buildObject(entries) {
1635
- if (entries.length === 0) return "{}";
1636
- return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
1637
- }
1638
- /**
1639
- * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
1640
- * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
1641
- * one level with a trailing comma and the closing bracket at column zero. Used for member lists such
1642
- * as `z.union([…])` and `z.array([…])`.
1643
- *
1644
- * @example
1645
- * ```ts
1646
- * buildList(['z.string()', 'z.number()'])
1647
- * // '[z.string(), z.number()]'
1648
- * ```
1649
- */
1650
- function buildList(items, brackets = ["[", "]"]) {
1651
- const [open, close] = brackets;
1652
- if (items.length === 0) return `${open}${close}`;
1653
- if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
1654
- return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
1655
- }
1656
- /**
1657
- * Emits a lazy getter for a circular-ref property position, `get name() { return body }`. The key
1658
- * is quoted only when it is not a valid identifier. Used by the string printers to defer evaluation
1659
- * of a recursive schema until first access.
1660
- *
1661
- * @example
1662
- * ```ts
1663
- * lazyGetter({ name: 'parent', body: 'z.lazy(() => Pet)' })
1664
- * // "get parent() { return z.lazy(() => Pet) }"
1665
- * ```
1666
- */
1667
- function lazyGetter({ name, body }) {
1668
- return `get ${objectKey(name)}() { return ${body} }`;
1669
- }
1670
- //#endregion
1671
1452
  //#region src/utils/mergeAdjacentSchemas.ts
1672
1453
  /**
1673
1454
  * Merges a run of adjacent anonymous object members into one. Named or non-object members break the
@@ -1974,120 +1755,6 @@ function mapSchemaItems(node, transform) {
1974
1755
  }));
1975
1756
  }
1976
1757
  //#endregion
1977
- //#region src/utils/strings.ts
1978
- /**
1979
- * Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
1980
- * Returns the string unchanged when no balanced quote pair is found.
1981
- *
1982
- * @example
1983
- * ```ts
1984
- * trimQuotes('"hello"') // 'hello'
1985
- * trimQuotes('hello') // 'hello'
1986
- * ```
1987
- */
1988
- function trimQuotes(text) {
1989
- if (text.length >= 2) {
1990
- const first = text[0];
1991
- const last = text[text.length - 1];
1992
- if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
1993
- }
1994
- return text;
1995
- }
1996
- /**
1997
- * Serializes a primitive to a single-quoted string literal, stripping any surrounding quotes first.
1998
- *
1999
- * Escaping runs through `JSON.stringify`, then the result switches to single quotes so the generated
2000
- * code matches the repo style without a formatter.
2001
- *
2002
- * @example
2003
- * ```ts
2004
- * stringify('hello') // "'hello'"
2005
- * stringify('"hello"') // "'hello'"
2006
- * ```
2007
- */
2008
- function stringify(value) {
2009
- if (value === void 0 || value === null) return "''";
2010
- return `'${JSON.stringify(trimQuotes(value.toString())).slice(1, -1).replace(/\\"/g, "\"").replace(/'/g, "\\'")}'`;
2011
- }
2012
- /**
2013
- * Escapes characters that are not allowed inside JS string literals, covering quotes, backslashes,
2014
- * and the Unicode line terminators U+2028 and U+2029.
2015
- *
2016
- * @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
2017
- *
2018
- * @example
2019
- * ```ts
2020
- * jsStringEscape('say "hi"\nbye') // 'say \\"hi\\"\\nbye'
2021
- * ```
2022
- */
2023
- function jsStringEscape(input) {
2024
- return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
2025
- switch (character) {
2026
- case "\"":
2027
- case "'":
2028
- case "\\": return `\\${character}`;
2029
- case "\n": return "\\n";
2030
- case "\r": return "\\r";
2031
- case "\u2028": return "\\u2028";
2032
- case "\u2029": return "\\u2029";
2033
- default: return "";
2034
- }
2035
- });
2036
- }
2037
- /**
2038
- * Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
2039
- * Inline flags expressed as a `^(?im)` prefix are extracted and applied to the resulting expression.
2040
- * Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
2041
- *
2042
- * @example
2043
- * ```ts
2044
- * toRegExpString('^(?im)foo') // 'new RegExp("^foo", "im")'
2045
- * toRegExpString('^(?im)foo', null) // '/^foo/im'
2046
- * ```
2047
- */
2048
- function toRegExpString(text, func = "RegExp") {
2049
- const raw = trimQuotes(text);
2050
- const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
2051
- const replacementTarget = match?.[1] ?? "";
2052
- const matchedFlags = match?.[2];
2053
- const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
2054
- const { source, flags } = new RegExp(cleaned, matchedFlags);
2055
- if (func === null) return `/${source}/${flags}`;
2056
- return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
2057
- }
2058
- /**
2059
- * Renders a plain object as multi-line `key: value` source for embedding in generated code. Nested
2060
- * objects recurse with fixed indentation, so the result drops straight into an object literal
2061
- * without re-parsing.
2062
- *
2063
- * @example
2064
- * ```ts
2065
- * stringifyObject({ foo: 'bar', nested: { a: 1 } })
2066
- * // 'foo: bar,\nnested: {\n a: 1\n }'
2067
- * ```
2068
- */
2069
- function stringifyObject(value) {
2070
- return Object.entries(value).map(([key, val]) => {
2071
- if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
2072
- return `${key}: ${val}`;
2073
- }).filter(Boolean).join(",\n");
2074
- }
2075
- /**
2076
- * Renders a dotted path or string array as an optional-chaining accessor expression rooted at
2077
- * `accessor`. Returns `null` for an empty path.
2078
- *
2079
- * @example
2080
- * ```ts
2081
- * getNestedAccessor('pagination.next.id', 'lastPage')
2082
- * // "lastPage?.['pagination']?.['next']?.['id']"
2083
- * ```
2084
- */
2085
- function getNestedAccessor(param, accessor) {
2086
- const parts = Array.isArray(param) ? param : param.split(".");
2087
- if (parts.length === 0 || parts.length === 1 && parts[0] === "") return null;
2088
- return `${accessor}?.['${`${parts.join("']?.['")}']`}`;
2089
- }
2090
- //#endregion
2091
1758
  //#region src/macros/macroDiscriminatorEnum.ts
2092
1759
  /**
2093
1760
  * Builds a macro that replaces a discriminator property's schema with a string enum of the given
@@ -2262,9 +1929,6 @@ var exports_exports = /* @__PURE__ */ __exportAll({
2262
1929
  applyMacros: () => applyMacros,
2263
1930
  arrowFunctionDef: () => arrowFunctionDef,
2264
1931
  breakDef: () => breakDef,
2265
- buildJSDoc: () => buildJSDoc,
2266
- buildList: () => buildList,
2267
- buildObject: () => buildObject,
2268
1932
  childName: () => childName,
2269
1933
  collect: () => collect,
2270
1934
  collectUsedSchemaNames: () => collectUsedSchemaNames,
@@ -2287,15 +1951,11 @@ var exports_exports = /* @__PURE__ */ __exportAll({
2287
1951
  fileDef: () => fileDef,
2288
1952
  findCircularSchemas: () => findCircularSchemas,
2289
1953
  functionDef: () => functionDef,
2290
- getNestedAccessor: () => getNestedAccessor,
2291
1954
  importDef: () => importDef,
2292
1955
  inputDef: () => inputDef,
2293
1956
  isHttpOperationNode: () => isHttpOperationNode,
2294
1957
  isStringType: () => isStringType,
2295
- isValidVarName: () => isValidVarName,
2296
- jsStringEscape: () => jsStringEscape,
2297
1958
  jsxDef: () => jsxDef,
2298
- lazyGetter: () => lazyGetter,
2299
1959
  macroDiscriminatorEnum: () => macroDiscriminatorEnum,
2300
1960
  macroEnumName: () => macroEnumName,
2301
1961
  macroSimplifyUnion: () => macroSimplifyUnion,
@@ -2305,7 +1965,6 @@ var exports_exports = /* @__PURE__ */ __exportAll({
2305
1965
  mergeAdjacentObjectsLazy: () => mergeAdjacentObjectsLazy,
2306
1966
  narrowSchema: () => narrowSchema,
2307
1967
  nodeDefs: () => nodeDefs,
2308
- objectKey: () => objectKey,
2309
1968
  operationDef: () => operationDef,
2310
1969
  optionality: () => optionality,
2311
1970
  outputDef: () => outputDef,
@@ -2316,13 +1975,9 @@ var exports_exports = /* @__PURE__ */ __exportAll({
2316
1975
  schemaDef: () => schemaDef,
2317
1976
  schemaTypes: () => schemaTypes,
2318
1977
  sourceDef: () => sourceDef,
2319
- stringify: () => stringify,
2320
- stringifyObject: () => stringifyObject,
2321
1978
  syncSchemaRef: () => syncSchemaRef,
2322
1979
  textDef: () => textDef,
2323
- toRegExpString: () => toRegExpString,
2324
1980
  transform: () => transform,
2325
- trimQuotes: () => trimQuotes,
2326
1981
  typeDef: () => typeDef,
2327
1982
  walk: () => walk
2328
1983
  });
@@ -2336,9 +1991,6 @@ Object.defineProperty(exports, "ast", {
2336
1991
  }
2337
1992
  });
2338
1993
  exports.breakDef = breakDef;
2339
- exports.buildJSDoc = buildJSDoc;
2340
- exports.buildList = buildList;
2341
- exports.buildObject = buildObject;
2342
1994
  exports.childName = childName;
2343
1995
  exports.collect = collect;
2344
1996
  exports.collectUsedSchemaNames = collectUsedSchemaNames;
@@ -2366,15 +2018,11 @@ Object.defineProperty(exports, "factory", {
2366
2018
  exports.fileDef = fileDef;
2367
2019
  exports.findCircularSchemas = findCircularSchemas;
2368
2020
  exports.functionDef = functionDef;
2369
- exports.getNestedAccessor = getNestedAccessor;
2370
2021
  exports.importDef = importDef;
2371
2022
  exports.inputDef = inputDef;
2372
2023
  exports.isHttpOperationNode = isHttpOperationNode;
2373
2024
  exports.isStringType = isStringType;
2374
- exports.isValidVarName = isValidVarName;
2375
- exports.jsStringEscape = jsStringEscape;
2376
2025
  exports.jsxDef = jsxDef;
2377
- exports.lazyGetter = lazyGetter;
2378
2026
  exports.macroDiscriminatorEnum = macroDiscriminatorEnum;
2379
2027
  exports.macroEnumName = macroEnumName;
2380
2028
  exports.macroSimplifyUnion = macroSimplifyUnion;
@@ -2384,7 +2032,6 @@ exports.mapSchemaProperties = mapSchemaProperties;
2384
2032
  exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
2385
2033
  exports.narrowSchema = narrowSchema;
2386
2034
  exports.nodeDefs = nodeDefs;
2387
- exports.objectKey = objectKey;
2388
2035
  exports.operationDef = operationDef;
2389
2036
  exports.optionality = optionality;
2390
2037
  exports.outputDef = outputDef;
@@ -2395,13 +2042,9 @@ exports.responseDef = responseDef;
2395
2042
  exports.schemaDef = schemaDef;
2396
2043
  exports.schemaTypes = schemaTypes;
2397
2044
  exports.sourceDef = sourceDef;
2398
- exports.stringify = stringify;
2399
- exports.stringifyObject = stringifyObject;
2400
2045
  exports.syncSchemaRef = syncSchemaRef;
2401
2046
  exports.textDef = textDef;
2402
- exports.toRegExpString = toRegExpString;
2403
2047
  exports.transform = transform;
2404
- exports.trimQuotes = trimQuotes;
2405
2048
  exports.typeDef = typeDef;
2406
2049
  exports.walk = walk;
2407
2050