From 5fa3d41b22be14854f0ef2a10493a492e677b438 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Sat, 14 Jan 2023 10:19:39 +0000 Subject: [PATCH] =?UTF-8?q?[=CE=BB]=20Make=20function=20expr=20tests=20be?= =?UTF-8?q?=20reactive=20to=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This lets us memoize more and test function expressions better --- .../hir/capturing-function-1.expect.md | 28 ++++++++++++++----- .../fixtures/hir/capturing-function-1.js | 4 +-- .../capturing-function-within-block.expect.md | 28 ++++++++++++++----- .../hir/capturing-function-within-block.js | 4 +-- ...pturing-variable-in-nested-block.expect.md | 28 ++++++++++++++----- .../hir/capturing-variable-in-nested-block.js | 4 +-- ...ring-variable-in-nested-function.expect.md | 28 ++++++++++++++----- .../capturing-variable-in-nested-function.js | 4 +-- 8 files changed, 92 insertions(+), 36 deletions(-) diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.expect.md index de7dd20aec..55a6c2fd94 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.expect.md @@ -2,8 +2,8 @@ ## Input ```javascript -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { z; }; @@ -15,18 +15,32 @@ function component() { ## Code ```javascript -function component() { +function component(a) { const $ = React.useMemoCache(); - const z = 100; + const c_0 = $[0] !== a; + let z; + if (c_0) { + z = { + a: a, + }; + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + + const c_2 = $[2] !== z; let x; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + + if (c_2) { x = function () { z; }; - $[0] = x; + $[2] = z; + $[3] = x; } else { - x = $[0]; + x = $[3]; } return x; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.js index e3fc3c5452..9862aadec3 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.js +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-1.js @@ -1,5 +1,5 @@ -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { z; }; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.expect.md index 6f8bbc044d..1c56b9b6ef 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.expect.md @@ -2,8 +2,8 @@ ## Input ```javascript -function component() { - let z = 100; +function component(a) { + let z = { a }; let x; { x = function () { @@ -18,19 +18,33 @@ function component() { ## Code ```javascript -function component() { +function component(a) { const $ = React.useMemoCache(); - const z = 100; + const c_0 = $[0] !== a; + let z; + if (c_0) { + z = { + a: a, + }; + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + const x = undefined; + const c_2 = $[2] !== z; let x$0; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + + if (c_2) { x$0 = function () { z; }; - $[0] = x$0; + $[2] = z; + $[3] = x$0; } else { - x$0 = $[0]; + x$0 = $[3]; } return x$0; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.js index 5154c4c8fb..1baa1c67a2 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.js +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-within-block.js @@ -1,5 +1,5 @@ -function component() { - let z = 100; +function component(a) { + let z = { a }; let x; { x = function () { diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.expect.md index a9aaf58a15..791e0d085f 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.expect.md @@ -2,8 +2,8 @@ ## Input ```javascript -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { { z; @@ -17,20 +17,34 @@ function component() { ## Code ```javascript -function component() { +function component(a) { const $ = React.useMemoCache(); - const z = 100; + const c_0 = $[0] !== a; + let z; + if (c_0) { + z = { + a: a, + }; + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + + const c_2 = $[2] !== z; let x; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + + if (c_2) { x = function () { { z; } }; - $[0] = x; + $[2] = z; + $[3] = x; } else { - x = $[0]; + x = $[3]; } return x; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.js index 26fd218e71..9509220eef 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.js +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-block.js @@ -1,5 +1,5 @@ -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { { z; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.expect.md index 5d90511ee5..33e214a5a9 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.expect.md +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.expect.md @@ -2,8 +2,8 @@ ## Input ```javascript -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { (function () { z; @@ -17,20 +17,34 @@ function component() { ## Code ```javascript -function component() { +function component(a) { const $ = React.useMemoCache(); - const z = 100; + const c_0 = $[0] !== a; + let z; + if (c_0) { + z = { + a: a, + }; + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + + const c_2 = $[2] !== z; let x; - if ($[0] === Symbol.for("react.memo_cache_sentinel")) { + + if (c_2) { x = function () { (function () { z; })(); }; - $[0] = x; + $[2] = z; + $[3] = x; } else { - x = $[0]; + x = $[3]; } return x; diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.js index 46eda93731..a074edb95d 100644 --- a/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.js +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-variable-in-nested-function.js @@ -1,5 +1,5 @@ -function component() { - let z = 100; +function component(a) { + let z = { a }; let x = function () { (function () { z;