From 123d024af7827c3de1df0d3ab3ae3c58fa84e461 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Fri, 20 Jan 2023 19:44:32 +0000 Subject: [PATCH] =?UTF-8?q?[=CE=BB]=20Add=20test=20for=20function=20expr?= =?UTF-8?q?=20passed=20to=20jsx?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Function exprs become frozen when captured by jsx. --- .../hir/capture-func-passed-to-jsx.expect.md | 70 +++++++++++++++++++ .../hir/capture-func-passed-to-jsx.js | 11 +++ 2 files changed, 81 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.js diff --git a/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.expect.md new file mode 100644 index 0000000000..5284c0b814 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.expect.md @@ -0,0 +1,70 @@ + +## Input + +```javascript +function component(a, b) { + let y = { b }; + let z = { a }; + let x = function () { + z.a = 2; + y.b; + }; + let t = ; + mutate(x); // x should be frozen here + return t; +} + +``` + +## Code + +```javascript +function component(a, b) { + const $ = React.useMemoCache(); + const c_0 = $[0] !== b; + let y; + if (c_0) { + y = { b: b }; + $[0] = b; + $[1] = y; + } else { + y = $[1]; + } + const c_2 = $[2] !== a; + let z; + if (c_2) { + z = { a: a }; + $[2] = a; + $[3] = z; + } else { + z = $[3]; + } + const c_4 = $[4] !== z.a; + const c_5 = $[5] !== y.b; + let x; + if (c_4 || c_5) { + x = function () { + z.a = 2; + y.b; + }; + $[4] = z.a; + $[5] = y.b; + $[6] = x; + } else { + x = $[6]; + } + const c_7 = $[7] !== x; + let t; + if (c_7) { + t = ; + $[7] = x; + $[8] = t; + } else { + t = $[8]; + } + mutate(x); + return t; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.js b/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.js new file mode 100644 index 0000000000..35357391f9 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capture-func-passed-to-jsx.js @@ -0,0 +1,11 @@ +function component(a, b) { + let y = { b }; + let z = { a }; + let x = function () { + z.a = 2; + y.b; + }; + let t = ; + mutate(x); // x should be frozen here + return t; +}