[λ] Parse and codegen concise arrow expressions
Sathya Gunasekaran committed
Jan 24, 2023 at 10:47 UTC
8041b0b0d5fee8cea8b1a20adca9a6cf207549b5
3 files changed
+62
-5
compiler/forget/src/HIR/BuildHIR.ts
+8
-5
@@ -81,11 +81,14 @@ export function lower(
81
82
const body = func.get("body");
83
if (body.isExpression()) {
84
- builder.pushError({
85
- reason: "Support arrow functions",
86
- severity: ErrorSeverity.Todo,
87
- nodePath: body,
88
- });
84
+ const fallthrough = builder.reserve();
85
+ const terminal: ReturnTerminal = {
86
+ kind: "return",
87
+ loc: GeneratedSource,
88
+ value: lowerExpressionToPlace(builder, body),
89
+ id: makeInstructionId(0),
90
+ };
91
+ builder.terminateWithContinuation("block", terminal, fallthrough);
92
} else if (body.isBlockStatement()) {
93
lowerStatement(builder, body);
94
} else {
compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.expect.md
new
+49
@@ -0,0 +1,49 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component() {
6
+ let [x, setX] = useState(0);
7
+ const handler = (v) => setX(v);
8
+ return <Foo handler={handler}></Foo>;
9
+}
10
+
11
+```
12
+
13
+## Code
14
+
15
+```javascript
16
+function component() {
17
+ const $ = React.useMemoCache();
18
+ let t0;
19
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
20
+ t0 = useState(0);
21
+ $[0] = t0;
22
+ } else {
23
+ t0 = $[0];
24
+ }
25
+ const x = t0[0];
26
+ const setX = t0[1];
27
+ const c_1 = $[1] !== setX;
28
+ let handler;
29
+ if (c_1) {
30
+ handler = (v) => setX(v);
31
+ $[1] = setX;
32
+ $[2] = handler;
33
+ } else {
34
+ handler = $[2];
35
+ }
36
+ const c_3 = $[3] !== handler;
37
+ let t4;
38
+ if (c_3) {
39
+ t4 = <Foo handler={handler}></Foo>;
40
+ $[3] = handler;
41
+ $[4] = t4;
42
+ } else {
43
+ t4 = $[4];
44
+ }
45
+ return t4;
46
+}
47
+
48
+```
49
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.js
new
+5
@@ -0,0 +1,5 @@
1
+function component() {
2
+ let [x, setX] = useState(0);
3
+ const handler = (v) => setX(v);
4
+ return <Foo handler={handler}></Foo>;
5
+}