Permalink
Browse files

Merge pull request #385 from RGustBardon/implement-368

Implement #368 (too many parentheses)
  • Loading branch information...
2 parents b259613 + 133b36f commit 301d37b6a30d31ed1260c0ad3ce608f268e65e9a @mishoo committed May 8, 2012
Showing with 22 additions and 2 deletions.
  1. +18 −2 lib/process.js
  2. +1 −0 test/unit/compress/expected/issue368.js
  3. +3 −0 test/unit/compress/test/issue368.js
View
20 lib/process.js
@@ -1508,7 +1508,7 @@ function gen_code(ast, options) {
function parenthesize(expr) {
var gen = make(expr);
- for (var i = 1; i < arguments.length; ++i) {
+ if (!has_parens(gen)) for (var i = 1; i < arguments.length; ++i) {
var el = arguments[i];
if ((el instanceof Function && el(expr)) || expr[0] == el)
return "(" + gen + ")";
@@ -1553,6 +1553,22 @@ function gen_code(ast, options) {
return !HOP(DOT_CALL_NO_PARENS, expr[0]);
};
+ function has_parens(expr) {
+ var parentheses = 0, position = 0, token;
+ do {
+ token = jsp.tokenizer(expr.slice(position))();
+ position += token.endpos;
+ if (token.type == "punc") {
+ if (token.value == "(") {
+ ++parentheses;
+ } else if (token.value == ")") {
+ --parentheses;
+ }
+ }
+ } while (position < expr.length && parentheses > 0);
+ return position == expr.length && parentheses == 0;
+ }
+
function make_num(num) {
var str = num.toString(10), a = [ str.replace(/^0\./, ".") ], m;
if (Math.floor(num) === num) {
@@ -1663,7 +1679,7 @@ function gen_code(ast, options) {
if (expr[0] == "num") {
if (!/[a-f.]/i.test(out))
out += ".";
- } else if (expr[0] != "function" && needs_parens(expr))
+ } else if (expr[0] != "function" && !has_parens(out) && needs_parens(expr))
out = "(" + out + ")";
while (i < arguments.length)
out += "." + make_name(arguments[i++]);
View
1 test/unit/compress/expected/issue368.js
@@ -0,0 +1 @@
+({}).constructor,new((0,Math)?Function:Math),((0,0)&0).valueOf()
View
3 test/unit/compress/test/issue368.js
@@ -0,0 +1,3 @@
+({}.constructor);
+new ((0, Math) ? Function : Math)();
+((0, 0) & 0).valueOf();

0 comments on commit 301d37b

Please sign in to comment.