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