Skip to content
Browse files

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...
1 parent a90b334 commit 7218ffca59d7307dee1a716f6d49940bac4f59a6 @andreyvit andreyvit committed with Nov 1, 2012
Showing with 6 additions and 1 deletion.
  1. +4 −1 lib/process.js
  2. +1 −0 test/unit/compress/expected/issue471.js
  3. +1 −0 test/unit/compress/test/issue471.js
View
5 lib/process.js
@@ -1733,7 +1733,10 @@ function gen_code(ast, options) {
},
"call": function(func, args) {
var f = make(func);
- if (f.charAt(0) != "(" && needs_parens(func))
+ // cannot simply test the first and/or the last characters in the genetic case,
+ // because the called expression might look like e.g. `(x || y) && (u || v)`.
+ var already_wrapped = (func[0] == "function" && f.charAt(0) == "(");
+ if (!already_wrapped && needs_parens(func))
f = "(" + f + ")";
return f + "(" + add_commas(MAP(args, function(expr){
return parenthesize(expr, "seq");
View
1 test/unit/compress/expected/issue471.js
@@ -0,0 +1 @@
+((a||b)&&c)()
View
1 test/unit/compress/test/issue471.js
@@ -0,0 +1 @@
+((a || b) && c)();

0 comments on commit 7218ffc

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