@samitouri / QOS-React-2 / commits / 702f43eb1e

Optimize away extraneous blocks

In codegen we can flatten away extraneous BlockStatements if they turn out not to need a label.

Joe Savona committed Apr 5, 2023 at 16:37 UTC 702f43eb1efb85af1f13d59c0927a615c4b10eab
3 files changed +7 -9
compiler/forget/src/ReactiveScopes/CodegenReactiveFunction.ts
+2
@@ -124,6 +124,8 @@ function codegenBlock(cx: Context, block: ReactiveBlock): t.BlockStatement {
124 statement
125 )
126 );
127 + } else if (statement.type === "BlockStatement") {
128 + statements.push(...statement.body);
129 } else {
130 statements.push(statement);
131 }
compiler/forget/src/__tests__/fixtures/compiler/complex-while.expect.md
+4 -6
@@ -19,12 +19,10 @@ function foo(a, b, c) {
19
20 ```javascript
21 function foo(a, b, c) {
22 - {
23 - if (a) {
24 - while (b) {
25 - if (c) {
26 - break;
27 - }
22 + if (a) {
23 + while (b) {
24 + if (c) {
25 + break;
26 }
27 }
28 }
compiler/forget/src/__tests__/fixtures/compiler/unconditional-break-label.expect.md
+1 -3
@@ -17,9 +17,7 @@ function foo(a) {
17
18 ```javascript
19 function foo(a) {
20 - {
21 - return a + 1;
22 - }
20 + return a + 1;
21 }
22
23 ```