From 8695ea05c3e8a0112c7cabcf1b7003b024776b85 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Wed, 8 Mar 2023 00:15:57 +0000 Subject: [PATCH] [test] Add test for capturing a ref before renaming The renaming does not affect the lambda correctly. --- ...nction-capture-ref-before-rename.expect.md | 66 +++++++++++++++++++ ...ring-function-capture-ref-before-rename.js | 14 ++++ 2 files changed, 80 insertions(+) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.js diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.expect.md b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.expect.md new file mode 100644 index 0000000000..8ba08649b5 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.expect.md @@ -0,0 +1,66 @@ + +## Input + +```javascript +function component(a, b) { + let z = { a }; + (function () { + mutate(z); + })(); + let y = z; + + { + // z is shadowed & renamed but the lambda is unaffected. + let z = { b }; + y = { y, z }; + } + return y; +} + +``` + +## Code + +```javascript +function component(a, b) { + const $ = React.unstable_useMemoCache(7); + const c_0 = $[0] !== a; + let z; + if (c_0) { + z = { a }; + (function () { + mutate(z); + })(); + $[0] = a; + $[1] = z; + } else { + z = $[1]; + } + let y = z; + const c_2 = $[2] !== b; + let t0; + if (c_2) { + t0 = { b }; + $[2] = b; + $[3] = t0; + } else { + t0 = $[3]; + } + const z_0 = t0; + const c_4 = $[4] !== y; + const c_5 = $[5] !== z_0; + let t1; + if (c_4 || c_5) { + t1 = { y, z: z_0 }; + $[4] = y; + $[5] = z_0; + $[6] = t1; + } else { + t1 = $[6]; + } + y = t1; + return y; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.js b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.js new file mode 100644 index 0000000000..6abd7c8612 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/capturing-function-capture-ref-before-rename.js @@ -0,0 +1,14 @@ +function component(a, b) { + let z = { a }; + (function () { + mutate(z); + })(); + let y = z; + + { + // z is shadowed & renamed but the lambda is unaffected. + let z = { b }; + y = { y, z }; + } + return y; +}