Support AssignmentPattern in params (param default values)
I already taught `lowerAssignment()` to handle assignment patterns for destructuring, we just have to call this helper for assignment pattern params too.
Joe Savona committed
Mar 28, 2023 at 09:52 UTC
57bc5fd56ef3859250756606346935108c62f30f
4 files changed
+50
-8
compiler/forget/src/HIR/BuildHIR.ts
+5
-1
@@ -102,7 +102,11 @@ export function lower(
102
loc: param.node.loc ?? GeneratedSource,
103
};
104
params.push(place);
105
- } else if (param.isObjectPattern() || param.isArrayPattern()) {
105
+ } else if (
106
+ param.isObjectPattern() ||
107
+ param.isArrayPattern() ||
108
+ param.isAssignmentPattern()
109
+ ) {
110
const place: Place = {
111
kind: "Identifier",
112
identifier: builder.makeTemporary(),
compiler/forget/src/__tests__/fixtures/compiler/error.todo-kitchensink.expect.md
-7
@@ -79,13 +79,6 @@ let moduleLocal = false;
79
## Error
80
81
```
82
-[ReactForget] TodoError: (BuildHIR::lower) Handle AssignmentPattern params
83
-> 1 | function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
84
- | ^^^^^^^
85
- 2 | let i = 0;
86
- 3 | var x = [];
87
- 4 |
88
-
82
[ReactForget] TodoError: (BuildHIR::lower) Handle RestElement params
83
> 1 | function foo([a, b], { c, d, e = "e" }, f = "f", ...args) {
84
| ^^^^^^^
compiler/forget/src/__tests__/fixtures/compiler/function-param-assignment-pattern.expect.md
new
+42
@@ -0,0 +1,42 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function Component(x = "default", y = [{}]) {
6
+ return [x, y];
7
+}
8
+
9
+```
10
+
11
+## Code
12
+
13
+```javascript
14
+function Component(t23, t0) {
15
+ const $ = React.unstable_useMemoCache(5);
16
+ const x = t23 === undefined ? "default" : t23;
17
+ const c_0 = $[0] !== t0;
18
+ let t1;
19
+ if (c_0) {
20
+ t1 = t0 === undefined ? [{}] : t0;
21
+ $[0] = t0;
22
+ $[1] = t1;
23
+ } else {
24
+ t1 = $[1];
25
+ }
26
+ const y = t1;
27
+ const c_2 = $[2] !== x;
28
+ const c_3 = $[3] !== y;
29
+ let t2;
30
+ if (c_2 || c_3) {
31
+ t2 = [x, y];
32
+ $[2] = x;
33
+ $[3] = y;
34
+ $[4] = t2;
35
+ } else {
36
+ t2 = $[4];
37
+ }
38
+ return t2;
39
+}
40
+
41
+```
42
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/compiler/function-param-assignment-pattern.js
new
+3
@@ -0,0 +1,3 @@
1
+function Component(x = "default", y = [{}]) {
2
+ return [x, y];
3
+}