Add failing test for DropMemoCalls failing to DCE dep array
Found this while running Forget on the React tests. This isn't a high priority because the ESLint plugin would've caught this. But it'd be nice if either our validation rules caught this or if our compiler did correctly eliminate the dep array.
Sathya Gunasekaran committed
Jun 20, 2023 at 14:58 UTC
0897cc8a0f51afb2576326bb2a91af15182e7c40
2 files changed
+50
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.useMemo-deps-array-not-cleared.expect.md
new
+41
@@ -0,0 +1,41 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function App({text, hasDeps}) {
6
+ const resolvedText = useMemo(
7
+ () => {
8
+ return text.toUpperCase();
9
+ },
10
+ hasDeps ? null : [text], // should be DCE'd
11
+ );
12
+ return resolvedText;
13
+}
14
+
15
+```
16
+
17
+## Code
18
+
19
+```javascript
20
+import { unstable_useMemoCache as useMemoCache } from "react";
21
+function App(t26) {
22
+ const $ = useMemoCache(2);
23
+ const { text, hasDeps } = t26;
24
+
25
+ hasDeps ? null : [text];
26
+ const c_0 = $[0] !== text;
27
+ let t0;
28
+ if (c_0) {
29
+ t0 = text.toUpperCase();
30
+ $[0] = text;
31
+ $[1] = t0;
32
+ } else {
33
+ t0 = $[1];
34
+ }
35
+ const t19 = t0;
36
+ const resolvedText = t19;
37
+ return resolvedText;
38
+}
39
+
40
+```
41
+
\ No newline at end of file
compiler/forget/packages/babel-plugin-react-forget/src/__tests__/fixtures/compiler/bug.useMemo-deps-array-not-cleared.js
new
+9
@@ -0,0 +1,9 @@
1
+function App({ text, hasDeps }) {
2
+ const resolvedText = useMemo(
3
+ () => {
4
+ return text.toUpperCase();
5
+ },
6
+ hasDeps ? null : [text] // should be DCE'd
7
+ );
8
+ return resolvedText;
9
+}