@tanstack/router-generator 1.166.8 → 1.166.10
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/_virtual/_rolldown/runtime.cjs +23 -0
- package/dist/cjs/config.cjs +111 -147
- package/dist/cjs/config.cjs.map +1 -1
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs +224 -303
- package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/filesystem/physical/rootPathId.cjs +5 -4
- package/dist/cjs/filesystem/physical/rootPathId.cjs.map +1 -1
- package/dist/cjs/filesystem/virtual/config.cjs +32 -30
- package/dist/cjs/filesystem/virtual/config.cjs.map +1 -1
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs +164 -209
- package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +1 -1
- package/dist/cjs/filesystem/virtual/loadConfigFile.cjs +9 -8
- package/dist/cjs/filesystem/virtual/loadConfigFile.cjs.map +1 -1
- package/dist/cjs/generator.cjs +766 -1106
- package/dist/cjs/generator.cjs.map +1 -1
- package/dist/cjs/index.cjs +32 -34
- package/dist/cjs/logger.cjs +28 -34
- package/dist/cjs/logger.cjs.map +1 -1
- package/dist/cjs/template.cjs +144 -151
- package/dist/cjs/template.cjs.map +1 -1
- package/dist/cjs/transform/transform.cjs +287 -426
- package/dist/cjs/transform/transform.cjs.map +1 -1
- package/dist/cjs/transform/utils.cjs +31 -33
- package/dist/cjs/transform/utils.cjs.map +1 -1
- package/dist/cjs/utils.cjs +534 -544
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/validate-route-params.cjs +66 -51
- package/dist/cjs/validate-route-params.cjs.map +1 -1
- package/dist/esm/config.js +106 -147
- package/dist/esm/config.js.map +1 -1
- package/dist/esm/filesystem/physical/getRouteNodes.js +220 -286
- package/dist/esm/filesystem/physical/getRouteNodes.js.map +1 -1
- package/dist/esm/filesystem/physical/rootPathId.js +6 -5
- package/dist/esm/filesystem/physical/rootPathId.js.map +1 -1
- package/dist/esm/filesystem/virtual/config.js +31 -30
- package/dist/esm/filesystem/virtual/config.js.map +1 -1
- package/dist/esm/filesystem/virtual/getRouteNodes.js +161 -208
- package/dist/esm/filesystem/virtual/getRouteNodes.js.map +1 -1
- package/dist/esm/filesystem/virtual/loadConfigFile.js +7 -7
- package/dist/esm/filesystem/virtual/loadConfigFile.js.map +1 -1
- package/dist/esm/generator.js +756 -1083
- package/dist/esm/generator.js.map +1 -1
- package/dist/esm/index.js +4 -31
- package/dist/esm/logger.js +29 -35
- package/dist/esm/logger.js.map +1 -1
- package/dist/esm/template.js +144 -152
- package/dist/esm/template.js.map +1 -1
- package/dist/esm/transform/transform.js +285 -425
- package/dist/esm/transform/transform.js.map +1 -1
- package/dist/esm/transform/utils.js +31 -33
- package/dist/esm/transform/utils.js.map +1 -1
- package/dist/esm/utils.js +529 -564
- package/dist/esm/utils.js.map +1 -1
- package/dist/esm/validate-route-params.js +67 -52
- package/dist/esm/validate-route-params.js.map +1 -1
- package/package.json +5 -5
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.js.map +0 -1
|
@@ -1,440 +1,300 @@
|
|
|
1
|
-
import { parseAst } from "@tanstack/router-utils";
|
|
2
|
-
import { parse, visit, types, print } from "recast";
|
|
3
|
-
import { SourceMapConsumer } from "source-map";
|
|
4
1
|
import { mergeImportDeclarations } from "../utils.js";
|
|
5
2
|
import { ensureStringArgument } from "./utils.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}) {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
);
|
|
216
|
-
requiredImports = void 0;
|
|
217
|
-
}
|
|
218
|
-
} else {
|
|
219
|
-
importStatementCandidates.push(n);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (bannedImports) {
|
|
223
|
-
const bannedImportIndex = bannedImports.specifiers.findIndex(
|
|
224
|
-
(imp) => imp.imported === spec.imported.name
|
|
225
|
-
);
|
|
226
|
-
if (bannedImportIndex !== -1) {
|
|
227
|
-
importSpecifiersToRemove.push(spec);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
if (importSpecifiersToRemove.length > 0) {
|
|
233
|
-
appliedChanges = true;
|
|
234
|
-
n.specifiers = n.specifiers.filter(
|
|
235
|
-
(spec) => !importSpecifiersToRemove.includes(spec)
|
|
236
|
-
);
|
|
237
|
-
if (n.specifiers.length === 0) {
|
|
238
|
-
importDeclarationsToRemove.push(n);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
imports.required.forEach((requiredImport) => {
|
|
245
|
-
if (requiredImport.specifiers.length > 0) {
|
|
246
|
-
appliedChanges = true;
|
|
247
|
-
if (importStatementCandidates.length > 0) {
|
|
248
|
-
const importStatement2 = importStatementCandidates.find(
|
|
249
|
-
(importStatement3) => {
|
|
250
|
-
if (importStatement3.source.value === requiredImport.source) {
|
|
251
|
-
const importKind = importStatement3.importKind || "value";
|
|
252
|
-
const requiredImportKind = requiredImport.importKind || "value";
|
|
253
|
-
return importKind === requiredImportKind;
|
|
254
|
-
}
|
|
255
|
-
return false;
|
|
256
|
-
}
|
|
257
|
-
);
|
|
258
|
-
if (importStatement2) {
|
|
259
|
-
if (importStatement2.specifiers === void 0) {
|
|
260
|
-
importStatement2.specifiers = [];
|
|
261
|
-
}
|
|
262
|
-
const importSpecifiersToAdd = requiredImport.specifiers.map(
|
|
263
|
-
(spec) => b.importSpecifier(
|
|
264
|
-
b.identifier(spec.imported),
|
|
265
|
-
b.identifier(spec.imported)
|
|
266
|
-
)
|
|
267
|
-
);
|
|
268
|
-
importStatement2.specifiers = [
|
|
269
|
-
...importStatement2.specifiers,
|
|
270
|
-
...importSpecifiersToAdd
|
|
271
|
-
];
|
|
272
|
-
return;
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
const importStatement = b.importDeclaration(
|
|
276
|
-
requiredImport.specifiers.map(
|
|
277
|
-
(spec) => b.importSpecifier(
|
|
278
|
-
b.identifier(spec.imported),
|
|
279
|
-
spec.local ? b.identifier(spec.local) : null
|
|
280
|
-
)
|
|
281
|
-
),
|
|
282
|
-
b.stringLiteral(requiredImport.source)
|
|
283
|
-
);
|
|
284
|
-
program.body.unshift(importStatement);
|
|
285
|
-
}
|
|
286
|
-
});
|
|
287
|
-
if (importDeclarationsToRemove.length > 0) {
|
|
288
|
-
appliedChanges = true;
|
|
289
|
-
for (const importDeclaration of importDeclarationsToRemove) {
|
|
290
|
-
if (importDeclaration.specifiers?.length === 0) {
|
|
291
|
-
const index = program.body.indexOf(importDeclaration);
|
|
292
|
-
if (index !== -1) {
|
|
293
|
-
program.body.splice(index, 1);
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
if (!appliedChanges) {
|
|
299
|
-
return {
|
|
300
|
-
result: "not-modified"
|
|
301
|
-
};
|
|
302
|
-
}
|
|
303
|
-
const printResult = print(ast, {
|
|
304
|
-
reuseWhitespace: true,
|
|
305
|
-
sourceMapName: "output.map"
|
|
306
|
-
});
|
|
307
|
-
let transformedCode = printResult.code;
|
|
308
|
-
if (printResult.map) {
|
|
309
|
-
const fixedOutput = await fixTransformedOutputText({
|
|
310
|
-
originalCode: source,
|
|
311
|
-
transformedCode,
|
|
312
|
-
sourceMap: printResult.map,
|
|
313
|
-
preferredQuote
|
|
314
|
-
});
|
|
315
|
-
transformedCode = fixedOutput;
|
|
316
|
-
}
|
|
317
|
-
return {
|
|
318
|
-
result: "modified",
|
|
319
|
-
output: transformedCode
|
|
320
|
-
};
|
|
3
|
+
import { parseAst } from "@tanstack/router-utils";
|
|
4
|
+
import { parse, print, types, visit } from "recast";
|
|
5
|
+
import { SourceMapConsumer } from "source-map";
|
|
6
|
+
//#region src/transform/transform.ts
|
|
7
|
+
var b = types.builders;
|
|
8
|
+
async function transform({ ctx, source, node }) {
|
|
9
|
+
let appliedChanges = false;
|
|
10
|
+
let ast;
|
|
11
|
+
try {
|
|
12
|
+
ast = parse(source, {
|
|
13
|
+
sourceFileName: "output.ts",
|
|
14
|
+
parser: { parse(code) {
|
|
15
|
+
return parseAst({
|
|
16
|
+
code,
|
|
17
|
+
tokens: true
|
|
18
|
+
});
|
|
19
|
+
} }
|
|
20
|
+
});
|
|
21
|
+
} catch (e) {
|
|
22
|
+
console.error("Error parsing code", ctx.routeId, source, e);
|
|
23
|
+
return {
|
|
24
|
+
result: "error",
|
|
25
|
+
error: e
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
const preferredQuote = detectPreferredQuoteStyle(ast);
|
|
29
|
+
let routeExportHandled = false;
|
|
30
|
+
function onExportFound(decl) {
|
|
31
|
+
if (decl.init?.type === "CallExpression") {
|
|
32
|
+
const callExpression = decl.init;
|
|
33
|
+
const firstArgument = callExpression.arguments[0];
|
|
34
|
+
if (firstArgument) {
|
|
35
|
+
if (firstArgument.type === "ObjectExpression") {
|
|
36
|
+
const staticProperties = firstArgument.properties.flatMap((p) => {
|
|
37
|
+
if (p.type === "ObjectProperty" && p.key.type === "Identifier") return p.key.name;
|
|
38
|
+
return [];
|
|
39
|
+
});
|
|
40
|
+
node.createFileRouteProps = new Set(staticProperties);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
let identifier;
|
|
44
|
+
if (callExpression.callee.type === "Identifier") {
|
|
45
|
+
identifier = callExpression.callee;
|
|
46
|
+
if (ctx.verboseFileRoutes) {
|
|
47
|
+
callExpression.callee = b.callExpression(identifier, [b.stringLiteral(ctx.routeId)]);
|
|
48
|
+
appliedChanges = true;
|
|
49
|
+
}
|
|
50
|
+
} else if (callExpression.callee.type === "CallExpression" && callExpression.callee.callee.type === "Identifier") {
|
|
51
|
+
identifier = callExpression.callee.callee;
|
|
52
|
+
if (!ctx.verboseFileRoutes) {
|
|
53
|
+
callExpression.callee = identifier;
|
|
54
|
+
appliedChanges = true;
|
|
55
|
+
} else appliedChanges = ensureStringArgument(callExpression.callee, ctx.routeId, ctx.preferredQuote);
|
|
56
|
+
}
|
|
57
|
+
if (identifier === void 0) throw new Error(`expected identifier to be present in ${ctx.routeId} for export "Route"`);
|
|
58
|
+
if (identifier.name === "createFileRoute" && ctx.lazy) {
|
|
59
|
+
identifier.name = "createLazyFileRoute";
|
|
60
|
+
appliedChanges = true;
|
|
61
|
+
} else if (identifier.name === "createLazyFileRoute" && !ctx.lazy) {
|
|
62
|
+
identifier.name = "createFileRoute";
|
|
63
|
+
appliedChanges = true;
|
|
64
|
+
}
|
|
65
|
+
} else throw new Error(`expected "Route" export to be initialized by a CallExpression`);
|
|
66
|
+
routeExportHandled = true;
|
|
67
|
+
}
|
|
68
|
+
const program = ast.program;
|
|
69
|
+
for (const n of program.body) {
|
|
70
|
+
if (n.type === "ExportNamedDeclaration") {
|
|
71
|
+
if (n.declaration?.type === "VariableDeclaration") {
|
|
72
|
+
const decl = n.declaration.declarations[0];
|
|
73
|
+
if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
|
|
74
|
+
if (decl.id.name === "Route") onExportFound(decl);
|
|
75
|
+
}
|
|
76
|
+
} else if (n.declaration === null && n.specifiers) {
|
|
77
|
+
for (const spec of n.specifiers) if (typeof spec.exported.name === "string") {
|
|
78
|
+
if (spec.exported.name === "Route") {
|
|
79
|
+
const variableName = spec.local?.name || spec.exported.name;
|
|
80
|
+
for (const decl of program.body) if (decl.type === "VariableDeclaration" && decl.declarations[0]) {
|
|
81
|
+
const variable = decl.declarations[0];
|
|
82
|
+
if (variable.type === "VariableDeclarator" && variable.id.type === "Identifier" && variable.id.name === variableName) {
|
|
83
|
+
onExportFound(variable);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
if (routeExportHandled) break;
|
|
92
|
+
}
|
|
93
|
+
if (!routeExportHandled) return { result: "no-route-export" };
|
|
94
|
+
const imports = {
|
|
95
|
+
required: [],
|
|
96
|
+
banned: []
|
|
97
|
+
};
|
|
98
|
+
const targetModule = `@tanstack/${ctx.target}-router`;
|
|
99
|
+
if (ctx.verboseFileRoutes === false) imports.banned = [{
|
|
100
|
+
source: targetModule,
|
|
101
|
+
specifiers: [{ imported: "createLazyFileRoute" }, { imported: "createFileRoute" }]
|
|
102
|
+
}];
|
|
103
|
+
else if (ctx.lazy) {
|
|
104
|
+
imports.required = [{
|
|
105
|
+
source: targetModule,
|
|
106
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
107
|
+
}];
|
|
108
|
+
imports.banned = [{
|
|
109
|
+
source: targetModule,
|
|
110
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
111
|
+
}];
|
|
112
|
+
} else {
|
|
113
|
+
imports.required = [{
|
|
114
|
+
source: targetModule,
|
|
115
|
+
specifiers: [{ imported: "createFileRoute" }]
|
|
116
|
+
}];
|
|
117
|
+
imports.banned = [{
|
|
118
|
+
source: targetModule,
|
|
119
|
+
specifiers: [{ imported: "createLazyFileRoute" }]
|
|
120
|
+
}];
|
|
121
|
+
}
|
|
122
|
+
imports.required = mergeImportDeclarations(imports.required);
|
|
123
|
+
imports.banned = mergeImportDeclarations(imports.banned);
|
|
124
|
+
const importStatementCandidates = [];
|
|
125
|
+
const importDeclarationsToRemove = [];
|
|
126
|
+
for (const n of program.body) {
|
|
127
|
+
const findImport = (opts) => (i) => {
|
|
128
|
+
if (i.source === opts.source) {
|
|
129
|
+
const importKind = i.importKind || "value";
|
|
130
|
+
return (opts.importKind || "value") === importKind;
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
};
|
|
134
|
+
if (n.type === "ImportDeclaration" && typeof n.source.value === "string") {
|
|
135
|
+
const filterImport = findImport({
|
|
136
|
+
source: n.source.value,
|
|
137
|
+
importKind: n.importKind
|
|
138
|
+
});
|
|
139
|
+
let requiredImports = imports.required.filter(filterImport)[0];
|
|
140
|
+
const bannedImports = imports.banned.filter(filterImport)[0];
|
|
141
|
+
if (!requiredImports && !bannedImports) continue;
|
|
142
|
+
const importSpecifiersToRemove = [];
|
|
143
|
+
if (n.specifiers) {
|
|
144
|
+
for (const spec of n.specifiers) {
|
|
145
|
+
if (!requiredImports && !bannedImports) break;
|
|
146
|
+
if (spec.type === "ImportSpecifier" && typeof spec.imported.name === "string") {
|
|
147
|
+
if (requiredImports) {
|
|
148
|
+
const requiredImportIndex = requiredImports.specifiers.findIndex((imp) => imp.imported === spec.imported.name);
|
|
149
|
+
if (requiredImportIndex !== -1) {
|
|
150
|
+
requiredImports.specifiers.splice(requiredImportIndex, 1);
|
|
151
|
+
if (requiredImports.specifiers.length === 0) {
|
|
152
|
+
imports.required = imports.required.splice(imports.required.indexOf(requiredImports), 1);
|
|
153
|
+
requiredImports = void 0;
|
|
154
|
+
}
|
|
155
|
+
} else importStatementCandidates.push(n);
|
|
156
|
+
}
|
|
157
|
+
if (bannedImports) {
|
|
158
|
+
if (bannedImports.specifiers.findIndex((imp) => imp.imported === spec.imported.name) !== -1) importSpecifiersToRemove.push(spec);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (importSpecifiersToRemove.length > 0) {
|
|
163
|
+
appliedChanges = true;
|
|
164
|
+
n.specifiers = n.specifiers.filter((spec) => !importSpecifiersToRemove.includes(spec));
|
|
165
|
+
if (n.specifiers.length === 0) importDeclarationsToRemove.push(n);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
imports.required.forEach((requiredImport) => {
|
|
171
|
+
if (requiredImport.specifiers.length > 0) {
|
|
172
|
+
appliedChanges = true;
|
|
173
|
+
if (importStatementCandidates.length > 0) {
|
|
174
|
+
const importStatement = importStatementCandidates.find((importStatement) => {
|
|
175
|
+
if (importStatement.source.value === requiredImport.source) return (importStatement.importKind || "value") === (requiredImport.importKind || "value");
|
|
176
|
+
return false;
|
|
177
|
+
});
|
|
178
|
+
if (importStatement) {
|
|
179
|
+
if (importStatement.specifiers === void 0) importStatement.specifiers = [];
|
|
180
|
+
const importSpecifiersToAdd = requiredImport.specifiers.map((spec) => b.importSpecifier(b.identifier(spec.imported), b.identifier(spec.imported)));
|
|
181
|
+
importStatement.specifiers = [...importStatement.specifiers, ...importSpecifiersToAdd];
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const importStatement = b.importDeclaration(requiredImport.specifiers.map((spec) => b.importSpecifier(b.identifier(spec.imported), spec.local ? b.identifier(spec.local) : null)), b.stringLiteral(requiredImport.source));
|
|
186
|
+
program.body.unshift(importStatement);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
if (importDeclarationsToRemove.length > 0) {
|
|
190
|
+
appliedChanges = true;
|
|
191
|
+
for (const importDeclaration of importDeclarationsToRemove) if (importDeclaration.specifiers?.length === 0) {
|
|
192
|
+
const index = program.body.indexOf(importDeclaration);
|
|
193
|
+
if (index !== -1) program.body.splice(index, 1);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (!appliedChanges) return { result: "not-modified" };
|
|
197
|
+
const printResult = print(ast, {
|
|
198
|
+
reuseWhitespace: true,
|
|
199
|
+
sourceMapName: "output.map"
|
|
200
|
+
});
|
|
201
|
+
let transformedCode = printResult.code;
|
|
202
|
+
if (printResult.map) transformedCode = await fixTransformedOutputText({
|
|
203
|
+
originalCode: source,
|
|
204
|
+
transformedCode,
|
|
205
|
+
sourceMap: printResult.map,
|
|
206
|
+
preferredQuote
|
|
207
|
+
});
|
|
208
|
+
return {
|
|
209
|
+
result: "modified",
|
|
210
|
+
output: transformedCode
|
|
211
|
+
};
|
|
321
212
|
}
|
|
322
|
-
async function fixTransformedOutputText({
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
return fixLine(line, {
|
|
357
|
-
originalLine: null,
|
|
358
|
-
useOriginalSemicolon: false,
|
|
359
|
-
useOriginalQuotes: false,
|
|
360
|
-
fallbackQuote: preferredQuote,
|
|
361
|
-
fallbackSemicolon: defaultUsesSemicolons
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
});
|
|
365
|
-
return fixedLines.join("\n");
|
|
213
|
+
async function fixTransformedOutputText({ originalCode, transformedCode, sourceMap, preferredQuote }) {
|
|
214
|
+
const originalLines = originalCode.split("\n");
|
|
215
|
+
const transformedLines = transformedCode.split("\n");
|
|
216
|
+
const defaultUsesSemicolons = detectSemicolonUsage(originalCode);
|
|
217
|
+
const consumer = await new SourceMapConsumer(sourceMap);
|
|
218
|
+
return transformedLines.map((line, i) => {
|
|
219
|
+
const transformedLineNum = i + 1;
|
|
220
|
+
let origLineText = void 0;
|
|
221
|
+
for (let col = 0; col < line.length; col++) {
|
|
222
|
+
const mapped = consumer.originalPositionFor({
|
|
223
|
+
line: transformedLineNum,
|
|
224
|
+
column: col
|
|
225
|
+
});
|
|
226
|
+
if (mapped.line != null && mapped.line > 0) {
|
|
227
|
+
origLineText = originalLines[mapped.line - 1];
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (origLineText !== void 0) {
|
|
232
|
+
if (origLineText === line) return origLineText;
|
|
233
|
+
return fixLine(line, {
|
|
234
|
+
originalLine: origLineText,
|
|
235
|
+
useOriginalSemicolon: true,
|
|
236
|
+
useOriginalQuotes: true,
|
|
237
|
+
fallbackQuote: preferredQuote
|
|
238
|
+
});
|
|
239
|
+
} else return fixLine(line, {
|
|
240
|
+
originalLine: null,
|
|
241
|
+
useOriginalSemicolon: false,
|
|
242
|
+
useOriginalQuotes: false,
|
|
243
|
+
fallbackQuote: preferredQuote,
|
|
244
|
+
fallbackSemicolon: defaultUsesSemicolons
|
|
245
|
+
});
|
|
246
|
+
}).join("\n");
|
|
366
247
|
}
|
|
367
|
-
function fixLine(line, {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
383
|
-
if (hadSemicolon && !hasSemicolon) result += ";";
|
|
384
|
-
if (!hadSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
385
|
-
} else if (!useOriginalSemicolon) {
|
|
386
|
-
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
387
|
-
if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
388
|
-
if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ";";
|
|
389
|
-
}
|
|
390
|
-
return result;
|
|
248
|
+
function fixLine(line, { originalLine, useOriginalSemicolon, useOriginalQuotes, fallbackQuote, fallbackSemicolon = true }) {
|
|
249
|
+
let result = line;
|
|
250
|
+
if (useOriginalQuotes && originalLine) result = fixQuotes(result, originalLine, fallbackQuote);
|
|
251
|
+
else if (!useOriginalQuotes && fallbackQuote) result = fixQuotesToPreferred(result, fallbackQuote);
|
|
252
|
+
if (useOriginalSemicolon && originalLine) {
|
|
253
|
+
const hadSemicolon = originalLine.trimEnd().endsWith(";");
|
|
254
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
255
|
+
if (hadSemicolon && !hasSemicolon) result += ";";
|
|
256
|
+
if (!hadSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
257
|
+
} else if (!useOriginalSemicolon) {
|
|
258
|
+
const hasSemicolon = result.trimEnd().endsWith(";");
|
|
259
|
+
if (!fallbackSemicolon && hasSemicolon) result = result.replace(/;\s*$/, "");
|
|
260
|
+
if (fallbackSemicolon && !hasSemicolon && result.trim()) result += ";";
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
391
263
|
}
|
|
392
264
|
function fixQuotes(line, originalLine, fallbackQuote) {
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
}
|
|
397
|
-
return fixQuotesToPreferred(line, originalQuote);
|
|
265
|
+
let originalQuote = detectQuoteFromLine(originalLine);
|
|
266
|
+
if (!originalQuote) originalQuote = fallbackQuote;
|
|
267
|
+
return fixQuotesToPreferred(line, originalQuote);
|
|
398
268
|
}
|
|
399
269
|
function fixQuotesToPreferred(line, quote) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
const escaped = content.replaceAll(quote, `\\${quote}`);
|
|
404
|
-
return `${quote}${escaped}${quote}`;
|
|
405
|
-
}
|
|
406
|
-
);
|
|
270
|
+
return line.replace(/(['"`])([^'"`\\]*(?:\\.[^'"`\\]*)*)\1/g, (_, q, content) => {
|
|
271
|
+
return `${quote}${content.replaceAll(quote, `\\${quote}`)}${quote}`;
|
|
272
|
+
});
|
|
407
273
|
}
|
|
408
274
|
function detectQuoteFromLine(line) {
|
|
409
|
-
|
|
410
|
-
|
|
275
|
+
const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
|
|
276
|
+
return match ? match[1] : null;
|
|
411
277
|
}
|
|
412
278
|
function detectSemicolonUsage(code) {
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
return withSemis > total / 2;
|
|
279
|
+
const lines = code.split("\n").map((l) => l.trim());
|
|
280
|
+
const total = lines.length;
|
|
281
|
+
return lines.filter((l) => l.endsWith(";")).length > total / 2;
|
|
417
282
|
}
|
|
418
283
|
function detectPreferredQuoteStyle(ast) {
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
if (single >= double) {
|
|
432
|
-
return "'";
|
|
433
|
-
}
|
|
434
|
-
return '"';
|
|
284
|
+
let single = 0;
|
|
285
|
+
let double = 0;
|
|
286
|
+
visit(ast, { visitStringLiteral(path) {
|
|
287
|
+
if (path.parent.node.type !== "JSXAttribute") {
|
|
288
|
+
const raw = path.node.extra?.raw;
|
|
289
|
+
if (raw?.startsWith("'")) single++;
|
|
290
|
+
else if (raw?.startsWith("\"")) double++;
|
|
291
|
+
}
|
|
292
|
+
return false;
|
|
293
|
+
} });
|
|
294
|
+
if (single >= double) return "'";
|
|
295
|
+
return "\"";
|
|
435
296
|
}
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
//# sourceMappingURL=transform.js.map
|
|
297
|
+
//#endregion
|
|
298
|
+
export { transform };
|
|
299
|
+
|
|
300
|
+
//# sourceMappingURL=transform.js.map
|