@samitouri / QOS-React-2 / commits / 123d024af7

[λ] Add test for function expr passed to jsx

Function exprs become frozen when captured by jsx.

Sathya Gunasekaran committed Jan 20, 2023 at 19:44 UTC 123d024af7827c3de1df0d3ab3ae3c58fa84e461
2 files changed +81
compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.expect.md new
+70
@@ -0,0 +1,70 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function component(a, b) {
6 + let y = { b };
7 + let z = { a };
8 + let x = function () {
9 + z.a = 2;
10 + y.b;
11 + };
12 + let t = <Foo x={x}></Foo>;
13 + mutate(x); // x should be frozen here
14 + return t;
15 +}
16 +
17 +```
18 +
19 +## Code
20 +
21 +```javascript
22 +function component(a, b) {
23 + const $ = React.useMemoCache();
24 + const c_0 = $[0] !== b;
25 + let y;
26 + if (c_0) {
27 + y = { b: b };
28 + $[0] = b;
29 + $[1] = y;
30 + } else {
31 + y = $[1];
32 + }
33 + const c_2 = $[2] !== a;
34 + let z;
35 + if (c_2) {
36 + z = { a: a };
37 + $[2] = a;
38 + $[3] = z;
39 + } else {
40 + z = $[3];
41 + }
42 + const c_4 = $[4] !== z.a;
43 + const c_5 = $[5] !== y.b;
44 + let x;
45 + if (c_4 || c_5) {
46 + x = function () {
47 + z.a = 2;
48 + y.b;
49 + };
50 + $[4] = z.a;
51 + $[5] = y.b;
52 + $[6] = x;
53 + } else {
54 + x = $[6];
55 + }
56 + const c_7 = $[7] !== x;
57 + let t;
58 + if (c_7) {
59 + t = <Foo x={x}></Foo>;
60 + $[7] = x;
61 + $[8] = t;
62 + } else {
63 + t = $[8];
64 + }
65 + mutate(x);
66 + return t;
67 +}
68 +
69 +```
70 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.js new
+11
@@ -0,0 +1,11 @@
1 +function component(a, b) {
2 + let y = { b };
3 + let z = { a };
4 + let x = function () {
5 + z.a = 2;
6 + y.b;
7 + };
8 + let t = <Foo x={x}></Foo>;
9 + mutate(x); // x should be frozen here
10 + return t;
11 +}