Skip to content

Commit

Permalink
JS: remove braces for if when nested if statement will be optimized away
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 5, 2022
1 parent 07b2257 commit 064b99a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,11 @@ func TestJS(t *testing.T) {
{`while(a)if(b)continue;else c`, `for(;a;){if(b)continue;c}`},
{`while(a)if(b)return c;else return d`, `for(;a;)return b?c:d`},
{`while(a){if(b)continue;else c}`, `for(;a;){if(b)continue;c}`},
{`if(a){while(b)if(c)5}else{6}`, `if(a){for(;b;)c&&5}else 6`},
{`if(a){while(b)if(c)5}else{6}`, `if(a)for(;b;)c&&5;else 6`},
{`if(a){for(;;)if(b)break}else c`, `if(a){for(;;)if(b)break}else c`},
{`if(a){for(d in e)if(b)break}else c`, `if(a){for(d in e)if(b)break}else c`},
{`if(a){for(d of e)if(b)break}else c`, `if(a){for(d of e)if(b)break}else c`},
{`if(a){for(d of e)if(f)g()}else c`, `if(a)for(d of e)f&&g();else c`},
{`if(a){d:if(b)break}else c`, `if(a){d:if(b)break}else c`},
{`if(a){with(d)if(b)break}else c`, `if(a){with(d)if(b)break}else c`},
{`if(a)return b;if(c)return d;return e`, `return a?b:c?d:e`},
Expand Down
3 changes: 2 additions & 1 deletion js/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ func endsInIf(istmt js.IStmt) bool {
switch stmt := istmt.(type) {
case *js.IfStmt:
if stmt.Else == nil {
return true
_, ok := optimizeStmt(stmt).(*js.IfStmt)
return ok
}
return endsInIf(stmt.Else)
case *js.BlockStmt:
Expand Down

0 comments on commit 064b99a

Please sign in to comment.