Skip to content

Loading…

Implement #368 (too many parentheses) #385

Merged
merged 1 commit into from

2 participants

@RGustBardon

In contrast to 67e6163, the tokenizer is used.

@mishoo mishoo merged commit 301d37b into mishoo:master
@mishoo
Owner

Nice one, thanks!

@rvanvelzen rvanvelzen referenced this pull request
Merged

Fix issue #386 #387

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 8, 2012
  1. @RGustBardon
This page is out of date. Refresh to see the latest.
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();
Something went wrong with that request. Please try again.