@kubb/ast 5.0.0-alpha.17 → 5.0.0-alpha.18

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
@@ -1,7 +1,6 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
2
  Object.defineProperty;
3
3
  //#endregion
4
- let node_util = require("node:util");
5
4
  //#region src/constants.ts
6
5
  const visitorDepths = {
7
6
  shallow: "shallow",
@@ -97,7 +96,7 @@ const mediaTypes = {
97
96
  videoMp4: "video/mp4"
98
97
  };
99
98
  //#endregion
100
- //#region ../../internals/utils/dist/index.js
99
+ //#region ../../internals/utils/src/casing.ts
101
100
  /**
102
101
  * Shared implementation for camelCase and PascalCase conversion.
103
102
  * Splits on common word boundaries (spaces, hyphens, underscores, dots, slashes, colons)
@@ -154,214 +153,17 @@ function pascalCase(text, { isFile, prefix = "", suffix = "" } = {}) {
154
153
  }) : camelCase(part));
155
154
  return toCamelOrPascal(`${prefix} ${text} ${suffix}`, true);
156
155
  }
157
- /** Returns a `CLIAdapter` with type inference. Pass a different adapter to `createCLI` to swap the CLI engine. */
158
- function defineCLIAdapter(adapter) {
159
- return adapter;
160
- }
161
- /**
162
- * Serializes `CommandDefinition[]` to a plain, JSON-serializable structure.
163
- * Use to expose CLI capabilities to AI agents or MCP tools.
164
- */
165
- function getCommandSchema(defs) {
166
- return defs.map(serializeCommand);
167
- }
168
- function serializeCommand(def) {
169
- return {
170
- name: def.name,
171
- description: def.description,
172
- arguments: def.arguments,
173
- options: serializeOptions(def.options ?? {}),
174
- subCommands: def.subCommands ? def.subCommands.map(serializeCommand) : []
175
- };
176
- }
177
- function serializeOptions(options) {
178
- return Object.entries(options).map(([name, opt]) => {
179
- return {
180
- name,
181
- flags: `${opt.short ? `-${opt.short}, ` : ""}--${name}${opt.type === "string" ? ` <${opt.hint ?? name}>` : ""}`,
182
- type: opt.type,
183
- description: opt.description,
184
- ...opt.default !== void 0 ? { default: opt.default } : {},
185
- ...opt.hint ? { hint: opt.hint } : {},
186
- ...opt.enum ? { enum: opt.enum } : {},
187
- ...opt.required ? { required: opt.required } : {}
188
- };
189
- });
190
- }
191
- /** Prints formatted help output for a command using its `CommandDefinition`. */
192
- function renderHelp(def, parentName) {
193
- const schema = getCommandSchema([def])[0];
194
- const programName = parentName ? `${parentName} ${schema.name}` : schema.name;
195
- const argsPart = schema.arguments?.length ? ` ${schema.arguments.join(" ")}` : "";
196
- const subCmdPart = schema.subCommands.length ? " <command>" : "";
197
- console.log(`\n${(0, node_util.styleText)("bold", "Usage:")} ${programName}${argsPart}${subCmdPart} [options]\n`);
198
- if (schema.description) console.log(` ${schema.description}\n`);
199
- if (schema.subCommands.length) {
200
- console.log((0, node_util.styleText)("bold", "Commands:"));
201
- for (const sub of schema.subCommands) console.log(` ${(0, node_util.styleText)("cyan", sub.name.padEnd(16))}${sub.description}`);
202
- console.log();
203
- }
204
- const options = [...schema.options, {
205
- name: "help",
206
- flags: "-h, --help",
207
- type: "boolean",
208
- description: "Show help"
209
- }];
210
- console.log((0, node_util.styleText)("bold", "Options:"));
211
- for (const opt of options) {
212
- const flags = (0, node_util.styleText)("cyan", opt.flags.padEnd(30));
213
- const defaultPart = opt.default !== void 0 ? (0, node_util.styleText)("dim", ` (default: ${opt.default})`) : "";
214
- console.log(` ${flags}${opt.description}${defaultPart}`);
215
- }
216
- console.log();
217
- }
218
- function buildParseOptions(def) {
219
- const result = { help: {
220
- type: "boolean",
221
- short: "h"
222
- } };
223
- for (const [name, opt] of Object.entries(def.options ?? {})) result[name] = {
224
- type: opt.type,
225
- ...opt.short ? { short: opt.short } : {},
226
- ...opt.default !== void 0 ? { default: opt.default } : {}
227
- };
228
- return result;
229
- }
230
- async function runCommand(def, argv, parentName) {
231
- const parseOptions = buildParseOptions(def);
232
- let parsed;
233
- try {
234
- const result = (0, node_util.parseArgs)({
235
- args: argv,
236
- options: parseOptions,
237
- allowPositionals: true,
238
- strict: false
239
- });
240
- parsed = {
241
- values: result.values,
242
- positionals: result.positionals
243
- };
244
- } catch {
245
- renderHelp(def, parentName);
246
- process.exit(1);
247
- }
248
- if (parsed.values["help"]) {
249
- renderHelp(def, parentName);
250
- process.exit(0);
251
- }
252
- for (const [name, opt] of Object.entries(def.options ?? {})) if (opt.required && parsed.values[name] === void 0) {
253
- console.error((0, node_util.styleText)("red", `Error: --${name} is required`));
254
- renderHelp(def, parentName);
255
- process.exit(1);
256
- }
257
- if (!def.run) {
258
- renderHelp(def, parentName);
259
- process.exit(0);
260
- }
261
- try {
262
- await def.run(parsed);
263
- } catch (err) {
264
- console.error((0, node_util.styleText)("red", `Error: ${err instanceof Error ? err.message : String(err)}`));
265
- renderHelp(def, parentName);
266
- process.exit(1);
267
- }
268
- }
269
- function printRootHelp(programName, version, defs) {
270
- console.log(`\n${(0, node_util.styleText)("bold", "Usage:")} ${programName} <command> [options]\n`);
271
- console.log(` Kubb generation — v${version}\n`);
272
- console.log((0, node_util.styleText)("bold", "Commands:"));
273
- for (const def of defs) console.log(` ${(0, node_util.styleText)("cyan", def.name.padEnd(16))}${def.description}`);
274
- console.log();
275
- console.log((0, node_util.styleText)("bold", "Options:"));
276
- console.log(` ${(0, node_util.styleText)("cyan", "-v, --version".padEnd(30))}Show version number`);
277
- console.log(` ${(0, node_util.styleText)("cyan", "-h, --help".padEnd(30))}Show help`);
278
- console.log();
279
- console.log(`Run ${(0, node_util.styleText)("cyan", `${programName} <command> --help`)} for command-specific help.\n`);
280
- }
281
- defineCLIAdapter({
282
- renderHelp(def, parentName) {
283
- renderHelp(def, parentName);
284
- },
285
- async run(defs, argv, opts) {
286
- const { programName, defaultCommandName, version } = opts;
287
- const args = argv.length >= 2 && argv[0]?.includes("node") ? argv.slice(2) : argv;
288
- if (args[0] === "--version" || args[0] === "-v") {
289
- console.log(version);
290
- process.exit(0);
291
- }
292
- if (args[0] === "--help" || args[0] === "-h") {
293
- printRootHelp(programName, version, defs);
294
- process.exit(0);
295
- }
296
- if (args.length === 0) {
297
- const defaultDef = defs.find((d) => d.name === defaultCommandName);
298
- if (defaultDef?.run) await runCommand(defaultDef, [], programName);
299
- else printRootHelp(programName, version, defs);
300
- return;
301
- }
302
- const [first, ...rest] = args;
303
- const isKnownSubcommand = defs.some((d) => d.name === first);
304
- let def;
305
- let commandArgv;
306
- let parentName;
307
- if (isKnownSubcommand) {
308
- def = defs.find((d) => d.name === first);
309
- commandArgv = rest;
310
- parentName = programName;
311
- } else {
312
- def = defs.find((d) => d.name === defaultCommandName);
313
- commandArgv = args;
314
- parentName = programName;
315
- }
316
- if (!def) {
317
- console.error(`Unknown command: ${first}`);
318
- printRootHelp(programName, version, defs);
319
- process.exit(1);
320
- }
321
- if (def.subCommands?.length) {
322
- const [subName, ...subRest] = commandArgv;
323
- const subDef = def.subCommands.find((s) => s.name === subName);
324
- if (subName === "--help" || subName === "-h") {
325
- renderHelp(def, parentName);
326
- process.exit(0);
327
- }
328
- if (!subDef) {
329
- renderHelp(def, parentName);
330
- process.exit(subName ? 1 : 0);
331
- }
332
- await runCommand(subDef, subRest, `${parentName} ${def.name}`);
333
- return;
334
- }
335
- await runCommand(def, commandArgv, parentName);
336
- }
337
- });
338
- /**
339
- * Parses a CSS hex color string (`#RGB`) into its RGB channels.
340
- * Falls back to `255` for any channel that cannot be parsed.
341
- */
342
- function parseHex(color) {
343
- const int = Number.parseInt(color.replace("#", ""), 16);
344
- return Number.isNaN(int) ? {
345
- r: 255,
346
- g: 255,
347
- b: 255
348
- } : {
349
- r: int >> 16 & 255,
350
- g: int >> 8 & 255,
351
- b: int & 255
352
- };
353
- }
354
- /**
355
- * Returns a function that wraps a string in a 24-bit ANSI true-color escape sequence
356
- * for the given hex color.
357
- */
358
- function hex(color) {
359
- const { r, g, b } = parseHex(color);
360
- return (text) => `\x1b[38;2;${r};${g};${b}m${text}\x1b[0m`;
361
- }
362
- hex("#F55A17"), hex("#F5A217"), hex("#F58517"), hex("#B45309"), hex("#FFFFFF"), hex("#adadc6"), hex("#FDA4AF");
156
+ //#endregion
157
+ //#region ../../internals/utils/src/reserved.ts
363
158
  /**
364
159
  * Returns `true` when `name` is a syntactically valid JavaScript variable name.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * isValidVarName('status') // true
164
+ * isValidVarName('class') // false (reserved word)
165
+ * isValidVarName('42foo') // false (starts with digit)
166
+ * ```
365
167
  */
366
168
  function isValidVarName(name) {
367
169
  try {
@@ -835,9 +637,9 @@ function createPrinterFactory(getKey) {
835
637
  options: resolvedOptions,
836
638
  print: (node) => {
837
639
  const key = getKey(node);
838
- if (key === void 0) return void 0;
640
+ if (key === void 0) return null;
839
641
  const handler = nodes[key];
840
- if (!handler) return void 0;
642
+ if (!handler) return null;
841
643
  return handler.call(context, node);
842
644
  }
843
645
  };
@@ -1340,11 +1142,11 @@ function collect(node, options) {
1340
1142
  //#endregion
1341
1143
  //#region src/resolvers.ts
1342
1144
  function findDiscriminator(mapping, ref) {
1343
- if (!mapping || !ref) return void 0;
1344
- return Object.entries(mapping).find(([, value]) => value === ref)?.[0];
1145
+ if (!mapping || !ref) return null;
1146
+ return Object.entries(mapping).find(([, value]) => value === ref)?.[0] ?? null;
1345
1147
  }
1346
1148
  function childName(parentName, propName) {
1347
- return parentName ? pascalCase([parentName, propName].join(" ")) : void 0;
1149
+ return parentName ? pascalCase([parentName, propName].join(" ")) : null;
1348
1150
  }
1349
1151
  function enumPropName(parentName, propName, enumSuffix) {
1350
1152
  return pascalCase([