UglifyJS-0001-Fix-broken-calls-of-complex-binary-expressions-471
From 46089d511b7f7d718b79c4ce8a685366e1c73161 Mon Sep 17 00:00:00 2001 | |
From: Andrey Tarantsov <andrey@tarantsov.com> | |
Date: Thu, 1 Nov 2012 10:10:34 +0700 | |
Subject: [PATCH] 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()`. | |
--- | |
lib/process.js | 5 ++++- | |
test/unit/compress/expected/issue471.js | 1 + | |
test/unit/compress/test/issue471.js | 1 + | |
3 files changed, 6 insertions(+), 1 deletion(-) | |
create mode 100644 test/unit/compress/expected/issue471.js | |
create mode 100644 test/unit/compress/test/issue471.js | |
diff --git a/lib/process.js b/lib/process.js | |
index 42d61df..b4f8e53 100644 | |
--- a/lib/process.js | |
+++ b/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"); | |
diff --git a/test/unit/compress/expected/issue471.js b/test/unit/compress/expected/issue471.js | |
new file mode 100644 | |
index 0000000..7aebe09 | |
--- /dev/null | |
+++ b/test/unit/compress/expected/issue471.js | |
@@ -0,0 +1 @@ | |
+((a||b)&&c)() | |
diff --git a/test/unit/compress/test/issue471.js b/test/unit/compress/test/issue471.js | |
new file mode 100644 | |
index 0000000..b0f4613 | |
--- /dev/null | |
+++ b/test/unit/compress/test/issue471.js | |
@@ -0,0 +1 @@ | |
+((a || b) && c)(); | |
-- | |
1.7.9.6 (Apple Git-31.1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment