@samitouri / QOS-React-1 / commits / 36f979d9d4

[patch] Repro and fix for duplicate import statements

Mofei Zhang committed Sep 19, 2023 at 18:34 UTC 36f979d9d46ffab8b7b77a9f57dd82928041e7e1
3 files changed +65 -1
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts
+6 -1
@@ -109,7 +109,12 @@ export function updateExistingReactImportDeclaration(
109 let didInsertUseMemoCache = false;
110 program.traverse({
111 ImportDeclaration(importDeclPath) {
112 - if (isNonNamespacedImportOfReact(importDeclPath)) {
112 + if (
113 + !didInsertUseMemoCache &&
114 + importDeclPath.node.importKind !== "type" &&
115 + importDeclPath.node.importKind !== "typeof" &&
116 + isNonNamespacedImportOfReact(importDeclPath)
117 + ) {
118 importDeclPath.pushContainer(
119 "specifiers",
120 t.importSpecifier(
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.expect.md new
+47
@@ -0,0 +1,47 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +import type { SetStateAction, Dispatch } from "react";
6 +import { useState } from "react";
7 +
8 +function Component(_props: {}) {
9 + const [x, _setX]: [number, Dispatch<SetStateAction<number>>] = useState(0);
10 + return { x };
11 +}
12 +
13 +export const FIXTURE_ENTRYPOINT = {
14 + fn: Component,
15 + params: [{}],
16 +};
17 +
18 +```
19 +
20 +## Code
21 +
22 +```javascript
23 +import type { SetStateAction, Dispatch } from "react";
24 +import { useState, unstable_useMemoCache as useMemoCache } from "react";
25 +
26 +function Component(_props) {
27 + const $ = useMemoCache(2);
28 + const [x] = useState(0);
29 + const c_0 = $[0] !== x;
30 + let t0;
31 + if (c_0) {
32 + t0 = { x };
33 + $[0] = x;
34 + $[1] = t0;
35 + } else {
36 + t0 = $[1];
37 + }
38 + return t0;
39 +}
40 +
41 +export const FIXTURE_ENTRYPOINT = {
42 + fn: Component,
43 + params: [{}],
44 +};
45 +
46 +```
47 +
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-import-specifier.ts new
+12
@@ -0,0 +1,12 @@
1 +import type { SetStateAction, Dispatch } from "react";
2 +import { useState } from "react";
3 +
4 +function Component(_props: {}) {
5 + const [x, _setX]: [number, Dispatch<SetStateAction<number>>] = useState(0);
6 + return { x };
7 +}
8 +
9 +export const FIXTURE_ENTRYPOINT = {
10 + fn: Component,
11 + params: [{}],
12 +};