From 8041b0b0d5fee8cea8b1a20adca9a6cf207549b5 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Tue, 24 Jan 2023 10:47:49 -0500 Subject: [PATCH] =?UTF-8?q?[=CE=BB]=20Parse=20and=20codegen=20concise=20ar?= =?UTF-8?q?row=20expressions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compiler/forget/src/HIR/BuildHIR.ts | 13 +++-- .../fixtures/hir/concise-arrow-expr.expect.md | 49 +++++++++++++++++++ .../fixtures/hir/concise-arrow-expr.js | 5 ++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.expect.md create mode 100644 compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.js diff --git a/compiler/forget/src/HIR/BuildHIR.ts b/compiler/forget/src/HIR/BuildHIR.ts index 0b0346e30f..a701683d6f 100644 --- a/compiler/forget/src/HIR/BuildHIR.ts +++ b/compiler/forget/src/HIR/BuildHIR.ts @@ -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 { diff --git a/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.expect.md b/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.expect.md new file mode 100644 index 0000000000..64bab0f4c1 --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.expect.md @@ -0,0 +1,49 @@ + +## Input + +```javascript +function component() { + let [x, setX] = useState(0); + const handler = (v) => setX(v); + return ; +} + +``` + +## 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 = ; + $[3] = handler; + $[4] = t4; + } else { + t4 = $[4]; + } + return t4; +} + +``` + \ No newline at end of file diff --git a/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.js b/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.js new file mode 100644 index 0000000000..d04270e3ac --- /dev/null +++ b/compiler/forget/src/__tests__/fixtures/hir/concise-arrow-expr.js @@ -0,0 +1,5 @@ +function component() { + let [x, setX] = useState(0); + const handler = (v) => setX(v); + return ; +}