diff --git a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts index 0fca72f1f9..04f5ed118e 100644 --- a/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts +++ b/compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts @@ -109,7 +109,12 @@ export function updateExistingReactImportDeclaration( let didInsertUseMemoCache = false; program.traverse({ ImportDeclaration(importDeclPath) { - if (isNonNamespacedImportOfReact(importDeclPath)) { + if ( + !didInsertUseMemoCache && + importDeclPath.node.importKind !== "type" && + importDeclPath.node.importKind !== "typeof" && + isNonNamespacedImportOfReact(importDeclPath) + ) { importDeclPath.pushContainer( "specifiers", t.importSpecifier( diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.expect.md b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.expect.md new file mode 100644 index 0000000000..3103f0bcb4 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.expect.md @@ -0,0 +1,47 @@ + +## Input + +```javascript +import type { SetStateAction, Dispatch } from "react"; +import { useState } from "react"; + +function Component(_props: {}) { + const [x, _setX]: [number, Dispatch>] = useState(0); + return { x }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + +## Code + +```javascript +import type { SetStateAction, Dispatch } from "react"; +import { useState, unstable_useMemoCache as useMemoCache } from "react"; + +function Component(_props) { + const $ = useMemoCache(2); + const [x] = useState(0); + const c_0 = $[0] !== x; + let t0; + if (c_0) { + t0 = { x }; + $[0] = x; + $[1] = t0; + } else { + t0 = $[1]; + } + return t0; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +}; + +``` + \ No newline at end of file diff --git a/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.ts b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.ts new file mode 100644 index 0000000000..14878aa822 --- /dev/null +++ b/compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.ts @@ -0,0 +1,12 @@ +import type { SetStateAction, Dispatch } from "react"; +import { useState } from "react"; + +function Component(_props: {}) { + const [x, _setX]: [number, Dispatch>] = useState(0); + return { x }; +} + +export const FIXTURE_ENTRYPOINT = { + fn: Component, + params: [{}], +};