From 5bc2c807c39cb38fc9ae8acaa69fb96fd3d2b393 Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 15 Mar 2023 15:33:14 -0700 Subject: [PATCH] Support array pattern params --- compiler/forget/src/HIR/BuildHIR.ts | 2 +- .../compiler/array-pattern-params.expect.md | 54 +++++++++++++++++++ .../fixtures/compiler/array-pattern-params.js | 5 ++ .../compiler/error.todo-kitchensink.expect.md | 16 ------ 4 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.js diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index d77b7db19a..d1b2ffbc21 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -102,7 +102,7 @@ export function lower( loc: param.node.loc ?? GeneratedSource, }; params.push(place); - } else if (param.isObjectPattern()) { + } else if (param.isObjectPattern() || param.isArrayPattern()) { const place: Place = { kind: "Identifier", identifier: builder.makeTemporary(), diff --git a/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.expect.md new file mode 100644 index 0000000000..e9e0b009e0 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.expect.md @@ -0,0 +1,54 @@ + +## Input + +```javascript +function component([a, b]) { + let y = { a }; + let z = { b }; + return [y, z]; +} + +``` + +## Code + +```javascript +function component(t16) { + const $ = React.unstable_useMemoCache(7); + const [a, b] = t16; + const c_0 = $[0] !== a; + let t0; + if (c_0) { + t0 = { a }; + $[0] = a; + $[1] = t0; + } else { + t0 = $[1]; + } + const y = t0; + const c_2 = $[2] !== b; + let t1; + if (c_2) { + t1 = { b }; + $[2] = b; + $[3] = t1; + } else { + t1 = $[3]; + } + const z = t1; + const c_4 = $[4] !== y; + const c_5 = $[5] !== z; + let t2; + if (c_4 || c_5) { + t2 = [y, z]; + $[4] = y; + $[5] = z; + $[6] = t2; + } else { + t2 = $[6]; + } + return t2; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.js b/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.js new file mode 100644 index 0000000000..5444d98405 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/compiler/array-pattern-params.js @@ -0,0 +1,5 @@ +function component([a, b]) { + let y = { a }; + let z = { b }; + return [y, z]; +} diff --git a/compiler/forget/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md index 33b4d0eeb0..6f29931579 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md @@ -83,13 +83,6 @@ let moduleLocal = false; ## Error ``` -[ReactForget] TodoError: (BuildHIR::lower) Handle ArrayPattern params -> 1 | function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { - | ^^^^^^ - 2 | let i = 0; - 3 | var x = []; - 4 | - [ReactForget] TodoError: (BuildHIR::lowerAssignment) Handle AssignmentPattern assignments > 1 | function foo([a, b], { c, d, e = "e" }, f = "f", ...args) { | ^^^^^^^ @@ -192,15 +185,6 @@ let moduleLocal = false; 22 | const j = function bar([quz, qux], ...args) {}; 23 | -[ReactForget] TodoError: (BuildHIR::lower) Handle ArrayPattern params - 20 | ; - 21 | -> 22 | const j = function bar([quz, qux], ...args) {}; - | ^^^^^^^^^^ - 23 | - 24 | for (; i < 3; i += 1) { - 25 | x.push(i); - [ReactForget] TodoError: (BuildHIR::lower) Handle RestElement params 20 | ; 21 |