Skip to content

Commit fa1503f

Browse files
committed
ast fix
1 parent e4c1852 commit fa1503f

File tree

4 files changed

+53
-14
lines changed

4 files changed

+53
-14
lines changed

src/vite/utils/data-functions-augment.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,24 @@ describe("transform", () => {
675675
`)
676676
expect(removeWhitespace(result.code)).toStrictEqual(expected)
677677
})
678+
it("should transform the client action export when it's imported from another file and exported and keep the export around if more things are exported", () => {
679+
const result = augmentDataFetchingFunctions(
680+
`
681+
import { withClientActionContextWrapper as _withClientActionContextWrapper } from "react-router-devtools/context";
682+
import { clientAction as _clientAction } from "./client-action.js";
683+
export const clientAction = _withClientActionContextWrapper(_clientAction, "test");
684+
`,
685+
"test",
686+
"/file/path"
687+
)
688+
const expected = removeWhitespace(`
689+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
690+
import { withClientActionContextWrapper as _withClientActionContextWrapper } from "react-router-devtools/context";
691+
import { clientAction as _clientAction } from "./client-action.js";
692+
export const clientAction = _withClientActionWrapper(_withClientActionContextWrapper(_clientAction, "test"), "test");
693+
`)
694+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
695+
})
678696

679697
it("should wrap the clientAction export when it's exported via export { clientAction } and declared within the file", () => {
680698
const result = augmentDataFetchingFunctions(

src/vite/utils/data-functions-augment.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
4747
if (!ALL_EXPORTS.includes(name)) {
4848
continue
4949
}
50-
const uniqueName = path.scope.generateUidIdentifier(name)
50+
const isReimported = specifier.local.name !== name
51+
const uniqueName = isReimported ? specifier.local : path.scope.generateUidIdentifier(name)
5152
imports.push([name, uniqueName])
5253
specifier.local = uniqueName
5354
// Replace the import specifier with a new one
54-
path.scope.rename(name, uniqueName.name)
55+
if (!isReimported) {
56+
path.scope.rename(name, uniqueName.name)
57+
}
5558
}
5659
},
5760
ExportDeclaration(path) {

src/vite/utils/inject-context.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,25 @@ describe("transform", () => {
584584
`)
585585
expect(removeWhitespace(result.code)).toStrictEqual(expected)
586586
})
587+
it("should transform the client action export when it's imported from another file and exported and already transformed", () => {
588+
const result = injectContext(
589+
`
590+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
591+
import { clientAction as _clientAction } from "./client-action.js";
592+
593+
export const clientAction = _withClientActionWrapper(_clientAction, "test");
594+
`,
595+
"test",
596+
"/file/path"
597+
)
598+
const expected = removeWhitespace(`
599+
import { withClientActionContextWrapper as _withClientActionContextWrapper } from "react-router-devtools/context";
600+
import { withClientActionWrapper as _withClientActionWrapper } from "react-router-devtools/client";
601+
import { clientAction as _clientAction } from "./client-action.js";
602+
export const clientAction = _withClientActionContextWrapper(_withClientActionWrapper(_clientAction, "test"),"test");
603+
`)
604+
expect(removeWhitespace(result.code)).toStrictEqual(expected)
605+
})
587606

588607
it("should transform the clientAction export when it's re-exported from another file and remove empty export declaration", () => {
589608
const result = injectContext(

src/vite/utils/inject-context.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,14 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
3737
if (!ALL_EXPORTS.includes(name)) {
3838
continue
3939
}
40-
const uniqueName = path.scope.generateUidIdentifier(name)
40+
const isReimported = specifier.local.name !== name
41+
const uniqueName = isReimported ? specifier.local : path.scope.generateUidIdentifier(name)
4142
imports.push([name, uniqueName])
4243
specifier.local = uniqueName
4344
// Replace the import specifier with a new one
44-
path.scope.rename(name, uniqueName.name)
45+
if (!isReimported) {
46+
path.scope.rename(name, uniqueName.name)
47+
}
4548
}
4649
},
4750
ExportDeclaration(path) {
@@ -64,7 +67,7 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
6467

6568
return
6669
}
67-
70+
// not this
6871
if (decl.isFunctionDeclaration()) {
6972
const { id } = decl.node
7073
if (!id) return
@@ -100,21 +103,16 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
100103
if (path.node.source) {
101104
// Special condition: export { loader, action } from "./path"
102105
const source = path.node.source.value
103-
106+
const unique = path.scope.generateUidIdentifier(name)
104107
importDeclarations.push(
105-
t.importDeclaration(
106-
[t.importSpecifier(t.identifier(`_${name}`), t.identifier(name))],
107-
t.stringLiteral(source)
108-
)
108+
t.importDeclaration([t.importSpecifier(unique, t.identifier(name))], t.stringLiteral(source))
109109
)
110+
110111
transformations.push(() => {
111112
path.insertBefore(
112113
t.exportNamedDeclaration(
113114
t.variableDeclaration("const", [
114-
t.variableDeclarator(
115-
t.identifier(name),
116-
t.callExpression(uid, [t.identifier(`_${name}`), t.stringLiteral(routeId)])
117-
),
115+
t.variableDeclarator(t.identifier(name), t.callExpression(uid, [unique, t.stringLiteral(routeId)])),
118116
])
119117
)
120118
)
@@ -150,6 +148,7 @@ const transform = (ast: ParseResult<Babel.File>, routeId: string) => {
150148
init.replaceWith(t.callExpression(uid, [init.node, t.stringLiteral(routeId)]))
151149
}
152150
} else {
151+
// not this
153152
transformations.push(() => {
154153
const existingImport = imports.find(([existingName]) => existingName === name)
155154
const uniqueName = existingImport?.[1].name ?? path.scope.generateUidIdentifier(name).name

0 commit comments

Comments
 (0)