Skip to content

Commit 99ec6e6

Browse files
committed
JS: don't merge let/const declarations with for/while, fixes #325
1 parent fc1c76b commit 99ec6e6

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

js/js_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,10 @@ func TestJS(t *testing.T) {
551551
{`a in 5;for(;b;)c()`, `for((a in 5);b;)c()`}, // is longer
552552
{`a in 5;for(b=4;b;)c()`, `a in 5;for(b=4;b;)c()`},
553553
{`var a=5;for(;b;)c()`, `for(var a=5;b;)c()`},
554+
{`let a=5;for(;b;)c()`, `let a=5;for(;b;)c()`},
554555
{`var a=b in c;for(;b;)c()`, `for(var a=(b in c);b;)c()`},
555556
{`var a=5;while(b)c()`, `for(var a=5;b;)c()`},
557+
{`let a=5;while(b)c()`, `let a=5;while(b)c()`},
556558
{`a=5;for(var b=4;b;)c()`, `a=5;for(var b=4;b;)c()`},
557559
{`a=5;switch(b=4){}`, `switch(a=5,b=4){}`},
558560
{`a=5;with(b=4){}`, `with(a=5,b=4);`},

js/stmtlist.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,11 @@ func (m *jsMinifier) optimizeStmtList(list []js.IStmt, blockType blockType) []js
150150
// merge const, let declarations
151151
right.List = append(left.List, right.List...)
152152
j--
153-
} else if forStmt, ok := list[i].(*js.ForStmt); ok && forStmt.Init == nil {
153+
} else if forStmt, ok := list[i].(*js.ForStmt); ok && left.TokenType == js.VarToken && forStmt.Init == nil {
154154
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
155155
forStmt.Init = left
156156
j--
157-
} else if whileStmt, ok := list[i].(*js.WhileStmt); ok {
157+
} else if whileStmt, ok := list[i].(*js.WhileStmt); ok && left.TokenType == js.VarToken {
158158
// TODO: only merge statements that don't have 'in' or 'of' keywords (slow to check?)
159159
var body js.BlockStmt
160160
if blockStmt, ok := whileStmt.Body.(*js.BlockStmt); ok {

0 commit comments

Comments
 (0)