@kubb/parser-ts 3.18.0 → 3.18.1
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 +32 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -2
- package/dist/index.d.ts +6 -2
- package/dist/index.js +32 -5
- package/dist/index.js.map +1 -1
- package/package.json +3 -2
- package/src/format.ts +25 -0
- package/src/index.ts +1 -0
- package/src/print.ts +13 -4
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
const require_factory = require('./factory-BSd2OjFQ.cjs');
|
|
2
2
|
const typescript = require_factory.__toESM(require("typescript"));
|
|
3
|
+
const prettier = require_factory.__toESM(require("prettier"));
|
|
4
|
+
const prettier_plugins_typescript = require_factory.__toESM(require("prettier/plugins/typescript"));
|
|
3
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: [prettier_plugins_typescript.default]
|
|
16
|
+
};
|
|
17
|
+
function format(source) {
|
|
18
|
+
if (!source) return Promise.resolve("");
|
|
19
|
+
try {
|
|
20
|
+
return (0, prettier.format)(source, formatOptions);
|
|
21
|
+
} catch (_e) {
|
|
22
|
+
return Promise.resolve(source);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
4
27
|
//#region src/print.ts
|
|
5
28
|
const { factory } = typescript.default;
|
|
6
29
|
/**
|
|
@@ -14,17 +37,21 @@ const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
|
|
|
14
37
|
/**
|
|
15
38
|
* Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
|
|
16
39
|
* Ensures consistent output across environments.
|
|
40
|
+
* Also works as a formatter when `source` is provided without `elements`.
|
|
17
41
|
*/
|
|
18
|
-
function print(elements, { source = "", baseName = "print.tsx", scriptKind = typescript.default.ScriptKind.TSX } = {}) {
|
|
19
|
-
const sourceFile = typescript.default.createSourceFile(baseName, escapeNewLines(source), typescript.default.ScriptTarget.ES2022,
|
|
42
|
+
function print(elements = [], { source = "", baseName = "print.tsx", scriptKind = typescript.default.ScriptKind.TSX } = {}) {
|
|
43
|
+
const sourceFile = typescript.default.createSourceFile(baseName, escapeNewLines(source), typescript.default.ScriptTarget.ES2022, true, scriptKind);
|
|
20
44
|
const printer = typescript.default.createPrinter({
|
|
21
45
|
omitTrailingSemicolon: true,
|
|
22
46
|
newLine: typescript.default.NewLineKind.LineFeed,
|
|
23
47
|
removeComments: false,
|
|
24
48
|
noEmitHelpers: true
|
|
25
49
|
});
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
let output;
|
|
51
|
+
if (elements.length > 0) {
|
|
52
|
+
const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0));
|
|
53
|
+
output = printer.printList(typescript.default.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile);
|
|
54
|
+
} else output = printer.printFile(sourceFile);
|
|
28
55
|
return restoreNewLines(output).replace(/\r\n/g, "\n");
|
|
29
56
|
}
|
|
30
57
|
|
|
@@ -35,5 +62,6 @@ Object.defineProperty(exports, 'factory', {
|
|
|
35
62
|
return require_factory.factory_exports;
|
|
36
63
|
}
|
|
37
64
|
});
|
|
65
|
+
exports.format = format;
|
|
38
66
|
exports.print = print;
|
|
39
67
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","names":["ts"],"sources":["../src/print.ts"],"sourcesContent":["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 */\nexport function print(elements: Array<ts.Node
|
|
1
|
+
{"version":3,"file":"index.cjs","names":["formatOptions: Options","pluginTypescript","ts","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,CAACC,oCAAiB;CAC5B;AACD,SAAgB,OAAO,QAAkC;AACvD,KAAI,CAAC,OACH,QAAO,QAAQ,QAAQ;AAGzB,KAAI;AACF,8BAAsB,QAAQ;CAC/B,SAAQ,IAAI;AACX,SAAO,QAAQ,QAAQ;CACxB;AACF;;;;ACtBD,MAAM,EAAE,SAAS,GAAGC;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS;;;;AAK/D,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB;;;;;;AAO/E,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAaA,mBAAG,WAAW,KAAmB,GAAG,EAAE,EAAU;CACvJ,MAAM,aAAaA,mBAAG,iBAAiB,UAAU,eAAe,SAASA,mBAAG,aAAa,QAAQ,MAAM;CAEvG,MAAM,UAAUA,mBAAG,cAAc;EAC/B,uBAAuB;EACvB,SAASA,mBAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB;CAED,IAAIC;AAEJ,KAAI,SAAS,SAAS,GAAG;EAEvB,MAAM,QAAQ,SAAS,OAAO,SAAS,MAAM,GAAG,OAAO,EAAE,OAAO,MAAM,EAAE,OAAO;AAC/E,WAAS,QAAQ,UAAUD,mBAAG,WAAW,WAAW,QAAQ,gBAAgB,QAAQ;CACrF,MAEC,UAAS,QAAQ,UAAU;AAG7B,QAAO,gBAAgB,QAAQ,QAAQ,SAAS;AACjD"}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { factory_d_exports } from "./factory-_F-JTqnG.cjs";
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
|
|
4
|
+
//#region src/format.d.ts
|
|
5
|
+
declare function format(source?: string): Promise<string>;
|
|
6
|
+
//#endregion
|
|
4
7
|
//#region src/print.d.ts
|
|
5
8
|
type PrintOptions = {
|
|
6
9
|
source?: string;
|
|
@@ -10,12 +13,13 @@ type PrintOptions = {
|
|
|
10
13
|
/**
|
|
11
14
|
* Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
|
|
12
15
|
* Ensures consistent output across environments.
|
|
16
|
+
* Also works as a formatter when `source` is provided without `elements`.
|
|
13
17
|
*/
|
|
14
|
-
declare function print(elements
|
|
18
|
+
declare function print(elements?: Array<ts.Node>, {
|
|
15
19
|
source,
|
|
16
20
|
baseName,
|
|
17
21
|
scriptKind
|
|
18
22
|
}?: PrintOptions): string;
|
|
19
23
|
//#endregion
|
|
20
|
-
export { factory_d_exports as factory, print };
|
|
24
|
+
export { factory_d_exports as factory, format, print };
|
|
21
25
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { factory_d_exports } from "./factory-CUjjpSgy.js";
|
|
2
2
|
import ts from "typescript";
|
|
3
3
|
|
|
4
|
+
//#region src/format.d.ts
|
|
5
|
+
declare function format(source?: string): Promise<string>;
|
|
6
|
+
//#endregion
|
|
4
7
|
//#region src/print.d.ts
|
|
5
8
|
type PrintOptions = {
|
|
6
9
|
source?: string;
|
|
@@ -10,12 +13,13 @@ type PrintOptions = {
|
|
|
10
13
|
/**
|
|
11
14
|
* Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
|
|
12
15
|
* Ensures consistent output across environments.
|
|
16
|
+
* Also works as a formatter when `source` is provided without `elements`.
|
|
13
17
|
*/
|
|
14
|
-
declare function print(elements
|
|
18
|
+
declare function print(elements?: Array<ts.Node>, {
|
|
15
19
|
source,
|
|
16
20
|
baseName,
|
|
17
21
|
scriptKind
|
|
18
22
|
}?: PrintOptions): string;
|
|
19
23
|
//#endregion
|
|
20
|
-
export { factory_d_exports as factory, print };
|
|
24
|
+
export { factory_d_exports as factory, format, print };
|
|
21
25
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
import { factory_exports } from "./factory-CDjjCkNo.js";
|
|
2
2
|
import ts from "typescript";
|
|
3
|
+
import { format as format$1 } from "prettier";
|
|
4
|
+
import pluginTypescript from "prettier/plugins/typescript";
|
|
3
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
|
|
4
27
|
//#region src/print.ts
|
|
5
28
|
const { factory } = ts;
|
|
6
29
|
/**
|
|
@@ -14,20 +37,24 @@ const restoreNewLines = (code) => code.replace(/\/\* :newline: \*\//g, "\n");
|
|
|
14
37
|
/**
|
|
15
38
|
* Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
|
|
16
39
|
* Ensures consistent output across environments.
|
|
40
|
+
* Also works as a formatter when `source` is provided without `elements`.
|
|
17
41
|
*/
|
|
18
|
-
function print(elements, { source = "", baseName = "print.tsx", scriptKind = ts.ScriptKind.TSX } = {}) {
|
|
19
|
-
const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022,
|
|
42
|
+
function print(elements = [], { source = "", baseName = "print.tsx", scriptKind = ts.ScriptKind.TSX } = {}) {
|
|
43
|
+
const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind);
|
|
20
44
|
const printer = ts.createPrinter({
|
|
21
45
|
omitTrailingSemicolon: true,
|
|
22
46
|
newLine: ts.NewLineKind.LineFeed,
|
|
23
47
|
removeComments: false,
|
|
24
48
|
noEmitHelpers: true
|
|
25
49
|
});
|
|
26
|
-
|
|
27
|
-
|
|
50
|
+
let output;
|
|
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);
|
|
28
55
|
return restoreNewLines(output).replace(/\r\n/g, "\n");
|
|
29
56
|
}
|
|
30
57
|
|
|
31
58
|
//#endregion
|
|
32
|
-
export { factory_exports as factory, print };
|
|
59
|
+
export { factory_exports as factory, format, print };
|
|
33
60
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/print.ts"],"sourcesContent":["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 */\nexport function print(elements: Array<ts.Node
|
|
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;AAGzB,KAAI;AACF,SAAOC,SAAe,QAAQ;CAC/B,SAAQ,IAAI;AACX,SAAO,QAAQ,QAAQ;CACxB;AACF;;;;ACtBD,MAAM,EAAE,SAAS,GAAG;;;;AAWpB,MAAM,kBAAkB,SAAiB,KAAK,QAAQ,SAAS;;;;AAK/D,MAAM,mBAAmB,SAAiB,KAAK,QAAQ,wBAAwB;;;;;;AAO/E,SAAgB,MAAM,WAA2B,EAAE,EAAE,EAAE,SAAS,IAAI,WAAW,aAAa,aAAa,GAAG,WAAW,KAAmB,GAAG,EAAE,EAAU;CACvJ,MAAM,aAAa,GAAG,iBAAiB,UAAU,eAAe,SAAS,GAAG,aAAa,QAAQ,MAAM;CAEvG,MAAM,UAAU,GAAG,cAAc;EAC/B,uBAAuB;EACvB,SAAS,GAAG,YAAY;EACxB,gBAAgB;EAChB,eAAe;EAChB;CAED,IAAIC;AAEJ,KAAI,SAAS,SAAS,GAAG;EAEvB,MAAM,QAAQ,SAAS,OAAO,SAAS,MAAM,GAAG,OAAO,EAAE,OAAO,MAAM,EAAE,OAAO;AAC/E,WAAS,QAAQ,UAAU,GAAG,WAAW,WAAW,QAAQ,gBAAgB,QAAQ;CACrF,MAEC,UAAS,QAAQ,UAAU;AAG7B,QAAO,gBAAgB,QAAQ,QAAQ,SAAS;AACjD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kubb/parser-ts",
|
|
3
|
-
"version": "3.18.
|
|
3
|
+
"version": "3.18.1",
|
|
4
4
|
"description": "TypeScript parsing and manipulation utilities for Kubb, enabling code generation with proper TypeScript syntax and formatting.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -49,12 +49,13 @@
|
|
|
49
49
|
"!/**/__tests__/**"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"prettier": "^3.6.2",
|
|
52
53
|
"remeda": "^2.30.0",
|
|
53
54
|
"typescript": "5.9.2"
|
|
54
55
|
},
|
|
55
56
|
"devDependencies": {
|
|
56
57
|
"tsdown": "^0.14.1",
|
|
57
|
-
"@kubb/config-ts": "3.18.
|
|
58
|
+
"@kubb/config-ts": "3.18.1"
|
|
58
59
|
},
|
|
59
60
|
"engines": {
|
|
60
61
|
"node": ">=20"
|
package/src/format.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { Options } from 'prettier'
|
|
2
|
+
import { format as prettierFormat } from 'prettier'
|
|
3
|
+
import pluginTypescript from 'prettier/plugins/typescript'
|
|
4
|
+
|
|
5
|
+
const formatOptions: Options = {
|
|
6
|
+
tabWidth: 2,
|
|
7
|
+
printWidth: 160,
|
|
8
|
+
parser: 'typescript',
|
|
9
|
+
singleQuote: true,
|
|
10
|
+
semi: false,
|
|
11
|
+
bracketSameLine: false,
|
|
12
|
+
endOfLine: 'auto',
|
|
13
|
+
plugins: [pluginTypescript],
|
|
14
|
+
}
|
|
15
|
+
export function format(source?: string): Promise<string> {
|
|
16
|
+
if (!source) {
|
|
17
|
+
return Promise.resolve('')
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
return prettierFormat(source, formatOptions)
|
|
22
|
+
} catch (_e) {
|
|
23
|
+
return Promise.resolve(source)
|
|
24
|
+
}
|
|
25
|
+
}
|
package/src/index.ts
CHANGED
package/src/print.ts
CHANGED
|
@@ -21,9 +21,10 @@ const restoreNewLines = (code: string) => code.replace(/\/\* :newline: \*\//g, '
|
|
|
21
21
|
/**
|
|
22
22
|
* Convert AST TypeScript/TSX nodes to a string based on the TypeScript printer.
|
|
23
23
|
* Ensures consistent output across environments.
|
|
24
|
+
* Also works as a formatter when `source` is provided without `elements`.
|
|
24
25
|
*/
|
|
25
|
-
export function print(elements: Array<ts.Node
|
|
26
|
-
const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022,
|
|
26
|
+
export function print(elements: Array<ts.Node> = [], { source = '', baseName = 'print.tsx', scriptKind = ts.ScriptKind.TSX }: PrintOptions = {}): string {
|
|
27
|
+
const sourceFile = ts.createSourceFile(baseName, escapeNewLines(source), ts.ScriptTarget.ES2022, true, scriptKind)
|
|
27
28
|
|
|
28
29
|
const printer = ts.createPrinter({
|
|
29
30
|
omitTrailingSemicolon: true,
|
|
@@ -32,8 +33,16 @@ export function print(elements: Array<ts.Node>, { source = '', baseName = 'print
|
|
|
32
33
|
noEmitHelpers: true,
|
|
33
34
|
})
|
|
34
35
|
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
let output: string
|
|
37
|
+
|
|
38
|
+
if (elements.length > 0) {
|
|
39
|
+
// Print only provided nodes
|
|
40
|
+
const nodes = elements.filter(Boolean).sort((a, b) => (a.pos ?? 0) - (b.pos ?? 0))
|
|
41
|
+
output = printer.printList(ts.ListFormat.MultiLine, factory.createNodeArray(nodes), sourceFile)
|
|
42
|
+
} else {
|
|
43
|
+
// Format the whole file
|
|
44
|
+
output = printer.printFile(sourceFile)
|
|
45
|
+
}
|
|
37
46
|
|
|
38
47
|
return restoreNewLines(output).replace(/\r\n/g, '\n')
|
|
39
48
|
}
|