Skip to content

Commit

Permalink
fix #313: parenthesis bug with arrow functions
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Aug 3, 2020
1 parent cb3122c commit 80273dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

Console output on Windows now uses color instead of being monochrome. This should make log messages easier to read.

* Parenthesize destructuring assignment in arrow function expressions ([#313](https://github.com/evanw/esbuild/issues/313))

This fixes a bug where `() => ({} = {})` was incorrectly printed as `() => ({}) = {}`, which is a syntax error. This case is now printed correctly.

## 0.6.15

* Support symlinks with absolute paths in `node_modules` ([#310](https://github.com/evanw/esbuild/issues/310))
Expand Down
2 changes: 1 addition & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,7 +1834,7 @@ func (p *printer) printExpr(expr ast.Expr, level ast.L, flags int) {
wrap := level >= entry.Level || (e.Op == ast.BinOpIn && (flags&forbidIn) != 0)

// Destructuring assignments must be parenthesized
if p.stmtStart == len(p.js) {
if n := len(p.js); p.stmtStart == n || p.arrowExprStart == n {
if _, ok := e.Left.Data.(*ast.EObject); ok {
wrap = true
}
Expand Down
10 changes: 9 additions & 1 deletion internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ func TestTemplate(t *testing.T) {

func TestObject(t *testing.T) {
expectPrinted(t, "let x = {'(':')'}", "let x = {\"(\": \")\"};\n")
expectPrinted(t, "({})", "({});\n")
expectPrinted(t, "({}.x)", "({}).x;\n")
expectPrinted(t, "({} = {})", "({} = {});\n")
expectPrinted(t, "(x, {} = {})", "x, {} = {};\n")
expectPrinted(t, "let x = () => ({})", "let x = () => ({});\n")
expectPrinted(t, "let x = () => ({}.x)", "let x = () => ({}).x;\n")
expectPrinted(t, "let x = () => ({} = {})", "let x = () => ({} = {});\n")
expectPrinted(t, "let x = () => (x, {} = {})", "let x = () => (x, {} = {});\n")
}

func TestFor(t *testing.T) {
Expand Down Expand Up @@ -574,7 +582,7 @@ func TestWhitespace(t *testing.T) {

expectPrintedMinify(t, "()=>({})", "()=>({});")
expectPrintedMinify(t, "()=>({}[1])", "()=>({})[1];")
expectPrintedMinify(t, "()=>({}+0)", "()=>({})+0;")
expectPrintedMinify(t, "()=>({}+0)", "()=>({}+0);")
expectPrintedMinify(t, "()=>function(){}", "()=>function(){};")

expectPrintedMinify(t, "(function(){})", "(function(){});")
Expand Down

0 comments on commit 80273dc

Please sign in to comment.