Skip to content

Commit

Permalink
JS: fix parenthesis around expressions object literals when followed …
Browse files Browse the repository at this point in the history
…by other nested statements in expressions, fixes #494
  • Loading branch information
tdewolff committed Jun 21, 2022
1 parent aec4628 commit 36a653f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 12 additions & 6 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -1240,25 +1240,31 @@ func (m *jsMinifier) minifyExpr(i js.IExpr, prec js.OpPrec) {
} else if m.expectExpr == expectExprStmt {
m.write(notBytes)
}
parentInFor := m.inFor
m.inFor = false
parentInFor, parentGroupedStmt := m.inFor, m.groupedStmt
m.inFor, m.groupedStmt = false, false
m.minifyFuncDecl(expr, true)
m.inFor = parentInFor
m.inFor, m.groupedStmt = parentInFor, parentGroupedStmt
if grouped {
m.write(closeParenBytes)
}
case *js.ArrowFunc:
parentGroupedStmt := m.groupedStmt
m.groupedStmt = false
m.minifyArrowFunc(expr)
m.groupedStmt = parentGroupedStmt
case *js.MethodDecl:
parentGroupedStmt := m.groupedStmt
m.groupedStmt = false
m.minifyMethodDecl(expr) // only happens in object literal
m.groupedStmt = parentGroupedStmt
case *js.ClassDecl:
if m.expectExpr == expectExprStmt {
m.write(notBytes)
}
parentInFor := m.inFor
m.inFor = false
parentInFor, parentGroupedStmt := m.inFor, m.groupedStmt
m.inFor, m.groupedStmt = false, false
m.minifyClassDecl(expr)
m.inFor = parentInFor
m.inFor, m.groupedStmt = parentInFor, parentGroupedStmt
case *js.CommaExpr:
for i, item := range expr.List {
if i != 0 {
Expand Down
4 changes: 4 additions & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,10 @@ func TestJS(t *testing.T) {
{`if(!(0<1&&1<2)){throw new Error()}`, `if(!(0<1&&1<2))throw new Error`}, // #479
{`class A{set x(e){}}`, `class A{set x(e){}}`}, // #481
{`if(a){let b=c(d)}`, `a&&c(d)`}, // #487
{`var a=5;({});var b={c:()=>3}`, `var b,a=5;({},b={c:()=>3})`}, // #494
{`var a=5;({});var b=function(){3}`, `var b,a=5;({},b=function(){3})`}, // #494
{`var a=5;({});var b=class{c(){3}}`, `var b,a=5;({},b=class{c(){3}})`}, // #494
{`({});a={b(){3}}`, `({},a={b(){3}})`}, // #494
}

m := minify.New()
Expand Down

0 comments on commit 36a653f

Please sign in to comment.