[patch] Patch import specifiers check for react
--- Patch for original patch of injecting `useMemoCache` logic 😅 This is failing on a good number React components internally (not in our current experiments)
Mofei Zhang committed
Oct 6, 2023 at 17:38 UTC
392d0b127b1fec2a235954f22ca828769c0413ce
3 files changed
+55
-3
compiler/packages/babel-plugin-react-forget/src/Entrypoint/Imports.ts
+3
-3
@@ -63,7 +63,9 @@ export function isNonNamespacedImportOfReact(
63
importDeclPath.get("source").node.value === "react" &&
64
importDeclPath
65
.get("specifiers")
66
- .every((specifier) => specifier.isImportSpecifier())
66
+ .every((specifier) => specifier.isImportSpecifier()) &&
67
+ importDeclPath.node.importKind !== "type" &&
68
+ importDeclPath.node.importKind !== "typeof"
69
);
70
}
71
@@ -111,8 +113,6 @@ export function updateExistingReactImportDeclaration(
113
ImportDeclaration(importDeclPath) {
114
if (
115
!didInsertUseMemoCache &&
114
- importDeclPath.node.importKind !== "type" &&
115
- importDeclPath.node.importKind !== "typeof" &&
116
isNonNamespacedImportOfReact(importDeclPath)
117
) {
118
importDeclPath.pushContainer(
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-type-import.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+import type { ReactElement } from "react";
6
+
7
+function Component(_props: {}): ReactElement {
8
+ return <div>hello world</div>;
9
+}
10
+
11
+export const FIXTURE_ENTRYPOINT = {
12
+ fn: Component,
13
+ params: [{}],
14
+};
15
+
16
+```
17
+
18
+## Code
19
+
20
+```javascript
21
+import { unstable_useMemoCache as useMemoCache } from "react";
22
+import type { ReactElement } from "react";
23
+
24
+function Component(_props) {
25
+ const $ = useMemoCache(1);
26
+ let t0;
27
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
28
+ t0 = <div>hello world</div>;
29
+ $[0] = t0;
30
+ } else {
31
+ t0 = $[0];
32
+ }
33
+ return t0;
34
+}
35
+
36
+export const FIXTURE_ENTRYPOINT = {
37
+ fn: Component,
38
+ params: [{}],
39
+};
40
+
41
+```
42
+
\ No newline at end of file
compiler/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/repro-duplicate-type-import.tsx
new
+10
@@ -0,0 +1,10 @@
1
+import type { ReactElement } from "react";
2
+
3
+function Component(_props: {}): ReactElement {
4
+ return <div>hello world</div>;
5
+}
6
+
7
+export const FIXTURE_ENTRYPOINT = {
8
+ fn: Component,
9
+ params: [{}],
10
+};