@kubb/ast 5.0.0-beta.41 → 5.0.0-beta.43

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
@@ -177,6 +177,10 @@ const httpMethods = {
177
177
  options: "OPTIONS",
178
178
  trace: "TRACE"
179
179
  };
180
+ /**
181
+ * One indentation level, derived from {@link INDENT_SIZE}.
182
+ */
183
+ const INDENT = Array.from({ length: 2 }, () => " ").join("");
180
184
  //#endregion
181
185
  //#region ../../internals/utils/src/casing.ts
182
186
  /**
@@ -1053,7 +1057,7 @@ function sortKey(node) {
1053
1057
  const isArray = Array.isArray(node.name) ? "1" : "0";
1054
1058
  const typeOnly = node.isTypeOnly ? "0" : "1";
1055
1059
  const hasName = node.name != null ? "1" : "0";
1056
- const name = Array.isArray(node.name) ? [...node.name].sort().join("\0") : node.name ?? "";
1060
+ const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
1057
1061
  return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
1058
1062
  }
1059
1063
  /**
@@ -1203,6 +1207,59 @@ function extractStringsFromNodes(nodes) {
1203
1207
  }).filter(Boolean).join("\n");
1204
1208
  }
1205
1209
  /**
1210
+ * Indents every non-empty line of `text` by one indent level, leaving blank lines empty.
1211
+ */
1212
+ function indentLines(text) {
1213
+ if (!text) return "";
1214
+ return text.split("\n").map((line) => line.trim() ? `${INDENT}${line}` : "").join("\n");
1215
+ }
1216
+ /**
1217
+ * Renders an object key, quoting it only when it is not a valid variable name.
1218
+ *
1219
+ * @example
1220
+ * ```ts
1221
+ * objectKey('id') // 'id'
1222
+ * objectKey('x-total') // '"x-total"'
1223
+ * ```
1224
+ */
1225
+ function objectKey(name) {
1226
+ return isValidVarName(name) ? name : JSON.stringify(name);
1227
+ }
1228
+ /**
1229
+ * Assembles a multi-line object literal from already-rendered `entries`, indenting each entry one
1230
+ * level and closing the brace at column zero. Nested objects built the same way indent cumulatively,
1231
+ * so callers never re-parse the generated code. A trailing comma is added per entry to match the
1232
+ * formatter's multi-line style.
1233
+ *
1234
+ * @example
1235
+ * ```ts
1236
+ * buildObject(['id: z.number()', 'name: z.string()'])
1237
+ * // '{\n id: z.number(),\n name: z.string(),\n}'
1238
+ * ```
1239
+ */
1240
+ function buildObject(entries) {
1241
+ if (entries.length === 0) return "{}";
1242
+ return `{\n${entries.map((entry) => `${indentLines(entry)},`).join("\n")}\n}`;
1243
+ }
1244
+ /**
1245
+ * Assembles a bracketed list (array by default) from already-rendered `items`. Keeps everything on
1246
+ * one line when no item spans multiple lines, and otherwise puts each item on its own line, indented
1247
+ * one level with a trailing comma and the closing bracket at column zero. Use it for `z.union([…])`,
1248
+ * `z.array([…])`, and similar member lists so objects inside them nest correctly.
1249
+ *
1250
+ * @example
1251
+ * ```ts
1252
+ * buildList(['z.string()', 'z.number()'])
1253
+ * // '[z.string(), z.number()]'
1254
+ * ```
1255
+ */
1256
+ function buildList(items, brackets = ["[", "]"]) {
1257
+ const [open, close] = brackets;
1258
+ if (items.length === 0) return `${open}${close}`;
1259
+ if (!items.some((item) => item.includes("\n"))) return `${open}${items.join(", ")}${close}`;
1260
+ return `${open}\n${items.map((item) => `${indentLines(item)},`).join("\n")}\n${close}`;
1261
+ }
1262
+ /**
1206
1263
  * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
1207
1264
  *
1208
1265
  * Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
@@ -1844,7 +1901,7 @@ function createFile(input) {
1844
1901
  return {
1845
1902
  kind: "File",
1846
1903
  ...input,
1847
- id: (0, node_crypto.createHash)("sha256").update(input.path).digest("hex"),
1904
+ id: (0, node_crypto.hash)("sha256", input.path, "hex"),
1848
1905
  name: trimExtName(input.baseName),
1849
1906
  extname,
1850
1907
  imports: resolvedImports,
@@ -2289,7 +2346,7 @@ const signatureCache = /* @__PURE__ */ new WeakMap();
2289
2346
  function signatureOf(node) {
2290
2347
  const cached = signatureCache.get(node);
2291
2348
  if (cached !== void 0) return cached;
2292
- const signature = (0, node_crypto.createHash)("sha256").update(describeShape(node)).digest("hex");
2349
+ const signature = (0, node_crypto.hash)("sha256", describeShape(node), "hex");
2293
2350
  signatureCache.set(node, signature);
2294
2351
  return signature;
2295
2352
  }
@@ -2691,6 +2748,8 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
2691
2748
  //#endregion
2692
2749
  exports.applyDedupe = applyDedupe;
2693
2750
  exports.buildDedupePlan = buildDedupePlan;
2751
+ exports.buildList = buildList;
2752
+ exports.buildObject = buildObject;
2694
2753
  exports.caseParams = caseParams;
2695
2754
  exports.childName = childName;
2696
2755
  exports.collect = collect;
@@ -2740,6 +2799,7 @@ exports.isSchemaNode = isSchemaNode;
2740
2799
  exports.isStringType = isStringType;
2741
2800
  exports.mergeAdjacentObjectsLazy = mergeAdjacentObjectsLazy;
2742
2801
  exports.narrowSchema = narrowSchema;
2802
+ exports.objectKey = objectKey;
2743
2803
  exports.schemaSignature = schemaSignature;
2744
2804
  exports.schemaTypes = schemaTypes;
2745
2805
  exports.setDiscriminatorEnum = setDiscriminatorEnum;