@tanstack/router-generator 1.166.9 → 1.166.11

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.
Files changed (58) hide show
  1. package/dist/cjs/_virtual/_rolldown/runtime.cjs +23 -0
  2. package/dist/cjs/config.cjs +111 -147
  3. package/dist/cjs/config.cjs.map +1 -1
  4. package/dist/cjs/filesystem/physical/getRouteNodes.cjs +224 -303
  5. package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +1 -1
  6. package/dist/cjs/filesystem/physical/rootPathId.cjs +5 -4
  7. package/dist/cjs/filesystem/physical/rootPathId.cjs.map +1 -1
  8. package/dist/cjs/filesystem/virtual/config.cjs +32 -30
  9. package/dist/cjs/filesystem/virtual/config.cjs.map +1 -1
  10. package/dist/cjs/filesystem/virtual/getRouteNodes.cjs +164 -209
  11. package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +1 -1
  12. package/dist/cjs/filesystem/virtual/loadConfigFile.cjs +9 -8
  13. package/dist/cjs/filesystem/virtual/loadConfigFile.cjs.map +1 -1
  14. package/dist/cjs/generator.cjs +766 -1106
  15. package/dist/cjs/generator.cjs.map +1 -1
  16. package/dist/cjs/index.cjs +32 -34
  17. package/dist/cjs/logger.cjs +28 -34
  18. package/dist/cjs/logger.cjs.map +1 -1
  19. package/dist/cjs/template.cjs +144 -151
  20. package/dist/cjs/template.cjs.map +1 -1
  21. package/dist/cjs/transform/transform.cjs +287 -426
  22. package/dist/cjs/transform/transform.cjs.map +1 -1
  23. package/dist/cjs/transform/utils.cjs +31 -33
  24. package/dist/cjs/transform/utils.cjs.map +1 -1
  25. package/dist/cjs/utils.cjs +534 -544
  26. package/dist/cjs/utils.cjs.map +1 -1
  27. package/dist/cjs/validate-route-params.cjs +66 -51
  28. package/dist/cjs/validate-route-params.cjs.map +1 -1
  29. package/dist/esm/config.js +106 -147
  30. package/dist/esm/config.js.map +1 -1
  31. package/dist/esm/filesystem/physical/getRouteNodes.js +220 -286
  32. package/dist/esm/filesystem/physical/getRouteNodes.js.map +1 -1
  33. package/dist/esm/filesystem/physical/rootPathId.js +6 -5
  34. package/dist/esm/filesystem/physical/rootPathId.js.map +1 -1
  35. package/dist/esm/filesystem/virtual/config.js +31 -30
  36. package/dist/esm/filesystem/virtual/config.js.map +1 -1
  37. package/dist/esm/filesystem/virtual/getRouteNodes.js +161 -208
  38. package/dist/esm/filesystem/virtual/getRouteNodes.js.map +1 -1
  39. package/dist/esm/filesystem/virtual/loadConfigFile.js +7 -7
  40. package/dist/esm/filesystem/virtual/loadConfigFile.js.map +1 -1
  41. package/dist/esm/generator.js +756 -1083
  42. package/dist/esm/generator.js.map +1 -1
  43. package/dist/esm/index.js +4 -31
  44. package/dist/esm/logger.js +29 -35
  45. package/dist/esm/logger.js.map +1 -1
  46. package/dist/esm/template.js +144 -152
  47. package/dist/esm/template.js.map +1 -1
  48. package/dist/esm/transform/transform.js +285 -425
  49. package/dist/esm/transform/transform.js.map +1 -1
  50. package/dist/esm/transform/utils.js +31 -33
  51. package/dist/esm/transform/utils.js.map +1 -1
  52. package/dist/esm/utils.js +529 -564
  53. package/dist/esm/utils.js.map +1 -1
  54. package/dist/esm/validate-route-params.js +67 -52
  55. package/dist/esm/validate-route-params.js.map +1 -1
  56. package/package.json +5 -5
  57. package/dist/cjs/index.cjs.map +0 -1
  58. 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
- const b = types.builders;
7
- async function transform({
8
- ctx,
9
- source,
10
- node
11
- }) {
12
- let appliedChanges = false;
13
- let ast;
14
- try {
15
- ast = parse(source, {
16
- sourceFileName: "output.ts",
17
- parser: {
18
- parse(code) {
19
- return parseAst({
20
- code,
21
- // we need to instruct babel to produce tokens,
22
- // otherwise recast will try to generate the tokens via its own parser and will fail
23
- tokens: true
24
- });
25
- }
26
- }
27
- });
28
- } catch (e) {
29
- console.error("Error parsing code", ctx.routeId, source, e);
30
- return {
31
- result: "error",
32
- error: e
33
- };
34
- }
35
- const preferredQuote = detectPreferredQuoteStyle(ast);
36
- let routeExportHandled = false;
37
- function onExportFound(decl) {
38
- if (decl.init?.type === "CallExpression") {
39
- const callExpression = decl.init;
40
- const firstArgument = callExpression.arguments[0];
41
- if (firstArgument) {
42
- if (firstArgument.type === "ObjectExpression") {
43
- const staticProperties = firstArgument.properties.flatMap((p) => {
44
- if (p.type === "ObjectProperty" && p.key.type === "Identifier") {
45
- return p.key.name;
46
- }
47
- return [];
48
- });
49
- node.createFileRouteProps = new Set(staticProperties);
50
- }
51
- }
52
- let identifier;
53
- if (callExpression.callee.type === "Identifier") {
54
- identifier = callExpression.callee;
55
- if (ctx.verboseFileRoutes) {
56
- callExpression.callee = b.callExpression(identifier, [
57
- b.stringLiteral(ctx.routeId)
58
- ]);
59
- appliedChanges = true;
60
- }
61
- } else if (callExpression.callee.type === "CallExpression" && callExpression.callee.callee.type === "Identifier") {
62
- identifier = callExpression.callee.callee;
63
- if (!ctx.verboseFileRoutes) {
64
- callExpression.callee = identifier;
65
- appliedChanges = true;
66
- } else {
67
- appliedChanges = ensureStringArgument(
68
- callExpression.callee,
69
- ctx.routeId,
70
- ctx.preferredQuote
71
- );
72
- }
73
- }
74
- if (identifier === void 0) {
75
- throw new Error(
76
- `expected identifier to be present in ${ctx.routeId} for export "Route"`
77
- );
78
- }
79
- if (identifier.name === "createFileRoute" && ctx.lazy) {
80
- identifier.name = "createLazyFileRoute";
81
- appliedChanges = true;
82
- } else if (identifier.name === "createLazyFileRoute" && !ctx.lazy) {
83
- identifier.name = "createFileRoute";
84
- appliedChanges = true;
85
- }
86
- } else {
87
- throw new Error(
88
- `expected "Route" export to be initialized by a CallExpression`
89
- );
90
- }
91
- routeExportHandled = true;
92
- }
93
- const program = ast.program;
94
- for (const n of program.body) {
95
- if (n.type === "ExportNamedDeclaration") {
96
- if (n.declaration?.type === "VariableDeclaration") {
97
- const decl = n.declaration.declarations[0];
98
- if (decl && decl.type === "VariableDeclarator" && decl.id.type === "Identifier") {
99
- if (decl.id.name === "Route") {
100
- onExportFound(decl);
101
- }
102
- }
103
- } else if (n.declaration === null && n.specifiers) {
104
- for (const spec of n.specifiers) {
105
- if (typeof spec.exported.name === "string") {
106
- if (spec.exported.name === "Route") {
107
- const variableName = spec.local?.name || spec.exported.name;
108
- for (const decl of program.body) {
109
- if (decl.type === "VariableDeclaration" && decl.declarations[0]) {
110
- const variable = decl.declarations[0];
111
- if (variable.type === "VariableDeclarator" && variable.id.type === "Identifier" && variable.id.name === variableName) {
112
- onExportFound(variable);
113
- break;
114
- }
115
- }
116
- }
117
- }
118
- }
119
- }
120
- }
121
- }
122
- if (routeExportHandled) {
123
- break;
124
- }
125
- }
126
- if (!routeExportHandled) {
127
- return {
128
- result: "no-route-export"
129
- };
130
- }
131
- const imports = {
132
- required: [],
133
- banned: []
134
- };
135
- const targetModule = `@tanstack/${ctx.target}-router`;
136
- if (ctx.verboseFileRoutes === false) {
137
- imports.banned = [
138
- {
139
- source: targetModule,
140
- specifiers: [
141
- { imported: "createLazyFileRoute" },
142
- { imported: "createFileRoute" }
143
- ]
144
- }
145
- ];
146
- } else {
147
- if (ctx.lazy) {
148
- imports.required = [
149
- {
150
- source: targetModule,
151
- specifiers: [{ imported: "createLazyFileRoute" }]
152
- }
153
- ];
154
- imports.banned = [
155
- {
156
- source: targetModule,
157
- specifiers: [{ imported: "createFileRoute" }]
158
- }
159
- ];
160
- } else {
161
- imports.required = [
162
- {
163
- source: targetModule,
164
- specifiers: [{ imported: "createFileRoute" }]
165
- }
166
- ];
167
- imports.banned = [
168
- {
169
- source: targetModule,
170
- specifiers: [{ imported: "createLazyFileRoute" }]
171
- }
172
- ];
173
- }
174
- }
175
- imports.required = mergeImportDeclarations(imports.required);
176
- imports.banned = mergeImportDeclarations(imports.banned);
177
- const importStatementCandidates = [];
178
- const importDeclarationsToRemove = [];
179
- for (const n of program.body) {
180
- const findImport = (opts) => (i) => {
181
- if (i.source === opts.source) {
182
- const importKind = i.importKind || "value";
183
- const expectedImportKind = opts.importKind || "value";
184
- return expectedImportKind === importKind;
185
- }
186
- return false;
187
- };
188
- if (n.type === "ImportDeclaration" && typeof n.source.value === "string") {
189
- const filterImport = findImport({
190
- source: n.source.value,
191
- importKind: n.importKind
192
- });
193
- let requiredImports = imports.required.filter(filterImport)[0];
194
- const bannedImports = imports.banned.filter(filterImport)[0];
195
- if (!requiredImports && !bannedImports) {
196
- continue;
197
- }
198
- const importSpecifiersToRemove = [];
199
- if (n.specifiers) {
200
- for (const spec of n.specifiers) {
201
- if (!requiredImports && !bannedImports) {
202
- break;
203
- }
204
- if (spec.type === "ImportSpecifier" && typeof spec.imported.name === "string") {
205
- if (requiredImports) {
206
- const requiredImportIndex = requiredImports.specifiers.findIndex(
207
- (imp) => imp.imported === spec.imported.name
208
- );
209
- if (requiredImportIndex !== -1) {
210
- requiredImports.specifiers.splice(requiredImportIndex, 1);
211
- if (requiredImports.specifiers.length === 0) {
212
- imports.required = imports.required.splice(
213
- imports.required.indexOf(requiredImports),
214
- 1
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
- originalCode,
324
- transformedCode,
325
- sourceMap,
326
- preferredQuote
327
- }) {
328
- const originalLines = originalCode.split("\n");
329
- const transformedLines = transformedCode.split("\n");
330
- const defaultUsesSemicolons = detectSemicolonUsage(originalCode);
331
- const consumer = await new SourceMapConsumer(sourceMap);
332
- const fixedLines = transformedLines.map((line, i) => {
333
- const transformedLineNum = i + 1;
334
- let origLineText = void 0;
335
- for (let col = 0; col < line.length; col++) {
336
- const mapped = consumer.originalPositionFor({
337
- line: transformedLineNum,
338
- column: col
339
- });
340
- if (mapped.line != null && mapped.line > 0) {
341
- origLineText = originalLines[mapped.line - 1];
342
- break;
343
- }
344
- }
345
- if (origLineText !== void 0) {
346
- if (origLineText === line) {
347
- return origLineText;
348
- }
349
- return fixLine(line, {
350
- originalLine: origLineText,
351
- useOriginalSemicolon: true,
352
- useOriginalQuotes: true,
353
- fallbackQuote: preferredQuote
354
- });
355
- } else {
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
- originalLine,
369
- useOriginalSemicolon,
370
- useOriginalQuotes,
371
- fallbackQuote,
372
- fallbackSemicolon = true
373
- }) {
374
- let result = line;
375
- if (useOriginalQuotes && originalLine) {
376
- result = fixQuotes(result, originalLine, fallbackQuote);
377
- } else if (!useOriginalQuotes && fallbackQuote) {
378
- result = fixQuotesToPreferred(result, fallbackQuote);
379
- }
380
- if (useOriginalSemicolon && originalLine) {
381
- const hadSemicolon = originalLine.trimEnd().endsWith(";");
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
- let originalQuote = detectQuoteFromLine(originalLine);
394
- if (!originalQuote) {
395
- originalQuote = fallbackQuote;
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
- return line.replace(
401
- /(['"`])([^'"`\\]*(?:\\.[^'"`\\]*)*)\1/g,
402
- (_, q, content) => {
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
- const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
410
- return match ? match[1] : null;
275
+ const match = line.match(/(['"`])(?:\\.|[^\\])*?\1/);
276
+ return match ? match[1] : null;
411
277
  }
412
278
  function detectSemicolonUsage(code) {
413
- const lines = code.split("\n").map((l) => l.trim());
414
- const total = lines.length;
415
- const withSemis = lines.filter((l) => l.endsWith(";")).length;
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
- let single = 0;
420
- let double = 0;
421
- visit(ast, {
422
- visitStringLiteral(path) {
423
- if (path.parent.node.type !== "JSXAttribute") {
424
- const raw = path.node.extra?.raw;
425
- if (raw?.startsWith("'")) single++;
426
- else if (raw?.startsWith('"')) double++;
427
- }
428
- return false;
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
- export {
437
- detectPreferredQuoteStyle,
438
- transform
439
- };
440
- //# sourceMappingURL=transform.js.map
297
+ //#endregion
298
+ export { transform };
299
+
300
+ //# sourceMappingURL=transform.js.map