mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
[λ] Parse and codegen concise arrow expressions
This commit is contained in:
@@ -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>;
|
||||
}
|
||||
Reference in New Issue
Block a user