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