Skip to content

Commit

Permalink
JS: fix bug when merging hoisted var-decl to let-decl, fixes #687
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 24, 2024
1 parent c1efe1c commit 86468ba
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
1 change: 1 addition & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,7 @@ func TestJS(t *testing.T) {
{`'\u000A\u000D'`, "`\n\r`"}, // #653
{`for(!a;b;c);`, "for(!a;b;c);"}, // #656
{"var a = /*! comment */ b;", "/*! comment */var a=b"}, // #664
{`var c="";for(let i=0;;);var d="";for(let i=0;;);`, `var d,c="";for(let i=0;;);d="";for(let i=0;;);`}, // #687
}

m := minify.New()
Expand Down
14 changes: 9 additions & 5 deletions js/stmtlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,15 @@ func optimizeStmtList(list []js.IStmt, blockType blockType) []js.IStmt {
j--
} else if forStmt, ok := list[i].(*js.ForStmt); ok {
// TODO: only merge lhs expression that don't have 'in' or 'of' keywords (slow to check?)
if varDecl, ok := forStmt.Init.(*js.VarDecl); ok {
if len(varDecl.List) == 0 || forStmt.Init == nil {
forStmt.Init = left.Value
j--
} else if mergeVarDeclExprStmt(varDecl, left, true) {
if forStmt.Init == nil {
forStmt.Init = left.Value
j--
} else if decl, ok := forStmt.Init.(*js.VarDecl); ok && len(decl.List) == 0 {
forStmt.Init = left.Value
j--
} else if ok && (decl.TokenType == js.VarToken || decl.TokenType == js.ErrorToken) {
// this is the second VarDecl, so we are hoisting var declarations, which means the forInit variables are already in 'left'
if merge := mergeVarDeclExprStmt(decl, left, true); merge {
j--
}
}
Expand Down

0 comments on commit 86468ba

Please sign in to comment.