diff --git a/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.input.js b/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.input.js new file mode 100644 index 000000000..07cb6f215 --- /dev/null +++ b/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.input.js @@ -0,0 +1,3 @@ +import { act as reactAct } from "react-dom/test-utils"; + +reactAct(); diff --git a/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.output.js b/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.output.js new file mode 100644 index 000000000..94c608bc6 --- /dev/null +++ b/packages/codemods/react/19/replace-act-import/__testfixtures__/aliased-import.output.js @@ -0,0 +1,3 @@ +import { act as reactAct } from "react"; + +reactAct(); diff --git a/packages/codemods/react/19/replace-act-import/src/index.ts b/packages/codemods/react/19/replace-act-import/src/index.ts index 410f0623d..1136b8e95 100644 --- a/packages/codemods/react/19/replace-act-import/src/index.ts +++ b/packages/codemods/react/19/replace-act-import/src/index.ts @@ -94,10 +94,12 @@ export default function transform( specifiers: [{ type: "ImportSpecifier", imported: { name: "act" } }], }) .forEach((path) => { - const newImportSpecifier = j.importSpecifier( - j.identifier("act"), - j.identifier("act"), - ); + const actSpecifiers = + path.node.specifiers?.filter( + (specifier) => + j.ImportSpecifier.check(specifier) && + specifier.imported.name === "act", + ) ?? []; const existingReactImportCollection = root.find(j.ImportDeclaration, { source: { value: "react" }, @@ -111,13 +113,13 @@ export default function transform( existingReactImportCollection .paths() .at(0) - ?.node.specifiers?.push(newImportSpecifier); + ?.node.specifiers?.push(...actSpecifiers); path.prune(); isDirty = true; } else { const newImportDeclaration = j.importDeclaration( - [newImportSpecifier], + actSpecifiers, j.literal("react"), ); diff --git a/packages/codemods/react/19/replace-act-import/test/test.ts b/packages/codemods/react/19/replace-act-import/test/test.ts index e618722c5..88e37d613 100644 --- a/packages/codemods/react/19/replace-act-import/test/test.ts +++ b/packages/codemods/react/19/replace-act-import/test/test.ts @@ -215,4 +215,29 @@ describe("react/19/replace-act-import: TestUtils.act -> React.act", () => { OUTPUT.replace(/\W/gm, ""), ); }); + + it("should preserve aliased import specifier name", async () => { + const INPUT = await readFile( + join(__dirname, "..", "__testfixtures__/aliased-import.input.js"), + "utf-8", + ); + const OUTPUT = await readFile( + join(__dirname, "..", "__testfixtures__/aliased-import.output.js"), + "utf-8", + ); + + const fileInfo: FileInfo = { + path: "index.ts", + source: INPUT, + }; + + const actualOutput = transform(fileInfo, buildApi("js"), { + quote: "single", + }); + + assert.deepEqual( + actualOutput?.replace(/\W/gm, ""), + OUTPUT.replace(/\W/gm, ""), + ); + }); });