[patch] Repro and fix for duplicate import statements

This commit is contained in:
Mofei Zhang
2023-09-19 18:34:14 -04:00
parent 2fa04d128e
commit 36f979d9d4
3 changed files with 65 additions and 1 deletions
@@ -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(
@@ -0,0 +1,47 @@
## Input
```javascript
import type { SetStateAction, Dispatch } from "react";
import { useState } from "react";
function Component(_props: {}) {
const [x, _setX]: [number, Dispatch<SetStateAction<number>>] = 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: [{}],
};
```
@@ -0,0 +1,12 @@
import type { SetStateAction, Dispatch } from "react";
import { useState } from "react";
function Component(_props: {}) {
const [x, _setX]: [number, Dispatch<SetStateAction<number>>] = useState(0);
return { x };
}
export const FIXTURE_ENTRYPOINT = {
fn: Component,
params: [{}],
};