@kubb/ast 5.0.0-beta.42 → 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
  /**
@@ -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
@@ -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;