@samitouri / QOS-React-2 / commits / c803558b2a

[hir] Parse object pattern params

Sathya Gunasekaran committed Jan 26, 2023 at 16:40 UTC c803558b2a6a76f37b40493ceeab29c8e0a45034
4 files changed +75 -2
compiler/forget/src/HIR/BuildHIR.ts
+15
@@ -72,6 +72,21 @@ export function lower(
72 loc: param.node.loc ?? GeneratedSource,
73 };
74 params.push(place);
75 + } else if (param.isObjectPattern()) {
76 + const place: Place = {
77 + kind: "Identifier",
78 + identifier: builder.makeTemporary(),
79 + effect: Effect.Unknown,
80 + loc: param.node.loc ?? GeneratedSource,
81 + };
82 + params.push(place);
83 + lowerAssignment(
84 + builder,
85 + param.node.loc ?? GeneratedSource,
86 + InstructionKind.Let,
87 + param,
88 + place
89 + );
90 } else {
91 builder.recordError({
92 reason: `(BuildHIR::lower) Handle ${param.node.type} params`,
compiler/forget/src/__tests__/fixtures/hir/error.todo-kitchensink.expect.md
+2 -2
@@ -55,9 +55,9 @@ function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
55 3 | var x = [];
56 4 |
57
58 -[ReactForget] TodoError: (BuildHIR::lower) Handle ObjectPattern params
58 +[ReactForget] TodoError: (BuildHIR::lowerAssignment) Handle AssignmentPattern assignments
59 > 1 | function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
60 - | ^^^^^^^^^^^^^^^^^
60 + | ^^^^^^^
61 2 | let i = 0;
62 3 | var x = [];
63 4 |
compiler/forget/src/__tests__/fixtures/hir/object-pattern-params.expect.md new
+53
@@ -0,0 +1,53 @@
1 +
2 +## Input
3 +
4 +```javascript
5 +function component({ a, b }) {
6 + let y = { a };
7 + let z = { b };
8 + return { y, z };
9 +}
10 +
11 +```
12 +
13 +## Code
14 +
15 +```javascript
16 +function component(t8) {
17 + const $ = React.useMemoCache();
18 + const a = t8.a;
19 + const b = t8.b;
20 + const c_0 = $[0] !== a;
21 + let y;
22 + if (c_0) {
23 + y = { a: a };
24 + $[0] = a;
25 + $[1] = y;
26 + } else {
27 + y = $[1];
28 + }
29 + const c_2 = $[2] !== b;
30 + let z;
31 + if (c_2) {
32 + z = { b: b };
33 + $[2] = b;
34 + $[3] = z;
35 + } else {
36 + z = $[3];
37 + }
38 + const c_4 = $[4] !== y;
39 + const c_5 = $[5] !== z;
40 + let t6;
41 + if (c_4 || c_5) {
42 + t6 = { y: y, z: z };
43 + $[4] = y;
44 + $[5] = z;
45 + $[6] = t6;
46 + } else {
47 + t6 = $[6];
48 + }
49 + return t6;
50 +}
51 +
52 +```
53 +
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/object-pattern-params.js new
+5
@@ -0,0 +1,5 @@
1 +function component({ a, b }) {
2 + let y = { a };
3 + let z = { b };
4 + return { y, z };
5 +}