From 36a653fd384259f7a8e39fb2e754d8379aaa6f06 Mon Sep 17 00:00:00 2001 From: Taco de Wolff Date: Tue, 21 Jun 2022 17:25:59 -0400 Subject: [PATCH] JS: fix parenthesis around expressions object literals when followed by other nested statements in expressions, fixes #494 --- js/js.go | 18 ++++++++++++------ js/js_test.go | 4 ++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/js/js.go b/js/js.go index fe7c35d2bf..d654e59b5a 100644 --- a/js/js.go +++ b/js/js.go @@ -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 { diff --git a/js/js_test.go b/js/js_test.go index bb47ba1dc5..8ae02e8984 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -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()