@tanstack/router-generator 1.121.0-alpha.22 → 1.121.0-alpha.27
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/cjs/config.cjs +23 -5
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/config.d.cts +7 -3
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs +5 -3
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs +1 -1
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/generator.cjs +828 -665
- package/dist/cjs/generator.cjs.map +1 -1
- package/dist/cjs/generator.d.cts +78 -1
- package/dist/cjs/index.cjs +5 -2
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +7 -3
- package/dist/cjs/logger.cjs +37 -0
- package/dist/cjs/logger.cjs.map +1 -0
- package/dist/cjs/logger.d.cts +10 -0
- package/dist/cjs/plugin/default-generator-plugin.cjs +88 -0
- package/dist/cjs/plugin/default-generator-plugin.cjs.map +1 -0
- package/dist/cjs/plugin/default-generator-plugin.d.cts +2 -0
- package/dist/cjs/plugin/types.d.cts +46 -0
- package/dist/cjs/template.cjs +10 -10
- package/dist/cjs/template.cjs.map +1 -1
- package/dist/cjs/template.d.cts +2 -2
- package/dist/cjs/transform/default-transform-plugin.cjs +95 -0
- package/dist/cjs/transform/default-transform-plugin.cjs.map +1 -0
- package/dist/cjs/transform/default-transform-plugin.d.cts +2 -0
- package/dist/cjs/transform/transform.cjs +351 -0
- package/dist/cjs/transform/transform.cjs.map +1 -0
- package/dist/cjs/transform/transform.d.cts +4 -0
- package/dist/cjs/transform/types.d.cts +43 -0
- package/dist/cjs/transform/utils.cjs +36 -0
- package/dist/cjs/transform/utils.cjs.map +1 -0
- package/dist/cjs/transform/utils.d.cts +2 -0
- package/dist/cjs/types.d.cts +22 -0
- package/dist/cjs/utils.cjs +237 -40
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +76 -9
- package/dist/esm/config.d.ts +7 -3
- package/dist/esm/config.js +21 -3
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/filesystem/physical/getRouteNodes.js +3 -1
- package/dist/esm/filesystem/physical/getRouteNodes.js.map +1 -1
- package/dist/esm/filesystem/virtual/getRouteNodes.js +1 -1
- package/dist/esm/filesystem/virtual/getRouteNodes.js.map +1 -1
- package/dist/esm/generator.d.ts +78 -1
- package/dist/esm/generator.js +817 -653
- package/dist/esm/generator.js.map +1 -1
- package/dist/esm/index.d.ts +7 -3
- package/dist/esm/index.js +7 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/logger.d.ts +10 -0
- package/dist/esm/logger.js +37 -0
- package/dist/esm/logger.js.map +1 -0
- package/dist/esm/plugin/default-generator-plugin.d.ts +2 -0
- package/dist/esm/plugin/default-generator-plugin.js +88 -0
- package/dist/esm/plugin/default-generator-plugin.js.map +1 -0
- package/dist/esm/plugin/types.d.ts +46 -0
- package/dist/esm/template.d.ts +2 -2
- package/dist/esm/template.js +10 -10
- package/dist/esm/template.js.map +1 -1
- package/dist/esm/transform/default-transform-plugin.d.ts +2 -0
- package/dist/esm/transform/default-transform-plugin.js +95 -0
- package/dist/esm/transform/default-transform-plugin.js.map +1 -0
- package/dist/esm/transform/transform.d.ts +4 -0
- package/dist/esm/transform/transform.js +351 -0
- package/dist/esm/transform/transform.js.map +1 -0
- package/dist/esm/transform/types.d.ts +43 -0
- package/dist/esm/transform/utils.d.ts +2 -0
- package/dist/esm/transform/utils.js +36 -0
- package/dist/esm/transform/utils.js.map +1 -0
- package/dist/esm/types.d.ts +22 -0
- package/dist/esm/utils.d.ts +76 -9
- package/dist/esm/utils.js +237 -40
- package/dist/esm/utils.js.map +1 -1
- package/package.json +9 -10
- package/src/config.ts +23 -2
- package/src/filesystem/physical/getRouteNodes.ts +2 -1
- package/src/filesystem/virtual/getRouteNodes.ts +1 -1
- package/src/generator.ts +1108 -934
- package/src/index.ts +25 -3
- package/src/logger.ts +43 -0
- package/src/plugin/default-generator-plugin.ts +96 -0
- package/src/plugin/types.ts +51 -0
- package/src/template.ts +33 -12
- package/src/transform/default-transform-plugin.ts +103 -0
- package/src/transform/transform.ts +430 -0
- package/src/transform/types.ts +50 -0
- package/src/transform/utils.ts +42 -0
- package/src/types.ts +25 -0
- package/src/utils.ts +351 -36
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { TransformPlugin } from '../transform/types.cjs';
|
|
2
|
+
import { HandleNodeAccumulator, ImportDeclaration, RouteNode } from '../types.cjs';
|
|
3
|
+
import { Generator } from '../generator.cjs';
|
|
4
|
+
export type GeneratorPlugin = GeneratorPluginBase | GeneratorPluginWithTransform;
|
|
5
|
+
export interface GeneratorPluginBase {
|
|
6
|
+
name: string;
|
|
7
|
+
onRouteTreesChanged?: (opts: {
|
|
8
|
+
routeTrees: Array<{
|
|
9
|
+
sortedRouteNodes: Array<RouteNode>;
|
|
10
|
+
acc: HandleNodeAccumulator;
|
|
11
|
+
exportName: string;
|
|
12
|
+
}>;
|
|
13
|
+
rootRouteNode: RouteNode;
|
|
14
|
+
generator: Generator;
|
|
15
|
+
}) => void;
|
|
16
|
+
}
|
|
17
|
+
export interface GeneratorPluginWithTransform extends GeneratorPluginBase {
|
|
18
|
+
transformPlugin: TransformPlugin;
|
|
19
|
+
moduleAugmentation: (opts: {
|
|
20
|
+
generator: Generator;
|
|
21
|
+
}) => {
|
|
22
|
+
module: string;
|
|
23
|
+
interfaceName: string;
|
|
24
|
+
};
|
|
25
|
+
imports: (opts: {
|
|
26
|
+
rootRouteNode: RouteNode;
|
|
27
|
+
sortedRouteNodes: Array<RouteNode>;
|
|
28
|
+
acc: HandleNodeAccumulator;
|
|
29
|
+
generator: Generator;
|
|
30
|
+
}) => Array<ImportDeclaration>;
|
|
31
|
+
routeModuleAugmentation: (opts: {
|
|
32
|
+
routeNode: RouteNode;
|
|
33
|
+
}) => string | undefined;
|
|
34
|
+
createRootRouteCode: () => string;
|
|
35
|
+
createVirtualRouteCode: (opts: {
|
|
36
|
+
node: RouteNode;
|
|
37
|
+
}) => string;
|
|
38
|
+
config: (opts: {
|
|
39
|
+
generator: Generator;
|
|
40
|
+
rootRouteNode: RouteNode;
|
|
41
|
+
sortedRouteNodes: Array<RouteNode>;
|
|
42
|
+
}) => {
|
|
43
|
+
virtualRootRoute?: boolean;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export {};
|
package/dist/cjs/template.cjs
CHANGED
|
@@ -8,9 +8,9 @@ function fillTemplate(config, template, values) {
|
|
|
8
8
|
);
|
|
9
9
|
return utils.format(replaced, config);
|
|
10
10
|
}
|
|
11
|
-
function getTargetTemplate(
|
|
11
|
+
function getTargetTemplate(config) {
|
|
12
|
+
const target = config.target;
|
|
12
13
|
switch (target) {
|
|
13
|
-
// TODO: Remove this disabled eslint rule when more target types are added.
|
|
14
14
|
case "react":
|
|
15
15
|
return {
|
|
16
16
|
fullPkg: "@tanstack/react-router",
|
|
@@ -37,8 +37,8 @@ function getTargetTemplate(target) {
|
|
|
37
37
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
38
38
|
].join(""),
|
|
39
39
|
imports: {
|
|
40
|
-
tsrImports: () => "",
|
|
41
|
-
tsrExportStart: () => `export const Route = createFileRoute(`,
|
|
40
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createFileRoute } from '@tanstack/react-router';",
|
|
41
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createFileRoute(" : `export const Route = createFileRoute('${routePath}')(`,
|
|
42
42
|
tsrExportEnd: () => ");"
|
|
43
43
|
}
|
|
44
44
|
},
|
|
@@ -50,8 +50,8 @@ function getTargetTemplate(target) {
|
|
|
50
50
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
51
51
|
].join(""),
|
|
52
52
|
imports: {
|
|
53
|
-
tsrImports: () => "import { createLazyFileRoute } from '@tanstack/react-router';",
|
|
54
|
-
tsrExportStart: (routePath) => `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
53
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createLazyFileRoute } from '@tanstack/react-router';",
|
|
54
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createLazyFileRoute(" : `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
55
55
|
tsrExportEnd: () => ");"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -82,8 +82,8 @@ function getTargetTemplate(target) {
|
|
|
82
82
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
83
83
|
].join(""),
|
|
84
84
|
imports: {
|
|
85
|
-
tsrImports: () => "",
|
|
86
|
-
tsrExportStart: () => `export const Route = createFileRoute(`,
|
|
85
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createFileRoute } from '@tanstack/solid-router';",
|
|
86
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createFileRoute(" : `export const Route = createFileRoute('${routePath}')(`,
|
|
87
87
|
tsrExportEnd: () => ");"
|
|
88
88
|
}
|
|
89
89
|
},
|
|
@@ -95,8 +95,8 @@ function getTargetTemplate(target) {
|
|
|
95
95
|
'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n'
|
|
96
96
|
].join(""),
|
|
97
97
|
imports: {
|
|
98
|
-
tsrImports: () => "import { createLazyFileRoute } from '@tanstack/solid-router';",
|
|
99
|
-
tsrExportStart: (routePath) => `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
98
|
+
tsrImports: () => config.verboseFileRoutes === false ? "" : "import { createLazyFileRoute } from '@tanstack/solid-router';",
|
|
99
|
+
tsrExportStart: (routePath) => config.verboseFileRoutes === false ? "export const Route = createLazyFileRoute(" : `export const Route = createLazyFileRoute('${routePath}')(`,
|
|
100
100
|
tsrExportEnd: () => ");"
|
|
101
101
|
}
|
|
102
102
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"template.cjs","sources":["../../src/template.ts"],"sourcesContent":["import { format } from './utils'\nimport type { Config } from './config'\n\ntype TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'\n\nexport function fillTemplate(\n config: Config,\n template: string,\n values: Record<TemplateTag, string>,\n) {\n const replaced = template.replace(\n /%%(\\w+)%%/g,\n (_, key) => values[key as TemplateTag] || '',\n )\n return format(replaced, config)\n}\n\
|
|
1
|
+
{"version":3,"file":"template.cjs","sources":["../../src/template.ts"],"sourcesContent":["import { format } from './utils'\nimport type { Config } from './config'\n\ntype TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd'\n\nexport function fillTemplate(\n config: Config,\n template: string,\n values: Record<TemplateTag, string>,\n) {\n const replaced = template.replace(\n /%%(\\w+)%%/g,\n (_, key) => values[key as TemplateTag] || '',\n )\n return format(replaced, config)\n}\n\nexport type TargetTemplate = {\n fullPkg: string\n subPkg: string\n rootRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: () => string\n tsrExportEnd: () => string\n }\n }\n route: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n lazyRoute: {\n template: () => string\n imports: {\n tsrImports: () => string\n tsrExportStart: (routePath: string) => string\n tsrExportEnd: () => string\n }\n }\n}\n\nexport function getTargetTemplate(config: Config): TargetTemplate {\n const target = config.target\n switch (target) {\n case 'react':\n return {\n fullPkg: '@tanstack/react-router',\n subPkg: 'react-router',\n rootRoute: {\n template: () =>\n [\n 'import * as React from \"react\"\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RootComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RootComponent() { return (<React.Fragment><div>Hello \"%%tsrPath%%\"!</div><Outlet /></React.Fragment>) };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { Outlet, createRootRoute } from '@tanstack/react-router';\",\n tsrExportStart: () => 'export const Route = createRootRoute(',\n tsrExportEnd: () => ');',\n },\n },\n route: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createFileRoute('\n : `export const Route = createFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n lazyRoute: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createLazyFileRoute } from '@tanstack/react-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createLazyFileRoute('\n : `export const Route = createLazyFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n }\n case 'solid':\n return {\n fullPkg: '@tanstack/solid-router',\n subPkg: 'solid-router',\n rootRoute: {\n template: () =>\n [\n 'import * as Solid from \"solid-js\"\\n',\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RootComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RootComponent() { return (<><div>Hello \"%%tsrPath%%\"!</div><Outlet /></>) };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n \"import { Outlet, createRootRoute } from '@tanstack/solid-router';\",\n tsrExportStart: () => 'export const Route = createRootRoute(',\n tsrExportEnd: () => ');',\n },\n },\n route: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createFileRoute } from '@tanstack/solid-router';\",\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createFileRoute('\n : `export const Route = createFileRoute('${routePath}')(`,\n tsrExportEnd: () => ');',\n },\n },\n lazyRoute: {\n template: () =>\n [\n '%%tsrImports%%',\n '\\n\\n',\n '%%tsrExportStart%%{\\n component: RouteComponent\\n }%%tsrExportEnd%%\\n\\n',\n 'function RouteComponent() { return <div>Hello \"%%tsrPath%%\"!</div> };\\n',\n ].join(''),\n imports: {\n tsrImports: () =>\n config.verboseFileRoutes === false\n ? ''\n : \"import { createLazyFileRoute } from '@tanstack/solid-router';\",\n\n tsrExportStart: (routePath) =>\n config.verboseFileRoutes === false\n ? 'export const Route = createLazyFileRoute('\n : `export const Route = createLazyFileRoute('${routePath}')(`,\n\n tsrExportEnd: () => ');',\n },\n },\n }\n default:\n throw new Error(`router-generator: Unknown target type: ${target}`)\n }\n}\n"],"names":["format"],"mappings":";;;AAKgB,SAAA,aACd,QACA,UACA,QACA;AACA,QAAM,WAAW,SAAS;AAAA,IACxB;AAAA,IACA,CAAC,GAAG,QAAQ,OAAO,GAAkB,KAAK;AAAA,EAC5C;AACO,SAAAA,MAAA,OAAO,UAAU,MAAM;AAChC;AA+BO,SAAS,kBAAkB,QAAgC;AAChE,QAAM,SAAS,OAAO;AACtB,UAAQ,QAAQ;AAAA,IACd,KAAK;AACI,aAAA;AAAA,QACL,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV;AAAA,YACF,gBAAgB,MAAM;AAAA,YACtB,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,OAAO;AAAA,UACL,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV,OAAO,sBAAsB,QACzB,KACA;AAAA,YACN,gBAAgB,CAAC,cACf,OAAO,sBAAsB,QACzB,0CACA,yCAAyC,SAAS;AAAA,YACxD,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV,OAAO,sBAAsB,QACzB,KACA;AAAA,YACN,gBAAgB,CAAC,cACf,OAAO,sBAAsB,QACzB,8CACA,6CAA6C,SAAS;AAAA,YAC5D,cAAc,MAAM;AAAA,UAAA;AAAA,QACtB;AAAA,MAEJ;AAAA,IACF,KAAK;AACI,aAAA;AAAA,QACL,SAAS;AAAA,QACT,QAAQ;AAAA,QACR,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV;AAAA,YACF,gBAAgB,MAAM;AAAA,YACtB,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,OAAO;AAAA,UACL,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV,OAAO,sBAAsB,QACzB,KACA;AAAA,YACN,gBAAgB,CAAC,cACf,OAAO,sBAAsB,QACzB,0CACA,yCAAyC,SAAS;AAAA,YACxD,cAAc,MAAM;AAAA,UAAA;AAAA,QAExB;AAAA,QACA,WAAW;AAAA,UACT,UAAU,MACR;AAAA,YACE;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA,EACA,KAAK,EAAE;AAAA,UACX,SAAS;AAAA,YACP,YAAY,MACV,OAAO,sBAAsB,QACzB,KACA;AAAA,YAEN,gBAAgB,CAAC,cACf,OAAO,sBAAsB,QACzB,8CACA,6CAA6C,SAAS;AAAA,YAE5D,cAAc,MAAM;AAAA,UAAA;AAAA,QACtB;AAAA,MAEJ;AAAA,IACF;AACE,YAAM,IAAI,MAAM,0CAA0C,MAAM,EAAE;AAAA,EAAA;AAExE;;;"}
|
package/dist/cjs/template.d.cts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Config } from './config.cjs';
|
|
2
2
|
type TemplateTag = 'tsrImports' | 'tsrPath' | 'tsrExportStart' | 'tsrExportEnd';
|
|
3
3
|
export declare function fillTemplate(config: Config, template: string, values: Record<TemplateTag, string>): Promise<string>;
|
|
4
|
-
type TargetTemplate = {
|
|
4
|
+
export type TargetTemplate = {
|
|
5
5
|
fullPkg: string;
|
|
6
6
|
subPkg: string;
|
|
7
7
|
rootRoute: {
|
|
@@ -29,5 +29,5 @@ type TargetTemplate = {
|
|
|
29
29
|
};
|
|
30
30
|
};
|
|
31
31
|
};
|
|
32
|
-
export declare function getTargetTemplate(
|
|
32
|
+
export declare function getTargetTemplate(config: Config): TargetTemplate;
|
|
33
33
|
export {};
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const recast = require("recast");
|
|
4
|
+
const utils = require("./utils.cjs");
|
|
5
|
+
const b = recast.types.builders;
|
|
6
|
+
const defaultTransformPlugin = {
|
|
7
|
+
name: "default-transform",
|
|
8
|
+
exportName: "Route",
|
|
9
|
+
imports: (ctx) => {
|
|
10
|
+
const imports = {};
|
|
11
|
+
const targetModule = `@tanstack/${ctx.target}-router`;
|
|
12
|
+
if (ctx.verboseFileRoutes === false) {
|
|
13
|
+
imports.banned = [
|
|
14
|
+
{
|
|
15
|
+
source: targetModule,
|
|
16
|
+
specifiers: [
|
|
17
|
+
{ imported: "createLazyFileRoute" },
|
|
18
|
+
{ imported: "createFileRoute" }
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
];
|
|
22
|
+
} else {
|
|
23
|
+
if (ctx.lazy) {
|
|
24
|
+
imports.required = [
|
|
25
|
+
{
|
|
26
|
+
source: targetModule,
|
|
27
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
28
|
+
}
|
|
29
|
+
];
|
|
30
|
+
imports.banned = [
|
|
31
|
+
{
|
|
32
|
+
source: targetModule,
|
|
33
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
34
|
+
}
|
|
35
|
+
];
|
|
36
|
+
} else {
|
|
37
|
+
imports.required = [
|
|
38
|
+
{
|
|
39
|
+
source: targetModule,
|
|
40
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
41
|
+
}
|
|
42
|
+
];
|
|
43
|
+
imports.banned = [
|
|
44
|
+
{
|
|
45
|
+
source: targetModule,
|
|
46
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
47
|
+
}
|
|
48
|
+
];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return imports;
|
|
52
|
+
},
|
|
53
|
+
onExportFound: ({ decl, ctx }) => {
|
|
54
|
+
var _a;
|
|
55
|
+
let appliedChanges = false;
|
|
56
|
+
if (((_a = decl.init) == null ? void 0 : _a.type) === "CallExpression") {
|
|
57
|
+
const callExpression = decl.init;
|
|
58
|
+
let identifier;
|
|
59
|
+
if (callExpression.callee.type === "Identifier") {
|
|
60
|
+
identifier = callExpression.callee;
|
|
61
|
+
if (ctx.verboseFileRoutes) {
|
|
62
|
+
callExpression.callee = b.callExpression(identifier, [
|
|
63
|
+
b.stringLiteral(ctx.routeId)
|
|
64
|
+
]);
|
|
65
|
+
appliedChanges = true;
|
|
66
|
+
}
|
|
67
|
+
} else if (callExpression.callee.type === "CallExpression" && callExpression.callee.callee.type === "Identifier") {
|
|
68
|
+
identifier = callExpression.callee.callee;
|
|
69
|
+
if (!ctx.verboseFileRoutes) {
|
|
70
|
+
callExpression.callee = identifier;
|
|
71
|
+
appliedChanges = true;
|
|
72
|
+
} else {
|
|
73
|
+
appliedChanges = utils.ensureStringArgument(
|
|
74
|
+
callExpression.callee,
|
|
75
|
+
ctx.routeId,
|
|
76
|
+
ctx.preferredQuote
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (identifier === void 0) {
|
|
81
|
+
throw new Error(`expected identifier to be present`);
|
|
82
|
+
}
|
|
83
|
+
if (identifier.name === "createFileRoute" && ctx.lazy) {
|
|
84
|
+
identifier.name = "createLazyFileRoute";
|
|
85
|
+
appliedChanges = true;
|
|
86
|
+
} else if (identifier.name === "createLazyFileRoute" && !ctx.lazy) {
|
|
87
|
+
identifier.name = "createFileRoute";
|
|
88
|
+
appliedChanges = true;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return appliedChanges;
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
exports.defaultTransformPlugin = defaultTransformPlugin;
|
|
95
|
+
//# sourceMappingURL=default-transform-plugin.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"default-transform-plugin.cjs","sources":["../../../src/transform/default-transform-plugin.ts"],"sourcesContent":["import { types } from 'recast'\nimport { ensureStringArgument } from './utils'\nimport type { TransformImportsConfig, TransformPlugin } from './types'\n\nconst b = types.builders\n\nexport const defaultTransformPlugin: TransformPlugin = {\n name: 'default-transform',\n exportName: 'Route',\n imports: (ctx) => {\n const imports: TransformImportsConfig = {}\n const targetModule = `@tanstack/${ctx.target}-router`\n if (ctx.verboseFileRoutes === false) {\n imports.banned = [\n {\n source: targetModule,\n specifiers: [\n { imported: 'createLazyFileRoute' },\n { imported: 'createFileRoute' },\n ],\n },\n ]\n } else {\n if (ctx.lazy) {\n imports.required = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createLazyFileRoute' }],\n },\n ]\n imports.banned = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createFileRoute' }],\n },\n ]\n } else {\n imports.required = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createFileRoute' }],\n },\n ]\n imports.banned = [\n {\n source: targetModule,\n specifiers: [{ imported: 'createLazyFileRoute' }],\n },\n ]\n }\n }\n return imports\n },\n onExportFound: ({ decl, ctx }) => {\n let appliedChanges = false\n if (decl.init?.type === 'CallExpression') {\n const callExpression = decl.init\n let identifier: types.namedTypes.Identifier | undefined\n // `const Route = createFileRoute({ ... })`\n if (callExpression.callee.type === 'Identifier') {\n identifier = callExpression.callee\n if (ctx.verboseFileRoutes) {\n // we need to add the string literal via another CallExpression\n callExpression.callee = b.callExpression(identifier, [\n b.stringLiteral(ctx.routeId),\n ])\n appliedChanges = true\n }\n }\n // `const Route = createFileRoute('/path')({ ... })`\n else if (\n callExpression.callee.type === 'CallExpression' &&\n callExpression.callee.callee.type === 'Identifier'\n ) {\n identifier = callExpression.callee.callee\n if (!ctx.verboseFileRoutes) {\n // we need to remove the route id\n callExpression.callee = identifier\n appliedChanges = true\n } else {\n // check if the route id is correct\n appliedChanges = ensureStringArgument(\n callExpression.callee,\n ctx.routeId,\n ctx.preferredQuote,\n )\n }\n }\n if (identifier === undefined) {\n throw new Error(`expected identifier to be present`)\n }\n if (identifier.name === 'createFileRoute' && ctx.lazy) {\n identifier.name = 'createLazyFileRoute'\n appliedChanges = true\n } else if (identifier.name === 'createLazyFileRoute' && !ctx.lazy) {\n identifier.name = 'createFileRoute'\n appliedChanges = true\n }\n }\n\n return appliedChanges\n },\n}\n"],"names":["types","ensureStringArgument"],"mappings":";;;;AAIA,MAAM,IAAIA,OAAM,MAAA;AAET,MAAM,yBAA0C;AAAA,EACrD,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,SAAS,CAAC,QAAQ;AAChB,UAAM,UAAkC,CAAC;AACnC,UAAA,eAAe,aAAa,IAAI,MAAM;AACxC,QAAA,IAAI,sBAAsB,OAAO;AACnC,cAAQ,SAAS;AAAA,QACf;AAAA,UACE,QAAQ;AAAA,UACR,YAAY;AAAA,YACV,EAAE,UAAU,sBAAsB;AAAA,YAClC,EAAE,UAAU,kBAAkB;AAAA,UAAA;AAAA,QAChC;AAAA,MAEJ;AAAA,IAAA,OACK;AACL,UAAI,IAAI,MAAM;AACZ,gBAAQ,WAAW;AAAA,UACjB;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,sBAAuB,CAAA;AAAA,UAAA;AAAA,QAEpD;AACA,gBAAQ,SAAS;AAAA,UACf;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,kBAAmB,CAAA;AAAA,UAAA;AAAA,QAEhD;AAAA,MAAA,OACK;AACL,gBAAQ,WAAW;AAAA,UACjB;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,kBAAmB,CAAA;AAAA,UAAA;AAAA,QAEhD;AACA,gBAAQ,SAAS;AAAA,UACf;AAAA,YACE,QAAQ;AAAA,YACR,YAAY,CAAC,EAAE,UAAU,sBAAuB,CAAA;AAAA,UAAA;AAAA,QAEpD;AAAA,MAAA;AAAA,IACF;AAEK,WAAA;AAAA,EACT;AAAA,EACA,eAAe,CAAC,EAAE,MAAM,UAAU;;AAChC,QAAI,iBAAiB;AACjB,UAAA,UAAK,SAAL,mBAAW,UAAS,kBAAkB;AACxC,YAAM,iBAAiB,KAAK;AACxB,UAAA;AAEA,UAAA,eAAe,OAAO,SAAS,cAAc;AAC/C,qBAAa,eAAe;AAC5B,YAAI,IAAI,mBAAmB;AAEV,yBAAA,SAAS,EAAE,eAAe,YAAY;AAAA,YACnD,EAAE,cAAc,IAAI,OAAO;AAAA,UAAA,CAC5B;AACgB,2BAAA;AAAA,QAAA;AAAA,MACnB,WAIA,eAAe,OAAO,SAAS,oBAC/B,eAAe,OAAO,OAAO,SAAS,cACtC;AACA,qBAAa,eAAe,OAAO;AAC/B,YAAA,CAAC,IAAI,mBAAmB;AAE1B,yBAAe,SAAS;AACP,2BAAA;AAAA,QAAA,OACZ;AAEY,2BAAAC,MAAA;AAAA,YACf,eAAe;AAAA,YACf,IAAI;AAAA,YACJ,IAAI;AAAA,UACN;AAAA,QAAA;AAAA,MACF;AAEF,UAAI,eAAe,QAAW;AACtB,cAAA,IAAI,MAAM,mCAAmC;AAAA,MAAA;AAErD,UAAI,WAAW,SAAS,qBAAqB,IAAI,MAAM;AACrD,mBAAW,OAAO;AACD,yBAAA;AAAA,MAAA,WACR,WAAW,SAAS,yBAAyB,CAAC,IAAI,MAAM;AACjE,mBAAW,OAAO;AACD,yBAAA;AAAA,MAAA;AAAA,IACnB;AAGK,WAAA;AAAA,EAAA;AAEX;;"}
|
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const routerUtils = require("@tanstack/router-utils");
|
|
4
|
+
const recast = require("recast");
|
|
5
|
+
const sourceMap = require("source-map");
|
|
6
|
+
const utils = require("../utils.cjs");
|
|
7
|
+
const b = recast.types.builders;
|
|
8
|
+
async function transform({
|
|
9
|
+
ctx,
|
|
10
|
+
source,
|
|
11
|
+
plugins
|
|
12
|
+
}) {
|
|
13
|
+
var _a, _b, _c;
|
|
14
|
+
let appliedChanges = false;
|
|
15
|
+
let ast;
|
|
16
|
+
const foundExports = [];
|
|
17
|
+
try {
|
|
18
|
+
ast = recast.parse(source, {
|
|
19
|
+
sourceFileName: "output.ts",
|
|
20
|
+
parser: {
|
|
21
|
+
parse(code) {
|
|
22
|
+
return routerUtils.parseAst({
|
|
23
|
+
code,
|
|
24
|
+
// we need to instruct babel to produce tokens,
|
|
25
|
+
// otherwise recast will try to generate the tokens via its own parser and will fail
|
|
26
|
+
tokens: true
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
} catch (e) {
|
|
32
|
+
console.error("Error parsing code", ctx.routeId, source, e);
|
|
33
|
+
return {
|
|
34
|
+
result: "error",
|
|
35
|
+
error: e
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
const preferredQuote = detectPreferredQuoteStyle(ast);
|
|
39
|
+
const registeredExports = /* @__PURE__ */ new Map();
|
|
40
|
+
for (const plugin of plugins ?? []) {
|
|
41
|
+
const exportName = plugin.exportName;
|
|
42
|
+
if (registeredExports.has(exportName)) {
|
|
43
|
+
throw new Error(
|
|
44
|
+
`Export ${exportName} is already registered by plugin ${(_a = registeredExports.get(exportName)) == null ? void 0 : _a.name}`
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
registeredExports.set(exportName, plugin);
|
|
48
|
+
}
|
|
49
|
+
const program = ast.program;
|
|
50
|
+
for (const n of program.body) {
|
|
51
|
+
if (registeredExports.size > 0 && n.type === "ExportNamedDeclaration" && ((_b = n.declaration) == null ? void 0 : _b.type) === "VariableDeclaration") {
|
|
52
|
+
const decl = n.declaration.declarations[0];
|
|
53
|
+
if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
|
|
54
|
+
const plugin = registeredExports.get(decl.id.name);
|
|
55
|
+
if (plugin) {
|
|
56
|
+
const pluginAppliedChanges = plugin.onExportFound({
|
|
57
|
+
decl,
|
|
58
|
+
ctx: { ...ctx, preferredQuote }
|
|
59
|
+
});
|
|
60
|
+
if (pluginAppliedChanges) {
|
|
61
|
+
appliedChanges = true;
|
|
62
|
+
}
|
|
63
|
+
registeredExports.delete(decl.id.name);
|
|
64
|
+
foundExports.push(decl.id.name);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
const imports = {
|
|
70
|
+
required: [],
|
|
71
|
+
banned: []
|
|
72
|
+
};
|
|
73
|
+
for (const plugin of plugins ?? []) {
|
|
74
|
+
const exportName = plugin.exportName;
|
|
75
|
+
if (foundExports.includes(exportName)) {
|
|
76
|
+
const pluginImports = plugin.imports(ctx);
|
|
77
|
+
if (pluginImports.required) {
|
|
78
|
+
imports.required.push(...pluginImports.required);
|
|
79
|
+
}
|
|
80
|
+
if (pluginImports.banned) {
|
|
81
|
+
imports.banned.push(...pluginImports.banned);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
imports.required = utils.mergeImportDeclarations(imports.required);
|
|
86
|
+
imports.banned = utils.mergeImportDeclarations(imports.banned);
|
|
87
|
+
const importStatementCandidates = [];
|
|
88
|
+
const importDeclarationsToRemove = [];
|
|
89
|
+
for (const n of program.body) {
|
|
90
|
+
const findImport = (opts) => (i) => {
|
|
91
|
+
if (i.source === opts.source) {
|
|
92
|
+
const importKind = i.importKind || "value";
|
|
93
|
+
const expectedImportKind = opts.importKind || "value";
|
|
94
|
+
return expectedImportKind === importKind;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
};
|
|
98
|
+
if (n.type === "ImportDeclaration" && typeof n.source.value === "string") {
|
|
99
|
+
const filterImport = findImport({
|
|
100
|
+
source: n.source.value,
|
|
101
|
+
importKind: n.importKind
|
|
102
|
+
});
|
|
103
|
+
let requiredImports = imports.required.filter(filterImport)[0];
|
|
104
|
+
const bannedImports = imports.banned.filter(filterImport)[0];
|
|
105
|
+
if (!requiredImports && !bannedImports) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
const importSpecifiersToRemove = [];
|
|
109
|
+
if (n.specifiers) {
|
|
110
|
+
for (const spec of n.specifiers) {
|
|
111
|
+
if (!requiredImports && !bannedImports) {
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
if (spec.type === "ImportSpecifier" && typeof spec.imported.name === "string") {
|
|
115
|
+
if (requiredImports) {
|
|
116
|
+
const requiredImportIndex = requiredImports.specifiers.findIndex(
|
|
117
|
+
(imp) => imp.imported === spec.imported.name
|
|
118
|
+
);
|
|
119
|
+
if (requiredImportIndex !== -1) {
|
|
120
|
+
requiredImports.specifiers.splice(requiredImportIndex, 1);
|
|
121
|
+
if (requiredImports.specifiers.length === 0) {
|
|
122
|
+
imports.required = imports.required.splice(
|
|
123
|
+
imports.required.indexOf(requiredImports),
|
|
124
|
+
1
|
|
125
|
+
);
|
|
126
|
+
requiredImports = void 0;
|
|
127
|
+
}
|
|
128
|
+
} else {
|
|
129
|
+
importStatementCandidates.push(n);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
if (bannedImports) {
|
|
133
|
+
const bannedImportIndex = bannedImports.specifiers.findIndex(
|
|
134
|
+
(imp) => imp.imported === spec.imported.name
|
|
135
|
+
);
|
|
136
|
+
if (bannedImportIndex !== -1) {
|
|
137
|
+
importSpecifiersToRemove.push(spec);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (importSpecifiersToRemove.length > 0) {
|
|
143
|
+
appliedChanges = true;
|
|
144
|
+
n.specifiers = n.specifiers.filter(
|
|
145
|
+
(spec) => !importSpecifiersToRemove.includes(spec)
|
|
146
|
+
);
|
|
147
|
+
if (n.specifiers.length === 0) {
|
|
148
|
+
importDeclarationsToRemove.push(n);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
imports.required.forEach((requiredImport) => {
|
|
155
|
+
if (requiredImport.specifiers.length > 0) {
|
|
156
|
+
appliedChanges = true;
|
|
157
|
+
if (importStatementCandidates.length > 0) {
|
|
158
|
+
const importStatement2 = importStatementCandidates.find(
|
|
159
|
+
(importStatement3) => {
|
|
160
|
+
if (importStatement3.source.value === requiredImport.source) {
|
|
161
|
+
const importKind = importStatement3.importKind || "value";
|
|
162
|
+
const requiredImportKind = requiredImport.importKind || "value";
|
|
163
|
+
return importKind === requiredImportKind;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
);
|
|
168
|
+
if (importStatement2) {
|
|
169
|
+
if (importStatement2.specifiers === void 0) {
|
|
170
|
+
importStatement2.specifiers = [];
|
|
171
|
+
}
|
|
172
|
+
const importSpecifiersToAdd = requiredImport.specifiers.map(
|
|
173
|
+
(spec) => b.importSpecifier(
|
|
174
|
+
b.identifier(spec.imported),
|
|
175
|
+
b.identifier(spec.imported)
|
|
176
|
+
)
|
|
177
|
+
);
|
|
178
|
+
importStatement2.specifiers = [
|
|
179
|
+
...importStatement2.specifiers,
|
|
180
|
+
...importSpecifiersToAdd
|
|
181
|
+
];
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const importStatement = b.importDeclaration(
|
|
186
|
+
requiredImport.specifiers.map(
|
|
187
|
+
(spec) => b.importSpecifier(
|
|
188
|
+
b.identifier(spec.imported),
|
|
189
|
+
spec.local ? b.identifier(spec.local) : null
|
|
190
|
+
)
|
|
191
|
+
),
|
|
192
|
+
b.stringLiteral(requiredImport.source)
|
|
193
|
+
);
|
|
194
|
+
program.body.unshift(importStatement);
|
|
195
|
+
}
|
|
196
|
+
});
|
|
197
|
+
if (importDeclarationsToRemove.length > 0) {
|
|
198
|
+
appliedChanges = true;
|
|
199
|
+
for (const importDeclaration of importDeclarationsToRemove) {
|
|
200
|
+
if (((_c = importDeclaration.specifiers) == null ? void 0 : _c.length) === 0) {
|
|
201
|
+
const index = program.body.indexOf(importDeclaration);
|
|
202
|
+
if (index !== -1) {
|
|
203
|
+
program.body.splice(index, 1);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if (!appliedChanges) {
|
|
209
|
+
return {
|
|
210
|
+
exports: foundExports,
|
|
211
|
+
result: "not-modified"
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const printResult = recast.print(ast, {
|
|
215
|
+
reuseWhitespace: true,
|
|
216
|
+
sourceMapName: "output.map"
|
|
217
|
+
});
|
|
218
|
+
let transformedCode = printResult.code;
|
|
219
|
+
if (printResult.map) {
|
|
220
|
+
const fixedOutput = await fixTransformedOutputText({
|
|
221
|
+
originalCode: source,
|
|
222
|
+
transformedCode,
|
|
223
|
+
sourceMap: printResult.map,
|
|
224
|
+
preferredQuote
|
|
225
|
+
});
|
|
226
|
+
transformedCode = fixedOutput;
|
|
227
|
+
}
|
|
228
|
+
return {
|
|
229
|
+
result: "modified",
|
|
230
|
+
exports: foundExports,
|
|
231
|
+
output: transformedCode
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
async function fixTransformedOutputText({
|
|
235
|
+
originalCode,
|
|
236
|
+
transformedCode,
|
|
237
|
+
sourceMap: sourceMap$1,
|
|
238
|
+
preferredQuote
|
|
239
|
+
}) {
|
|
240
|
+
const originalLines = originalCode.split("\n");
|
|
241
|
+
const transformedLines = transformedCode.split("\n");
|
|
242
|
+
const defaultUsesSemicolons = detectSemicolonUsage(originalCode);
|
|
243
|
+
const consumer = await new sourceMap.SourceMapConsumer(sourceMap$1);
|
|
244
|
+
const fixedLines = transformedLines.map((line, i) => {
|
|
245
|
+
const transformedLineNum = i + 1;
|
|
246
|
+
let origLineText = void 0;
|
|
247
|
+
for (let col = 0; col < line.length; col++) {
|
|
248
|
+
const mapped = consumer.originalPositionFor({
|
|
249
|
+
line: transformedLineNum,
|
|
250
|
+
column: col
|
|
251
|
+
});
|
|
252
|
+
if (mapped.line != null && mapped.line > 0) {
|
|
253
|
+
origLineText = originalLines[mapped.line - 1];
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
if (origLineText !== void 0) {
|
|
258
|
+
if (origLineText === line) {
|
|
259
|
+
return origLineText;
|
|
260
|
+
}
|
|
261
|
+
return fixLine(line, {
|
|
262
|
+
originalLine: origLineText,
|
|
263
|
+
useOriginalSemicolon: true,
|
|
264
|
+
useOriginalQuotes: true,
|
|
265
|
+
fallbackQuote: preferredQuote
|
|
266
|
+
});
|
|
267
|
+
} else {
|
|
268
|
+
return fixLine(line, {
|
|
269
|
+
originalLine: null,
|
|
270
|
+
useOriginalSemicolon: false,
|
|
271
|
+
useOriginalQuotes: false,
|
|
272
|
+
fallbackQuote: preferredQuote,
|
|
273
|
+
fallbackSemicolon: defaultUsesSemicolons
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
return fixedLines.join("\n");
|
|
278
|
+
}
|
|
279
|
+
function fixLine(line, {
|
|
280
|
+
originalLine,
|
|
281
|
+
useOriginalSemicolon,
|
|
282
|
+
useOriginalQuotes,
|
|
283
|
+
fallbackQuote,
|
|
284
|
+
fallbackSemicolon = true
|
|
285
|
+
}) {
|
|
286
|
+
let result = line;
|
|
287
|
+
if (useOriginalQuotes && originalLine) {
|
|
288
|
+
result = fixQuotes(result, originalLine, fallbackQuote);
|
|
289
|
+
} else if (!useOriginalQuotes && fallbackQuote) {
|
|
290
|
+
result = fixQuotesToPreferred(result, fallbackQuote);
|
|
291
|
+
}
|
|
292
|
+
if (useOriginalSemicolon && originalLine) {
|
|
293
|
+
const hadSemicolon = originalLine.trimEnd().endsWith(";");
|
|
294
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
295
|
+
if (hadSemicolon && !hasSemicolon) result += ";";
|
|
296
|
+
if (!hadSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
297
|
+
} else if (!useOriginalSemicolon) {
|
|
298
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
299
|
+
if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
300
|
+
if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ";";
|
|
301
|
+
}
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
function fixQuotes(line, originalLine, fallbackQuote) {
|
|
305
|
+
let originalQuote = detectQuoteFromLine(originalLine);
|
|
306
|
+
if (!originalQuote) {
|
|
307
|
+
originalQuote = fallbackQuote;
|
|
308
|
+
}
|
|
309
|
+
return fixQuotesToPreferred(line, originalQuote);
|
|
310
|
+
}
|
|
311
|
+
function fixQuotesToPreferred(line, quote) {
|
|
312
|
+
return line.replace(
|
|
313
|
+
/(['"`])([^'"`\\]*(?:\\.[^'"`\\]*)*)\1/g,
|
|
314
|
+
(_, q, content) => {
|
|
315
|
+
const escaped = content.replaceAll(quote, `\\${quote}`);
|
|
316
|
+
return `${quote}${escaped}${quote}`;
|
|
317
|
+
}
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
function detectQuoteFromLine(line) {
|
|
321
|
+
const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
|
|
322
|
+
return match ? match[1] : null;
|
|
323
|
+
}
|
|
324
|
+
function detectSemicolonUsage(code) {
|
|
325
|
+
const lines = code.split("\n").map((l) => l.trim());
|
|
326
|
+
const total = lines.length;
|
|
327
|
+
const withSemis = lines.filter((l) => l.endsWith(";")).length;
|
|
328
|
+
return withSemis > total / 2;
|
|
329
|
+
}
|
|
330
|
+
function detectPreferredQuoteStyle(ast) {
|
|
331
|
+
let single = 0;
|
|
332
|
+
let double = 0;
|
|
333
|
+
recast.visit(ast, {
|
|
334
|
+
visitStringLiteral(path) {
|
|
335
|
+
var _a;
|
|
336
|
+
if (path.parent.node.type !== "JSXAttribute") {
|
|
337
|
+
const raw = (_a = path.node.extra) == null ? void 0 : _a.raw;
|
|
338
|
+
if (raw == null ? void 0 : raw.startsWith("'")) single++;
|
|
339
|
+
else if (raw == null ? void 0 : raw.startsWith('"')) double++;
|
|
340
|
+
}
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
if (single >= double) {
|
|
345
|
+
return "'";
|
|
346
|
+
}
|
|
347
|
+
return '"';
|
|
348
|
+
}
|
|
349
|
+
exports.detectPreferredQuoteStyle = detectPreferredQuoteStyle;
|
|
350
|
+
exports.transform = transform;
|
|
351
|
+
//# sourceMappingURL=transform.cjs.map
|