Rich-Harris / butternut

The fast, future-friendly minifier
https://butternut.now.sh
MIT License
1.17k stars 17 forks source link

Object literal outputting without parens causes syntax error #77

Closed jbt closed 7 years ago

jbt commented 7 years ago

The following example:

if ({}.foo) {
  b();
}

gets minified to {}.foo&&b() which causes a syntax error because {}.foo needs parens to be parsed as an expression.

A few other cases where object literals / object destructuring in expressions are getting parens stripped or would need them added, but not sure if they'd be related or need tackling separately:

The less-contrived real-life example I'm running into is essentially the same as the first example (from setImmediate):

if ({}.toString.call(global.process) === "[object process]") { ...

For the if statements it could be as simple as making sure shouldParenthesiseTest is true if the test starts with {, but it feels like it might need a more general solution also incorporating those other cases.

Rich-Harris commented 7 years ago

Fixed in 0.4.2, thanks

jbt commented 7 years ago

Awesome, nice one 🙌