@kubb/parser-ts 4.1.4 → 5.0.0-alpha.32
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/LICENSE +10 -17
- package/dist/chunk--u3MIqq1.js +8 -0
- package/dist/index.cjs +362 -53
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +61 -19
- package/dist/index.js +336 -46
- package/dist/index.js.map +1 -1
- package/package.json +15 -27
- package/src/index.ts +2 -3
- package/src/parserTs.ts +474 -0
- package/src/parserTsx.ts +20 -0
- package/README.md +0 -54
- package/dist/chunk-CTAAG5j7.js +0 -13
- package/dist/factory-BJCGLhSr.d.ts +0 -227
- package/dist/factory-BNDICAoK.cjs +0 -496
- package/dist/factory-BNDICAoK.cjs.map +0 -1
- package/dist/factory-CroRqsMR.js +0 -249
- package/dist/factory-CroRqsMR.js.map +0 -1
- package/dist/factory-DqBZ_C7_.d.cts +0 -227
- package/dist/factory.cjs +0 -36
- package/dist/factory.d.cts +0 -2
- package/dist/factory.d.ts +0 -2
- package/dist/factory.js +0 -3
- package/dist/index.d.cts +0 -25
- package/src/factory.ts +0 -578
- package/src/format.ts +0 -25
- package/src/print.ts +0 -48
package/dist/index.js
CHANGED
|
@@ -1,60 +1,350 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./chunk--u3MIqq1.js";
|
|
2
|
+
import { normalize, relative } from "node:path";
|
|
3
|
+
import { defineParser } from "@kubb/core";
|
|
2
4
|
import ts from "typescript";
|
|
3
|
-
|
|
4
|
-
import pluginTypescript from "prettier/plugins/typescript";
|
|
5
|
-
|
|
6
|
-
//#region src/format.ts
|
|
7
|
-
const formatOptions = {
|
|
8
|
-
tabWidth: 2,
|
|
9
|
-
printWidth: 160,
|
|
10
|
-
parser: "typescript",
|
|
11
|
-
singleQuote: true,
|
|
12
|
-
semi: false,
|
|
13
|
-
bracketSameLine: false,
|
|
14
|
-
endOfLine: "auto",
|
|
15
|
-
plugins: [pluginTypescript]
|
|
16
|
-
};
|
|
17
|
-
function format(source) {
|
|
18
|
-
if (!source) return Promise.resolve("");
|
|
19
|
-
try {
|
|
20
|
-
return format$1(source, formatOptions);
|
|
21
|
-
} catch (_e) {
|
|
22
|
-
return Promise.resolve(source);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//#endregion
|
|
27
|
-
//#region src/print.ts
|
|
5
|
+
//#region src/parserTs.ts
|
|
28
6
|
const { factory } = ts;
|
|
7
|
+
function slash(path) {
|
|
8
|
+
return normalize(path).replaceAll(/\\/g, "/").replace("../", "");
|
|
9
|
+
}
|
|
10
|
+
function getRelativePath(rootDir, filePath) {
|
|
11
|
+
const slashed = slash(relative(rootDir, filePath));
|
|
12
|
+
return slashed.startsWith("../") ? slashed : `./${slashed}`;
|
|
13
|
+
}
|
|
14
|
+
function trimExtName(text) {
|
|
15
|
+
return text.replace(/\.[^/.]+$/, "");
|
|
16
|
+
}
|
|
29
17
|
/**
|
|
30
|
-
*
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
/**
|
|
34
|
-
* Reverses {@link escapeNewLines} and restores new lines
|
|
18
|
+
* Validates TypeScript AST nodes before printing.
|
|
19
|
+
* Throws an error if any node has SyntaxKind.Unknown which would cause the
|
|
20
|
+
* TypeScript printer to crash.
|
|
35
21
|
*/
|
|
36
|
-
|
|
22
|
+
function validateNodes(...nodes) {
|
|
23
|
+
for (const node of nodes) {
|
|
24
|
+
if (!node) throw new Error("Attempted to print undefined or null TypeScript node");
|
|
25
|
+
if (node.kind === ts.SyntaxKind.Unknown) throw new Error(`Invalid TypeScript AST node detected with SyntaxKind.Unknown. This typically indicates a schema pattern that could not be properly converted to TypeScript. Node: ${JSON.stringify(node, null, 2)}`);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
37
28
|
/**
|
|
38
|
-
*
|
|
39
|
-
* Ensures consistent output across environments.
|
|
40
|
-
* Also works as a formatter when `source` is provided without `elements`.
|
|
29
|
+
* Converts TypeScript/TSX AST nodes to a string using the TypeScript printer.
|
|
41
30
|
*/
|
|
42
|
-
function print(elements
|
|
43
|
-
const sourceFile = ts.createSourceFile(
|
|
44
|
-
|
|
31
|
+
function print(...elements) {
|
|
32
|
+
const sourceFile = ts.createSourceFile("print.tsx", "", ts.ScriptTarget.ES2022, true, ts.ScriptKind.TSX);
|
|
33
|
+
return ts.createPrinter({
|
|
45
34
|
omitTrailingSemicolon: true,
|
|
46
35
|
newLine: ts.NewLineKind.LineFeed,
|
|
47
36
|
removeComments: false,
|
|
48
37
|
noEmitHelpers: true
|
|
38
|
+
}).printList(ts.ListFormat.MultiLine, factory.createNodeArray(elements.filter(Boolean)), sourceFile).replace(/\r\n/g, "\n");
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Like `print` but validates nodes first to surface issues early.
|
|
42
|
+
*/
|
|
43
|
+
function safePrint(...elements) {
|
|
44
|
+
validateNodes(...elements);
|
|
45
|
+
return print(...elements);
|
|
46
|
+
}
|
|
47
|
+
function createImport({ name, path, root, isTypeOnly = false, isNameSpace = false }) {
|
|
48
|
+
const resolvePath = root ? getRelativePath(root, path) : path;
|
|
49
|
+
if (!Array.isArray(name)) {
|
|
50
|
+
if (isNameSpace) return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamespaceImport(factory.createIdentifier(name))), factory.createStringLiteral(resolvePath), void 0);
|
|
51
|
+
return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, factory.createIdentifier(name), void 0), factory.createStringLiteral(resolvePath), void 0);
|
|
52
|
+
}
|
|
53
|
+
const specifiers = name.map((item) => {
|
|
54
|
+
if (typeof item === "object") {
|
|
55
|
+
const { propertyName, name: alias } = item;
|
|
56
|
+
return factory.createImportSpecifier(false, alias ? factory.createIdentifier(propertyName) : void 0, factory.createIdentifier(alias ?? propertyName));
|
|
57
|
+
}
|
|
58
|
+
return factory.createImportSpecifier(false, void 0, factory.createIdentifier(item));
|
|
49
59
|
});
|
|
50
|
-
|
|
51
|
-
if (elements.length > 0) {
|
|
52
|
-
const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0));
|
|
53
|
-
output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile);
|
|
54
|
-
} else output = printer.printFile(sourceFile);
|
|
55
|
-
return restoreNewLines(output).replace(/\r\n/g, "\n");
|
|
60
|
+
return factory.createImportDeclaration(void 0, factory.createImportClause(isTypeOnly, void 0, factory.createNamedImports(specifiers)), factory.createStringLiteral(resolvePath), void 0);
|
|
56
61
|
}
|
|
57
|
-
|
|
62
|
+
function createExport({ path, asAlias, isTypeOnly = false, name }) {
|
|
63
|
+
if (name && !Array.isArray(name) && !asAlias) console.warn(`When using name as string, asAlias should be true: ${name}`);
|
|
64
|
+
if (!Array.isArray(name)) {
|
|
65
|
+
const parsedName = name?.match(/^\d/) ? `_${name?.slice(1)}` : name;
|
|
66
|
+
return factory.createExportDeclaration(void 0, isTypeOnly, asAlias && parsedName ? factory.createNamespaceExport(factory.createIdentifier(parsedName)) : void 0, factory.createStringLiteral(path), void 0);
|
|
67
|
+
}
|
|
68
|
+
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);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Converts a {@link JSDocNode} to a JSDoc comment block string.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* printJSDoc({ comments: ['@description A pet', '@deprecated'] })
|
|
76
|
+
* // /**
|
|
77
|
+
* // * @description A pet
|
|
78
|
+
* // * @deprecated
|
|
79
|
+
* // *\/
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
function printJSDoc(jsDoc) {
|
|
83
|
+
const comments = (jsDoc.comments ?? []).filter((c) => c != null);
|
|
84
|
+
if (comments.length === 0) return "";
|
|
85
|
+
const lines = comments.flatMap((c) => c.split(/\r?\n/)).map((l) => l.replace(/\*\//g, "* /").replace(/\r/g, "")).filter((l) => l.trim().length > 0);
|
|
86
|
+
if (lines.length === 0) return "";
|
|
87
|
+
return [
|
|
88
|
+
"/**",
|
|
89
|
+
...lines.map((l) => ` * ${l}`),
|
|
90
|
+
" */"
|
|
91
|
+
].join("\n");
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Serialises the body / value content from a `nodes` array.
|
|
95
|
+
*
|
|
96
|
+
* Each element is either a raw string or a structured {@link CodeNode}
|
|
97
|
+
* (recursively converted via {@link printCodeNode}).
|
|
98
|
+
* Elements are joined with `\n`.
|
|
99
|
+
*/
|
|
100
|
+
function printNodes(nodes) {
|
|
101
|
+
if (!nodes || nodes.length === 0) return "";
|
|
102
|
+
return nodes.map((n) => typeof n === "string" ? n : printCodeNode(n)).join("\n");
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Indents every non-empty line of `text` by `spaces` spaces.
|
|
106
|
+
*/
|
|
107
|
+
function indentLines(text, spaces = 2) {
|
|
108
|
+
if (!text) return "";
|
|
109
|
+
const pad = " ".repeat(spaces);
|
|
110
|
+
return text.split("\n").map((line) => line.trim() ? `${pad}${line}` : "").join("\n");
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Converts a {@link ConstNode} to a TypeScript `const` declaration string.
|
|
114
|
+
*
|
|
115
|
+
* Mirrors the `Const` component from `@kubb/react-fabric`.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```ts
|
|
119
|
+
* printConst(createConst({ name: 'pet', export: true, nodes: ['{}'] }))
|
|
120
|
+
* // 'export const pet = {}'
|
|
121
|
+
* ```
|
|
122
|
+
*
|
|
123
|
+
* @example With type and `as const`
|
|
124
|
+
* ```ts
|
|
125
|
+
* printConst(createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true, nodes: ['[]'] }))
|
|
126
|
+
* // 'export const pets: Pet[] = [] as const'
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
function printConst(node) {
|
|
130
|
+
const { name, export: canExport, type, JSDoc, asConst, nodes } = node;
|
|
131
|
+
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
132
|
+
const body = printNodes(nodes);
|
|
133
|
+
const parts = [];
|
|
134
|
+
if (canExport) parts.push("export ");
|
|
135
|
+
parts.push("const ");
|
|
136
|
+
parts.push(name);
|
|
137
|
+
if (type) parts.push(`: ${type}`);
|
|
138
|
+
parts.push(" = ");
|
|
139
|
+
parts.push(body);
|
|
140
|
+
if (asConst) parts.push(" as const");
|
|
141
|
+
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Converts a {@link TypeNode} to a TypeScript `type` alias declaration string.
|
|
145
|
+
*
|
|
146
|
+
* Mirrors the `Type` component from `@kubb/react-fabric`.
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```ts
|
|
150
|
+
* printType(createType({ name: 'Pet', export: true, nodes: ['{ id: number }'] }))
|
|
151
|
+
* // 'export type Pet = { id: number }'
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
function printType(node) {
|
|
155
|
+
const { name, export: canExport, JSDoc, nodes } = node;
|
|
156
|
+
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
157
|
+
const body = printNodes(nodes);
|
|
158
|
+
const parts = [];
|
|
159
|
+
if (canExport) parts.push("export ");
|
|
160
|
+
parts.push("type ");
|
|
161
|
+
parts.push(name);
|
|
162
|
+
parts.push(" = ");
|
|
163
|
+
parts.push(body);
|
|
164
|
+
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Converts a {@link FunctionNode} to a TypeScript `function` declaration string.
|
|
168
|
+
*
|
|
169
|
+
* Mirrors the `Function` component from `@kubb/react-fabric`.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* printFunction(createFunction({ name: 'getPet', export: true, params: 'id: string', returnType: 'Pet', nodes: ['return fetch(id)'] }))
|
|
174
|
+
* // 'export function getPet(id: string): Pet {\n return fetch(id)\n}'
|
|
175
|
+
* ```
|
|
176
|
+
*
|
|
177
|
+
* @example Async with generics
|
|
178
|
+
* ```ts
|
|
179
|
+
* printFunction(createFunction({ name: 'fetchPet', export: true, async: true, generics: ['T'], params: 'id: string', returnType: 'T' }))
|
|
180
|
+
* // 'export async function fetchPet<T>(id: string): Promise<T> {\n}'
|
|
181
|
+
* ```
|
|
182
|
+
*/
|
|
183
|
+
function printFunction(node) {
|
|
184
|
+
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes } = node;
|
|
185
|
+
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
186
|
+
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
187
|
+
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
188
|
+
const body = printNodes(nodes);
|
|
189
|
+
const indented = body ? indentLines(body) : "";
|
|
190
|
+
const parts = [];
|
|
191
|
+
if (canExport) parts.push("export ");
|
|
192
|
+
if (isDefault) parts.push("default ");
|
|
193
|
+
if (isAsync) parts.push("async ");
|
|
194
|
+
parts.push("function ");
|
|
195
|
+
parts.push(name);
|
|
196
|
+
parts.push(genericsStr);
|
|
197
|
+
parts.push(`(${params ?? ""})`);
|
|
198
|
+
parts.push(returnTypeStr);
|
|
199
|
+
parts.push(" {");
|
|
200
|
+
if (indented) parts.push(`\n${indented}\n`);
|
|
201
|
+
parts.push("}");
|
|
202
|
+
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Converts an {@link ArrowFunctionNode} to a TypeScript arrow function declaration string.
|
|
206
|
+
*
|
|
207
|
+
* Mirrors the `Function.Arrow` component from `@kubb/react-fabric`.
|
|
208
|
+
*
|
|
209
|
+
* @example Multi-line arrow function
|
|
210
|
+
* ```ts
|
|
211
|
+
* printArrowFunction(createArrowFunction({ name: 'getPet', export: true, params: 'id: string', nodes: ['return fetch(id)'] }))
|
|
212
|
+
* // 'export const getPet = (id: string) => {\n return fetch(id)\n}'
|
|
213
|
+
* ```
|
|
214
|
+
*
|
|
215
|
+
* @example Single-line arrow function
|
|
216
|
+
* ```ts
|
|
217
|
+
* printArrowFunction(createArrowFunction({ name: 'double', params: 'n: number', singleLine: true, nodes: ['n * 2'] }))
|
|
218
|
+
* // 'const double = (n: number) => n * 2'
|
|
219
|
+
* ```
|
|
220
|
+
*/
|
|
221
|
+
function printArrowFunction(node) {
|
|
222
|
+
const { name, default: isDefault, export: canExport, async: isAsync, generics, params, returnType, JSDoc, nodes, singleLine } = node;
|
|
223
|
+
const jsDocStr = JSDoc ? printJSDoc(JSDoc) : "";
|
|
224
|
+
const genericsStr = generics ? `<${Array.isArray(generics) ? generics.join(", ") : generics}>` : "";
|
|
225
|
+
const returnTypeStr = returnType ? isAsync ? `: Promise<${returnType}>` : `: ${returnType}` : "";
|
|
226
|
+
const body = printNodes(nodes);
|
|
227
|
+
const arrowBody = singleLine ? ` => ${body}` : body ? ` => {\n${indentLines(body)}\n}` : " => {}";
|
|
228
|
+
const parts = [];
|
|
229
|
+
if (canExport) parts.push("export ");
|
|
230
|
+
if (isDefault) parts.push("default ");
|
|
231
|
+
parts.push("const ");
|
|
232
|
+
parts.push(name);
|
|
233
|
+
parts.push(" = ");
|
|
234
|
+
if (isAsync) parts.push("async ");
|
|
235
|
+
parts.push(genericsStr);
|
|
236
|
+
parts.push(`(${params ?? ""})`);
|
|
237
|
+
parts.push(returnTypeStr);
|
|
238
|
+
parts.push(arrowBody);
|
|
239
|
+
return [jsDocStr, parts.join("")].filter(Boolean).join("\n");
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* Converts a {@link CodeNode} to its TypeScript string representation.
|
|
243
|
+
*
|
|
244
|
+
* Dispatches to the appropriate printer based on the node's `kind`.
|
|
245
|
+
*
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* printCodeNode(createConst({ name: 'x', nodes: ['1'] }))
|
|
249
|
+
* // 'const x = 1'
|
|
250
|
+
* ```
|
|
251
|
+
*/
|
|
252
|
+
function printCodeNode(node) {
|
|
253
|
+
switch (node.kind) {
|
|
254
|
+
case "Const": return printConst(node);
|
|
255
|
+
case "Type": return printType(node);
|
|
256
|
+
case "Function": return printFunction(node);
|
|
257
|
+
case "ArrowFunction": return printArrowFunction(node);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Converts a {@link SourceNode} to its TypeScript string representation.
|
|
262
|
+
*
|
|
263
|
+
* Uses `value` if present; otherwise converts the structured `nodes` array
|
|
264
|
+
* via {@link printCodeNode}.
|
|
265
|
+
*
|
|
266
|
+
* @example From value
|
|
267
|
+
* ```ts
|
|
268
|
+
* printSource({ kind: 'Source', value: 'const x = 1' })
|
|
269
|
+
* // 'const x = 1'
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @example From nodes
|
|
273
|
+
* ```ts
|
|
274
|
+
* printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: ['1'] })] })
|
|
275
|
+
* // 'const x = 1'
|
|
276
|
+
* ```
|
|
277
|
+
*/
|
|
278
|
+
function printSource(node) {
|
|
279
|
+
if (node.value) return node.value;
|
|
280
|
+
if (node.nodes && node.nodes.length > 0) return node.nodes.map(printCodeNode).join("\n");
|
|
281
|
+
return "";
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Parser that converts `.ts` and `.js` files to strings using the TypeScript
|
|
285
|
+
* compiler. Handles import/export statement generation from file metadata.
|
|
286
|
+
*
|
|
287
|
+
* @default Used automatically when no `parsers` option is set in `defineConfig`.
|
|
288
|
+
*/
|
|
289
|
+
const parserTs = defineParser({
|
|
290
|
+
name: "typescript",
|
|
291
|
+
extNames: [".ts", ".js"],
|
|
292
|
+
async parse(file, options = { extname: ".ts" }) {
|
|
293
|
+
const sourceParts = [];
|
|
294
|
+
for (const item of file.sources) {
|
|
295
|
+
const sourceStr = printSource(item);
|
|
296
|
+
if (sourceStr) sourceParts.push(sourceStr);
|
|
297
|
+
}
|
|
298
|
+
const source = sourceParts.join("\n\n");
|
|
299
|
+
const importNodes = [];
|
|
300
|
+
for (const item of file.imports) {
|
|
301
|
+
const importPath = item.root ? getRelativePath(item.root, item.path) : item.path;
|
|
302
|
+
const hasExtname = !!/\.[^/.]+$/.exec(importPath);
|
|
303
|
+
importNodes.push(createImport({
|
|
304
|
+
name: item.name,
|
|
305
|
+
path: options?.extname && hasExtname ? `${trimExtName(importPath)}${options.extname}` : item.root ? trimExtName(importPath) : importPath,
|
|
306
|
+
isTypeOnly: item.isTypeOnly,
|
|
307
|
+
isNameSpace: item.isNameSpace
|
|
308
|
+
}));
|
|
309
|
+
}
|
|
310
|
+
const exportNodes = [];
|
|
311
|
+
for (const item of file.exports) {
|
|
312
|
+
const exportPath = item.path;
|
|
313
|
+
const hasExtname = !!/\.[^/.]+$/.exec(exportPath);
|
|
314
|
+
exportNodes.push(createExport({
|
|
315
|
+
name: item.name,
|
|
316
|
+
path: options?.extname && hasExtname ? `${trimExtName(item.path)}${options.extname}` : trimExtName(item.path),
|
|
317
|
+
isTypeOnly: item.isTypeOnly,
|
|
318
|
+
asAlias: item.asAlias
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
return [
|
|
322
|
+
file.banner,
|
|
323
|
+
print(...importNodes, ...exportNodes),
|
|
324
|
+
source,
|
|
325
|
+
file.footer
|
|
326
|
+
].filter((segment) => segment != null).join("\n");
|
|
327
|
+
}
|
|
328
|
+
});
|
|
58
329
|
//#endregion
|
|
59
|
-
|
|
330
|
+
//#region src/parserTsx.ts
|
|
331
|
+
/**
|
|
332
|
+
* Parser that converts `.tsx` and `.jsx` files to strings.
|
|
333
|
+
* Delegates to `typescriptParser` since the TypeScript compiler natively
|
|
334
|
+
* supports JSX/TSX syntax via `ScriptKind.TSX`.
|
|
335
|
+
*
|
|
336
|
+
* Add this parser to the `parsers` option in `defineConfig` when generating `.tsx`/`.jsx` files.
|
|
337
|
+
*
|
|
338
|
+
* @default extname '.tsx'
|
|
339
|
+
*/
|
|
340
|
+
const parserTsx = defineParser({
|
|
341
|
+
name: "tsx",
|
|
342
|
+
extNames: [".tsx", ".jsx"],
|
|
343
|
+
async parse(file, options = { extname: ".tsx" }) {
|
|
344
|
+
return parserTs.parse(file, options);
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
//#endregion
|
|
348
|
+
export { createExport, createImport, parserTs, parserTsx, print, safePrint, validateNodes };
|
|
349
|
+
|
|
60
350
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["formatOptions: Options","prettierFormat","output: string"],"sources":["../src/format.ts","../src/print.ts"],"sourcesContent":["import type { Options } from 'prettier'\nimport { format as prettierFormat } from 'prettier'\nimport pluginTypescript from 'prettier/plugins/typescript'\n\nconst formatOptions: Options = {\n tabWidth: 2,\n printWidth: 160,\n parser: 'typescript',\n singleQuote: true,\n semi: false,\n bracketSameLine: false,\n endOfLine: 'auto',\n plugins: [pluginTypescript],\n}\nexport function format(source?: string): Promise<string> {\n if (!source) {\n return Promise.resolve('')\n }\n\n try {\n return prettierFormat(source, formatOptions)\n } catch (_e) {\n return Promise.resolve(source)\n }\n}\n","import ts from 'typescript'\n\nconst { factory } = ts\n\nexport type PrintOptions = {\n source?: string\n baseName?: string\n scriptKind?: ts.ScriptKind\n}\n\n/**\n * Escaped new lines in code with block comments so they can be restored by {@link restoreNewLines}\n */\nconst escapeNewLines = (code: string) => code.replace(/\\n\\n/g, '\\n/* :newline: */')\n\n/**\n * Reverses {@link escapeNewLines} and restores new lines\n */\nconst restoreNewLines = (code: string) => code.replace(/\\/\\* :newline: \\*\\//g, '\\n')\n\n/**\n * Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.\n * Ensures consistent output across environments.\n * Also works as a formatter when `source` is provided without `elements`.\n */\nexport function print(elements: Array<ts.Node> = [], { source = '', baseName = 'print.tsx', scriptKind = ts.ScriptKind.TSX }: PrintOptions = {}): string {\n const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind)\n\n const printer = ts.createPrinter({\n omitTrailingSemicolon: true,\n newLine: ts.NewLineKind.LineFeed,\n removeComments: false,\n noEmitHelpers: true,\n })\n\n let output: string\n\n if (elements.length > 0) {\n // Print only provided nodes\n const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))\n output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)\n } else {\n // Format the whole file\n output = printer.printFile(sourceFile)\n }\n\n return restoreNewLines(output).replace(/\\r\\n/g, '\\n')\n}\n"],"mappings":";;;;;;AAIA,MAAMA,gBAAyB;CAC7B,UAAU;CACV,YAAY;CACZ,QAAQ;CACR,aAAa;CACb,MAAM;CACN,iBAAiB;CACjB,WAAW;CACX,SAAS,CAAC,iBAAiB;CAC5B;AACD,SAAgB,OAAO,QAAkC;AACvD,KAAI,CAAC,OACH,QAAO,QAAQ,QAAQ,GAAG;AAG5B,KAAI;AACF,SAAOC,SAAe,QAAQ,cAAc;UACrC,IAAI;AACX,SAAO,QAAQ,QAAQ,OAAO;;;;;;ACpBlC,MAAM,EAAE,YAAY;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS,oBAAoB;;;;AAKnF,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB,KAAK;;;;;;AAOpF,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAa,GAAG,WAAW,QAAsB,EAAE,EAAU;CACvJ,MAAM,aAAa,GAAG,iBAAiB,UAAU,eAAe,OAAO,EAAE,GAAG,aAAa,QAAQ,MAAM,WAAW;CAElH,MAAM,UAAU,GAAG,cAAc;EAC/B,uBAAuB;EACvB,SAAS,GAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC;CAEF,IAAIC;AAEJ,KAAI,SAAS,SAAS,GAAG;EAEvB,MAAM,QAAQ,SAAS,OAAO,QAAQ,CAAC,MAAM,GAAG,OAAO,EAAE,OAAO,MAAM,EAAE,OAAO,GAAG;AAClF,WAAS,QAAQ,UAAU,GAAG,WAAW,WAAW,QAAQ,gBAAgB,MAAM,EAAE,WAAW;OAG/F,UAAS,QAAQ,UAAU,WAAW;AAGxC,QAAO,gBAAgB,OAAO,CAAC,QAAQ,SAAS,KAAK"}
|
|
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, SourceNode, TypeNode } from '@kubb/ast/types'\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 * Serialises 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 | string> | undefined): string {\n if (!nodes || nodes.length === 0) return ''\n return nodes.map((n) => (typeof n === 'string' ? n : printCodeNode(n))).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/react-fabric`.\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/react-fabric`.\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/react-fabric`.\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/react-fabric`.\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 '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 * Uses `value` if present; otherwise converts the structured `nodes` array\n * via {@link printCodeNode}.\n *\n * @example From value\n * ```ts\n * printSource({ kind: 'Source', value: 'const x = 1' })\n * // 'const x = 1'\n * ```\n *\n * @example From nodes\n * ```ts\n * printSource({ kind: 'Source', nodes: [createConst({ name: 'x', nodes: ['1'] })] })\n * // 'const x = 1'\n * ```\n */\nexport function printSource(node: SourceNode): string {\n if (node.value) return node.value\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)\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].filter((segment): segment is string => segment != null)\n return parts.join('\\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,IAAI,CAAC,QAAQ,OAAO,GAAG;;AAGlE,SAAS,gBAAgB,SAAiB,UAA0B;CAElE,MAAM,UAAU,MADJ,SAAS,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,SAAS,GAAG,WAAW,QAC9B,OAAM,IAAI,MACR,qKAEW,KAAK,UAAU,MAAM,MAAM,EAAE,GACzC;;;;;;AAQP,SAAgB,MAAM,GAAG,UAAkC;CACzD,MAAM,aAAa,GAAG,iBAAiB,aAAa,IAAI,GAAG,aAAa,QAAQ,MAAM,GAAG,WAAW,IAAI;AAWxG,QATgB,GAAG,cAAc;EAC/B,uBAAuB;EACvB,SAAS,GAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB,CAAC,CAEqB,UAAU,GAAG,WAAW,WAAW,QAAQ,gBAAgB,SAAS,OAAO,QAAQ,CAAC,EAAE,WAAW,CAE1G,QAAQ,SAAS,KAAK;;;;;AAMtC,SAAgB,UAAU,GAAG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W,GAAG;IAC9H,YAAY,KAAK;IACjB,aAAa,KAAK;IACnB,CAAC,CACH;;EAGH,MAAM,cAA2C,EAAE;AACnD,OAAK,MAAM,QAAS,KAAkB,SAAS;GAC7C,MAAM,aAAa,KAAK;GACxB,MAAM,aAAa,CAAC,CAAC,YAAY,KAAK,WAAW;AAEjD,eAAY,KACV,aAAa;IACX,MAAM,KAAK;IACX,MAAM,SAAS,WAAW,aAAa,GAAG,YAAY,KAAK,KAAK,GAAG,QAAQ,YAAY,YAAY,KAAK,KAAK;IAC7G,YAAY,KAAK;IACjB,SAAS,KAAK;IACf,CAAC,CACH;;AAIH,SADc;GAAC,KAAK;GAAQ,MAAM,GAAG,aAAa,GAAG,YAAY;GAAE;GAAQ,KAAK;GAAO,CAAC,QAAQ,YAA+B,WAAW,KAAK,CAClI,KAAK,KAAK;;CAE1B,CAAC;;;;;;;;;;;;AC5cF,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"}
|
package/package.json
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/parser-ts",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "TypeScript
|
|
3
|
+
"version": "5.0.0-alpha.32",
|
|
4
|
+
"description": "TypeScript and TSX file parser for Kubb, converting generated files to strings using the TypeScript compiler.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
|
+
"tsx",
|
|
7
8
|
"parser",
|
|
8
|
-
"
|
|
9
|
-
"code-generation",
|
|
10
|
-
"syntax",
|
|
11
|
-
"formatting",
|
|
9
|
+
"code-generator",
|
|
12
10
|
"codegen",
|
|
13
11
|
"kubb"
|
|
14
12
|
],
|
|
15
13
|
"repository": {
|
|
16
14
|
"type": "git",
|
|
17
|
-
"url": "https://github.com/kubb-labs/kubb.git",
|
|
15
|
+
"url": "git+https://github.com/kubb-labs/kubb.git",
|
|
18
16
|
"directory": "packages/parser-ts"
|
|
19
17
|
},
|
|
20
18
|
"license": "MIT",
|
|
@@ -26,43 +24,33 @@
|
|
|
26
24
|
"import": "./dist/index.js",
|
|
27
25
|
"require": "./dist/index.cjs"
|
|
28
26
|
},
|
|
29
|
-
"./factory": {
|
|
30
|
-
"import": "./dist/factory.js",
|
|
31
|
-
"require": "./dist/factory.cjs"
|
|
32
|
-
},
|
|
33
27
|
"./package.json": "./package.json"
|
|
34
28
|
},
|
|
35
|
-
"
|
|
36
|
-
"module": "./dist/index.js",
|
|
37
|
-
"types": "./dist/index.d.cts",
|
|
38
|
-
"typesVersions": {
|
|
39
|
-
"*": {
|
|
40
|
-
"factory": [
|
|
41
|
-
"./dist/factory.d.ts"
|
|
42
|
-
]
|
|
43
|
-
}
|
|
44
|
-
},
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
45
30
|
"files": [
|
|
46
31
|
"src",
|
|
47
32
|
"dist",
|
|
48
33
|
"!/**/**.test.**",
|
|
49
|
-
"!/**/__tests__/**"
|
|
34
|
+
"!/**/__tests__/**",
|
|
35
|
+
"!/**/__snapshots__/**"
|
|
50
36
|
],
|
|
51
37
|
"dependencies": {
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"typescript": "5.9.3"
|
|
38
|
+
"typescript": "^6.0.2",
|
|
39
|
+
"@kubb/core": "5.0.0-alpha.32"
|
|
55
40
|
},
|
|
56
41
|
"devDependencies": {
|
|
57
|
-
"
|
|
42
|
+
"@internals/utils": "0.0.0",
|
|
43
|
+
"@kubb/ast": "5.0.0-alpha.32"
|
|
58
44
|
},
|
|
59
45
|
"engines": {
|
|
60
|
-
"node": ">=
|
|
46
|
+
"node": ">=22"
|
|
61
47
|
},
|
|
62
48
|
"publishConfig": {
|
|
63
49
|
"access": "public",
|
|
64
50
|
"registry": "https://registry.npmjs.org/"
|
|
65
51
|
},
|
|
52
|
+
"main": "./dist/index.cjs",
|
|
53
|
+
"module": "./dist/index.js",
|
|
66
54
|
"scripts": {
|
|
67
55
|
"build": "tsdown",
|
|
68
56
|
"clean": "npx rimraf ./dist",
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
export
|
|
2
|
-
export {
|
|
3
|
-
export { print } from './print.ts'
|
|
1
|
+
export { createExport, createImport, parserTs, print, safePrint, validateNodes } from './parserTs.ts'
|
|
2
|
+
export { parserTsx } from './parserTsx.ts'
|