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