@samitouri / QOS-React-2 / commits / 986c95bc09

[hir] Add test case for incorrect memoisation of mutated objects

In this case, x should not be cached within the if or else blocks as it is mutated later.

Sathya Gunasekaran committed Jan 11, 2023 at 15:17 UTC 986c95bc09189f81f5a6a324f36195fe9bafcf47
2 files changed +76
compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.expect.md new
+65
@@ -0,0 +1,65 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function foo(a, b, c, d) {
6 + let x = someObj();
7 + if (a) {
8 + x = someObj();
9 + } else {
10 + x = someObj();
11 + }
12 +
13 + x.f = 1;
14 + return x;
15 +}
16 +
17 +```
18 +
19 +## Code
20 +
21 +```javascript
22 +function foo(a, b, c, d) {
23 + const $ = React.useMemoCache();
24 + const x = someObj();
25 + const c_0 = $[0] !== a;
26 + let x$0;
27 + if (c_0) {
28 + x$0 = undefined;
29 +
30 + if (a) {
31 + let x$1;
32 +
33 + if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
34 + x$1 = someObj();
35 + $[2] = x$1;
36 + } else {
37 + x$1 = $[2];
38 + }
39 +
40 + x$0 = x$1;
41 + } else {
42 + let x$2;
43 +
44 + if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
45 + x$2 = someObj();
46 + $[3] = x$2;
47 + } else {
48 + x$2 = $[3];
49 + }
50 +
51 + x$0 = x$2;
52 + }
53 +
54 + x$0.f = 1;
55 + $[0] = a;
56 + $[1] = x$0;
57 + } else {
58 + x$0 = $[1];
59 + }
60 +
61 + return x$0;
62 +}
63 +
64 +```
65 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/obj-mutated-after-if-else.js new
+11
@@ -0,0 +1,11 @@
1 +function foo(a, b, c, d) {
2 + let x = someObj();
3 + if (a) {
4 + x = someObj();
5 + } else {
6 + x = someObj();
7 + }
8 +
9 + x.f = 1;
10 + return x;
11 +}