From 5ee23af1a6d10b79e96c6bf82a28e1bef5b0130a Mon Sep 17 00:00:00 2001 From: Joe Savona Date: Wed, 17 May 2023 15:55:51 -0700 Subject: [PATCH] Elide empty if alternate blocks in codegen --- .../ReactiveScopes/CodegenReactiveFunction.ts | 17 ++++---- .../constant-propagation-phi.expect.md | 1 - .../compiler/reverse-postorder.expect.md | 1 - .../rules-of-hooks-c1e8c7f4c191.expect.md | 40 ------------------- 4 files changed, 10 insertions(+), 49 deletions(-) diff --git a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts index 11a189d18f..473dbb8906 100644 --- a/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts +++ b/compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts @@ -385,13 +385,16 @@ function codegenTerminal( ); } case "if": { - return t.ifStatement( - codegenPlace(cx, terminal.test), - codegenBlock(cx, terminal.consequent), - terminal.alternate !== null - ? codegenBlock(cx, terminal.alternate) - : null - ); + const test = codegenPlace(cx, terminal.test); + const consequent = codegenBlock(cx, terminal.consequent); + let alternate: t.Statement | null = null; + if (terminal.alternate !== null) { + const block = codegenBlock(cx, terminal.alternate); + if (block.body.length !== 0) { + alternate = block; + } + } + return t.ifStatement(test, consequent, alternate); } case "return": { const value = codegenPlace(cx, terminal.value); diff --git a/compiler/forget/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md index a0a4f00831..0ad43506d1 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/constant-propagation-phi.expect.md @@ -23,7 +23,6 @@ function foo(a, b, c) { ```javascript function foo(a, b, c) { if (a) { - } else { } return b; } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/reverse-postorder.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/reverse-postorder.expect.md index 1153d8dea3..50708f6147 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/reverse-postorder.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/reverse-postorder.expect.md @@ -51,7 +51,6 @@ function Component(props) { } } else { if (props.cond2) { - } else { } } } diff --git a/compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-c1e8c7f4c191.expect.md b/compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-c1e8c7f4c191.expect.md index fd04af4171..7761022d62 100644 --- a/compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-c1e8c7f4c191.expect.md +++ b/compiler/forget/src/__tests__/fixtures/compiler/rules-of-hooks/rules-of-hooks-c1e8c7f4c191.expect.md @@ -147,124 +147,84 @@ function MyComponent() { // Is valid but hard to compute by brute-forcing function MyComponent() { if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } if (c) { - } else { } useHook();