[λ] Parse and codegen concise arrow expressions

This commit is contained in:
Sathya Gunasekaran
2023-01-24 10:47:49 -05:00
parent 9aed0fc6b1
commit 8041b0b0d5
3 changed files with 62 additions and 5 deletions
+8 -5
View File
@@ -81,11 +81,14 @@ export function lower(
const body = func.get("body");
if (body.isExpression()) {
builder.pushError({
reason: "Support arrow functions",
severity: ErrorSeverity.Todo,
nodePath: body,
});
const fallthrough = builder.reserve();
const terminal: ReturnTerminal = {
kind: "return",
loc: GeneratedSource,
value: lowerExpressionToPlace(builder, body),
id: makeInstructionId(0),
};
builder.terminateWithContinuation("block", terminal, fallthrough);
} else if (body.isBlockStatement()) {
lowerStatement(builder, body);
} else {
@@ -0,0 +1,49 @@
## Input
```javascript
function component() {
let [x, setX] = useState(0);
const handler = (v) => setX(v);
return <Foo handler={handler}></Foo>;
}
```
## Code
```javascript
function component() {
const $ = React.useMemoCache();
let t0;
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
t0 = useState(0);
$[0] = t0;
} else {
t0 = $[0];
}
const x = t0[0];
const setX = t0[1];
const c_1 = $[1] !== setX;
let handler;
if (c_1) {
handler = (v) => setX(v);
$[1] = setX;
$[2] = handler;
} else {
handler = $[2];
}
const c_3 = $[3] !== handler;
let t4;
if (c_3) {
t4 = <Foo handler={handler}></Foo>;
$[3] = handler;
$[4] = t4;
} else {
t4 = $[4];
}
return t4;
}
```
@@ -0,0 +1,5 @@
function component() {
let [x, setX] = useState(0);
const handler = (v) => setX(v);
return <Foo handler={handler}></Foo>;
}