Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fix broken calls of complex binary expressions [#471]

When deciding whether to parenthesize the expression being called, a
simple test of f.charAt(0) results in a broken code when calling a
complex expression which just happens to start with a paren but is
not actually parenthesized.

As an example, the code in issue471.js (see the diff) used to
incorrectly output `(a||b)&&c()`.
  • Loading branch information...
commit 7218ffca59d7307dee1a716f6d49940bac4f59a6 1 parent a90b334
Andrey Tarantsov andreyvit authored committed
5 lib/process.js
@@ -1733,7 +1733,10 @@ function gen_code(ast, options) {
1733 1733 },
1734 1734 "call": function(func, args) {
1735 1735 var f = make(func);
1736   - if (f.charAt(0) != "(" && needs_parens(func))
  1736 + // cannot simply test the first and/or the last characters in the genetic case,
  1737 + // because the called expression might look like e.g. `(x || y) && (u || v)`.
  1738 + var already_wrapped = (func[0] == "function" && f.charAt(0) == "(");
  1739 + if (!already_wrapped && needs_parens(func))
1737 1740 f = "(" + f + ")";
1738 1741 return f + "(" + add_commas(MAP(args, function(expr){
1739 1742 return parenthesize(expr, "seq");
1  test/unit/compress/expected/issue471.js
... ... @@ -0,0 +1 @@
  1 +((a||b)&&c)()
1  test/unit/compress/test/issue471.js
... ... @@ -0,0 +1 @@
  1 +((a || b) && c)();

0 comments on commit 7218ffc

Please sign in to comment.
Something went wrong with that request. Please try again.