From d155d2dd81a70985166ff4ee09217cde8a2f9ad5 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 8 Mar 2023 00:15:51 +0000 Subject: [PATCH] [test] Add test for lambda that conditionally captures ref Conditionally captured refs are correctly added to dependency list of the lambda. --- ...ction-conditional-capture-mutate.expect.md | 54 +++++++++++++++++++ ...ing-function-conditional-capture-mutate.js | 10 ++++ 2 files changed, 64 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.js diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.expect.md new file mode 100644 index 0000000000..9e2265fcce --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +function component(a, b) { + let z = { a }; + let y = b; + let x = function () { + if (y) { + mutate(z); + } + }; + return x; +} + +``` + +## Code + +```javascript +function component(a, b) { + const $ = React.unstable_useMemoCache(5); + const c_0 = $[0] !== a; + let t0; + if (c_0) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const z = t0; + const y = b; + const c_2 = $[2] !== y; + const c_3 = $[3] !== z; + let t1; + if (c_2 || c_3) { + t1 = function () { + if (y) { + mutate(z); + } + }; + $[2] = y; + $[3] = z; + $[4] = t1; + } else { + t1 = $[4]; + } + const x = t1; + return x; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.js new file mode 100644 index 0000000000..10bac9e4df --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-conditional-capture-mutate.js @@ -0,0 +1,10 @@ +function component(a, b) { + let z = { a }; + let y = b; + let x = function () { + if (y) { + mutate(z); + } + }; + return x; +}