@kubb/parser-ts 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
@@ -25,7 +25,10 @@ let _kubb_core = require("@kubb/core");
25
25
  let node_path = require("node:path");
26
26
  let typescript = require("typescript");
27
27
  typescript = __toESM(typescript, 1);
28
- //#region src/constants.ts
28
+ /**
29
+ * Indentation unit prepended once per nesting level when pretty-printing.
30
+ */
31
+ const INDENT = " ".repeat(2);
29
32
  /**
30
33
  * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
31
34
  * intact by only trimming the last run of non-`/`/`.` characters).
@@ -101,14 +104,39 @@ function printNodes(nodes) {
101
104
  return parts.join("\n");
102
105
  }
103
106
  /**
104
- * Indents every non-empty line of `text` by `spaces` spaces.
107
+ * Indents every non-empty line of `text` by one indent unit. Pass a number to repeat
108
+ * {@link INDENT_CHAR} that many times, or a string to use as the indent verbatim.
105
109
  */
106
- function indentLines(text, spaces = 2) {
110
+ function indentLines(text, indent = INDENT) {
107
111
  if (!text) return "";
108
- const pad = " ".repeat(spaces);
112
+ const pad = typeof indent === "string" ? indent : " ".repeat(indent);
109
113
  return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : "").join("\n");
110
114
  }
111
115
  /**
116
+ * Removes the common leading whitespace shared by every non-blank line and trims
117
+ * surrounding blank lines, normalizing multi-line content authored inside an
118
+ * indented template literal back to a column-zero baseline. Leading whitespace is
119
+ * counted by character, so N tabs and N spaces are treated as the same depth.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * dedent('\n foo\n bar\n ')
124
+ * // 'foo\n bar'
125
+ * ```
126
+ */
127
+ function dedent(text) {
128
+ if (!text) return "";
129
+ const lines = text.split("\n");
130
+ const isBlank = (line) => line.trim() === "";
131
+ const start = lines.findIndex((line) => !isBlank(line));
132
+ if (start === -1) return "";
133
+ const end = lines.findLastIndex((line) => !isBlank(line));
134
+ const trimmed = lines.slice(start, end + 1);
135
+ const indents = trimmed.filter((line) => !isBlank(line)).map((line) => line.match(/^\s*/)?.[0].length ?? 0);
136
+ const min = indents.length ? Math.min(...indents) : 0;
137
+ return trimmed.map((line) => isBlank(line) ? "" : line.slice(min)).join("\n");
138
+ }
139
+ /**
112
140
  * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
113
141
  * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
114
142
  */
@@ -311,8 +339,8 @@ function printArrowFunction(node) {
311
339
  */
312
340
  function printCodeNode(node) {
313
341
  if (node.kind === "Break") return "";
314
- if (node.kind === "Text") return node.value;
315
- if (node.kind === "Jsx") return node.value;
342
+ if (node.kind === "Text") return dedent(node.value);
343
+ if (node.kind === "Jsx") return dedent(node.value);
316
344
  if (node.kind === "Const") return printConst(node);
317
345
  if (node.kind === "Type") return printType(node);
318
346
  if (node.kind === "Function") return printFunction(node);
@@ -325,45 +353,68 @@ function printCodeNode(node) {
325
353
  * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via
326
354
  * {@link printCodeNode}.
327
355
  *
356
+ * Top-level declarations are separated by a blank line so the source reads
357
+ * cleanly without an external formatter.
358
+ *
328
359
  * @example From nodes
329
360
  * ```ts
330
361
  * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })
331
- * // 'const x = 1\nx.toString()'
362
+ * // 'const x = 1\n\nx.toString()'
332
363
  * ```
333
364
  */
334
365
  function printSource(node) {
335
366
  const nodes = node.nodes;
336
367
  if (!nodes || nodes.length === 0) return "";
337
- const parts = [];
338
- for (const child of nodes) parts.push(printCodeNode(child));
339
- return parts.join("\n");
368
+ return nodes.map((child) => printCodeNode(child)).filter(Boolean).join("\n\n");
340
369
  }
341
- function createImport({ name, path, root, isTypeOnly: isTypeOnlyRaw = false, isNameSpace: isNameSpaceRaw = false }) {
342
- const isTypeOnly = isTypeOnlyRaw ?? false;
343
- const isNameSpace = isNameSpaceRaw ?? false;
344
- const resolvePath = root ? getRelativePath(root, path) : path;
345
- if (!Array.isArray(name)) {
346
- if (isNameSpace) return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamespaceImport(factory.createIdentifier(name))), factory.createStringLiteral(resolvePath), void 0);
347
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), factory.createStringLiteral(resolvePath), void 0);
348
- }
349
- const specifiers = name.map((item) => {
350
- if (typeof item === "object") {
351
- const { propertyName, name: alias } = item;
352
- return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : void 0, factory.createIdentifier(alias ?? propertyName));
353
- }
354
- return factory.createImportSpecifier(false, void 0, factory.createIdentifier(item));
355
- });
356
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(specifiers)), factory.createStringLiteral(resolvePath), void 0);
370
+ /**
371
+ * Wraps a module specifier in single quotes, escaping any embedded backslash or quote so the emitted
372
+ * statement stays valid even for unusual paths.
373
+ */
374
+ function quoteModulePath(path) {
375
+ return `'${path.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
357
376
  }
358
- function createExport({ path, asAlias: asAliasRaw, isTypeOnly: isTypeOnlyRaw = false, name }) {
359
- const asAlias = asAliasRaw ?? false;
360
- const isTypeOnly = isTypeOnlyRaw ?? false;
361
- if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true: ${name}`);
377
+ /**
378
+ * Renders an import declaration string in the repo style (single quotes, no semicolons), mirroring
379
+ * the shapes that {@link createImport} builds: default, namespace (`* as`), and named imports with
380
+ * `{ a as b }` aliases, each optionally `type`-only. `path` is used verbatim, so resolve it first.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * printImport({ name: ['z'], path: './zod.ts' })
385
+ * // "import { z } from './zod.ts'"
386
+ * ```
387
+ */
388
+ function printImport({ name, path, isTypeOnly = false, isNameSpace = false }) {
389
+ const typePrefix = isTypeOnly ? "type " : "";
390
+ const from = quoteModulePath(path);
362
391
  if (!Array.isArray(name)) {
363
- const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name;
364
- return factory.createExportDeclaration(void 0, isTypeOnly, asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, factory.createStringLiteral(path), void 0);
392
+ if (isNameSpace) return `import ${typePrefix}* as ${name} from ${from}`;
393
+ return `import ${typePrefix}${name} from ${from}`;
365
394
  }
366
- return factory.createExportDeclaration(void 0, isTypeOnly, factory.createNamedExports(name.map((propertyName) => factory.createExportSpecifier(false, void 0, typeof propertyName === "string" ? factory.createIdentifier(propertyName) : propertyName))), factory.createStringLiteral(path), void 0);
395
+ return `import ${typePrefix}{ ${name.map((item) => {
396
+ if (typeof item === "object") return item.name ? `${item.propertyName} as ${item.name}` : item.propertyName;
397
+ return item;
398
+ }).join(", ")} } from ${from}`;
399
+ }
400
+ /**
401
+ * Renders an export declaration string in the repo style (single quotes, no semicolons), mirroring
402
+ * the shapes that {@link createExport} builds: named re-exports, namespace alias (`* as name`), and
403
+ * wildcard, each optionally `type`-only. `path` is used verbatim, so resolve it first.
404
+ *
405
+ * @example
406
+ * ```ts
407
+ * printExport({ name: ['Pet', 'Order'], path: './models.ts' })
408
+ * // "export { Pet, Order } from './models.ts'"
409
+ * ```
410
+ */
411
+ function printExport({ path, name, isTypeOnly = false, asAlias = false }) {
412
+ const typePrefix = isTypeOnly ? "type " : "";
413
+ const from = quoteModulePath(path);
414
+ if (Array.isArray(name)) return `export ${typePrefix}{ ${name.map((item) => typeof item === "string" ? item : item.text).join(", ")} } from ${from}`;
415
+ if (asAlias && name) return `export ${typePrefix}* as ${LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name} from ${from}`;
416
+ if (name) console.warn(`When using name as string, asAlias should be true: ${name}`);
417
+ return `export ${typePrefix}* from ${from}`;
367
418
  }
368
419
  //#endregion
369
420
  //#region src/parserTs.ts
@@ -404,26 +455,27 @@ const parserTs = (0, _kubb_core.defineParser)({
404
455
  if (sourceStr) sourceParts.push(sourceStr.trimEnd());
405
456
  }
406
457
  const source = sourceParts.join("\n\n");
407
- const importNodes = [];
458
+ const importLines = [];
408
459
  for (const item of file.imports) {
409
460
  const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
410
- importNodes.push(createImport({
461
+ importLines.push(printImport({
411
462
  name: item.name,
412
463
  path: resolveOutputPath(importPath, options, Boolean(item.root)),
413
464
  isTypeOnly: item.isTypeOnly,
414
465
  isNameSpace: item.isNameSpace
415
466
  }));
416
467
  }
417
- const exportNodes = [];
418
- for (const item of file.exports) exportNodes.push(createExport({
468
+ const exportLines = [];
469
+ for (const item of file.exports) exportLines.push(printExport({
419
470
  name: item.name,
420
471
  path: resolveOutputPath(item.path, options, true),
421
472
  isTypeOnly: item.isTypeOnly,
422
473
  asAlias: item.asAlias
423
474
  }));
475
+ const importExportBlock = [...importLines, ...exportLines].join("\n");
424
476
  return [
425
477
  file.banner,
426
- print(...importNodes, ...exportNodes),
478
+ importExportBlock,
427
479
  source,
428
480
  file.footer
429
481
  ].filter((segment) => Boolean(segment)).map((s) => s.trimEnd()).join("\n\n");
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","names":["ts"],"sources":["../src/constants.ts","../src/utils.ts","../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["/**\n * Number of spaces used to indent a nested block when pretty-printing.\n */\nexport const INDENT_SIZE = 2 as const\n\n/**\n * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`\n * intact by only trimming the last run of non-`/`/`.` characters).\n */\nexport const FILE_EXTENSION_PATTERN = /\\.[^/.]+$/\n\n/**\n * Matches Windows-style backslash path separators.\n */\nexport const WINDOWS_PATH_SEPARATOR = /\\\\/g\n\n/**\n * Matches `*\\/` in free-form text so JSDoc bodies can neutralize premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalizing CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalizing TypeScript printer output.\n */\nexport const CRLF_PATTERN = /\\r\\n/g\n\n/**\n * Matches an identifier that starts with a digit. JavaScript disallows this,\n * so the printer prefixes such names with `_`.\n */\nexport const LEADING_DIGIT_PATTERN = /^\\d/\n\n/**\n * Relative path prefix used to detect traversal segments (`../`).\n */\nexport const PARENT_DIRECTORY_PREFIX = '../' as const\n\n/**\n * Relative path prefix used when resolving imports within the output root.\n */\nexport const CURRENT_DIRECTORY_PREFIX = './' as const\n","import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport ts from 'typescript'\nimport {\n CARRIAGE_RETURN_PATTERN,\n CRLF_PATTERN,\n CURRENT_DIRECTORY_PREFIX,\n FILE_EXTENSION_PATTERN,\n INDENT_SIZE,\n JSDOC_TERMINATOR_PATTERN,\n LEADING_DIGIT_PATTERN,\n PARENT_DIRECTORY_PREFIX,\n WINDOWS_PATH_SEPARATOR,\n} from './constants.ts'\n\nconst { factory } = ts\n\n/**\n * Normalizes a file-system path to POSIX separators and strips any leading `../` segment.\n */\nexport function slash(path: string): string {\n return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, '/').replace(PARENT_DIRECTORY_PREFIX, '')\n}\n\n/**\n * Resolves `filePath` relative to `rootDir` and returns a POSIX-style path\n * prefixed with `./` when the target sits inside the root, or `../` when it escapes it.\n */\nexport function getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith(PARENT_DIRECTORY_PREFIX) ? slashed : `${CURRENT_DIRECTORY_PREFIX}${slashed}`\n}\n\n/**\n * Strips the trailing file extension (for example `.ts`) from a path.\n * Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.\n */\nexport function trimExtName(text: string): string {\n return text.replace(FILE_EXTENSION_PATTERN, '')\n}\n\n/**\n * Rewrites an import/export path so its extension matches the caller-supplied\n * `options.extname`. When the source path has no extension the original is kept,\n * so virtual/module-only paths flow through unchanged.\n */\nexport function resolveOutputPath(path: string, options: { extname?: string } | undefined, rootAware: boolean): string {\n const hasExtname = FILE_EXTENSION_PATTERN.test(path)\n if (options?.extname && hasExtname) {\n return `${trimExtName(path)}${options.extname}`\n }\n return rootAware ? trimExtName(path) : path\n}\n\n/**\n * Serializes the body / value content from a `nodes` array.\n *\n * Each element is either a raw string or a structured {@link CodeNode}\n * (recursively converted via {@link printCodeNode}).\n * Elements are joined with `\\n`.\n */\nexport function printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n\n const parts: Array<string> = []\n\n for (const node of nodes) {\n parts.push(printCodeNode(node))\n }\n\n return parts.join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nexport function indentLines(text: string, spaces: number = INDENT_SIZE): string {\n if (!text) return ''\n const pad = ' '.repeat(spaces)\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.\n * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.\n */\nexport function formatGenerics(generics: FunctionNode['generics'] | ArrowFunctionNode['generics']): string {\n if (!generics) return ''\n return `<${Array.isArray(generics) ? generics.join(', ') : generics}>`\n}\n\n/**\n * Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).\n * Returns an empty string when no return type is provided.\n */\nexport function formatReturnType(returnType: string | null | undefined, isAsync: boolean | null | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\n}\n\n/**\n * Module-scoped TypeScript printer instance. `ts.createPrinter()` is stateless across calls\n * (it does not mutate the source file) so a single instance can be safely reused for every\n * `print()` call. Hoisting it out of `print()` avoids re-running the printer initialization\n * for each file's import/export section.\n */\nconst TS_PRINTER = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n})\n\n/**\n * Module-scoped source file used as the print target. `printList` only reads the source\n * file's compiler options / language version. It never mutates it.\n */\nconst PRINT_SOURCE_FILE = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n// Pre-warm the printer at module load. The first `printList` call lazily initializes\n// the printer's internal string-builder and identifier tables. Doing it once at import\n// time keeps that cost off the critical path for short-lived CLI builds.\nTS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray([]), PRINT_SOURCE_FILE)\n\n/**\n * Converts TypeScript/TSX AST nodes to a string using the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const filtered = elements.filter(Boolean)\n if (filtered.length === 0) return ''\n\n const output = TS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray(filtered), PRINT_SOURCE_FILE)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Converts a {@link JSDocNode} to a JSDoc comment block string.\n *\n * @example\n * ```ts\n * printJSDoc({ comments: ['@description A pet', '@deprecated'] })\n * // /**\n * // * @description A pet\n * // * @deprecated\n * // *\\/\n * ```\n */\nexport function printJSDoc(jsDoc: JSDocNode): string {\n const comments = (jsDoc.comments ?? []).filter((c) => c != null)\n if (comments.length === 0) return ''\n\n const lines = comments\n .flatMap((c) => c.split(/\\r?\\n/))\n .map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, '* /').replace(CARRIAGE_RETURN_PATTERN, ''))\n .filter((l) => l.trim().length > 0)\n\n if (lines.length === 0) return ''\n\n return ['/**', ...lines.map((l) => ` * ${l}`), ' */'].join('\\n')\n}\n\n/**\n * Converts a {@link ConstNode} to a TypeScript `const` declaration string.\n *\n * Mirrors the `Const` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printConst(createConst({ name: 'pet', export: true, nodes: ['{}'] }))\n * // 'export const pet = {}'\n * ```\n *\n * @example With type and `as const`\n * ```ts\n * printConst(createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true, nodes: ['[]'] }))\n * // 'export const pets: Pet[] = [] as const'\n * ```\n */\nexport function printConst(node: ConstNode): string {\n const { name, export: canExport, type, JSDoc, asConst, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('const ')\n parts.push(name)\n if (type) {\n parts.push(`: ${type}`)\n }\n parts.push(' = ')\n parts.push(body)\n if (asConst) parts.push(' as const')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link TypeNode} to a TypeScript `type` alias declaration string.\n *\n * Mirrors the `Type` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printType(createType({ name: 'Pet', export: true, nodes: ['{ id: number }'] }))\n * // 'export type Pet = { id: number }'\n * ```\n */\nexport function printType(node: TypeNode): string {\n const { name, export: canExport, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('type ')\n parts.push(name)\n parts.push(' = ')\n parts.push(body)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link FunctionNode} to a TypeScript `function` declaration string.\n *\n * Mirrors the `Function` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printFunction(createFunction({ name: 'getPet', export: true, params: 'id: string', returnType: 'Pet', nodes: ['return fetch(id)'] }))\n * // 'export function getPet(id: string): Pet {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Async with generics\n * ```ts\n * printFunction(createFunction({ name: 'fetchPet', export: true, async: true, generics: ['T'], params: 'id: string', returnType: 'T' }))\n * // 'export async function fetchPet<T>(id: string): Promise<T> {\\n}'\n * ```\n */\nexport function printFunction(node: FunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n if (isAsync) parts.push('async ')\n parts.push('function ')\n parts.push(name)\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(' {')\n if (indented) {\n parts.push(`\\n${indented}\\n`)\n }\n parts.push('}')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts an {@link ArrowFunctionNode} to a TypeScript arrow function declaration string.\n *\n * Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.\n *\n * @example Multi-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'getPet', export: true, params: 'id: string', nodes: ['return fetch(id)'] }))\n * // 'export const getPet = (id: string) => {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Single-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'double', params: 'n: number', singleLine: true, nodes: ['n * 2'] }))\n * // 'const double = (n: number) => n * 2'\n * ```\n */\nexport function printArrowFunction(node: ArrowFunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n parts.push('const ')\n parts.push(name)\n parts.push(' = ')\n if (isAsync) parts.push('async ')\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(arrowBody)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link CodeNode} to its TypeScript string representation.\n *\n * Dispatches to the appropriate printer based on the node's `kind`.\n *\n * @example\n * ```ts\n * printCodeNode(createConst({ name: 'x', nodes: ['1'] }))\n * // 'const x = 1'\n * ```\n */\nexport function printCodeNode(node: CodeNode): string {\n if (node.kind === 'Break') return ''\n if (node.kind === 'Text') return (node as TextNode).value\n if (node.kind === 'Jsx') return (node as JsxNode).value\n if (node.kind === 'Const') return printConst(node)\n if (node.kind === 'Type') return printType(node)\n if (node.kind === 'Function') return printFunction(node)\n if (node.kind === 'ArrowFunction') return printArrowFunction(node)\n return ''\n}\n\n/**\n * Converts a {@link SourceNode} to its TypeScript string representation.\n *\n * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via\n * {@link printCodeNode}.\n *\n * @example From nodes\n * ```ts\n * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })\n * // 'const x = 1\\nx.toString()'\n * ```\n */\nexport function printSource(node: SourceNode): string {\n const nodes = node.nodes\n\n if (!nodes || nodes.length === 0) return ''\n const parts: Array<string> = []\n\n for (const child of nodes) {\n parts.push(printCodeNode(child as CodeNode))\n }\n\n return parts.join('\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly: isTypeOnlyRaw = false,\n isNameSpace: isNameSpaceRaw = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string | null\n /** @default false */\n isTypeOnly?: boolean | null\n /** @default false */\n isNameSpace?: boolean | null\n}): ts.ImportDeclaration {\n const isTypeOnly = isTypeOnlyRaw ?? false\n const isNameSpace = isNameSpaceRaw ?? false\n const resolvePath = root ? getRelativePath(root, path) : path\n\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias: asAliasRaw,\n isTypeOnly: isTypeOnlyRaw = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean | null\n /** @default false */\n isTypeOnly?: boolean | null\n name?: string | Array<ts.Identifier | string> | null\n}): ts.ExportDeclaration {\n const asAlias = asAliasRaw ?? false\n const isTypeOnly = isTypeOnlyRaw ?? false\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) =>\n factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName),\n ),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n","import type { FileNode, SourceNode } from '@kubb/ast'\nimport { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { createExport, createImport, getRelativePath, print, printSource, resolveOutputPath } from './utils.ts'\n\n/**\n * Default Kubb parser for `.ts` and `.js` files. Takes the universal AST\n * produced by an adapter and prints it as TypeScript source using the official\n * TypeScript compiler. Imports and exports are rewritten based on each file's\n * metadata.\n *\n * Used automatically when no `parsers` option is set on `defineConfig`. Use\n * `parserTsx` instead for React projects that emit JSX.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTs } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTs],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTs = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n const sourceStr = printSource(item as SourceNode)\n if (sourceStr) {\n sourceParts.push(sourceStr.trimEnd())\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importNodes: Array<ts.ImportDeclaration> = []\n for (const item of (file as FileNode).imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n importNodes.push(\n createImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: resolveOutputPath(importPath, options, Boolean(item.root)),\n isTypeOnly: item.isTypeOnly,\n isNameSpace: item.isNameSpace,\n }),\n )\n }\n\n const exportNodes: Array<ts.ExportDeclaration> = []\n for (const item of (file as FileNode).exports) {\n exportNodes.push(\n createExport({\n name: item.name as string | Array<ts.Identifier | string> | null | undefined,\n path: resolveOutputPath(item.path, options, true),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const parts = [file.banner, print(...importNodes, ...exportNodes), source, file.footer]\n .filter((segment): segment is string => Boolean(segment))\n .map((s) => s.trimEnd())\n return parts.join('\\n\\n')\n },\n})\n","import { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { parserTs } from './parserTs.ts'\nimport { print } from './utils.ts'\n\n/**\n * Kubb parser for `.tsx` and `.jsx` files. Delegates to `parserTs` because the\n * TypeScript compiler handles JSX natively via `ScriptKind.TSX`.\n *\n * Add to the `parsers` array on `defineConfig` when generating components for\n * React (or any framework that emits JSX).\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTsx } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTsx],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTsx = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASA,MAAa,yBAAyB;;;;AAKtC,MAAa,yBAAyB;;;;;AAMtC,MAAa,2BAA2B;;;;AAKxC,MAAa,0BAA0B;;;;AAKvC,MAAa,eAAe;;;;;AAM5B,MAAa,wBAAwB;;;ACrBrC,MAAM,EAAE,YAAYA,WAAAA;;;;AAKpB,SAAgB,MAAM,MAAsB;CAC1C,QAAA,GAAA,UAAA,WAAiB,IAAI,EAAE,WAAW,wBAAwB,GAAG,EAAE,QAAA,OAAiC,EAAE;AACpG;;;;;AAMA,SAAgB,gBAAgB,SAAiB,UAA0B;CAEzE,MAAM,UAAU,OAAA,GAAA,UAAA,UADK,SAAS,QACN,CAAC;CACzB,OAAO,QAAQ,WAAA,KAAkC,IAAI,UAAU,KAA8B;AAC/F;;;;;AAMA,SAAgB,YAAY,MAAsB;CAChD,OAAO,KA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;IACjB,aAAa,KAAK;GACpB,CAAC,CACH;EACF;EAEA,MAAM,cAA2C,CAAC;EAClD,KAAK,MAAM,QAAS,KAAkB,SACpC,YAAY,KACV,aAAa;GACX,MAAM,KAAK;GACX,MAAM,kBAAkB,KAAK,MAAM,SAAS,IAAI;GAChD,YAAY,KAAK;GACjB,SAAS,KAAK;EAChB,CAAC,CACH;EAMF,OAHc;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,WAAW;GAAG;GAAQ,KAAK;EAAM,EACnF,QAAQ,YAA+B,QAAQ,OAAO,CAAC,EACvD,KAAK,MAAM,EAAE,QAAQ,CACb,EAAE,KAAK,MAAM;CAC1B;AACF,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AChDD,MAAa,aAAA,GAAA,WAAA,cAAyB;CACpC,MAAM;CACN,UAAU,CAAC,QAAQ,MAAM;CACzB,MAAM,GAAG,OAAuB;EAC9B,OAAO,MAAM,GAAG,KAAK;CACvB;CACA,MAAM,MAAM,UAAU,EAAE,SAAS,OAAO,GAAG;EACzC,OAAO,SAAS,MAAM,MAAM,OAAO;CACrC;AACF,CAAC"}
1
+ {"version":3,"file":"index.cjs","names":["ts"],"sources":["../src/constants.ts","../src/utils.ts","../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["/**\n * Character used for a single indent step. Set to `'\\t'` to emit tab-indented output.\n */\nexport const INDENT_CHAR = ' '\n\n/**\n * Number of {@link INDENT_CHAR} repeats that make up one nesting level.\n */\nexport const INDENT_SIZE = 2 as const\n\n/**\n * Indentation unit prepended once per nesting level when pretty-printing.\n */\nexport const INDENT = INDENT_CHAR.repeat(INDENT_SIZE)\n\n/**\n * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`\n * intact by only trimming the last run of non-`/`/`.` characters).\n */\nexport const FILE_EXTENSION_PATTERN = /\\.[^/.]+$/\n\n/**\n * Matches Windows-style backslash path separators.\n */\nexport const WINDOWS_PATH_SEPARATOR = /\\\\/g\n\n/**\n * Matches `*\\/` in free-form text so JSDoc bodies can neutralize premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalizing CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalizing TypeScript printer output.\n */\nexport const CRLF_PATTERN = /\\r\\n/g\n\n/**\n * Matches an identifier that starts with a digit. JavaScript disallows this,\n * so the printer prefixes such names with `_`.\n */\nexport const LEADING_DIGIT_PATTERN = /^\\d/\n\n/**\n * Relative path prefix used to detect traversal segments (`../`).\n */\nexport const PARENT_DIRECTORY_PREFIX = '../' as const\n\n/**\n * Relative path prefix used when resolving imports within the output root.\n */\nexport const CURRENT_DIRECTORY_PREFIX = './' as const\n","import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport ts from 'typescript'\nimport {\n CARRIAGE_RETURN_PATTERN,\n CRLF_PATTERN,\n CURRENT_DIRECTORY_PREFIX,\n FILE_EXTENSION_PATTERN,\n INDENT,\n INDENT_CHAR,\n JSDOC_TERMINATOR_PATTERN,\n LEADING_DIGIT_PATTERN,\n PARENT_DIRECTORY_PREFIX,\n WINDOWS_PATH_SEPARATOR,\n} from './constants.ts'\n\nconst { factory } = ts\n\n/**\n * Normalizes a file-system path to POSIX separators and strips any leading `../` segment.\n */\nexport function slash(path: string): string {\n return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, '/').replace(PARENT_DIRECTORY_PREFIX, '')\n}\n\n/**\n * Resolves `filePath` relative to `rootDir` and returns a POSIX-style path\n * prefixed with `./` when the target sits inside the root, or `../` when it escapes it.\n */\nexport function getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith(PARENT_DIRECTORY_PREFIX) ? slashed : `${CURRENT_DIRECTORY_PREFIX}${slashed}`\n}\n\n/**\n * Strips the trailing file extension (for example `.ts`) from a path.\n * Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.\n */\nexport function trimExtName(text: string): string {\n return text.replace(FILE_EXTENSION_PATTERN, '')\n}\n\n/**\n * Rewrites an import/export path so its extension matches the caller-supplied\n * `options.extname`. When the source path has no extension the original is kept,\n * so virtual/module-only paths flow through unchanged.\n */\nexport function resolveOutputPath(path: string, options: { extname?: string } | undefined, rootAware: boolean): string {\n const hasExtname = FILE_EXTENSION_PATTERN.test(path)\n if (options?.extname && hasExtname) {\n return `${trimExtName(path)}${options.extname}`\n }\n return rootAware ? trimExtName(path) : path\n}\n\n/**\n * Serializes the body / value content from a `nodes` array.\n *\n * Each element is either a raw string or a structured {@link CodeNode}\n * (recursively converted via {@link printCodeNode}).\n * Elements are joined with `\\n`.\n */\nexport function printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n\n const parts: Array<string> = []\n\n for (const node of nodes) {\n parts.push(printCodeNode(node))\n }\n\n return parts.join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by one indent unit. Pass a number to repeat\n * {@link INDENT_CHAR} that many times, or a string to use as the indent verbatim.\n */\nexport function indentLines(text: string, indent: number | string = INDENT): string {\n if (!text) return ''\n const pad = typeof indent === 'string' ? indent : INDENT_CHAR.repeat(indent)\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Removes the common leading whitespace shared by every non-blank line and trims\n * surrounding blank lines, normalizing multi-line content authored inside an\n * indented template literal back to a column-zero baseline. Leading whitespace is\n * counted by character, so N tabs and N spaces are treated as the same depth.\n *\n * @example\n * ```ts\n * dedent('\\n foo\\n bar\\n ')\n * // 'foo\\n bar'\n * ```\n */\nexport function dedent(text: string): string {\n if (!text) return ''\n\n const lines = text.split('\\n')\n const isBlank = (line: string) => line.trim() === ''\n\n const start = lines.findIndex((line) => !isBlank(line))\n if (start === -1) return ''\n const end = lines.findLastIndex((line) => !isBlank(line))\n\n const trimmed = lines.slice(start, end + 1)\n const indents = trimmed.filter((line) => !isBlank(line)).map((line) => line.match(/^\\s*/)?.[0].length ?? 0)\n const min = indents.length ? Math.min(...indents) : 0\n\n return trimmed.map((line) => (isBlank(line) ? '' : line.slice(min))).join('\\n')\n}\n\n/**\n * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.\n * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.\n */\nexport function formatGenerics(generics: FunctionNode['generics'] | ArrowFunctionNode['generics']): string {\n if (!generics) return ''\n return `<${Array.isArray(generics) ? generics.join(', ') : generics}>`\n}\n\n/**\n * Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).\n * Returns an empty string when no return type is provided.\n */\nexport function formatReturnType(returnType: string | null | undefined, isAsync: boolean | null | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\n}\n\n/**\n * Module-scoped TypeScript printer instance. `ts.createPrinter()` is stateless across calls\n * (it does not mutate the source file) so a single instance can be safely reused for every\n * `print()` call. Hoisting it out of `print()` avoids re-running the printer initialization\n * for each file's import/export section.\n */\nconst TS_PRINTER = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n})\n\n/**\n * Module-scoped source file used as the print target. `printList` only reads the source\n * file's compiler options / language version. It never mutates it.\n */\nconst PRINT_SOURCE_FILE = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n// Pre-warm the printer at module load. The first `printList` call lazily initializes\n// the printer's internal string-builder and identifier tables. Doing it once at import\n// time keeps that cost off the critical path for short-lived CLI builds.\nTS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray([]), PRINT_SOURCE_FILE)\n\n/**\n * Converts TypeScript/TSX AST nodes to a string using the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const filtered = elements.filter(Boolean)\n if (filtered.length === 0) return ''\n\n const output = TS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray(filtered), PRINT_SOURCE_FILE)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Converts a {@link JSDocNode} to a JSDoc comment block string.\n *\n * @example\n * ```ts\n * printJSDoc({ comments: ['@description A pet', '@deprecated'] })\n * // /**\n * // * @description A pet\n * // * @deprecated\n * // *\\/\n * ```\n */\nexport function printJSDoc(jsDoc: JSDocNode): string {\n const comments = (jsDoc.comments ?? []).filter((c) => c != null)\n if (comments.length === 0) return ''\n\n const lines = comments\n .flatMap((c) => c.split(/\\r?\\n/))\n .map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, '* /').replace(CARRIAGE_RETURN_PATTERN, ''))\n .filter((l) => l.trim().length > 0)\n\n if (lines.length === 0) return ''\n\n return ['/**', ...lines.map((l) => ` * ${l}`), ' */'].join('\\n')\n}\n\n/**\n * Converts a {@link ConstNode} to a TypeScript `const` declaration string.\n *\n * Mirrors the `Const` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printConst(createConst({ name: 'pet', export: true, nodes: ['{}'] }))\n * // 'export const pet = {}'\n * ```\n *\n * @example With type and `as const`\n * ```ts\n * printConst(createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true, nodes: ['[]'] }))\n * // 'export const pets: Pet[] = [] as const'\n * ```\n */\nexport function printConst(node: ConstNode): string {\n const { name, export: canExport, type, JSDoc, asConst, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('const ')\n parts.push(name)\n if (type) {\n parts.push(`: ${type}`)\n }\n parts.push(' = ')\n parts.push(body)\n if (asConst) parts.push(' as const')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link TypeNode} to a TypeScript `type` alias declaration string.\n *\n * Mirrors the `Type` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printType(createType({ name: 'Pet', export: true, nodes: ['{ id: number }'] }))\n * // 'export type Pet = { id: number }'\n * ```\n */\nexport function printType(node: TypeNode): string {\n const { name, export: canExport, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('type ')\n parts.push(name)\n parts.push(' = ')\n parts.push(body)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link FunctionNode} to a TypeScript `function` declaration string.\n *\n * Mirrors the `Function` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printFunction(createFunction({ name: 'getPet', export: true, params: 'id: string', returnType: 'Pet', nodes: ['return fetch(id)'] }))\n * // 'export function getPet(id: string): Pet {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Async with generics\n * ```ts\n * printFunction(createFunction({ name: 'fetchPet', export: true, async: true, generics: ['T'], params: 'id: string', returnType: 'T' }))\n * // 'export async function fetchPet<T>(id: string): Promise<T> {\\n}'\n * ```\n */\nexport function printFunction(node: FunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n if (isAsync) parts.push('async ')\n parts.push('function ')\n parts.push(name)\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(' {')\n if (indented) {\n parts.push(`\\n${indented}\\n`)\n }\n parts.push('}')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts an {@link ArrowFunctionNode} to a TypeScript arrow function declaration string.\n *\n * Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.\n *\n * @example Multi-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'getPet', export: true, params: 'id: string', nodes: ['return fetch(id)'] }))\n * // 'export const getPet = (id: string) => {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Single-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'double', params: 'n: number', singleLine: true, nodes: ['n * 2'] }))\n * // 'const double = (n: number) => n * 2'\n * ```\n */\nexport function printArrowFunction(node: ArrowFunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n parts.push('const ')\n parts.push(name)\n parts.push(' = ')\n if (isAsync) parts.push('async ')\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(arrowBody)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link CodeNode} to its TypeScript string representation.\n *\n * Dispatches to the appropriate printer based on the node's `kind`.\n *\n * @example\n * ```ts\n * printCodeNode(createConst({ name: 'x', nodes: ['1'] }))\n * // 'const x = 1'\n * ```\n */\nexport function printCodeNode(node: CodeNode): string {\n if (node.kind === 'Break') return ''\n if (node.kind === 'Text') return dedent((node as TextNode).value)\n if (node.kind === 'Jsx') return dedent((node as JsxNode).value)\n if (node.kind === 'Const') return printConst(node)\n if (node.kind === 'Type') return printType(node)\n if (node.kind === 'Function') return printFunction(node)\n if (node.kind === 'ArrowFunction') return printArrowFunction(node)\n return ''\n}\n\n/**\n * Converts a {@link SourceNode} to its TypeScript string representation.\n *\n * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via\n * {@link printCodeNode}.\n *\n * Top-level declarations are separated by a blank line so the source reads\n * cleanly without an external formatter.\n *\n * @example From nodes\n * ```ts\n * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })\n * // 'const x = 1\\n\\nx.toString()'\n * ```\n */\nexport function printSource(node: SourceNode): string {\n const nodes = node.nodes\n\n if (!nodes || nodes.length === 0) return ''\n\n return nodes\n .map((child) => printCodeNode(child as CodeNode))\n .filter(Boolean)\n .join('\\n\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly: isTypeOnlyRaw = false,\n isNameSpace: isNameSpaceRaw = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string | null\n /** @default false */\n isTypeOnly?: boolean | null\n /** @default false */\n isNameSpace?: boolean | null\n}): ts.ImportDeclaration {\n const isTypeOnly = isTypeOnlyRaw ?? false\n const isNameSpace = isNameSpaceRaw ?? false\n const resolvePath = root ? getRelativePath(root, path) : path\n\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias: asAliasRaw,\n isTypeOnly: isTypeOnlyRaw = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean | null\n /** @default false */\n isTypeOnly?: boolean | null\n name?: string | Array<ts.Identifier | string> | null\n}): ts.ExportDeclaration {\n const asAlias = asAliasRaw ?? false\n const isTypeOnly = isTypeOnlyRaw ?? false\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) =>\n factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName),\n ),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n\n/**\n * Wraps a module specifier in single quotes, escaping any embedded backslash or quote so the emitted\n * statement stays valid even for unusual paths.\n */\nfunction quoteModulePath(path: string): string {\n return `'${path.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")}'`\n}\n\n/**\n * Renders an import declaration string in the repo style (single quotes, no semicolons), mirroring\n * the shapes that {@link createImport} builds: default, namespace (`* as`), and named imports with\n * `{ a as b }` aliases, each optionally `type`-only. `path` is used verbatim, so resolve it first.\n *\n * @example\n * ```ts\n * printImport({ name: ['z'], path: './zod.ts' })\n * // \"import { z } from './zod.ts'\"\n * ```\n */\nexport function printImport({\n name,\n path,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n isTypeOnly?: boolean | null\n isNameSpace?: boolean | null\n}): string {\n const typePrefix = isTypeOnly ? 'type ' : ''\n const from = quoteModulePath(path)\n\n if (!Array.isArray(name)) {\n if (isNameSpace) return `import ${typePrefix}* as ${name} from ${from}`\n return `import ${typePrefix}${name} from ${from}`\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n return item.name ? `${item.propertyName} as ${item.name}` : item.propertyName\n }\n return item\n })\n\n return `import ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`\n}\n\n/**\n * Renders an export declaration string in the repo style (single quotes, no semicolons), mirroring\n * the shapes that {@link createExport} builds: named re-exports, namespace alias (`* as name`), and\n * wildcard, each optionally `type`-only. `path` is used verbatim, so resolve it first.\n *\n * @example\n * ```ts\n * printExport({ name: ['Pet', 'Order'], path: './models.ts' })\n * // \"export { Pet, Order } from './models.ts'\"\n * ```\n */\nexport function printExport({\n path,\n name,\n isTypeOnly = false,\n asAlias = false,\n}: {\n path: string\n name?: string | Array<ts.Identifier | string> | null\n isTypeOnly?: boolean | null\n asAlias?: boolean | null\n}): string {\n const typePrefix = isTypeOnly ? 'type ' : ''\n const from = quoteModulePath(path)\n\n if (Array.isArray(name)) {\n const specifiers = name.map((item) => (typeof item === 'string' ? item : item.text))\n return `export ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`\n }\n\n if (asAlias && name) {\n const parsedName = LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n return `export ${typePrefix}* as ${parsedName} from ${from}`\n }\n\n if (name) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n return `export ${typePrefix}* from ${from}`\n}\n","import type { FileNode, SourceNode } from '@kubb/ast'\nimport { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { getRelativePath, print, printExport, printImport, printSource, resolveOutputPath } from './utils.ts'\n\n/**\n * Default Kubb parser for `.ts` and `.js` files. Takes the universal AST\n * produced by an adapter and prints it as TypeScript source using the official\n * TypeScript compiler. Imports and exports are rewritten based on each file's\n * metadata.\n *\n * Used automatically when no `parsers` option is set on `defineConfig`. Use\n * `parserTsx` instead for React projects that emit JSX.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTs } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTs],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTs = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n const sourceStr = printSource(item as SourceNode)\n if (sourceStr) {\n sourceParts.push(sourceStr.trimEnd())\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importLines: Array<string> = []\n for (const item of (file as FileNode).imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n importLines.push(\n printImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: resolveOutputPath(importPath, options, Boolean(item.root)),\n isTypeOnly: item.isTypeOnly,\n isNameSpace: item.isNameSpace,\n }),\n )\n }\n\n const exportLines: Array<string> = []\n for (const item of (file as FileNode).exports) {\n exportLines.push(\n printExport({\n name: item.name as string | Array<ts.Identifier | string> | null | undefined,\n path: resolveOutputPath(item.path, options, true),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const importExportBlock = [...importLines, ...exportLines].join('\\n')\n\n const parts = [file.banner, importExportBlock, source, file.footer].filter((segment): segment is string => Boolean(segment)).map((s) => s.trimEnd())\n return parts.join('\\n\\n')\n },\n})\n","import { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { parserTs } from './parserTs.ts'\nimport { print } from './utils.ts'\n\n/**\n * Kubb parser for `.tsx` and `.jsx` files. Delegates to `parserTs` because the\n * TypeScript compiler handles JSX natively via `ScriptKind.TSX`.\n *\n * Add to the `parsers` array on `defineConfig` when generating components for\n * React (or any framework that emits JSX).\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTsx } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTsx],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTsx = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAaA,MAAa,SAAA,IAAqB,OAAA,CAAkB;;;;;AAMpD,MAAa,yBAAyB;;;;AAKtC,MAAa,yBAAyB;;;;;AAMtC,MAAa,2BAA2B;;;;AAKxC,MAAa,0BAA0B;;;;AAKvC,MAAa,eAAe;;;;;AAM5B,MAAa,wBAAwB;;;AC9BrC,MAAM,EAAE,YAAYA,WAAAA;;;;AAKpB,SAAgB,MAAM,MAAsB;CAC1C,QAAA,GAAA,UAAA,UAAA,CAAiB,IAAI,CAAC,CAAC,WAAW,wBAAwB,GAAG,CAAC,CAAC,QAAA,OAAiC,EAAE;AACpG;;;;;AAMA,SAAgB,gBAAgB,SAAiB,UAA0B;CAEzE,MAAM,UAAU,OAAA,GAAA,UAAA,SAAA,CADK,SAAS,QACN,CAAC;CACzB,OAAO,QAAQ,WAAA,KAAkC,IAAI,UAAU,KAA8B;AAC/F;;;;;AAMA,SAAgB,YAAY,MAAsB;CAChD,OAAO,KAAK,QAAQ,wBAAwB,EAAE;AAChD;;;;;;AAOA,SAAgB,kBAAkB,MAAc,SAA2C,WAA4B;CACrH,MAAM,aAAa,uBAAuB,KAAK,IAAI;CACnD,IAAI,SAAS,WAAW,YACtB,OAAO,GAAG,YA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AAC;IAC/D,YAAY,KAAK;IACjB,aAAa,KAAK;GACpB,CAAC,CACH;EACF;EAEA,MAAM,cAA6B,CAAC;EACpC,KAAK,MAAM,QAAS,KAAkB,SACpC,YAAY,KACV,YAAY;GACV,MAAM,KAAK;GACX,MAAM,kBAAkB,KAAK,MAAM,SAAS,IAAI;GAChD,YAAY,KAAK;GACjB,SAAS,KAAK;EAChB,CAAC,CACH;EAGF,MAAM,oBAAoB,CAAC,GAAG,aAAa,GAAG,WAAW,CAAC,CAAC,KAAK,IAAI;EAGpE,OADc;GAAC,KAAK;GAAQ;GAAmB;GAAQ,KAAK;EAAM,CAAC,CAAC,QAAQ,YAA+B,QAAQ,OAAO,CAAC,CAAC,CAAC,KAAK,MAAM,EAAE,QAAQ,CACvI,CAAC,CAAC,KAAK,MAAM;CAC1B;AACF,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;AChDD,MAAa,aAAA,GAAA,WAAA,aAAA,CAAyB;CACpC,MAAM;CACN,UAAU,CAAC,QAAQ,MAAM;CACzB,MAAM,GAAG,OAAuB;EAC9B,OAAO,MAAM,GAAG,KAAK;CACvB;CACA,MAAM,MAAM,UAAU,EAAE,SAAS,OAAO,GAAG;EACzC,OAAO,SAAS,MAAM,MAAM,OAAO;CACrC;AACF,CAAC"}
package/dist/index.js CHANGED
@@ -2,7 +2,10 @@ import "./chunk-C0LytTxp.js";
2
2
  import { defineParser } from "@kubb/core";
3
3
  import { normalize, relative } from "node:path";
4
4
  import ts from "typescript";
5
- //#region src/constants.ts
5
+ /**
6
+ * Indentation unit prepended once per nesting level when pretty-printing.
7
+ */
8
+ const INDENT = " ".repeat(2);
6
9
  /**
7
10
  * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
8
11
  * intact by only trimming the last run of non-`/`/`.` characters).
@@ -78,14 +81,39 @@ function printNodes(nodes) {
78
81
  return parts.join("\n");
79
82
  }
80
83
  /**
81
- * Indents every non-empty line of `text` by `spaces` spaces.
84
+ * Indents every non-empty line of `text` by one indent unit. Pass a number to repeat
85
+ * {@link INDENT_CHAR} that many times, or a string to use as the indent verbatim.
82
86
  */
83
- function indentLines(text, spaces = 2) {
87
+ function indentLines(text, indent = INDENT) {
84
88
  if (!text) return "";
85
- const pad = " ".repeat(spaces);
89
+ const pad = typeof indent === "string" ? indent : " ".repeat(indent);
86
90
  return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : "").join("\n");
87
91
  }
88
92
  /**
93
+ * Removes the common leading whitespace shared by every non-blank line and trims
94
+ * surrounding blank lines, normalizing multi-line content authored inside an
95
+ * indented template literal back to a column-zero baseline. Leading whitespace is
96
+ * counted by character, so N tabs and N spaces are treated as the same depth.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * dedent('\n foo\n bar\n ')
101
+ * // 'foo\n bar'
102
+ * ```
103
+ */
104
+ function dedent(text) {
105
+ if (!text) return "";
106
+ const lines = text.split("\n");
107
+ const isBlank = (line) => line.trim() === "";
108
+ const start = lines.findIndex((line) => !isBlank(line));
109
+ if (start === -1) return "";
110
+ const end = lines.findLastIndex((line) => !isBlank(line));
111
+ const trimmed = lines.slice(start, end + 1);
112
+ const indents = trimmed.filter((line) => !isBlank(line)).map((line) => line.match(/^\s*/)?.[0].length ?? 0);
113
+ const min = indents.length ? Math.min(...indents) : 0;
114
+ return trimmed.map((line) => isBlank(line) ? "" : line.slice(min)).join("\n");
115
+ }
116
+ /**
89
117
  * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
90
118
  * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
91
119
  */
@@ -288,8 +316,8 @@ function printArrowFunction(node) {
288
316
  */
289
317
  function printCodeNode(node) {
290
318
  if (node.kind === "Break") return "";
291
- if (node.kind === "Text") return node.value;
292
- if (node.kind === "Jsx") return node.value;
319
+ if (node.kind === "Text") return dedent(node.value);
320
+ if (node.kind === "Jsx") return dedent(node.value);
293
321
  if (node.kind === "Const") return printConst(node);
294
322
  if (node.kind === "Type") return printType(node);
295
323
  if (node.kind === "Function") return printFunction(node);
@@ -302,45 +330,68 @@ function printCodeNode(node) {
302
330
  * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via
303
331
  * {@link printCodeNode}.
304
332
  *
333
+ * Top-level declarations are separated by a blank line so the source reads
334
+ * cleanly without an external formatter.
335
+ *
305
336
  * @example From nodes
306
337
  * ```ts
307
338
  * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })
308
- * // 'const x = 1\nx.toString()'
339
+ * // 'const x = 1\n\nx.toString()'
309
340
  * ```
310
341
  */
311
342
  function printSource(node) {
312
343
  const nodes = node.nodes;
313
344
  if (!nodes || nodes.length === 0) return "";
314
- const parts = [];
315
- for (const child of nodes) parts.push(printCodeNode(child));
316
- return parts.join("\n");
345
+ return nodes.map((child) => printCodeNode(child)).filter(Boolean).join("\n\n");
317
346
  }
318
- function createImport({ name, path, root, isTypeOnly: isTypeOnlyRaw = false, isNameSpace: isNameSpaceRaw = false }) {
319
- const isTypeOnly = isTypeOnlyRaw ?? false;
320
- const isNameSpace = isNameSpaceRaw ?? false;
321
- const resolvePath = root ? getRelativePath(root, path) : path;
322
- if (!Array.isArray(name)) {
323
- if (isNameSpace) return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamespaceImport(factory.createIdentifier(name))), factory.createStringLiteral(resolvePath), void 0);
324
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), factory.createStringLiteral(resolvePath), void 0);
325
- }
326
- const specifiers = name.map((item) => {
327
- if (typeof item === "object") {
328
- const { propertyName, name: alias } = item;
329
- return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : void 0, factory.createIdentifier(alias ?? propertyName));
330
- }
331
- return factory.createImportSpecifier(false, void 0, factory.createIdentifier(item));
332
- });
333
- return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(specifiers)), factory.createStringLiteral(resolvePath), void 0);
347
+ /**
348
+ * Wraps a module specifier in single quotes, escaping any embedded backslash or quote so the emitted
349
+ * statement stays valid even for unusual paths.
350
+ */
351
+ function quoteModulePath(path) {
352
+ return `'${path.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
334
353
  }
335
- function createExport({ path, asAlias: asAliasRaw, isTypeOnly: isTypeOnlyRaw = false, name }) {
336
- const asAlias = asAliasRaw ?? false;
337
- const isTypeOnly = isTypeOnlyRaw ?? false;
338
- if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true: ${name}`);
354
+ /**
355
+ * Renders an import declaration string in the repo style (single quotes, no semicolons), mirroring
356
+ * the shapes that {@link createImport} builds: default, namespace (`* as`), and named imports with
357
+ * `{ a as b }` aliases, each optionally `type`-only. `path` is used verbatim, so resolve it first.
358
+ *
359
+ * @example
360
+ * ```ts
361
+ * printImport({ name: ['z'], path: './zod.ts' })
362
+ * // "import { z } from './zod.ts'"
363
+ * ```
364
+ */
365
+ function printImport({ name, path, isTypeOnly = false, isNameSpace = false }) {
366
+ const typePrefix = isTypeOnly ? "type " : "";
367
+ const from = quoteModulePath(path);
339
368
  if (!Array.isArray(name)) {
340
- const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name;
341
- return factory.createExportDeclaration(void 0, isTypeOnly, asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, factory.createStringLiteral(path), void 0);
369
+ if (isNameSpace) return `import ${typePrefix}* as ${name} from ${from}`;
370
+ return `import ${typePrefix}${name} from ${from}`;
342
371
  }
343
- return factory.createExportDeclaration(void 0, isTypeOnly, factory.createNamedExports(name.map((propertyName) => factory.createExportSpecifier(false, void 0, typeof propertyName === "string" ? factory.createIdentifier(propertyName) : propertyName))), factory.createStringLiteral(path), void 0);
372
+ return `import ${typePrefix}{ ${name.map((item) => {
373
+ if (typeof item === "object") return item.name ? `${item.propertyName} as ${item.name}` : item.propertyName;
374
+ return item;
375
+ }).join(", ")} } from ${from}`;
376
+ }
377
+ /**
378
+ * Renders an export declaration string in the repo style (single quotes, no semicolons), mirroring
379
+ * the shapes that {@link createExport} builds: named re-exports, namespace alias (`* as name`), and
380
+ * wildcard, each optionally `type`-only. `path` is used verbatim, so resolve it first.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * printExport({ name: ['Pet', 'Order'], path: './models.ts' })
385
+ * // "export { Pet, Order } from './models.ts'"
386
+ * ```
387
+ */
388
+ function printExport({ path, name, isTypeOnly = false, asAlias = false }) {
389
+ const typePrefix = isTypeOnly ? "type " : "";
390
+ const from = quoteModulePath(path);
391
+ if (Array.isArray(name)) return `export ${typePrefix}{ ${name.map((item) => typeof item === "string" ? item : item.text).join(", ")} } from ${from}`;
392
+ if (asAlias && name) return `export ${typePrefix}* as ${LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name} from ${from}`;
393
+ if (name) console.warn(`When using name as string, asAlias should be true: ${name}`);
394
+ return `export ${typePrefix}* from ${from}`;
344
395
  }
345
396
  //#endregion
346
397
  //#region src/parserTs.ts
@@ -381,26 +432,27 @@ const parserTs = defineParser({
381
432
  if (sourceStr) sourceParts.push(sourceStr.trimEnd());
382
433
  }
383
434
  const source = sourceParts.join("\n\n");
384
- const importNodes = [];
435
+ const importLines = [];
385
436
  for (const item of file.imports) {
386
437
  const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
387
- importNodes.push(createImport({
438
+ importLines.push(printImport({
388
439
  name: item.name,
389
440
  path: resolveOutputPath(importPath, options, Boolean(item.root)),
390
441
  isTypeOnly: item.isTypeOnly,
391
442
  isNameSpace: item.isNameSpace
392
443
  }));
393
444
  }
394
- const exportNodes = [];
395
- for (const item of file.exports) exportNodes.push(createExport({
445
+ const exportLines = [];
446
+ for (const item of file.exports) exportLines.push(printExport({
396
447
  name: item.name,
397
448
  path: resolveOutputPath(item.path, options, true),
398
449
  isTypeOnly: item.isTypeOnly,
399
450
  asAlias: item.asAlias
400
451
  }));
452
+ const importExportBlock = [...importLines, ...exportLines].join("\n");
401
453
  return [
402
454
  file.banner,
403
- print(...importNodes, ...exportNodes),
455
+ importExportBlock,
404
456
  source,
405
457
  file.footer
406
458
  ].filter((segment) => Boolean(segment)).map((s) => s.trimEnd()).join("\n\n");
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../src/constants.ts","../src/utils.ts","../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["/**\n * Number of spaces used to indent a nested block when pretty-printing.\n */\nexport const INDENT_SIZE = 2 as const\n\n/**\n * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`\n * intact by only trimming the last run of non-`/`/`.` characters).\n */\nexport const FILE_EXTENSION_PATTERN = /\\.[^/.]+$/\n\n/**\n * Matches Windows-style backslash path separators.\n */\nexport const WINDOWS_PATH_SEPARATOR = /\\\\/g\n\n/**\n * Matches `*\\/` in free-form text so JSDoc bodies can neutralize premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalizing CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalizing TypeScript printer output.\n */\nexport const CRLF_PATTERN = /\\r\\n/g\n\n/**\n * Matches an identifier that starts with a digit. JavaScript disallows this,\n * so the printer prefixes such names with `_`.\n */\nexport const LEADING_DIGIT_PATTERN = /^\\d/\n\n/**\n * Relative path prefix used to detect traversal segments (`../`).\n */\nexport const PARENT_DIRECTORY_PREFIX = '../' as const\n\n/**\n * Relative path prefix used when resolving imports within the output root.\n */\nexport const CURRENT_DIRECTORY_PREFIX = './' as const\n","import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport ts from 'typescript'\nimport {\n CARRIAGE_RETURN_PATTERN,\n CRLF_PATTERN,\n CURRENT_DIRECTORY_PREFIX,\n FILE_EXTENSION_PATTERN,\n INDENT_SIZE,\n JSDOC_TERMINATOR_PATTERN,\n LEADING_DIGIT_PATTERN,\n PARENT_DIRECTORY_PREFIX,\n WINDOWS_PATH_SEPARATOR,\n} from './constants.ts'\n\nconst { factory } = ts\n\n/**\n * Normalizes a file-system path to POSIX separators and strips any leading `../` segment.\n */\nexport function slash(path: string): string {\n return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, '/').replace(PARENT_DIRECTORY_PREFIX, '')\n}\n\n/**\n * Resolves `filePath` relative to `rootDir` and returns a POSIX-style path\n * prefixed with `./` when the target sits inside the root, or `../` when it escapes it.\n */\nexport function getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith(PARENT_DIRECTORY_PREFIX) ? slashed : `${CURRENT_DIRECTORY_PREFIX}${slashed}`\n}\n\n/**\n * Strips the trailing file extension (for example `.ts`) from a path.\n * Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.\n */\nexport function trimExtName(text: string): string {\n return text.replace(FILE_EXTENSION_PATTERN, '')\n}\n\n/**\n * Rewrites an import/export path so its extension matches the caller-supplied\n * `options.extname`. When the source path has no extension the original is kept,\n * so virtual/module-only paths flow through unchanged.\n */\nexport function resolveOutputPath(path: string, options: { extname?: string } | undefined, rootAware: boolean): string {\n const hasExtname = FILE_EXTENSION_PATTERN.test(path)\n if (options?.extname && hasExtname) {\n return `${trimExtName(path)}${options.extname}`\n }\n return rootAware ? trimExtName(path) : path\n}\n\n/**\n * Serializes the body / value content from a `nodes` array.\n *\n * Each element is either a raw string or a structured {@link CodeNode}\n * (recursively converted via {@link printCodeNode}).\n * Elements are joined with `\\n`.\n */\nexport function printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n\n const parts: Array<string> = []\n\n for (const node of nodes) {\n parts.push(printCodeNode(node))\n }\n\n return parts.join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nexport function indentLines(text: string, spaces: number = INDENT_SIZE): string {\n if (!text) return ''\n const pad = ' '.repeat(spaces)\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.\n * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.\n */\nexport function formatGenerics(generics: FunctionNode['generics'] | ArrowFunctionNode['generics']): string {\n if (!generics) return ''\n return `<${Array.isArray(generics) ? generics.join(', ') : generics}>`\n}\n\n/**\n * Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).\n * Returns an empty string when no return type is provided.\n */\nexport function formatReturnType(returnType: string | null | undefined, isAsync: boolean | null | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\n}\n\n/**\n * Module-scoped TypeScript printer instance. `ts.createPrinter()` is stateless across calls\n * (it does not mutate the source file) so a single instance can be safely reused for every\n * `print()` call. Hoisting it out of `print()` avoids re-running the printer initialization\n * for each file's import/export section.\n */\nconst TS_PRINTER = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n})\n\n/**\n * Module-scoped source file used as the print target. `printList` only reads the source\n * file's compiler options / language version. It never mutates it.\n */\nconst PRINT_SOURCE_FILE = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n// Pre-warm the printer at module load. The first `printList` call lazily initializes\n// the printer's internal string-builder and identifier tables. Doing it once at import\n// time keeps that cost off the critical path for short-lived CLI builds.\nTS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray([]), PRINT_SOURCE_FILE)\n\n/**\n * Converts TypeScript/TSX AST nodes to a string using the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const filtered = elements.filter(Boolean)\n if (filtered.length === 0) return ''\n\n const output = TS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray(filtered), PRINT_SOURCE_FILE)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Converts a {@link JSDocNode} to a JSDoc comment block string.\n *\n * @example\n * ```ts\n * printJSDoc({ comments: ['@description A pet', '@deprecated'] })\n * // /**\n * // * @description A pet\n * // * @deprecated\n * // *\\/\n * ```\n */\nexport function printJSDoc(jsDoc: JSDocNode): string {\n const comments = (jsDoc.comments ?? []).filter((c) => c != null)\n if (comments.length === 0) return ''\n\n const lines = comments\n .flatMap((c) => c.split(/\\r?\\n/))\n .map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, '* /').replace(CARRIAGE_RETURN_PATTERN, ''))\n .filter((l) => l.trim().length > 0)\n\n if (lines.length === 0) return ''\n\n return ['/**', ...lines.map((l) => ` * ${l}`), ' */'].join('\\n')\n}\n\n/**\n * Converts a {@link ConstNode} to a TypeScript `const` declaration string.\n *\n * Mirrors the `Const` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printConst(createConst({ name: 'pet', export: true, nodes: ['{}'] }))\n * // 'export const pet = {}'\n * ```\n *\n * @example With type and `as const`\n * ```ts\n * printConst(createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true, nodes: ['[]'] }))\n * // 'export const pets: Pet[] = [] as const'\n * ```\n */\nexport function printConst(node: ConstNode): string {\n const { name, export: canExport, type, JSDoc, asConst, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('const ')\n parts.push(name)\n if (type) {\n parts.push(`: ${type}`)\n }\n parts.push(' = ')\n parts.push(body)\n if (asConst) parts.push(' as const')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link TypeNode} to a TypeScript `type` alias declaration string.\n *\n * Mirrors the `Type` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printType(createType({ name: 'Pet', export: true, nodes: ['{ id: number }'] }))\n * // 'export type Pet = { id: number }'\n * ```\n */\nexport function printType(node: TypeNode): string {\n const { name, export: canExport, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('type ')\n parts.push(name)\n parts.push(' = ')\n parts.push(body)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link FunctionNode} to a TypeScript `function` declaration string.\n *\n * Mirrors the `Function` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printFunction(createFunction({ name: 'getPet', export: true, params: 'id: string', returnType: 'Pet', nodes: ['return fetch(id)'] }))\n * // 'export function getPet(id: string): Pet {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Async with generics\n * ```ts\n * printFunction(createFunction({ name: 'fetchPet', export: true, async: true, generics: ['T'], params: 'id: string', returnType: 'T' }))\n * // 'export async function fetchPet<T>(id: string): Promise<T> {\\n}'\n * ```\n */\nexport function printFunction(node: FunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n if (isAsync) parts.push('async ')\n parts.push('function ')\n parts.push(name)\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(' {')\n if (indented) {\n parts.push(`\\n${indented}\\n`)\n }\n parts.push('}')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts an {@link ArrowFunctionNode} to a TypeScript arrow function declaration string.\n *\n * Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.\n *\n * @example Multi-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'getPet', export: true, params: 'id: string', nodes: ['return fetch(id)'] }))\n * // 'export const getPet = (id: string) => {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Single-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'double', params: 'n: number', singleLine: true, nodes: ['n * 2'] }))\n * // 'const double = (n: number) => n * 2'\n * ```\n */\nexport function printArrowFunction(node: ArrowFunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n parts.push('const ')\n parts.push(name)\n parts.push(' = ')\n if (isAsync) parts.push('async ')\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(arrowBody)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link CodeNode} to its TypeScript string representation.\n *\n * Dispatches to the appropriate printer based on the node's `kind`.\n *\n * @example\n * ```ts\n * printCodeNode(createConst({ name: 'x', nodes: ['1'] }))\n * // 'const x = 1'\n * ```\n */\nexport function printCodeNode(node: CodeNode): string {\n if (node.kind === 'Break') return ''\n if (node.kind === 'Text') return (node as TextNode).value\n if (node.kind === 'Jsx') return (node as JsxNode).value\n if (node.kind === 'Const') return printConst(node)\n if (node.kind === 'Type') return printType(node)\n if (node.kind === 'Function') return printFunction(node)\n if (node.kind === 'ArrowFunction') return printArrowFunction(node)\n return ''\n}\n\n/**\n * Converts a {@link SourceNode} to its TypeScript string representation.\n *\n * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via\n * {@link printCodeNode}.\n *\n * @example From nodes\n * ```ts\n * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })\n * // 'const x = 1\\nx.toString()'\n * ```\n */\nexport function printSource(node: SourceNode): string {\n const nodes = node.nodes\n\n if (!nodes || nodes.length === 0) return ''\n const parts: Array<string> = []\n\n for (const child of nodes) {\n parts.push(printCodeNode(child as CodeNode))\n }\n\n return parts.join('\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly: isTypeOnlyRaw = false,\n isNameSpace: isNameSpaceRaw = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string | null\n /** @default false */\n isTypeOnly?: boolean | null\n /** @default false */\n isNameSpace?: boolean | null\n}): ts.ImportDeclaration {\n const isTypeOnly = isTypeOnlyRaw ?? false\n const isNameSpace = isNameSpaceRaw ?? false\n const resolvePath = root ? getRelativePath(root, path) : path\n\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias: asAliasRaw,\n isTypeOnly: isTypeOnlyRaw = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean | null\n /** @default false */\n isTypeOnly?: boolean | null\n name?: string | Array<ts.Identifier | string> | null\n}): ts.ExportDeclaration {\n const asAlias = asAliasRaw ?? false\n const isTypeOnly = isTypeOnlyRaw ?? false\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) =>\n factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName),\n ),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n","import type { FileNode, SourceNode } from '@kubb/ast'\nimport { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { createExport, createImport, getRelativePath, print, printSource, resolveOutputPath } from './utils.ts'\n\n/**\n * Default Kubb parser for `.ts` and `.js` files. Takes the universal AST\n * produced by an adapter and prints it as TypeScript source using the official\n * TypeScript compiler. Imports and exports are rewritten based on each file's\n * metadata.\n *\n * Used automatically when no `parsers` option is set on `defineConfig`. Use\n * `parserTsx` instead for React projects that emit JSX.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTs } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTs],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTs = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n const sourceStr = printSource(item as SourceNode)\n if (sourceStr) {\n sourceParts.push(sourceStr.trimEnd())\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importNodes: Array<ts.ImportDeclaration> = []\n for (const item of (file as FileNode).imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n importNodes.push(\n createImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: resolveOutputPath(importPath, options, Boolean(item.root)),\n isTypeOnly: item.isTypeOnly,\n isNameSpace: item.isNameSpace,\n }),\n )\n }\n\n const exportNodes: Array<ts.ExportDeclaration> = []\n for (const item of (file as FileNode).exports) {\n exportNodes.push(\n createExport({\n name: item.name as string | Array<ts.Identifier | string> | null | undefined,\n path: resolveOutputPath(item.path, options, true),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const parts = [file.banner, print(...importNodes, ...exportNodes), source, file.footer]\n .filter((segment): segment is string => Boolean(segment))\n .map((s) => s.trimEnd())\n return parts.join('\\n\\n')\n },\n})\n","import { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { parserTs } from './parserTs.ts'\nimport { print } from './utils.ts'\n\n/**\n * Kubb parser for `.tsx` and `.jsx` files. Delegates to `parserTs` because the\n * TypeScript compiler handles JSX natively via `ScriptKind.TSX`.\n *\n * Add to the `parsers` array on `defineConfig` when generating components for\n * React (or any framework that emits JSX).\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTsx } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTsx],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTsx = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;;;;;AASA,MAAa,yBAAyB;;;;AAKtC,MAAa,yBAAyB;;;;;AAMtC,MAAa,2BAA2B;;;;AAKxC,MAAa,0BAA0B;;;;AAKvC,MAAa,eAAe;;;;;AAM5B,MAAa,wBAAwB;;;ACrBrC,MAAM,EAAE,YAAY;;;;AAKpB,SAAgB,MAAM,MAAsB;CAC1C,OAAO,UAAU,IAAI,EAAE,WAAW,wBAAwB,GAAG,EAAE,QAAA,OAAiC,EAAE;AACpG;;;;;AAMA,SAAgB,gBAAgB,SAAiB,UAA0B;CAEzE,MAAM,UAAU,MADJ,SAAS,SAAS,QACN,CAAC;CACzB,OAAO,QAAQ,WAAA,KAAkC,IAAI,UAAU,KAA8B;AAC/F;;;;;AAMA,SAAgB,YAAY,MAAsB;CAChD,OAAO,KAAK,QAAQ,wBAAwB,EAAE;AAChD;;;;;;AAOA,SAAgB,kBAAkB,MAAc,SAA2C,WAA4B;CACrH,MAAM,aAAa,uBAAuB,KAAK,IAAI;CACnD,IAAI,SAAS,WAAW,YACtB,OAAO,GAAG,YAAY,IAAI,IAAI,QAAQ;CAExC,OAAO,YAAY,YAAY,IAAI,IAAI;AACzC;;;;;;;;AASA,SAAgB,WAAW,OAA4C;CACrE,IAAI,CAAC,SAAS,MAAM,WAAW,GAAG,OAAO;CAEzC,MAAM,QAAuB,CAAC;CAE9B,KAAK,MAAM,QAAQ,OACjB,MAAM,KAAK,cAAc,IAAI,CAAC;CAGhC,OAAO,MAAM,KAAK,IAAI;AACxB;;;;AAKA,SAAgB,YAAY,MAAc,SAAA,GAAsC;CAC9E,IAAI,CAAC,MAAM,OAAO;CAClB,MAAM,MAAM,IAAI,OAAO,MAAM;CAC7B,OAAO,KACJ,MAAM,IAAI,EACV,KAAK,SAAU,KAAK,KAAK,IAAI,GAAG,M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}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../src/constants.ts","../src/utils.ts","../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["/**\n * Character used for a single indent step. Set to `'\\t'` to emit tab-indented output.\n */\nexport const INDENT_CHAR = ' '\n\n/**\n * Number of {@link INDENT_CHAR} repeats that make up one nesting level.\n */\nexport const INDENT_SIZE = 2 as const\n\n/**\n * Indentation unit prepended once per nesting level when pretty-printing.\n */\nexport const INDENT = INDENT_CHAR.repeat(INDENT_SIZE)\n\n/**\n * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`\n * intact by only trimming the last run of non-`/`/`.` characters).\n */\nexport const FILE_EXTENSION_PATTERN = /\\.[^/.]+$/\n\n/**\n * Matches Windows-style backslash path separators.\n */\nexport const WINDOWS_PATH_SEPARATOR = /\\\\/g\n\n/**\n * Matches `*\\/` in free-form text so JSDoc bodies can neutralize premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalizing CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalizing TypeScript printer output.\n */\nexport const CRLF_PATTERN = /\\r\\n/g\n\n/**\n * Matches an identifier that starts with a digit. JavaScript disallows this,\n * so the printer prefixes such names with `_`.\n */\nexport const LEADING_DIGIT_PATTERN = /^\\d/\n\n/**\n * Relative path prefix used to detect traversal segments (`../`).\n */\nexport const PARENT_DIRECTORY_PREFIX = '../' as const\n\n/**\n * Relative path prefix used when resolving imports within the output root.\n */\nexport const CURRENT_DIRECTORY_PREFIX = './' as const\n","import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport ts from 'typescript'\nimport {\n CARRIAGE_RETURN_PATTERN,\n CRLF_PATTERN,\n CURRENT_DIRECTORY_PREFIX,\n FILE_EXTENSION_PATTERN,\n INDENT,\n INDENT_CHAR,\n JSDOC_TERMINATOR_PATTERN,\n LEADING_DIGIT_PATTERN,\n PARENT_DIRECTORY_PREFIX,\n WINDOWS_PATH_SEPARATOR,\n} from './constants.ts'\n\nconst { factory } = ts\n\n/**\n * Normalizes a file-system path to POSIX separators and strips any leading `../` segment.\n */\nexport function slash(path: string): string {\n return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, '/').replace(PARENT_DIRECTORY_PREFIX, '')\n}\n\n/**\n * Resolves `filePath` relative to `rootDir` and returns a POSIX-style path\n * prefixed with `./` when the target sits inside the root, or `../` when it escapes it.\n */\nexport function getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith(PARENT_DIRECTORY_PREFIX) ? slashed : `${CURRENT_DIRECTORY_PREFIX}${slashed}`\n}\n\n/**\n * Strips the trailing file extension (for example `.ts`) from a path.\n * Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.\n */\nexport function trimExtName(text: string): string {\n return text.replace(FILE_EXTENSION_PATTERN, '')\n}\n\n/**\n * Rewrites an import/export path so its extension matches the caller-supplied\n * `options.extname`. When the source path has no extension the original is kept,\n * so virtual/module-only paths flow through unchanged.\n */\nexport function resolveOutputPath(path: string, options: { extname?: string } | undefined, rootAware: boolean): string {\n const hasExtname = FILE_EXTENSION_PATTERN.test(path)\n if (options?.extname && hasExtname) {\n return `${trimExtName(path)}${options.extname}`\n }\n return rootAware ? trimExtName(path) : path\n}\n\n/**\n * Serializes the body / value content from a `nodes` array.\n *\n * Each element is either a raw string or a structured {@link CodeNode}\n * (recursively converted via {@link printCodeNode}).\n * Elements are joined with `\\n`.\n */\nexport function printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n\n const parts: Array<string> = []\n\n for (const node of nodes) {\n parts.push(printCodeNode(node))\n }\n\n return parts.join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by one indent unit. Pass a number to repeat\n * {@link INDENT_CHAR} that many times, or a string to use as the indent verbatim.\n */\nexport function indentLines(text: string, indent: number | string = INDENT): string {\n if (!text) return ''\n const pad = typeof indent === 'string' ? indent : INDENT_CHAR.repeat(indent)\n return text\n .split('\\n')\n .map((line) => (line.trim() ? `${pad}${line}` : ''))\n .join('\\n')\n}\n\n/**\n * Removes the common leading whitespace shared by every non-blank line and trims\n * surrounding blank lines, normalizing multi-line content authored inside an\n * indented template literal back to a column-zero baseline. Leading whitespace is\n * counted by character, so N tabs and N spaces are treated as the same depth.\n *\n * @example\n * ```ts\n * dedent('\\n foo\\n bar\\n ')\n * // 'foo\\n bar'\n * ```\n */\nexport function dedent(text: string): string {\n if (!text) return ''\n\n const lines = text.split('\\n')\n const isBlank = (line: string) => line.trim() === ''\n\n const start = lines.findIndex((line) => !isBlank(line))\n if (start === -1) return ''\n const end = lines.findLastIndex((line) => !isBlank(line))\n\n const trimmed = lines.slice(start, end + 1)\n const indents = trimmed.filter((line) => !isBlank(line)).map((line) => line.match(/^\\s*/)?.[0].length ?? 0)\n const min = indents.length ? Math.min(...indents) : 0\n\n return trimmed.map((line) => (isBlank(line) ? '' : line.slice(min))).join('\\n')\n}\n\n/**\n * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.\n * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.\n */\nexport function formatGenerics(generics: FunctionNode['generics'] | ArrowFunctionNode['generics']): string {\n if (!generics) return ''\n return `<${Array.isArray(generics) ? generics.join(', ') : generics}>`\n}\n\n/**\n * Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).\n * Returns an empty string when no return type is provided.\n */\nexport function formatReturnType(returnType: string | null | undefined, isAsync: boolean | null | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\n}\n\n/**\n * Module-scoped TypeScript printer instance. `ts.createPrinter()` is stateless across calls\n * (it does not mutate the source file) so a single instance can be safely reused for every\n * `print()` call. Hoisting it out of `print()` avoids re-running the printer initialization\n * for each file's import/export section.\n */\nconst TS_PRINTER = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n})\n\n/**\n * Module-scoped source file used as the print target. `printList` only reads the source\n * file's compiler options / language version. It never mutates it.\n */\nconst PRINT_SOURCE_FILE = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n// Pre-warm the printer at module load. The first `printList` call lazily initializes\n// the printer's internal string-builder and identifier tables. Doing it once at import\n// time keeps that cost off the critical path for short-lived CLI builds.\nTS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray([]), PRINT_SOURCE_FILE)\n\n/**\n * Converts TypeScript/TSX AST nodes to a string using the TypeScript printer.\n */\nexport function print(...elements: Array<ts.Node>): string {\n const filtered = elements.filter(Boolean)\n if (filtered.length === 0) return ''\n\n const output = TS_PRINTER.printList(ts.ListFormat.MultiLine, factory.createNodeArray(filtered), PRINT_SOURCE_FILE)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Converts a {@link JSDocNode} to a JSDoc comment block string.\n *\n * @example\n * ```ts\n * printJSDoc({ comments: ['@description A pet', '@deprecated'] })\n * // /**\n * // * @description A pet\n * // * @deprecated\n * // *\\/\n * ```\n */\nexport function printJSDoc(jsDoc: JSDocNode): string {\n const comments = (jsDoc.comments ?? []).filter((c) => c != null)\n if (comments.length === 0) return ''\n\n const lines = comments\n .flatMap((c) => c.split(/\\r?\\n/))\n .map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, '* /').replace(CARRIAGE_RETURN_PATTERN, ''))\n .filter((l) => l.trim().length > 0)\n\n if (lines.length === 0) return ''\n\n return ['/**', ...lines.map((l) => ` * ${l}`), ' */'].join('\\n')\n}\n\n/**\n * Converts a {@link ConstNode} to a TypeScript `const` declaration string.\n *\n * Mirrors the `Const` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printConst(createConst({ name: 'pet', export: true, nodes: ['{}'] }))\n * // 'export const pet = {}'\n * ```\n *\n * @example With type and `as const`\n * ```ts\n * printConst(createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true, nodes: ['[]'] }))\n * // 'export const pets: Pet[] = [] as const'\n * ```\n */\nexport function printConst(node: ConstNode): string {\n const { name, export: canExport, type, JSDoc, asConst, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('const ')\n parts.push(name)\n if (type) {\n parts.push(`: ${type}`)\n }\n parts.push(' = ')\n parts.push(body)\n if (asConst) parts.push(' as const')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link TypeNode} to a TypeScript `type` alias declaration string.\n *\n * Mirrors the `Type` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printType(createType({ name: 'Pet', export: true, nodes: ['{ id: number }'] }))\n * // 'export type Pet = { id: number }'\n * ```\n */\nexport function printType(node: TypeNode): string {\n const { name, export: canExport, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n parts.push('type ')\n parts.push(name)\n parts.push(' = ')\n parts.push(body)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link FunctionNode} to a TypeScript `function` declaration string.\n *\n * Mirrors the `Function` component from `@kubb/renderer-jsx`.\n *\n * @example\n * ```ts\n * printFunction(createFunction({ name: 'getPet', export: true, params: 'id: string', returnType: 'Pet', nodes: ['return fetch(id)'] }))\n * // 'export function getPet(id: string): Pet {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Async with generics\n * ```ts\n * printFunction(createFunction({ name: 'fetchPet', export: true, async: true, generics: ['T'], params: 'id: string', returnType: 'T' }))\n * // 'export async function fetchPet<T>(id: string): Promise<T> {\\n}'\n * ```\n */\nexport function printFunction(node: FunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n if (isAsync) parts.push('async ')\n parts.push('function ')\n parts.push(name)\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(' {')\n if (indented) {\n parts.push(`\\n${indented}\\n`)\n }\n parts.push('}')\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts an {@link ArrowFunctionNode} to a TypeScript arrow function declaration string.\n *\n * Mirrors the `Function.Arrow` component from `@kubb/renderer-jsx`.\n *\n * @example Multi-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'getPet', export: true, params: 'id: string', nodes: ['return fetch(id)'] }))\n * // 'export const getPet = (id: string) => {\\n return fetch(id)\\n}'\n * ```\n *\n * @example Single-line arrow function\n * ```ts\n * printArrowFunction(createArrowFunction({ name: 'double', params: 'n: number', singleLine: true, nodes: ['n * 2'] }))\n * // 'const double = (n: number) => n * 2'\n * ```\n */\nexport function printArrowFunction(node: ArrowFunctionNode): string {\n const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node\n\n const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''\n const body = printNodes(nodes)\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: Array<string> = []\n if (canExport) parts.push('export ')\n if (isDefault) parts.push('default ')\n parts.push('const ')\n parts.push(name)\n parts.push(' = ')\n if (isAsync) parts.push('async ')\n parts.push(formatGenerics(generics))\n parts.push(`(${params ?? ''})`)\n parts.push(formatReturnType(returnType, isAsync))\n parts.push(arrowBody)\n\n const declaration = parts.join('')\n return [jsDocStr, declaration].filter(Boolean).join('\\n')\n}\n\n/**\n * Converts a {@link CodeNode} to its TypeScript string representation.\n *\n * Dispatches to the appropriate printer based on the node's `kind`.\n *\n * @example\n * ```ts\n * printCodeNode(createConst({ name: 'x', nodes: ['1'] }))\n * // 'const x = 1'\n * ```\n */\nexport function printCodeNode(node: CodeNode): string {\n if (node.kind === 'Break') return ''\n if (node.kind === 'Text') return dedent((node as TextNode).value)\n if (node.kind === 'Jsx') return dedent((node as JsxNode).value)\n if (node.kind === 'Const') return printConst(node)\n if (node.kind === 'Type') return printType(node)\n if (node.kind === 'Function') return printFunction(node)\n if (node.kind === 'ArrowFunction') return printArrowFunction(node)\n return ''\n}\n\n/**\n * Converts a {@link SourceNode} to its TypeScript string representation.\n *\n * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via\n * {@link printCodeNode}.\n *\n * Top-level declarations are separated by a blank line so the source reads\n * cleanly without an external formatter.\n *\n * @example From nodes\n * ```ts\n * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })\n * // 'const x = 1\\n\\nx.toString()'\n * ```\n */\nexport function printSource(node: SourceNode): string {\n const nodes = node.nodes\n\n if (!nodes || nodes.length === 0) return ''\n\n return nodes\n .map((child) => printCodeNode(child as CodeNode))\n .filter(Boolean)\n .join('\\n\\n')\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly: isTypeOnlyRaw = false,\n isNameSpace: isNameSpaceRaw = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string | null\n /** @default false */\n isTypeOnly?: boolean | null\n /** @default false */\n isNameSpace?: boolean | null\n}): ts.ImportDeclaration {\n const isTypeOnly = isTypeOnlyRaw ?? false\n const isNameSpace = isNameSpaceRaw ?? false\n const resolvePath = root ? getRelativePath(root, path) : path\n\n if (!Array.isArray(name)) {\n if (isNameSpace) {\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamespaceImport(factory.createIdentifier(name))),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, factory.createIdentifier(name), undefined),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n const { propertyName, name: alias } = item\n return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : undefined, factory.createIdentifier(alias ?? propertyName))\n }\n return factory.createImportSpecifier(false, undefined, factory.createIdentifier(item))\n })\n\n return factory.createImportDeclaration(\n undefined,\n factory.createImportClause(isTypeOnly, undefined, factory.createNamedImports(specifiers)),\n factory.createStringLiteral(resolvePath),\n undefined,\n )\n}\n\nexport function createExport({\n path,\n asAlias: asAliasRaw,\n isTypeOnly: isTypeOnlyRaw = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean | null\n /** @default false */\n isTypeOnly?: boolean | null\n name?: string | Array<ts.Identifier | string> | null\n}): ts.ExportDeclaration {\n const asAlias = asAliasRaw ?? false\n const isTypeOnly = isTypeOnlyRaw ?? false\n if (name && !Array.isArray(name) && !asAlias) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n if (!Array.isArray(name)) {\n const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : undefined,\n factory.createStringLiteral(path),\n undefined,\n )\n }\n\n return factory.createExportDeclaration(\n undefined,\n isTypeOnly,\n factory.createNamedExports(\n name.map((propertyName) =>\n factory.createExportSpecifier(false, undefined, typeof propertyName === 'string' ? factory.createIdentifier(propertyName) : propertyName),\n ),\n ),\n factory.createStringLiteral(path),\n undefined,\n )\n}\n\n/**\n * Wraps a module specifier in single quotes, escaping any embedded backslash or quote so the emitted\n * statement stays valid even for unusual paths.\n */\nfunction quoteModulePath(path: string): string {\n return `'${path.replace(/\\\\/g, '\\\\\\\\').replace(/'/g, \"\\\\'\")}'`\n}\n\n/**\n * Renders an import declaration string in the repo style (single quotes, no semicolons), mirroring\n * the shapes that {@link createImport} builds: default, namespace (`* as`), and named imports with\n * `{ a as b }` aliases, each optionally `type`-only. `path` is used verbatim, so resolve it first.\n *\n * @example\n * ```ts\n * printImport({ name: ['z'], path: './zod.ts' })\n * // \"import { z } from './zod.ts'\"\n * ```\n */\nexport function printImport({\n name,\n path,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n isTypeOnly?: boolean | null\n isNameSpace?: boolean | null\n}): string {\n const typePrefix = isTypeOnly ? 'type ' : ''\n const from = quoteModulePath(path)\n\n if (!Array.isArray(name)) {\n if (isNameSpace) return `import ${typePrefix}* as ${name} from ${from}`\n return `import ${typePrefix}${name} from ${from}`\n }\n\n const specifiers = name.map((item) => {\n if (typeof item === 'object') {\n return item.name ? `${item.propertyName} as ${item.name}` : item.propertyName\n }\n return item\n })\n\n return `import ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`\n}\n\n/**\n * Renders an export declaration string in the repo style (single quotes, no semicolons), mirroring\n * the shapes that {@link createExport} builds: named re-exports, namespace alias (`* as name`), and\n * wildcard, each optionally `type`-only. `path` is used verbatim, so resolve it first.\n *\n * @example\n * ```ts\n * printExport({ name: ['Pet', 'Order'], path: './models.ts' })\n * // \"export { Pet, Order } from './models.ts'\"\n * ```\n */\nexport function printExport({\n path,\n name,\n isTypeOnly = false,\n asAlias = false,\n}: {\n path: string\n name?: string | Array<ts.Identifier | string> | null\n isTypeOnly?: boolean | null\n asAlias?: boolean | null\n}): string {\n const typePrefix = isTypeOnly ? 'type ' : ''\n const from = quoteModulePath(path)\n\n if (Array.isArray(name)) {\n const specifiers = name.map((item) => (typeof item === 'string' ? item : item.text))\n return `export ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`\n }\n\n if (asAlias && name) {\n const parsedName = LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name\n return `export ${typePrefix}* as ${parsedName} from ${from}`\n }\n\n if (name) {\n console.warn(`When using name as string, asAlias should be true: ${name}`)\n }\n\n return `export ${typePrefix}* from ${from}`\n}\n","import type { FileNode, SourceNode } from '@kubb/ast'\nimport { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { getRelativePath, print, printExport, printImport, printSource, resolveOutputPath } from './utils.ts'\n\n/**\n * Default Kubb parser for `.ts` and `.js` files. Takes the universal AST\n * produced by an adapter and prints it as TypeScript source using the official\n * TypeScript compiler. Imports and exports are rewritten based on each file's\n * metadata.\n *\n * Used automatically when no `parsers` option is set on `defineConfig`. Use\n * `parserTsx` instead for React projects that emit JSX.\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTs } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTs],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTs = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.ts' }) {\n const sourceParts: Array<string> = []\n for (const item of file.sources) {\n const sourceStr = printSource(item as SourceNode)\n if (sourceStr) {\n sourceParts.push(sourceStr.trimEnd())\n }\n }\n const source = sourceParts.join('\\n\\n')\n\n const importLines: Array<string> = []\n for (const item of (file as FileNode).imports) {\n const importPath = item.root ? getRelativePath(item.root, item.path) : item.path\n importLines.push(\n printImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: resolveOutputPath(importPath, options, Boolean(item.root)),\n isTypeOnly: item.isTypeOnly,\n isNameSpace: item.isNameSpace,\n }),\n )\n }\n\n const exportLines: Array<string> = []\n for (const item of (file as FileNode).exports) {\n exportLines.push(\n printExport({\n name: item.name as string | Array<ts.Identifier | string> | null | undefined,\n path: resolveOutputPath(item.path, options, true),\n isTypeOnly: item.isTypeOnly,\n asAlias: item.asAlias,\n }),\n )\n }\n\n const importExportBlock = [...importLines, ...exportLines].join('\\n')\n\n const parts = [file.banner, importExportBlock, source, file.footer].filter((segment): segment is string => Boolean(segment)).map((s) => s.trimEnd())\n return parts.join('\\n\\n')\n },\n})\n","import { defineParser } from '@kubb/core'\nimport type * as ts from 'typescript'\nimport { parserTs } from './parserTs.ts'\nimport { print } from './utils.ts'\n\n/**\n * Kubb parser for `.tsx` and `.jsx` files. Delegates to `parserTs` because the\n * TypeScript compiler handles JSX natively via `ScriptKind.TSX`.\n *\n * Add to the `parsers` array on `defineConfig` when generating components for\n * React (or any framework that emits JSX).\n *\n * @example\n * ```ts\n * import { defineConfig } from 'kubb'\n * import { adapterOas } from '@kubb/adapter-oas'\n * import { parserTsx } from '@kubb/parser-ts'\n *\n * export default defineConfig({\n * input: { path: './petStore.yaml' },\n * output: { path: './src/gen' },\n * adapter: adapterOas(),\n * parsers: [parserTsx],\n * plugins: [],\n * })\n * ```\n */\nexport const parserTsx = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n print(...nodes: Array<ts.Node>) {\n return print(...nodes)\n },\n parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;;;AAaA,MAAa,SAAA,IAAqB,OAAA,CAAkB;;;;;AAMpD,MAAa,yBAAyB;;;;AAKtC,MAAa,yBAAyB;;;;;AAMtC,MAAa,2BAA2B;;;;AAKxC,MAAa,0BAA0B;;;;AAKvC,MAAa,eAAe;;;;;AAM5B,MAAa,wBAAwB;;;AC9BrC,MAAM,EAAE,YAAY;;;;AAKpB,SAAgB,MAAM,MAAsB;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.0-beta.42",
3
+ "version": "5.0.0-beta.43",
4
4
  "description": "TypeScript and TSX source file parser for Kubb. Converts AST nodes and raw TypeScript code into formatted source strings using the TypeScript compiler API.",
5
5
  "keywords": [
6
6
  "codegen",
@@ -42,18 +42,18 @@
42
42
  },
43
43
  "dependencies": {
44
44
  "typescript": "^6.0.3",
45
- "@kubb/core": "5.0.0-beta.42"
45
+ "@kubb/core": "5.0.0-beta.43"
46
46
  },
47
47
  "devDependencies": {
48
48
  "@internals/utils": "0.0.0",
49
- "@kubb/ast": "5.0.0-beta.42"
49
+ "@kubb/ast": "5.0.0-beta.43"
50
50
  },
51
51
  "engines": {
52
52
  "node": ">=22"
53
53
  },
54
54
  "scripts": {
55
55
  "build": "tsdown",
56
- "clean": "npx rimraf ./dist",
56
+ "clean": "node -e \"require('node:fs').rmSync('./dist', {recursive:true,force:true})\"",
57
57
  "lint": "oxlint .",
58
58
  "lint:fix": "oxlint --fix .",
59
59
  "release": "pnpm publish --no-git-check",
package/src/constants.ts CHANGED
@@ -1,8 +1,18 @@
1
1
  /**
2
- * Number of spaces used to indent a nested block when pretty-printing.
2
+ * Character used for a single indent step. Set to `'\t'` to emit tab-indented output.
3
+ */
4
+ export const INDENT_CHAR = ' '
5
+
6
+ /**
7
+ * Number of {@link INDENT_CHAR} repeats that make up one nesting level.
3
8
  */
4
9
  export const INDENT_SIZE = 2 as const
5
10
 
11
+ /**
12
+ * Indentation unit prepended once per nesting level when pretty-printing.
13
+ */
14
+ export const INDENT = INDENT_CHAR.repeat(INDENT_SIZE)
15
+
6
16
  /**
7
17
  * Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
8
18
  * intact by only trimming the last run of non-`/`/`.` characters).
package/src/parserTs.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { FileNode, SourceNode } from '@kubb/ast'
2
2
  import { defineParser } from '@kubb/core'
3
3
  import type * as ts from 'typescript'
4
- import { createExport, createImport, getRelativePath, print, printSource, resolveOutputPath } from './utils.ts'
4
+ import { getRelativePath, print, printExport, printImport, printSource, resolveOutputPath } from './utils.ts'
5
5
 
6
6
  /**
7
7
  * Default Kubb parser for `.ts` and `.js` files. Takes the universal AST
@@ -43,11 +43,11 @@ export const parserTs = defineParser({
43
43
  }
44
44
  const source = sourceParts.join('\n\n')
45
45
 
46
- const importNodes: Array<ts.ImportDeclaration> = []
46
+ const importLines: Array<string> = []
47
47
  for (const item of (file as FileNode).imports) {
48
48
  const importPath = item.root ? getRelativePath(item.root, item.path) : item.path
49
- importNodes.push(
50
- createImport({
49
+ importLines.push(
50
+ printImport({
51
51
  name: item.name as string | Array<string | { propertyName: string; name?: string }>,
52
52
  path: resolveOutputPath(importPath, options, Boolean(item.root)),
53
53
  isTypeOnly: item.isTypeOnly,
@@ -56,10 +56,10 @@ export const parserTs = defineParser({
56
56
  )
57
57
  }
58
58
 
59
- const exportNodes: Array<ts.ExportDeclaration> = []
59
+ const exportLines: Array<string> = []
60
60
  for (const item of (file as FileNode).exports) {
61
- exportNodes.push(
62
- createExport({
61
+ exportLines.push(
62
+ printExport({
63
63
  name: item.name as string | Array<ts.Identifier | string> | null | undefined,
64
64
  path: resolveOutputPath(item.path, options, true),
65
65
  isTypeOnly: item.isTypeOnly,
@@ -68,9 +68,9 @@ export const parserTs = defineParser({
68
68
  )
69
69
  }
70
70
 
71
- const parts = [file.banner, print(...importNodes, ...exportNodes), source, file.footer]
72
- .filter((segment): segment is string => Boolean(segment))
73
- .map((s) => s.trimEnd())
71
+ const importExportBlock = [...importLines, ...exportLines].join('\n')
72
+
73
+ const parts = [file.banner, importExportBlock, source, file.footer].filter((segment): segment is string => Boolean(segment)).map((s) => s.trimEnd())
74
74
  return parts.join('\n\n')
75
75
  },
76
76
  })
package/src/utils.ts CHANGED
@@ -6,7 +6,8 @@ import {
6
6
  CRLF_PATTERN,
7
7
  CURRENT_DIRECTORY_PREFIX,
8
8
  FILE_EXTENSION_PATTERN,
9
- INDENT_SIZE,
9
+ INDENT,
10
+ INDENT_CHAR,
10
11
  JSDOC_TERMINATOR_PATTERN,
11
12
  LEADING_DIGIT_PATTERN,
12
13
  PARENT_DIRECTORY_PREFIX,
@@ -73,17 +74,47 @@ export function printNodes(nodes: Array<CodeNode> | undefined): string {
73
74
  }
74
75
 
75
76
  /**
76
- * Indents every non-empty line of `text` by `spaces` spaces.
77
+ * Indents every non-empty line of `text` by one indent unit. Pass a number to repeat
78
+ * {@link INDENT_CHAR} that many times, or a string to use as the indent verbatim.
77
79
  */
78
- export function indentLines(text: string, spaces: number = INDENT_SIZE): string {
80
+ export function indentLines(text: string, indent: number | string = INDENT): string {
79
81
  if (!text) return ''
80
- const pad = ' '.repeat(spaces)
82
+ const pad = typeof indent === 'string' ? indent : INDENT_CHAR.repeat(indent)
81
83
  return text
82
84
  .split('\n')
83
85
  .map((line) => (line.trim() ? `${pad}${line}` : ''))
84
86
  .join('\n')
85
87
  }
86
88
 
89
+ /**
90
+ * Removes the common leading whitespace shared by every non-blank line and trims
91
+ * surrounding blank lines, normalizing multi-line content authored inside an
92
+ * indented template literal back to a column-zero baseline. Leading whitespace is
93
+ * counted by character, so N tabs and N spaces are treated as the same depth.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * dedent('\n foo\n bar\n ')
98
+ * // 'foo\n bar'
99
+ * ```
100
+ */
101
+ export function dedent(text: string): string {
102
+ if (!text) return ''
103
+
104
+ const lines = text.split('\n')
105
+ const isBlank = (line: string) => line.trim() === ''
106
+
107
+ const start = lines.findIndex((line) => !isBlank(line))
108
+ if (start === -1) return ''
109
+ const end = lines.findLastIndex((line) => !isBlank(line))
110
+
111
+ const trimmed = lines.slice(start, end + 1)
112
+ const indents = trimmed.filter((line) => !isBlank(line)).map((line) => line.match(/^\s*/)?.[0].length ?? 0)
113
+ const min = indents.length ? Math.min(...indents) : 0
114
+
115
+ return trimmed.map((line) => (isBlank(line) ? '' : line.slice(min))).join('\n')
116
+ }
117
+
87
118
  /**
88
119
  * Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
89
120
  * Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
@@ -326,8 +357,8 @@ export function printArrowFunction(node: ArrowFunctionNode): string {
326
357
  */
327
358
  export function printCodeNode(node: CodeNode): string {
328
359
  if (node.kind === 'Break') return ''
329
- if (node.kind === 'Text') return (node as TextNode).value
330
- if (node.kind === 'Jsx') return (node as JsxNode).value
360
+ if (node.kind === 'Text') return dedent((node as TextNode).value)
361
+ if (node.kind === 'Jsx') return dedent((node as JsxNode).value)
331
362
  if (node.kind === 'Const') return printConst(node)
332
363
  if (node.kind === 'Type') return printType(node)
333
364
  if (node.kind === 'Function') return printFunction(node)
@@ -341,23 +372,24 @@ export function printCodeNode(node: CodeNode): string {
341
372
  * Iterates `nodes` in DOM order, rendering each {@link CodeNode} via
342
373
  * {@link printCodeNode}.
343
374
  *
375
+ * Top-level declarations are separated by a blank line so the source reads
376
+ * cleanly without an external formatter.
377
+ *
344
378
  * @example From nodes
345
379
  * ```ts
346
380
  * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: [createText('1')] }), createText('x.toString()')] })
347
- * // 'const x = 1\nx.toString()'
381
+ * // 'const x = 1\n\nx.toString()'
348
382
  * ```
349
383
  */
350
384
  export function printSource(node: SourceNode): string {
351
385
  const nodes = node.nodes
352
386
 
353
387
  if (!nodes || nodes.length === 0) return ''
354
- const parts: Array<string> = []
355
-
356
- for (const child of nodes) {
357
- parts.push(printCodeNode(child as CodeNode))
358
- }
359
388
 
360
- return parts.join('\n')
389
+ return nodes
390
+ .map((child) => printCodeNode(child as CodeNode))
391
+ .filter(Boolean)
392
+ .join('\n\n')
361
393
  }
362
394
 
363
395
  export function createImport({
@@ -456,3 +488,93 @@ export function createExport({
456
488
  undefined,
457
489
  )
458
490
  }
491
+
492
+ /**
493
+ * Wraps a module specifier in single quotes, escaping any embedded backslash or quote so the emitted
494
+ * statement stays valid even for unusual paths.
495
+ */
496
+ function quoteModulePath(path: string): string {
497
+ return `'${path.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`
498
+ }
499
+
500
+ /**
501
+ * Renders an import declaration string in the repo style (single quotes, no semicolons), mirroring
502
+ * the shapes that {@link createImport} builds: default, namespace (`* as`), and named imports with
503
+ * `{ a as b }` aliases, each optionally `type`-only. `path` is used verbatim, so resolve it first.
504
+ *
505
+ * @example
506
+ * ```ts
507
+ * printImport({ name: ['z'], path: './zod.ts' })
508
+ * // "import { z } from './zod.ts'"
509
+ * ```
510
+ */
511
+ export function printImport({
512
+ name,
513
+ path,
514
+ isTypeOnly = false,
515
+ isNameSpace = false,
516
+ }: {
517
+ name: string | Array<string | { propertyName: string; name?: string }>
518
+ path: string
519
+ isTypeOnly?: boolean | null
520
+ isNameSpace?: boolean | null
521
+ }): string {
522
+ const typePrefix = isTypeOnly ? 'type ' : ''
523
+ const from = quoteModulePath(path)
524
+
525
+ if (!Array.isArray(name)) {
526
+ if (isNameSpace) return `import ${typePrefix}* as ${name} from ${from}`
527
+ return `import ${typePrefix}${name} from ${from}`
528
+ }
529
+
530
+ const specifiers = name.map((item) => {
531
+ if (typeof item === 'object') {
532
+ return item.name ? `${item.propertyName} as ${item.name}` : item.propertyName
533
+ }
534
+ return item
535
+ })
536
+
537
+ return `import ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`
538
+ }
539
+
540
+ /**
541
+ * Renders an export declaration string in the repo style (single quotes, no semicolons), mirroring
542
+ * the shapes that {@link createExport} builds: named re-exports, namespace alias (`* as name`), and
543
+ * wildcard, each optionally `type`-only. `path` is used verbatim, so resolve it first.
544
+ *
545
+ * @example
546
+ * ```ts
547
+ * printExport({ name: ['Pet', 'Order'], path: './models.ts' })
548
+ * // "export { Pet, Order } from './models.ts'"
549
+ * ```
550
+ */
551
+ export function printExport({
552
+ path,
553
+ name,
554
+ isTypeOnly = false,
555
+ asAlias = false,
556
+ }: {
557
+ path: string
558
+ name?: string | Array<ts.Identifier | string> | null
559
+ isTypeOnly?: boolean | null
560
+ asAlias?: boolean | null
561
+ }): string {
562
+ const typePrefix = isTypeOnly ? 'type ' : ''
563
+ const from = quoteModulePath(path)
564
+
565
+ if (Array.isArray(name)) {
566
+ const specifiers = name.map((item) => (typeof item === 'string' ? item : item.text))
567
+ return `export ${typePrefix}{ ${specifiers.join(', ')} } from ${from}`
568
+ }
569
+
570
+ if (asAlias && name) {
571
+ const parsedName = LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name
572
+ return `export ${typePrefix}* as ${parsedName} from ${from}`
573
+ }
574
+
575
+ if (name) {
576
+ console.warn(`When using name as string, asAlias should be true: ${name}`)
577
+ }
578
+
579
+ return `export ${typePrefix}* from ${from}`
580
+ }