Skip to content

Commit

Permalink
JS: merge sequential var declarations into another declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Apr 2, 2022
1 parent 2a0dc90 commit e7fcbe9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 4 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,10 @@ func TestJS(t *testing.T) {
{`var a,b=c;b=a`, `var a,b=c;b=a`},
{`var{a}=f;var b=c,d=e;`, `var{a}=f,b=c,d=e`},
{`var a,b;a=1,b=2,c=3`, `var a=1,b=2;c=3`},
{`var a=[];var b={};var c=d,e=f`, `var a=[],b={},c=d,e=f`},
{`var a=[];var b={};var c=d,e=f`, `var a=[],b={},c=d,e=f`},
{`var a=[];var b;var c,e=f`, `var a=[],b,c,e=f`},
{`var a=[];f();var b;f();var c;f();var e=f`, `var a=[],b,c,e;f(),f(),f(),e=f`},
// TODO: test for variables renaming (first rename, then merge vars)

// function and method declarations
Expand Down
8 changes: 7 additions & 1 deletion js/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,13 @@ func mergeVarDeclExprStmt(decl *js.VarDecl, exprStmt *js.ExprStmt, forward bool)
if forward {
item = commaExpr.List[len(commaExpr.List)-i-1]
}
if binaryExpr, ok := item.(*js.BinaryExpr); ok && binaryExpr.Op == js.EqToken {
if src, ok := item.(*js.VarDecl); ok {
// this happens when a variable declarations is converted to an expression
if mergeVarDecls(decl, src, forward) {
n++
continue
}
} else if binaryExpr, ok := item.(*js.BinaryExpr); ok && binaryExpr.Op == js.EqToken {
if v, ok := binaryExpr.X.(*js.Var); ok && v.Decl == js.VariableDecl {
if addDefinition(decl, v, binaryExpr.Y, forward) {
n++
Expand Down

0 comments on commit e7fcbe9

Please sign in to comment.