@kubb/parser-ts 5.0.0-alpha.57 → 5.0.0-alpha.59
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 +79 -25
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +79 -25
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/constants.ts +47 -0
- package/src/parserTs.ts +63 -29
package/dist/index.cjs
CHANGED
|
@@ -25,17 +25,64 @@ let node_path = require("node:path");
|
|
|
25
25
|
let _kubb_core = require("@kubb/core");
|
|
26
26
|
let typescript = require("typescript");
|
|
27
27
|
typescript = __toESM(typescript, 1);
|
|
28
|
+
//#region src/constants.ts
|
|
29
|
+
/**
|
|
30
|
+
* Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
|
|
31
|
+
* intact by only trimming the last run of non-`/`/`.` characters).
|
|
32
|
+
*/
|
|
33
|
+
const FILE_EXTENSION_PATTERN = /\.[^/.]+$/;
|
|
34
|
+
/**
|
|
35
|
+
* Matches Windows-style backslash path separators.
|
|
36
|
+
*/
|
|
37
|
+
const WINDOWS_PATH_SEPARATOR = /\\/g;
|
|
38
|
+
/**
|
|
39
|
+
* Matches `*\/` in free-form text so JSDoc bodies can neutralise premature
|
|
40
|
+
* comment terminators (`*\/` → `* /`).
|
|
41
|
+
*/
|
|
42
|
+
const JSDOC_TERMINATOR_PATTERN = /\*\//g;
|
|
43
|
+
/**
|
|
44
|
+
* Matches carriage returns for normalising CRLF/CR line endings to LF.
|
|
45
|
+
*/
|
|
46
|
+
const CARRIAGE_RETURN_PATTERN = /\r/g;
|
|
47
|
+
/**
|
|
48
|
+
* Matches CRLF sequences used when normalising TypeScript printer output.
|
|
49
|
+
*/
|
|
50
|
+
const CRLF_PATTERN = /\r\n/g;
|
|
51
|
+
/**
|
|
52
|
+
* Matches an identifier that starts with a digit — JavaScript disallows this
|
|
53
|
+
* so the printer prefixes such names with `_`.
|
|
54
|
+
*/
|
|
55
|
+
const LEADING_DIGIT_PATTERN = /^\d/;
|
|
56
|
+
//#endregion
|
|
28
57
|
//#region src/parserTs.ts
|
|
29
58
|
const { factory } = typescript.default;
|
|
30
59
|
function slash(path) {
|
|
31
|
-
return (0, node_path.normalize)(path).replaceAll(
|
|
60
|
+
return (0, node_path.normalize)(path).replaceAll(WINDOWS_PATH_SEPARATOR, "/").replace("../", "");
|
|
32
61
|
}
|
|
62
|
+
/**
|
|
63
|
+
* Resolves `filePath` relative to `rootDir` and returns a POSIX-style path
|
|
64
|
+
* prefixed with `./` when the target sits inside the root, or `../` when it escapes it.
|
|
65
|
+
*/
|
|
33
66
|
function getRelativePath(rootDir, filePath) {
|
|
34
67
|
const slashed = slash((0, node_path.relative)(rootDir, filePath));
|
|
35
68
|
return slashed.startsWith("../") ? slashed : `./${slashed}`;
|
|
36
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Strips the trailing file extension (for example `.ts`) from a path.
|
|
72
|
+
* Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.
|
|
73
|
+
*/
|
|
37
74
|
function trimExtName(text) {
|
|
38
|
-
return text.replace(
|
|
75
|
+
return text.replace(FILE_EXTENSION_PATTERN, "");
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Rewrites an import/export path so its extension matches the caller-supplied
|
|
79
|
+
* `options.extname`. When the source path has no extension the original is kept,
|
|
80
|
+
* so virtual/module-only paths flow through unchanged.
|
|
81
|
+
*/
|
|
82
|
+
function resolveOutputPath(path, options, rootAware) {
|
|
83
|
+
const hasExtname = FILE_EXTENSION_PATTERN.test(path);
|
|
84
|
+
if (options?.extname && hasExtname) return `${trimExtName(path)}${options.extname}`;
|
|
85
|
+
return rootAware ? trimExtName(path) : path;
|
|
39
86
|
}
|
|
40
87
|
/**
|
|
41
88
|
* Validates TypeScript AST nodes before printing.
|
|
@@ -58,7 +105,7 @@ function print(...elements) {
|
|
|
58
105
|
newLine: typescript.default.NewLineKind.LineFeed,
|
|
59
106
|
removeComments: false,
|
|
60
107
|
noEmitHelpers: true
|
|
61
|
-
}).printList(typescript.default.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(
|
|
108
|
+
}).printList(typescript.default.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(CRLF_PATTERN, "\n");
|
|
62
109
|
}
|
|
63
110
|
/**
|
|
64
111
|
* Like `print` but validates nodes first to surface issues early.
|
|
@@ -85,7 +132,7 @@ function createImport({ name, path, root, isTypeOnly = false, isNameSpace = fals
|
|
|
85
132
|
function createExport({ path, asAlias, isTypeOnly = false, name }) {
|
|
86
133
|
if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true: ${name}`);
|
|
87
134
|
if (!Array.isArray(name)) {
|
|
88
|
-
const parsedName = name
|
|
135
|
+
const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name;
|
|
89
136
|
return factory.createExportDeclaration(void 0, isTypeOnly, asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, factory.createStringLiteral(path), void 0);
|
|
90
137
|
}
|
|
91
138
|
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);
|
|
@@ -105,7 +152,7 @@ function createExport({ path, asAlias, isTypeOnly = false, name }) {
|
|
|
105
152
|
function printJSDoc(jsDoc) {
|
|
106
153
|
const comments = (jsDoc.comments ?? []).filter((c) => c != null);
|
|
107
154
|
if (comments.length === 0) return "";
|
|
108
|
-
const lines = comments.flatMap((c) => c.split(/\r?\n/)).map((l) => l.replace(
|
|
155
|
+
const lines = comments.flatMap((c) => c.split(/\r?\n/)).map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, "* /").replace(CARRIAGE_RETURN_PATTERN, "")).filter((l) => l.trim().length > 0);
|
|
109
156
|
if (lines.length === 0) return "";
|
|
110
157
|
return [
|
|
111
158
|
"/**",
|
|
@@ -133,6 +180,22 @@ function indentLines(text, spaces = 2) {
|
|
|
133
180
|
return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : "").join("\n");
|
|
134
181
|
}
|
|
135
182
|
/**
|
|
183
|
+
* Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
|
|
184
|
+
* Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
|
|
185
|
+
*/
|
|
186
|
+
function formatGenerics(generics) {
|
|
187
|
+
if (!generics) return "";
|
|
188
|
+
return `<${Array.isArray(generics) ? generics.join(", ") : generics}>`;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).
|
|
192
|
+
* Returns an empty string when no return type is provided.
|
|
193
|
+
*/
|
|
194
|
+
function formatReturnType(returnType, isAsync) {
|
|
195
|
+
if (!returnType) return "";
|
|
196
|
+
return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`;
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
136
199
|
* Converts a {@link ConstNode} to a TypeScript `const` declaration string.
|
|
137
200
|
*
|
|
138
201
|
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
@@ -206,8 +269,6 @@ function printType(node) {
|
|
|
206
269
|
function printFunction(node) {
|
|
207
270
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node;
|
|
208
271
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
209
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
210
|
-
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
211
272
|
const body = printNodes(nodes);
|
|
212
273
|
const indented = body ? indentLines(body) : "";
|
|
213
274
|
const parts = [];
|
|
@@ -216,9 +277,9 @@ function printFunction(node) {
|
|
|
216
277
|
if (isAsync) parts.push("async ");
|
|
217
278
|
parts.push("function ");
|
|
218
279
|
parts.push(name);
|
|
219
|
-
parts.push(
|
|
280
|
+
parts.push(formatGenerics(generics));
|
|
220
281
|
parts.push(`(${params ?? ""})`);
|
|
221
|
-
parts.push(
|
|
282
|
+
parts.push(formatReturnType(returnType, isAsync));
|
|
222
283
|
parts.push(" {");
|
|
223
284
|
if (indented) parts.push(`\n${indented}\n`);
|
|
224
285
|
parts.push("}");
|
|
@@ -244,8 +305,6 @@ function printFunction(node) {
|
|
|
244
305
|
function printArrowFunction(node) {
|
|
245
306
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node;
|
|
246
307
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
247
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
248
|
-
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
249
308
|
const body = printNodes(nodes);
|
|
250
309
|
const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\n${indentLines(body)}\n}` : " => {}";
|
|
251
310
|
const parts = [];
|
|
@@ -255,9 +314,9 @@ function printArrowFunction(node) {
|
|
|
255
314
|
parts.push(name);
|
|
256
315
|
parts.push(" = ");
|
|
257
316
|
if (isAsync) parts.push("async ");
|
|
258
|
-
parts.push(
|
|
317
|
+
parts.push(formatGenerics(generics));
|
|
259
318
|
parts.push(`(${params ?? ""})`);
|
|
260
|
-
parts.push(
|
|
319
|
+
parts.push(formatReturnType(returnType, isAsync));
|
|
261
320
|
parts.push(arrowBody);
|
|
262
321
|
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
263
322
|
}
|
|
@@ -318,25 +377,20 @@ const parserTs = (0, _kubb_core.defineParser)({
|
|
|
318
377
|
const importNodes = [];
|
|
319
378
|
for (const item of file.imports) {
|
|
320
379
|
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
|
|
321
|
-
const hasExtname = !!/\.[^/.]+$/.exec(importPath);
|
|
322
380
|
importNodes.push(createImport({
|
|
323
381
|
name: item.name,
|
|
324
|
-
path:
|
|
382
|
+
path: resolveOutputPath(importPath, options, Boolean(item.root)),
|
|
325
383
|
isTypeOnly: item.isTypeOnly,
|
|
326
384
|
isNameSpace: item.isNameSpace
|
|
327
385
|
}));
|
|
328
386
|
}
|
|
329
387
|
const exportNodes = [];
|
|
330
|
-
for (const item of file.exports) {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
isTypeOnly: item.isTypeOnly,
|
|
337
|
-
asAlias: item.asAlias
|
|
338
|
-
}));
|
|
339
|
-
}
|
|
388
|
+
for (const item of file.exports) exportNodes.push(createExport({
|
|
389
|
+
name: item.name,
|
|
390
|
+
path: resolveOutputPath(item.path, options, true),
|
|
391
|
+
isTypeOnly: item.isTypeOnly,
|
|
392
|
+
asAlias: item.asAlias
|
|
393
|
+
}));
|
|
340
394
|
return [
|
|
341
395
|
file.banner,
|
|
342
396
|
print(...importNodes, ...exportNodes),
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["ts"],"sources":["../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FileNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport ts from 'typescript'\n\nconst { factory } = ts\n\nfunction slash(path: string): string {\n return normalize(path).replaceAll(/\\\\/g, '/').replace('../', '')\n}\n\nfunction getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith('../') ? slashed : `./${slashed}`\n}\n\nfunction trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n\n/**\n * Validates TypeScript AST nodes before printing.\n * Throws an error if any node has SyntaxKind.Unknown which would cause the\n * TypeScript printer to crash.\n */\nexport function validateNodes(...nodes: ts.Node[]): void {\n for (const node of nodes) {\n if (!node) {\n throw new Error('Attempted to print undefined or null TypeScript node')\n }\n if (node.kind === ts.SyntaxKind.Unknown) {\n throw new Error(\n 'Invalid TypeScript AST node detected with SyntaxKind.Unknown. ' +\n 'This typically indicates a schema pattern that could not be properly converted to TypeScript. ' +\n `Node: ${JSON.stringify(node, null, 2)}`,\n )\n }\n }\n}\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 sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(/\\r\\n/g, '\\n')\n}\n\n/**\n * Like `print` but validates nodes first to surface issues early.\n */\nexport function safePrint(...elements: Array<ts.Node>): string {\n validateNodes(...elements)\n return print(...elements)\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n /** @default false */\n isTypeOnly?: boolean\n /** @default false */\n isNameSpace?: boolean\n}): ts.ImportDeclaration {\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,\n isTypeOnly = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean\n /** @default false */\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}): ts.ExportDeclaration {\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?.match(/^\\d/) ? `_${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 * 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(/\\*\\//g, '* /').replace(/\\r/g, ''))\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 * 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 */\nfunction printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n return nodes.map(printCodeNode).join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nfunction indentLines(text: string, spaces = 2): 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 * 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: 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: 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\n const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''\n\n const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''\n\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: 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(genericsStr)\n parts.push(`(${params ?? ''})`)\n parts.push(returnTypeStr)\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\n const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''\n\n const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''\n\n const body = printNodes(nodes)\n\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: 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(genericsStr)\n parts.push(`(${params ?? ''})`)\n parts.push(returnTypeStr)\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 switch (node.kind) {\n case 'Break':\n return ''\n case 'Text':\n return (node as TextNode).value\n case 'Jsx':\n return (node as JsxNode).value\n case 'Const':\n return printConst(node)\n case 'Type':\n return printType(node)\n case 'Function':\n return printFunction(node)\n case 'ArrowFunction':\n return printArrowFunction(node)\n }\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 if (node.nodes && node.nodes.length > 0) {\n return node.nodes.map(printCodeNode).join('\\n')\n }\n return ''\n}\n\n/**\n * Parser that converts `.ts` and `.js` files to strings using the TypeScript\n * compiler. Handles import/export statement generation from file metadata.\n *\n * @default Used automatically when no `parsers` option is set in `defineConfig`.\n */\nexport const parserTs: Parser = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n async 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 const hasExtname = !!/\\.[^/.]+$/.exec(importPath)\n\n importNodes.push(\n createImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: options?.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,\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 const exportPath = item.path\n const hasExtname = !!/\\.[^/.]+$/.exec(exportPath)\n\n exportNodes.push(\n createExport({\n name: item.name as string | Array<ts.Identifier | string> | undefined,\n path: options?.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),\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 type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport { parserTs } from './parserTs.ts'\n\n/**\n * Parser that converts `.tsx` and `.jsx` files to strings.\n * Delegates to `typescriptParser` since the TypeScript compiler natively\n * supports JSX/TSX syntax via `ScriptKind.TSX`.\n *\n * Add this parser to the `parsers` option in `defineConfig` when generating `.tsx`/`.jsx` files.\n *\n * @default extname '.tsx'\n */\nexport const parserTsx: Parser = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n async parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,MAAM,EAAE,YAAYA,WAAAA;AAEpB,SAAS,MAAM,MAAsB;AACnC,SAAA,GAAA,UAAA,WAAiB,KAAK,CAAC,WAAW,OAAO,IAAI,CAAC,QAAQ,OAAO,GAAG;;AAGlE,SAAS,gBAAgB,SAAiB,UAA0B;CAElE,MAAM,UAAU,OAAA,GAAA,UAAA,UADK,SAAS,SAAS,CACb;AAC1B,QAAO,QAAQ,WAAW,MAAM,GAAG,UAAU,KAAK;;AAGpD,SAAS,YAAY,MAAsB;AACzC,QAAO,KAAK,QAAQ,aAAa,GAAG;;;;;;;AAQtC,SAAgB,cAAc,GAAG,OAAwB;AACvD,MAAK,MAAM,QAAQ,OAAO;AACxB,MAAI,CAAC,KACH,OAAM,IAAI,MAAM,uDAAuD;AAEzE,MAAI,KAAK,SAASA,WAAAA,QAAG,WAAW,QAC9B,OAAM,IAAI,MACR,qKAEW,KAAK,UAAU,MAAM,MAAM,EAAE,GACzC;;;;;;AAQP,SAAgB,MAAM,GAAG,UAAkC;CACz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ources":["../src/constants.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 neutralise premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalising CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalising 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, FileNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\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\nfunction 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 */\nfunction 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 */\nfunction 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 */\nfunction 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 * Validates TypeScript AST nodes before printing.\n * Throws an error if any node has SyntaxKind.Unknown which would cause the\n * TypeScript printer to crash.\n */\nexport function validateNodes(...nodes: ts.Node[]): void {\n for (const node of nodes) {\n if (!node) {\n throw new Error('Attempted to print undefined or null TypeScript node')\n }\n if (node.kind === ts.SyntaxKind.Unknown) {\n throw new Error(\n 'Invalid TypeScript AST node detected with SyntaxKind.Unknown. ' +\n 'This typically indicates a schema pattern that could not be properly converted to TypeScript. ' +\n `Node: ${JSON.stringify(node, null, 2)}`,\n )\n }\n }\n}\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 sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Like `print` but validates nodes first to surface issues early.\n */\nexport function safePrint(...elements: Array<ts.Node>): string {\n validateNodes(...elements)\n return print(...elements)\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n /** @default false */\n isTypeOnly?: boolean\n /** @default false */\n isNameSpace?: boolean\n}): ts.ImportDeclaration {\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,\n isTypeOnly = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean\n /** @default false */\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}): ts.ExportDeclaration {\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 * 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 * 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 */\nfunction printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n return nodes.map(printCodeNode).join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nfunction 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 */\nfunction 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 */\nfunction formatReturnType(returnType: string | undefined, isAsync: boolean | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\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: 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: 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: 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: 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 switch (node.kind) {\n case 'Break':\n return ''\n case 'Text':\n return (node as TextNode).value\n case 'Jsx':\n return (node as JsxNode).value\n case 'Const':\n return printConst(node)\n case 'Type':\n return printType(node)\n case 'Function':\n return printFunction(node)\n case 'ArrowFunction':\n return printArrowFunction(node)\n }\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 if (node.nodes && node.nodes.length > 0) {\n return node.nodes.map(printCodeNode).join('\\n')\n }\n return ''\n}\n\n/**\n * Parser that converts `.ts` and `.js` files to strings using the TypeScript\n * compiler. Handles import/export statement generation from file metadata.\n *\n * @default Used automatically when no `parsers` option is set in `defineConfig`.\n */\nexport const parserTs: Parser = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n async 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> | 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 type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport { parserTs } from './parserTs.ts'\n\n/**\n * Parser that converts `.tsx` and `.jsx` files to strings.\n * Delegates to `typescriptParser` since the TypeScript compiler natively\n * supports JSX/TSX syntax via `ScriptKind.TSX`.\n *\n * Add this parser to the `parsers` option in `defineConfig` when generating `.tsx`/`.jsx` files.\n *\n * @default extname '.tsx'\n */\nexport const parserTsx: Parser =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AAO;EAEvC,MAAM,cAA2C,EAAE;AACnD,OAAK,MAAM,QAAS,KAAkB,SAAS;GAC7C,MAAM,aAAa,KAAK,OAAO,gBAAgB,KAAK,MAAM,KAAK,KAAK,GAAG,KAAK;AAC5E,eAAY,KACV,aAAa;IACX,MAAM,KAAK;IACX,MAAM,kBAAkB,YAAY,SAAS,QAAQ,KAAK,KAAK,CAAC;IAChE,YAAY,KAAK;IACjB,aAAa,KAAK;IACnB,CAAC,CACH;;EAGH,MAAM,cAA2C,EAAE;AACnD,OAAK,MAAM,QAAS,KAAkB,QACpC,aAAY,KACV,aAAa;GACX,MAAM,KAAK;GACX,MAAM,kBAAkB,KAAK,MAAM,SAAS,KAAK;GACjD,YAAY,KAAK;GACjB,SAAS,KAAK;GACf,CAAC,CACH;AAMH,SAHc;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,YAAY;GAAE;GAAQ,KAAK;GAAO,CACpF,QAAQ,YAA+B,QAAQ,QAAQ,CAAC,CACxD,KAAK,MAAM,EAAE,SAAS,CACb,CAAC,KAAK,OAAO;;CAE5B,CAAC;;;;;;;;;;;;AC/eF,MAAa,aAAA,GAAA,WAAA,cAAiC;CAC5C,MAAM;CACN,UAAU,CAAC,QAAQ,OAAO;CAC1B,MAAM,MAAM,MAAM,UAAU,EAAE,SAAS,QAAQ,EAAE;AAC/C,SAAO,SAAS,MAAM,MAAM,QAAQ;;CAEvC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -2,17 +2,64 @@ import "./chunk--u3MIqq1.js";
|
|
|
2
2
|
import { normalize, relative } from "node:path";
|
|
3
3
|
import { defineParser } from "@kubb/core";
|
|
4
4
|
import ts from "typescript";
|
|
5
|
+
//#region src/constants.ts
|
|
6
|
+
/**
|
|
7
|
+
* Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
|
|
8
|
+
* intact by only trimming the last run of non-`/`/`.` characters).
|
|
9
|
+
*/
|
|
10
|
+
const FILE_EXTENSION_PATTERN = /\.[^/.]+$/;
|
|
11
|
+
/**
|
|
12
|
+
* Matches Windows-style backslash path separators.
|
|
13
|
+
*/
|
|
14
|
+
const WINDOWS_PATH_SEPARATOR = /\\/g;
|
|
15
|
+
/**
|
|
16
|
+
* Matches `*\/` in free-form text so JSDoc bodies can neutralise premature
|
|
17
|
+
* comment terminators (`*\/` → `* /`).
|
|
18
|
+
*/
|
|
19
|
+
const JSDOC_TERMINATOR_PATTERN = /\*\//g;
|
|
20
|
+
/**
|
|
21
|
+
* Matches carriage returns for normalising CRLF/CR line endings to LF.
|
|
22
|
+
*/
|
|
23
|
+
const CARRIAGE_RETURN_PATTERN = /\r/g;
|
|
24
|
+
/**
|
|
25
|
+
* Matches CRLF sequences used when normalising TypeScript printer output.
|
|
26
|
+
*/
|
|
27
|
+
const CRLF_PATTERN = /\r\n/g;
|
|
28
|
+
/**
|
|
29
|
+
* Matches an identifier that starts with a digit — JavaScript disallows this
|
|
30
|
+
* so the printer prefixes such names with `_`.
|
|
31
|
+
*/
|
|
32
|
+
const LEADING_DIGIT_PATTERN = /^\d/;
|
|
33
|
+
//#endregion
|
|
5
34
|
//#region src/parserTs.ts
|
|
6
35
|
const { factory } = ts;
|
|
7
36
|
function slash(path) {
|
|
8
|
-
return normalize(path).replaceAll(
|
|
37
|
+
return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, "/").replace("../", "");
|
|
9
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Resolves `filePath` relative to `rootDir` and returns a POSIX-style path
|
|
41
|
+
* prefixed with `./` when the target sits inside the root, or `../` when it escapes it.
|
|
42
|
+
*/
|
|
10
43
|
function getRelativePath(rootDir, filePath) {
|
|
11
44
|
const slashed = slash(relative(rootDir, filePath));
|
|
12
45
|
return slashed.startsWith("../") ? slashed : `./${slashed}`;
|
|
13
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Strips the trailing file extension (for example `.ts`) from a path.
|
|
49
|
+
* Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.
|
|
50
|
+
*/
|
|
14
51
|
function trimExtName(text) {
|
|
15
|
-
return text.replace(
|
|
52
|
+
return text.replace(FILE_EXTENSION_PATTERN, "");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Rewrites an import/export path so its extension matches the caller-supplied
|
|
56
|
+
* `options.extname`. When the source path has no extension the original is kept,
|
|
57
|
+
* so virtual/module-only paths flow through unchanged.
|
|
58
|
+
*/
|
|
59
|
+
function resolveOutputPath(path, options, rootAware) {
|
|
60
|
+
const hasExtname = FILE_EXTENSION_PATTERN.test(path);
|
|
61
|
+
if (options?.extname && hasExtname) return `${trimExtName(path)}${options.extname}`;
|
|
62
|
+
return rootAware ? trimExtName(path) : path;
|
|
16
63
|
}
|
|
17
64
|
/**
|
|
18
65
|
* Validates TypeScript AST nodes before printing.
|
|
@@ -35,7 +82,7 @@ function print(...elements) {
|
|
|
35
82
|
newLine: ts.NewLineKind.LineFeed,
|
|
36
83
|
removeComments: false,
|
|
37
84
|
noEmitHelpers: true
|
|
38
|
-
}).printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(
|
|
85
|
+
}).printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(CRLF_PATTERN, "\n");
|
|
39
86
|
}
|
|
40
87
|
/**
|
|
41
88
|
* Like `print` but validates nodes first to surface issues early.
|
|
@@ -62,7 +109,7 @@ function createImport({ name, path, root, isTypeOnly = false, isNameSpace = fals
|
|
|
62
109
|
function createExport({ path, asAlias, isTypeOnly = false, name }) {
|
|
63
110
|
if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true: ${name}`);
|
|
64
111
|
if (!Array.isArray(name)) {
|
|
65
|
-
const parsedName = name
|
|
112
|
+
const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name;
|
|
66
113
|
return factory.createExportDeclaration(void 0, isTypeOnly, asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, factory.createStringLiteral(path), void 0);
|
|
67
114
|
}
|
|
68
115
|
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);
|
|
@@ -82,7 +129,7 @@ function createExport({ path, asAlias, isTypeOnly = false, name }) {
|
|
|
82
129
|
function printJSDoc(jsDoc) {
|
|
83
130
|
const comments = (jsDoc.comments ?? []).filter((c) => c != null);
|
|
84
131
|
if (comments.length === 0) return "";
|
|
85
|
-
const lines = comments.flatMap((c) => c.split(/\r?\n/)).map((l) => l.replace(
|
|
132
|
+
const lines = comments.flatMap((c) => c.split(/\r?\n/)).map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, "* /").replace(CARRIAGE_RETURN_PATTERN, "")).filter((l) => l.trim().length > 0);
|
|
86
133
|
if (lines.length === 0) return "";
|
|
87
134
|
return [
|
|
88
135
|
"/**",
|
|
@@ -110,6 +157,22 @@ function indentLines(text, spaces = 2) {
|
|
|
110
157
|
return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : "").join("\n");
|
|
111
158
|
}
|
|
112
159
|
/**
|
|
160
|
+
* Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
|
|
161
|
+
* Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
|
|
162
|
+
*/
|
|
163
|
+
function formatGenerics(generics) {
|
|
164
|
+
if (!generics) return "";
|
|
165
|
+
return `<${Array.isArray(generics) ? generics.join(", ") : generics}>`;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).
|
|
169
|
+
* Returns an empty string when no return type is provided.
|
|
170
|
+
*/
|
|
171
|
+
function formatReturnType(returnType, isAsync) {
|
|
172
|
+
if (!returnType) return "";
|
|
173
|
+
return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
113
176
|
* Converts a {@link ConstNode} to a TypeScript `const` declaration string.
|
|
114
177
|
*
|
|
115
178
|
* Mirrors the `Const` component from `@kubb/renderer-jsx`.
|
|
@@ -183,8 +246,6 @@ function printType(node) {
|
|
|
183
246
|
function printFunction(node) {
|
|
184
247
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node;
|
|
185
248
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
186
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
187
|
-
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
188
249
|
const body = printNodes(nodes);
|
|
189
250
|
const indented = body ? indentLines(body) : "";
|
|
190
251
|
const parts = [];
|
|
@@ -193,9 +254,9 @@ function printFunction(node) {
|
|
|
193
254
|
if (isAsync) parts.push("async ");
|
|
194
255
|
parts.push("function ");
|
|
195
256
|
parts.push(name);
|
|
196
|
-
parts.push(
|
|
257
|
+
parts.push(formatGenerics(generics));
|
|
197
258
|
parts.push(`(${params ?? ""})`);
|
|
198
|
-
parts.push(
|
|
259
|
+
parts.push(formatReturnType(returnType, isAsync));
|
|
199
260
|
parts.push(" {");
|
|
200
261
|
if (indented) parts.push(`\n${indented}\n`);
|
|
201
262
|
parts.push("}");
|
|
@@ -221,8 +282,6 @@ function printFunction(node) {
|
|
|
221
282
|
function printArrowFunction(node) {
|
|
222
283
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node;
|
|
223
284
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
224
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
225
|
-
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
226
285
|
const body = printNodes(nodes);
|
|
227
286
|
const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\n${indentLines(body)}\n}` : " => {}";
|
|
228
287
|
const parts = [];
|
|
@@ -232,9 +291,9 @@ function printArrowFunction(node) {
|
|
|
232
291
|
parts.push(name);
|
|
233
292
|
parts.push(" = ");
|
|
234
293
|
if (isAsync) parts.push("async ");
|
|
235
|
-
parts.push(
|
|
294
|
+
parts.push(formatGenerics(generics));
|
|
236
295
|
parts.push(`(${params ?? ""})`);
|
|
237
|
-
parts.push(
|
|
296
|
+
parts.push(formatReturnType(returnType, isAsync));
|
|
238
297
|
parts.push(arrowBody);
|
|
239
298
|
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
240
299
|
}
|
|
@@ -295,25 +354,20 @@ const parserTs = defineParser({
|
|
|
295
354
|
const importNodes = [];
|
|
296
355
|
for (const item of file.imports) {
|
|
297
356
|
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
|
|
298
|
-
const hasExtname = !!/\.[^/.]+$/.exec(importPath);
|
|
299
357
|
importNodes.push(createImport({
|
|
300
358
|
name: item.name,
|
|
301
|
-
path:
|
|
359
|
+
path: resolveOutputPath(importPath, options, Boolean(item.root)),
|
|
302
360
|
isTypeOnly: item.isTypeOnly,
|
|
303
361
|
isNameSpace: item.isNameSpace
|
|
304
362
|
}));
|
|
305
363
|
}
|
|
306
364
|
const exportNodes = [];
|
|
307
|
-
for (const item of file.exports) {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
isTypeOnly: item.isTypeOnly,
|
|
314
|
-
asAlias: item.asAlias
|
|
315
|
-
}));
|
|
316
|
-
}
|
|
365
|
+
for (const item of file.exports) exportNodes.push(createExport({
|
|
366
|
+
name: item.name,
|
|
367
|
+
path: resolveOutputPath(item.path, options, true),
|
|
368
|
+
isTypeOnly: item.isTypeOnly,
|
|
369
|
+
asAlias: item.asAlias
|
|
370
|
+
}));
|
|
317
371
|
return [
|
|
318
372
|
file.banner,
|
|
319
373
|
print(...importNodes, ...exportNodes),
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/parserTs.ts","../src/parserTsx.ts"],"sourcesContent":["import { normalize, relative } from 'node:path'\nimport type { ArrowFunctionNode, CodeNode, ConstNode, FileNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport ts from 'typescript'\n\nconst { factory } = ts\n\nfunction slash(path: string): string {\n return normalize(path).replaceAll(/\\\\/g, '/').replace('../', '')\n}\n\nfunction getRelativePath(rootDir: string, filePath: string): string {\n const rel = relative(rootDir, filePath)\n const slashed = slash(rel)\n return slashed.startsWith('../') ? slashed : `./${slashed}`\n}\n\nfunction trimExtName(text: string): string {\n return text.replace(/\\.[^/.]+$/, '')\n}\n\n/**\n * Validates TypeScript AST nodes before printing.\n * Throws an error if any node has SyntaxKind.Unknown which would cause the\n * TypeScript printer to crash.\n */\nexport function validateNodes(...nodes: ts.Node[]): void {\n for (const node of nodes) {\n if (!node) {\n throw new Error('Attempted to print undefined or null TypeScript node')\n }\n if (node.kind === ts.SyntaxKind.Unknown) {\n throw new Error(\n 'Invalid TypeScript AST node detected with SyntaxKind.Unknown. ' +\n 'This typically indicates a schema pattern that could not be properly converted to TypeScript. ' +\n `Node: ${JSON.stringify(node, null, 2)}`,\n )\n }\n }\n}\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 sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(/\\r\\n/g, '\\n')\n}\n\n/**\n * Like `print` but validates nodes first to surface issues early.\n */\nexport function safePrint(...elements: Array<ts.Node>): string {\n validateNodes(...elements)\n return print(...elements)\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n /** @default false */\n isTypeOnly?: boolean\n /** @default false */\n isNameSpace?: boolean\n}): ts.ImportDeclaration {\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,\n isTypeOnly = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean\n /** @default false */\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}): ts.ExportDeclaration {\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?.match(/^\\d/) ? `_${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 * 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(/\\*\\//g, '* /').replace(/\\r/g, ''))\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 * 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 */\nfunction printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n return nodes.map(printCodeNode).join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nfunction indentLines(text: string, spaces = 2): 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 * 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: 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: 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\n const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''\n\n const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''\n\n const body = printNodes(nodes)\n const indented = body ? indentLines(body) : ''\n\n const parts: 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(genericsStr)\n parts.push(`(${params ?? ''})`)\n parts.push(returnTypeStr)\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\n const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''\n\n const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''\n\n const body = printNodes(nodes)\n\n const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\\n${indentLines(body)}\\n}` : ' => {}'\n\n const parts: 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(genericsStr)\n parts.push(`(${params ?? ''})`)\n parts.push(returnTypeStr)\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 switch (node.kind) {\n case 'Break':\n return ''\n case 'Text':\n return (node as TextNode).value\n case 'Jsx':\n return (node as JsxNode).value\n case 'Const':\n return printConst(node)\n case 'Type':\n return printType(node)\n case 'Function':\n return printFunction(node)\n case 'ArrowFunction':\n return printArrowFunction(node)\n }\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 if (node.nodes && node.nodes.length > 0) {\n return node.nodes.map(printCodeNode).join('\\n')\n }\n return ''\n}\n\n/**\n * Parser that converts `.ts` and `.js` files to strings using the TypeScript\n * compiler. Handles import/export statement generation from file metadata.\n *\n * @default Used automatically when no `parsers` option is set in `defineConfig`.\n */\nexport const parserTs: Parser = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n async 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 const hasExtname = !!/\\.[^/.]+$/.exec(importPath)\n\n importNodes.push(\n createImport({\n name: item.name as string | Array<string | { propertyName: string; name?: string }>,\n path: options?.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,\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 const exportPath = item.path\n const hasExtname = !!/\\.[^/.]+$/.exec(exportPath)\n\n exportNodes.push(\n createExport({\n name: item.name as string | Array<ts.Identifier | string> | undefined,\n path: options?.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),\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 type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport { parserTs } from './parserTs.ts'\n\n/**\n * Parser that converts `.tsx` and `.jsx` files to strings.\n * Delegates to `typescriptParser` since the TypeScript compiler natively\n * supports JSX/TSX syntax via `ScriptKind.TSX`.\n *\n * Add this parser to the `parsers` option in `defineConfig` when generating `.tsx`/`.jsx` files.\n *\n * @default extname '.tsx'\n */\nexport const parserTsx: Parser = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n async parse(file, options = { extname: '.tsx' }) {\n return parserTs.parse(file, options)\n },\n})\n"],"mappings":";;;;;AAMA,MAAM,EAAE,YAAY;AAEpB,SAAS,MAAM,MAAsB;AACnC,QAAO,UAAU,KAAK,CAAC,WAAW,OAAO,IAA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c;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,YAAY;GAAE;GAAQ,KAAK;GAAO,CACpF,QAAQ,YAA+B,QAAQ,QAAQ,CAAC,CACxD,KAAK,MAAM,EAAE,SAAS,CAAC,CACb,KAAK,OAAO;;CAE5B,CAAC;;;;;;;;;;;;AC7cF,MAAa,YAAoB,aAAa;CAC5C,MAAM;CACN,UAAU,CAAC,QAAQ,OAAO;CAC1B,MAAM,MAAM,MAAM,UAAU,EAAE,SAAS,QAAQ,EAAE;AAC/C,SAAO,SAAS,MAAM,MAAM,QAAQ;;CAEvC,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/constants.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 neutralise premature\n * comment terminators (`*\\/` → `* /`).\n */\nexport const JSDOC_TERMINATOR_PATTERN = /\\*\\//g\n\n/**\n * Matches carriage returns for normalising CRLF/CR line endings to LF.\n */\nexport const CARRIAGE_RETURN_PATTERN = /\\r/g\n\n/**\n * Matches CRLF sequences used when normalising 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, FileNode, FunctionNode, JSDocNode, JsxNode, SourceNode, TextNode, TypeNode } from '@kubb/ast'\nimport type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\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\nfunction 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 */\nfunction 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 */\nfunction 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 */\nfunction 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 * Validates TypeScript AST nodes before printing.\n * Throws an error if any node has SyntaxKind.Unknown which would cause the\n * TypeScript printer to crash.\n */\nexport function validateNodes(...nodes: ts.Node[]): void {\n for (const node of nodes) {\n if (!node) {\n throw new Error('Attempted to print undefined or null TypeScript node')\n }\n if (node.kind === ts.SyntaxKind.Unknown) {\n throw new Error(\n 'Invalid TypeScript AST node detected with SyntaxKind.Unknown. ' +\n 'This typically indicates a schema pattern that could not be properly converted to TypeScript. ' +\n `Node: ${JSON.stringify(node, null, 2)}`,\n )\n }\n }\n}\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 sourceFile = ts.createSourceFile('print.tsx', '', ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)\n\n return output.replace(CRLF_PATTERN, '\\n')\n}\n\n/**\n * Like `print` but validates nodes first to surface issues early.\n */\nexport function safePrint(...elements: Array<ts.Node>): string {\n validateNodes(...elements)\n return print(...elements)\n}\n\nexport function createImport({\n name,\n path,\n root,\n isTypeOnly = false,\n isNameSpace = false,\n}: {\n name: string | Array<string | { propertyName: string; name?: string }>\n path: string\n root?: string\n /** @default false */\n isTypeOnly?: boolean\n /** @default false */\n isNameSpace?: boolean\n}): ts.ImportDeclaration {\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,\n isTypeOnly = false,\n name,\n}: {\n path: string\n /** @default false */\n asAlias?: boolean\n /** @default false */\n isTypeOnly?: boolean\n name?: string | Array<ts.Identifier | string>\n}): ts.ExportDeclaration {\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 * 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 * 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 */\nfunction printNodes(nodes: Array<CodeNode> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n return nodes.map(printCodeNode).join('\\n')\n}\n\n/**\n * Indents every non-empty line of `text` by `spaces` spaces.\n */\nfunction 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 */\nfunction 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 */\nfunction formatReturnType(returnType: string | undefined, isAsync: boolean | undefined): string {\n if (!returnType) return ''\n return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`\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: 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: 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: 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: 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 switch (node.kind) {\n case 'Break':\n return ''\n case 'Text':\n return (node as TextNode).value\n case 'Jsx':\n return (node as JsxNode).value\n case 'Const':\n return printConst(node)\n case 'Type':\n return printType(node)\n case 'Function':\n return printFunction(node)\n case 'ArrowFunction':\n return printArrowFunction(node)\n }\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 if (node.nodes && node.nodes.length > 0) {\n return node.nodes.map(printCodeNode).join('\\n')\n }\n return ''\n}\n\n/**\n * Parser that converts `.ts` and `.js` files to strings using the TypeScript\n * compiler. Handles import/export statement generation from file metadata.\n *\n * @default Used automatically when no `parsers` option is set in `defineConfig`.\n */\nexport const parserTs: Parser = defineParser({\n name: 'typescript',\n extNames: ['.ts', '.js'],\n async 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> | 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 type { Parser } from '@kubb/core'\nimport { defineParser } from '@kubb/core'\nimport { parserTs } from './parserTs.ts'\n\n/**\n * Parser that converts `.tsx` and `.jsx` files to strings.\n * Delegates to `typescriptParser` since the TypeScript compiler natively\n * supports JSX/TSX syntax via `ScriptKind.TSX`.\n *\n * Add this parser to the `parsers` option in `defineConfig` when generating `.tsx`/`.jsx` files.\n *\n * @default extname '.tsx'\n */\nexport const parserTsx: Parser = defineParser({\n name: 'tsx',\n extNames: ['.tsx', '.jsx'],\n async 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;;;ACnBrC,MAAM,EAAE,YAAY;AAEpB,SAAS,MAAM,MAAsB;AACnC,QAAO,UAAU,KAAK,CAAC,WAAW,wBAAwB,IAAI,CAAC,QAAA,OAAiC,GAAG;;;;;;AAOrG,SAAS,gBAAgB,SAAiB,UAA0B;CAElE,MAAM,UAAU,MADJ,SAAS,SAAS,SACL,CAAC;AAC1B,QAAO,QAAQ,WAAA,MAAmC,GAAG,UAAU,KAA8B;;;;;;AAO/F,SAAS,YAAY,MAAsB;AACzC,QAAO,KAAK,QAAQ,wBAAwB,GAAG;;;;;;;AAQjD,SAAS,kBAAkB,MAAc,SAA2C,WAA4B;CAC9G,MAAM,aAAa,uBAAuB,KAAK,KAAK;AACpD,KAAI,SAAS,WAAW,WACtB,QAAO,GAAG,YAAY,KAAK,GAAG,QAAQ;AAExC,QAAO,YAAY,YAAY,KAAK,GAAG;;;;;;;AAQzC,S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"name": "@kubb/parser-ts",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.59",
|
|
4
4
|
"description": "TypeScript and TSX file parser for Kubb, converting generated files to strings using the TypeScript compiler.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"code-generator",
|
|
@@ -42,11 +42,11 @@
|
|
|
42
42
|
},
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"typescript": "^6.0.3",
|
|
45
|
-
"@kubb/core": "5.0.0-alpha.
|
|
45
|
+
"@kubb/core": "5.0.0-alpha.59"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@internals/utils": "0.0.0",
|
|
49
|
-
"@kubb/ast": "5.0.0-alpha.
|
|
49
|
+
"@kubb/ast": "5.0.0-alpha.59"
|
|
50
50
|
},
|
|
51
51
|
"engines": {
|
|
52
52
|
"node": ">=22"
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Number of spaces used to indent a nested block when pretty-printing.
|
|
3
|
+
*/
|
|
4
|
+
export const INDENT_SIZE = 2 as const
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Matches the trailing `.<ext>` segment of a path (keeps segments like `foo.bar.ts`
|
|
8
|
+
* intact by only trimming the last run of non-`/`/`.` characters).
|
|
9
|
+
*/
|
|
10
|
+
export const FILE_EXTENSION_PATTERN = /\.[^/.]+$/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Matches Windows-style backslash path separators.
|
|
14
|
+
*/
|
|
15
|
+
export const WINDOWS_PATH_SEPARATOR = /\\/g
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Matches `*\/` in free-form text so JSDoc bodies can neutralise premature
|
|
19
|
+
* comment terminators (`*\/` → `* /`).
|
|
20
|
+
*/
|
|
21
|
+
export const JSDOC_TERMINATOR_PATTERN = /\*\//g
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Matches carriage returns for normalising CRLF/CR line endings to LF.
|
|
25
|
+
*/
|
|
26
|
+
export const CARRIAGE_RETURN_PATTERN = /\r/g
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Matches CRLF sequences used when normalising TypeScript printer output.
|
|
30
|
+
*/
|
|
31
|
+
export const CRLF_PATTERN = /\r\n/g
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Matches an identifier that starts with a digit — JavaScript disallows this
|
|
35
|
+
* so the printer prefixes such names with `_`.
|
|
36
|
+
*/
|
|
37
|
+
export const LEADING_DIGIT_PATTERN = /^\d/
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Relative path prefix used to detect traversal segments (`../`).
|
|
41
|
+
*/
|
|
42
|
+
export const PARENT_DIRECTORY_PREFIX = '../' as const
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Relative path prefix used when resolving imports within the output root.
|
|
46
|
+
*/
|
|
47
|
+
export const CURRENT_DIRECTORY_PREFIX = './' as const
|
package/src/parserTs.ts
CHANGED
|
@@ -3,21 +3,53 @@ import type { ArrowFunctionNode, CodeNode, ConstNode, FileNode, FunctionNode, JS
|
|
|
3
3
|
import type { Parser } from '@kubb/core'
|
|
4
4
|
import { defineParser } from '@kubb/core'
|
|
5
5
|
import ts from 'typescript'
|
|
6
|
+
import {
|
|
7
|
+
CARRIAGE_RETURN_PATTERN,
|
|
8
|
+
CRLF_PATTERN,
|
|
9
|
+
CURRENT_DIRECTORY_PREFIX,
|
|
10
|
+
FILE_EXTENSION_PATTERN,
|
|
11
|
+
INDENT_SIZE,
|
|
12
|
+
JSDOC_TERMINATOR_PATTERN,
|
|
13
|
+
LEADING_DIGIT_PATTERN,
|
|
14
|
+
PARENT_DIRECTORY_PREFIX,
|
|
15
|
+
WINDOWS_PATH_SEPARATOR,
|
|
16
|
+
} from './constants.ts'
|
|
6
17
|
|
|
7
18
|
const { factory } = ts
|
|
8
19
|
|
|
9
20
|
function slash(path: string): string {
|
|
10
|
-
return normalize(path).replaceAll(
|
|
21
|
+
return normalize(path).replaceAll(WINDOWS_PATH_SEPARATOR, '/').replace(PARENT_DIRECTORY_PREFIX, '')
|
|
11
22
|
}
|
|
12
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Resolves `filePath` relative to `rootDir` and returns a POSIX-style path
|
|
26
|
+
* prefixed with `./` when the target sits inside the root, or `../` when it escapes it.
|
|
27
|
+
*/
|
|
13
28
|
function getRelativePath(rootDir: string, filePath: string): string {
|
|
14
29
|
const rel = relative(rootDir, filePath)
|
|
15
30
|
const slashed = slash(rel)
|
|
16
|
-
return slashed.startsWith(
|
|
31
|
+
return slashed.startsWith(PARENT_DIRECTORY_PREFIX) ? slashed : `${CURRENT_DIRECTORY_PREFIX}${slashed}`
|
|
17
32
|
}
|
|
18
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Strips the trailing file extension (for example `.ts`) from a path.
|
|
36
|
+
* Preserves intermediate dots like `foo.bar.ts` → `foo.bar`.
|
|
37
|
+
*/
|
|
19
38
|
function trimExtName(text: string): string {
|
|
20
|
-
return text.replace(
|
|
39
|
+
return text.replace(FILE_EXTENSION_PATTERN, '')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Rewrites an import/export path so its extension matches the caller-supplied
|
|
44
|
+
* `options.extname`. When the source path has no extension the original is kept,
|
|
45
|
+
* so virtual/module-only paths flow through unchanged.
|
|
46
|
+
*/
|
|
47
|
+
function resolveOutputPath(path: string, options: { extname?: string } | undefined, rootAware: boolean): string {
|
|
48
|
+
const hasExtname = FILE_EXTENSION_PATTERN.test(path)
|
|
49
|
+
if (options?.extname && hasExtname) {
|
|
50
|
+
return `${trimExtName(path)}${options.extname}`
|
|
51
|
+
}
|
|
52
|
+
return rootAware ? trimExtName(path) : path
|
|
21
53
|
}
|
|
22
54
|
|
|
23
55
|
/**
|
|
@@ -55,7 +87,7 @@ export function print(...elements: Array<ts.Node>): string {
|
|
|
55
87
|
|
|
56
88
|
const output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile)
|
|
57
89
|
|
|
58
|
-
return output.replace(
|
|
90
|
+
return output.replace(CRLF_PATTERN, '\n')
|
|
59
91
|
}
|
|
60
92
|
|
|
61
93
|
/**
|
|
@@ -135,7 +167,7 @@ export function createExport({
|
|
|
135
167
|
}
|
|
136
168
|
|
|
137
169
|
if (!Array.isArray(name)) {
|
|
138
|
-
const parsedName = name
|
|
170
|
+
const parsedName = name && LEADING_DIGIT_PATTERN.test(name) ? `_${name.slice(1)}` : name
|
|
139
171
|
|
|
140
172
|
return factory.createExportDeclaration(
|
|
141
173
|
undefined,
|
|
@@ -177,7 +209,7 @@ export function printJSDoc(jsDoc: JSDocNode): string {
|
|
|
177
209
|
|
|
178
210
|
const lines = comments
|
|
179
211
|
.flatMap((c) => c.split(/\r?\n/))
|
|
180
|
-
.map((l) => l.replace(
|
|
212
|
+
.map((l) => l.replace(JSDOC_TERMINATOR_PATTERN, '* /').replace(CARRIAGE_RETURN_PATTERN, ''))
|
|
181
213
|
.filter((l) => l.trim().length > 0)
|
|
182
214
|
|
|
183
215
|
if (lines.length === 0) return ''
|
|
@@ -200,7 +232,7 @@ function printNodes(nodes: Array<CodeNode> | undefined): string {
|
|
|
200
232
|
/**
|
|
201
233
|
* Indents every non-empty line of `text` by `spaces` spaces.
|
|
202
234
|
*/
|
|
203
|
-
function indentLines(text: string, spaces =
|
|
235
|
+
function indentLines(text: string, spaces: number = INDENT_SIZE): string {
|
|
204
236
|
if (!text) return ''
|
|
205
237
|
const pad = ' '.repeat(spaces)
|
|
206
238
|
return text
|
|
@@ -209,6 +241,24 @@ function indentLines(text: string, spaces = 2): string {
|
|
|
209
241
|
.join('\n')
|
|
210
242
|
}
|
|
211
243
|
|
|
244
|
+
/**
|
|
245
|
+
* Renders the generic clause (`<T, U>`) shared by function and arrow-function nodes.
|
|
246
|
+
* Accepts either a raw string (rendered verbatim) or an array of type-parameter names.
|
|
247
|
+
*/
|
|
248
|
+
function formatGenerics(generics: FunctionNode['generics'] | ArrowFunctionNode['generics']): string {
|
|
249
|
+
if (!generics) return ''
|
|
250
|
+
return `<${Array.isArray(generics) ? generics.join(', ') : generics}>`
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Renders the return-type suffix (`: T` or `: Promise<T>` when `isAsync` is true).
|
|
255
|
+
* Returns an empty string when no return type is provided.
|
|
256
|
+
*/
|
|
257
|
+
function formatReturnType(returnType: string | undefined, isAsync: boolean | undefined): string {
|
|
258
|
+
if (!returnType) return ''
|
|
259
|
+
return isAsync ? `: Promise<${returnType}>` : `: ${returnType}`
|
|
260
|
+
}
|
|
261
|
+
|
|
212
262
|
/**
|
|
213
263
|
* Converts a {@link ConstNode} to a TypeScript `const` declaration string.
|
|
214
264
|
*
|
|
@@ -296,11 +346,6 @@ export function printFunction(node: FunctionNode): string {
|
|
|
296
346
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node
|
|
297
347
|
|
|
298
348
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''
|
|
299
|
-
|
|
300
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''
|
|
301
|
-
|
|
302
|
-
const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''
|
|
303
|
-
|
|
304
349
|
const body = printNodes(nodes)
|
|
305
350
|
const indented = body ? indentLines(body) : ''
|
|
306
351
|
|
|
@@ -310,9 +355,9 @@ export function printFunction(node: FunctionNode): string {
|
|
|
310
355
|
if (isAsync) parts.push('async ')
|
|
311
356
|
parts.push('function ')
|
|
312
357
|
parts.push(name)
|
|
313
|
-
parts.push(
|
|
358
|
+
parts.push(formatGenerics(generics))
|
|
314
359
|
parts.push(`(${params ?? ''})`)
|
|
315
|
-
parts.push(
|
|
360
|
+
parts.push(formatReturnType(returnType, isAsync))
|
|
316
361
|
parts.push(' {')
|
|
317
362
|
if (indented) {
|
|
318
363
|
parts.push(`\n${indented}\n`)
|
|
@@ -344,13 +389,7 @@ export function printArrowFunction(node: ArrowFunctionNode): string {
|
|
|
344
389
|
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node
|
|
345
390
|
|
|
346
391
|
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : ''
|
|
347
|
-
|
|
348
|
-
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(', ') : generics}>` : ''
|
|
349
|
-
|
|
350
|
-
const returnTypeStr = returnType ? (isAsync ? `: Promise<${returnType}>` : `: ${returnType}`) : ''
|
|
351
|
-
|
|
352
392
|
const body = printNodes(nodes)
|
|
353
|
-
|
|
354
393
|
const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\n${indentLines(body)}\n}` : ' => {}'
|
|
355
394
|
|
|
356
395
|
const parts: string[] = []
|
|
@@ -360,9 +399,9 @@ export function printArrowFunction(node: ArrowFunctionNode): string {
|
|
|
360
399
|
parts.push(name)
|
|
361
400
|
parts.push(' = ')
|
|
362
401
|
if (isAsync) parts.push('async ')
|
|
363
|
-
parts.push(
|
|
402
|
+
parts.push(formatGenerics(generics))
|
|
364
403
|
parts.push(`(${params ?? ''})`)
|
|
365
|
-
parts.push(
|
|
404
|
+
parts.push(formatReturnType(returnType, isAsync))
|
|
366
405
|
parts.push(arrowBody)
|
|
367
406
|
|
|
368
407
|
const declaration = parts.join('')
|
|
@@ -440,12 +479,10 @@ export const parserTs: Parser = defineParser({
|
|
|
440
479
|
const importNodes: Array<ts.ImportDeclaration> = []
|
|
441
480
|
for (const item of (file as FileNode).imports) {
|
|
442
481
|
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path
|
|
443
|
-
const hasExtname = !!/\.[^/.]+$/.exec(importPath)
|
|
444
|
-
|
|
445
482
|
importNodes.push(
|
|
446
483
|
createImport({
|
|
447
484
|
name: item.name as string | Array<string | { propertyName: string; name?: string }>,
|
|
448
|
-
path:
|
|
485
|
+
path: resolveOutputPath(importPath, options, Boolean(item.root)),
|
|
449
486
|
isTypeOnly: item.isTypeOnly,
|
|
450
487
|
isNameSpace: item.isNameSpace,
|
|
451
488
|
}),
|
|
@@ -454,13 +491,10 @@ export const parserTs: Parser = defineParser({
|
|
|
454
491
|
|
|
455
492
|
const exportNodes: Array<ts.ExportDeclaration> = []
|
|
456
493
|
for (const item of (file as FileNode).exports) {
|
|
457
|
-
const exportPath = item.path
|
|
458
|
-
const hasExtname = !!/\.[^/.]+$/.exec(exportPath)
|
|
459
|
-
|
|
460
494
|
exportNodes.push(
|
|
461
495
|
createExport({
|
|
462
496
|
name: item.name as string | Array<ts.Identifier | string> | undefined,
|
|
463
|
-
path:
|
|
497
|
+
path: resolveOutputPath(item.path, options, true),
|
|
464
498
|
isTypeOnly: item.isTypeOnly,
|
|
465
499
|
asAlias: item.asAlias,
|
|
466
500
|
}),
|