diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.expect.md b/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.expect.md deleted file mode 100644 index c88db2a210..0000000000 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.expect.md +++ /dev/null @@ -1,21 +0,0 @@ - -## Input - -```javascript -function g(a) { - a.b.c = a.b.c + 1; - a.b.c *= 2; -} - -``` - -## Code - -```javascript -function g(a) { - a.b.c = a.b.c + 1; - a.b.c = a.b.c * 2; -} - -``` - \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.js b/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.js deleted file mode 100644 index b40ff31a2f..0000000000 --- a/compiler/forget/src/__tests__/fixtures/hir/_bug_invalid-scope.js +++ /dev/null @@ -1,4 +0,0 @@ -function g(a) { - a.b.c = a.b.c + 1; - a.b.c *= 2; -} diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md index c63453d5f4..5c18c548c3 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.expect.md @@ -9,6 +9,7 @@ function component() { let x = {}; x.y = y; mutate(x.y.z); + return x; } ``` @@ -17,14 +18,22 @@ function component() { ```javascript function component() { - const z = []; - const y = {}; - y.z = z; + const $ = React.useMemoCache(); + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + const z = []; + const y = {}; + y.z = z; - const x = {}; - x.y = y; + x = {}; + x.y = y; - mutate(x.y.z); + mutate(x.y.z); + $[0] = x; + } else { + x = $[0]; + } + return x; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.js b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.js index d7cc5710b2..fb35630038 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.js +++ b/compiler/forget/src/__tests__/fixtures/hir/alias-nested-member-path-mutate.js @@ -5,4 +5,5 @@ function component() { let x = {}; x.y = y; mutate(x.y.z); + return x; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.expect.md index 00e6cb24b4..f44bcd5636 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.expect.md @@ -3,8 +3,12 @@ ```javascript function foo(a, b, c) { - a[0] = b[1]; - c[0][0] = a[0]; + const x = [a]; + const y = [null, b]; + const z = [[], [], [c]]; + x[0] = y[1]; + z[0][0] = x[0]; + return [x, z]; } ``` @@ -13,8 +17,47 @@ function foo(a, b, c) { ```javascript function foo(a, b, c) { - a[0] = b[1]; - c[0][0] = a[0]; + const $ = React.useMemoCache(); + const c_0 = $[0] !== a; + const c_1 = $[1] !== b; + const c_2 = $[2] !== c; + let x; + let z; + if (c_0 || c_1 || c_2) { + x = [a]; + const c_5 = $[5] !== b; + let y; + if (c_5) { + y = [null, b]; + $[5] = b; + $[6] = y; + } else { + y = $[6]; + } + z = [[], [], [c]]; + x[0] = y[1]; + z[0][0] = x[0]; + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + $[4] = z; + } else { + x = $[3]; + z = $[4]; + } + const c_7 = $[7] !== x; + const c_8 = $[8] !== z; + let t0; + if (c_7 || c_8) { + t0 = [x, z]; + $[7] = x; + $[8] = z; + $[9] = t0; + } else { + t0 = $[9]; + } + return t0; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.js b/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.js index 3871b2fccc..10e52dd8f0 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.js +++ b/compiler/forget/src/__tests__/fixtures/hir/array-access-assignment.js @@ -1,4 +1,8 @@ function foo(a, b, c) { - a[0] = b[1]; - c[0][0] = a[0]; + const x = [a]; + const y = [null, b]; + const z = [[], [], [c]]; + x[0] = y[1]; + z[0][0] = x[0]; + return [x, z]; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.expect.md new file mode 100644 index 0000000000..db5ea9f733 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.expect.md @@ -0,0 +1,34 @@ + +## Input + +```javascript +function g(props) { + const a = { b: { c: props.c } }; + a.b.c = a.b.c + 1; + a.b.c *= 2; + return a; +} + +``` + +## Code + +```javascript +function g(props) { + const $ = React.useMemoCache(); + const c_0 = $[0] !== props.c; + let a; + if (c_0) { + a = { b: { c: props.c } }; + a.b.c = a.b.c + 1; + a.b.c = a.b.c * 2; + $[0] = props.c; + $[1] = a; + } else { + a = $[1]; + } + return a; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.js b/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.js new file mode 100644 index 0000000000..7a6a304185 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-expression-nested-path.js @@ -0,0 +1,6 @@ +function g(props) { + const a = { b: { c: props.c } }; + a.b.c = a.b.c + 1; + a.b.c *= 2; + return a; +} diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md index edb9c4e0fa..15f9566526 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.expect.md @@ -7,6 +7,7 @@ function f() { x = x + 1; x += 1; x >>>= 1; + return x; } ``` @@ -14,7 +15,11 @@ function f() { ## Code ```javascript -function f() {} +function f() { + const x = 3; + const x$0 = x >>> 1; + return x$0; +} ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.js b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.js index 2815565959..af39d33a9b 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.js +++ b/compiler/forget/src/__tests__/fixtures/hir/assignment-variations.js @@ -3,4 +3,5 @@ function f() { x = x + 1; x += 1; x >>>= 1; + return x; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md b/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md index 5eb07aca89..2b52261931 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/complex-while.expect.md @@ -6,10 +6,11 @@ function foo(a, b, c) { label: if (a) { while (b) { if (c) { - break; + break label; } } } + return c; } ``` @@ -25,6 +26,7 @@ function foo(a, b, c) { } } } + return c; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/complex-while.js b/compiler/forget/src/__tests__/fixtures/hir/complex-while.js index 0ecf98c545..50a77acc49 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/complex-while.js +++ b/compiler/forget/src/__tests__/fixtures/hir/complex-while.js @@ -2,8 +2,9 @@ function foo(a, b, c) { label: if (a) { while (b) { if (c) { - break; + break label; } } } + return c; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/dce-loop.expect.md b/compiler/forget/src/__tests__/fixtures/hir/dce-loop.expect.md index 9377023a0b..c2b7845a28 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dce-loop.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/dce-loop.expect.md @@ -9,6 +9,7 @@ function foo(props) { x++; y++; } + return y; } ``` @@ -17,10 +18,20 @@ function foo(props) { ```javascript function foo(props) { - let y = 0; - while (y < props.max) { - y = y + 1; + const $ = React.useMemoCache(); + const c_0 = $[0] !== props.max; + let y; + if (c_0) { + y = 0; + while (y < props.max) { + y = y + 1; + } + $[0] = props.max; + $[1] = y; + } else { + y = $[1]; } + return y; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/dce-loop.js b/compiler/forget/src/__tests__/fixtures/hir/dce-loop.js index 1ce81fe2c6..6e43595e4d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dce-loop.js +++ b/compiler/forget/src/__tests__/fixtures/hir/dce-loop.js @@ -5,4 +5,5 @@ function foo(props) { x++; y++; } + return y; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md index d95167c0e7..baf81214a5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.expect.md @@ -14,6 +14,7 @@ function foo(a, b) { if (b) { y.push(b); } + return y; } ``` @@ -33,14 +34,24 @@ function foo(a, b) { } else { x = $[1]; } - - const y = []; - if (x.length) { - y.push(x); - } - if (b) { - y.push(b); + const c_2 = $[2] !== x; + const c_3 = $[3] !== b; + let y; + if (c_2 || c_3) { + y = []; + if (x.length) { + y.push(x); + } + if (b) { + y.push(b); + } + $[2] = x; + $[3] = b; + $[4] = y; + } else { + y = $[4]; } + return y; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.js b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.js index f86b180155..a5e6152c8a 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.js +++ b/compiler/forget/src/__tests__/fixtures/hir/dependencies-outputs.js @@ -10,4 +10,5 @@ function foo(a, b) { if (b) { y.push(b); } + return y; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.expect.md b/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.expect.md index b49d9d73cb..a27a8b2c74 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.expect.md @@ -18,6 +18,7 @@ function foo(a, b, c) { }, o, } = b); + return { e, g, n, o }; } ``` @@ -25,7 +26,28 @@ function foo(a, b, c) { ## Code ```javascript -function foo(a, b, c) {} +function foo(a, b, c) { + const $ = React.useMemoCache(); + + const g = a[1][0].e.f; + + const n = b.l.m[0][0]; + const o = b.o; + const c_0 = $[0] !== g; + const c_1 = $[1] !== n; + const c_2 = $[2] !== o; + let t0; + if (c_0 || c_1 || c_2) { + t0 = { e: e, g: g, n: n, o: o }; + $[0] = g; + $[1] = n; + $[2] = o; + $[3] = t0; + } else { + t0 = $[3]; + } + return t0; +} ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.js b/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.js index a228348a64..1fc546fdd5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.js +++ b/compiler/forget/src/__tests__/fixtures/hir/destructuring-assignment.js @@ -14,4 +14,5 @@ function foo(a, b, c) { }, o, } = b); + return { e, g, n, o }; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructuring.expect.md b/compiler/forget/src/__tests__/fixtures/hir/destructuring.expect.md index d153e036f2..1158f59d65 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/destructuring.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/destructuring.expect.md @@ -17,6 +17,7 @@ function foo(a, b, c) { }, o, } = b; + return [d, f, n, o]; } ``` @@ -24,7 +25,30 @@ function foo(a, b, c) { ## Code ```javascript -function foo(a, b, c) {} +function foo(a, b, c) { + const $ = React.useMemoCache(); + const d = a[0]; + const f = a[1][0].e.f; + + const n = b.l.m[0][0]; + const o = b.o; + const c_0 = $[0] !== d; + const c_1 = $[1] !== f; + const c_2 = $[2] !== n; + const c_3 = $[3] !== o; + let t0; + if (c_0 || c_1 || c_2 || c_3) { + t0 = [d, f, n, o]; + $[0] = d; + $[1] = f; + $[2] = n; + $[3] = o; + $[4] = t0; + } else { + t0 = $[4]; + } + return t0; +} ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/destructuring.js b/compiler/forget/src/__tests__/fixtures/hir/destructuring.js index 72ffef9759..d75719b816 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/destructuring.js +++ b/compiler/forget/src/__tests__/fixtures/hir/destructuring.js @@ -13,4 +13,5 @@ function foo(a, b, c) { }, o, } = b; + return [d, f, n, o]; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md index a65a60c09e..b5bd7c275b 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.expect.md @@ -11,8 +11,8 @@ function foo(a, b, c) { } } } - if (a.length) { - return a; + if (x.length) { + return x; } return null; } @@ -23,16 +23,29 @@ function foo(a, b, c) { ```javascript function foo(a, b, c) { - const x = []; - if (a) { - if (b) { - if (c) { - x.push(0); + const $ = React.useMemoCache(); + const c_0 = $[0] !== a; + const c_1 = $[1] !== b; + const c_2 = $[2] !== c; + let x; + if (c_0 || c_1 || c_2) { + x = []; + if (a) { + if (b) { + if (c) { + x.push(0); + } } } + $[0] = a; + $[1] = b; + $[2] = c; + $[3] = x; + } else { + x = $[3]; } - if (a.length) { - return a; + if (x.length) { + return x; } return null; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.js b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.js index ec028378a1..fd3b253585 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.js +++ b/compiler/forget/src/__tests__/fixtures/hir/extend-scopes-if.js @@ -7,8 +7,8 @@ function foo(a, b, c) { } } } - if (a.length) { - return a; + if (x.length) { + return x; } return null; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md index b282163c2e..46379b12f1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.expect.md @@ -7,6 +7,7 @@ function Component() { const b = a; useFreeze(a); foo(b); // should be readonly, value is guaranteed frozen via alias + return b; } function useFreeze() {} @@ -29,6 +30,7 @@ function Component() { const b = a; useFreeze(a); foo(b); + return b; } function useFreeze() {} diff --git a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.js b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.js index 0f240a4205..852504b81d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.js +++ b/compiler/forget/src/__tests__/fixtures/hir/frozen-after-alias.js @@ -3,6 +3,7 @@ function Component() { const b = a; useFreeze(a); foo(b); // should be readonly, value is guaranteed frozen via alias + return b; } function useFreeze() {} diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md index 2c34709bfc..df11da8dcd 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.expect.md @@ -8,7 +8,8 @@ function foo(a, b, c) { if (c) { x = c; } - x; + // TODO: move the return to the end of the function + return x; } } @@ -18,9 +19,23 @@ function foo(a, b, c) { ```javascript function foo(a, b, c) { + const $ = React.useMemoCache(); + const x = a; if (b) { - if (c) { + const c_0 = $[0] !== c; + let x$0; + if (c_0) { + x$0 = x; + if (c) { + const x$1 = c; + x$0 = x$1; + } + $[0] = c; + $[1] = x$0; + } else { + x$0 = $[1]; } + return x$0; } } diff --git a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.js b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.js index 9118aa4cc2..399b4b7fc4 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.js +++ b/compiler/forget/src/__tests__/fixtures/hir/ssa-nested-partial-phi.js @@ -4,6 +4,7 @@ function foo(a, b, c) { if (c) { x = c; } - x; + // TODO: move the return to the end of the function + return x; } } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-field-load.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-field-load.expect.md index 44cdc18891..f5fa3a7b5a 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-field-load.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-field-load.expect.md @@ -5,6 +5,7 @@ function component() { let x = { t: 1 }; let p = x.t; + return p; } ``` @@ -12,7 +13,18 @@ function component() { ## Code ```javascript -function component() {} +function component() { + const $ = React.useMemoCache(); + let x; + if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + x = { t: 1 }; + $[0] = x; + } else { + x = $[0]; + } + const p = x.t; + return p; +} ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-field-load.js b/compiler/forget/src/__tests__/fixtures/hir/type-field-load.js index 5760193121..673f8846fb 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-field-load.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-field-load.js @@ -1,4 +1,5 @@ function component() { let x = { t: 1 }; let p = x.t; + return p; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.expect.md index bdf321a918..1c6354d7dd 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.expect.md @@ -11,6 +11,7 @@ function component() { let y = x.u; let z = x.v; + return z; } ``` @@ -45,6 +46,9 @@ function component() { const v = x.v; if (u > v) { } + + const z = x.v; + return z; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.js b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.js index 048ad13086..eb8384313c 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-load-binary-op.js @@ -7,4 +7,5 @@ function component() { let y = x.u; let z = x.v; + return z; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.expect.md index 1eea2cb83f..70854ce4a1 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.expect.md @@ -7,6 +7,7 @@ function component() { let q = {}; x.t = q; let z = x.t; + return z; } ``` @@ -16,15 +17,24 @@ function component() { ```javascript function component() { const $ = React.useMemoCache(); - const x = {}; - let q; + let x; if ($[0] === Symbol.for("react.memo_cache_sentinel")) { - q = {}; - $[0] = q; + x = {}; + let q; + if ($[1] === Symbol.for("react.memo_cache_sentinel")) { + q = {}; + $[1] = q; + } else { + q = $[1]; + } + x.t = q; + $[0] = x; } else { - q = $[0]; + x = $[0]; } - x.t = q; + + const z = x.t; + return z; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.js b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.js index 5fd06961dd..f406104c41 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-field-store.js @@ -3,4 +3,5 @@ function component() { let q = {}; x.t = q; let z = x.t; + return z; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md index af57a0b5f8..5e2703a7bc 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.expect.md @@ -14,6 +14,7 @@ function component() { x.t = o; // generalize x.t let y = x.t; + return y; } ``` @@ -37,10 +38,18 @@ function component() { } else { o = $[1]; } + let x; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + x = {}; + x.t = p; + x.t = o; + $[2] = x; + } else { + x = $[2]; + } - const x = {}; - x.t = p; - x.t = o; + const y = x.t; + return y; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.js b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.js index c7ce6bc9b8..3246ea5693 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-polymorphic.js @@ -10,4 +10,5 @@ function component() { x.t = o; // generalize x.t let y = x.t; + return y; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.expect.md index 72e1d42f6e..c94e6e4dc5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.expect.md @@ -5,6 +5,8 @@ function component() { let x = 1; let y = 2; + + return y; } ``` @@ -12,7 +14,10 @@ function component() { ## Code ```javascript -function component() {} +function component() { + const y = 2; + return y; +} ``` \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.js b/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.js index 13f6e3acc4..fca923b255 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-primitive.js @@ -1,4 +1,6 @@ function component() { let x = 1; let y = 2; + + return y; } diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.expect.md b/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.expect.md index 24ca54aeb0..6f193b80d5 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.expect.md @@ -10,6 +10,7 @@ function component() { } let z = foo(); + return z; } ``` @@ -35,8 +36,14 @@ function component() { } if (x > y) { } - - const z_0 = foo(); + let z_0; + if ($[2] === Symbol.for("react.memo_cache_sentinel")) { + z_0 = foo(); + $[2] = z_0; + } else { + z_0 = $[2]; + } + return z_0; } ``` diff --git a/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.js b/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.js index c595d71009..1b07b987f3 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.js +++ b/compiler/forget/src/__tests__/fixtures/hir/type-test-return-type-inference.js @@ -6,4 +6,5 @@ function component() { } let z = foo(); + return z; }