Skip to content
Browse files

Remove unnecessary punctuators in some expressions

Object initialisers and function expressions were unnecessarily
wrapped in parentheses in unary and binary expressions.  Unnecessary
parentheses were also added around a new expression within a
UnaryExpression nonterminal.  Superfluous space was added after
delete, void, and typeof tokens.

Object initialisers were redundantly wrapped in extra parentheses in
static MemberExpressions.  This patch is expected, inter alia, to
close issue #368.
  • Loading branch information...
1 parent e87718e commit b4894538c681ac504f75f02afd83e435e4b594a3 @RGustBardon RGustBardon committed Jun 5, 2012
View
19 lib/process.js
@@ -1684,7 +1684,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" && expr[0] != "object" && needs_parens(expr))
out = "(" + out + ")";
while (i < arguments.length)
out += "." + make_name(arguments[i++]);
@@ -1740,8 +1740,7 @@ function gen_code(ast, options) {
// we need to be smarter.
// adding parens all the time is the safest bet.
if (member(lvalue[0], [ "assign", "conditional", "seq" ]) ||
- lvalue[0] == "binary" && PRECEDENCE[operator] > PRECEDENCE[lvalue[1]] ||
- lvalue[0] == "function" && needs_parens(this)) {
+ lvalue[0] == "binary" && PRECEDENCE[operator] > PRECEDENCE[lvalue[1]]) {
left = "(" + left + ")";
}
if (member(rvalue[0], [ "assign", "conditional", "seq" ]) ||
@@ -1757,13 +1756,21 @@ function gen_code(ast, options) {
},
"unary-prefix": function(operator, expr) {
var val = make(expr);
- if (!(expr[0] == "num" || (expr[0] == "unary-prefix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
+ if (!HOP(DOT_CALL_NO_PARENS, expr[0])
+ && (expr[0] != "unary-prefix" || HOP(OPERATORS, operator + expr[1]))
+ && expr[0] != "unary-postfix"
+ && expr[0] != "new"
+ && expr[0] != "num"
+ && expr[0] != "atom"
+ && expr[0] != "function")
val = "(" + val + ")";
- return operator + (jsp.is_alphanumeric_char(operator.charAt(0)) ? " " : "") + val;
+ return (operator == "delete" || operator == "void" || operator == "typeof" ?
+ add_spaces([ operator, val ]) : operator + val);
},
"unary-postfix": function(operator, expr) {
var val = make(expr);
- if (!(expr[0] == "num" || (expr[0] == "unary-postfix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
+ if (!(expr[0] == "num" || expr[0] == "function" || expr[0] == "object"
+ || (expr[0] == "unary-postfix" && !HOP(OPERATORS, operator + expr[1])) || !needs_parens(expr)))
val = "(" + val + ")";
return val + operator;
},
View
1 test/unit/compress/expected/issue368.js
@@ -0,0 +1 @@
+({}).constructor!==Object&&alert("Object was overwritten. Environment is broken!")
View
2 test/unit/compress/expected/issue372.js
@@ -1 +1 @@
-"use strict";function a(){function a(){function a(){void "c"}function b(){"salmon";void "d"}void "b"}function b(){"salmon";function a(){void "f";for(var a=0;a<10;a++)var b=function(){void "g"}()}void "e"}void "a"}
+"use strict";function a(){function a(){function a(){void"c"}function b(){"salmon";void"d"}void"b"}function b(){"salmon";function a(){void"f";for(var a=0;a<10;a++)var b=function(){void"g"}()}void"e"}void"a"}
View
2 test/unit/compress/expected/issues222_324.js
@@ -1 +1 @@
-!(function(){"use strict";var a,b,c}),!function(){"use strict";var a,b},!function(){"use strict";var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B},!function(){"use strict";var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B}
+!function(){"use strict";var a,b,c},!function(){"use strict";var a,b},!function(){"use strict";var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B},!function(){"use strict";var a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B}
View
1 test/unit/compress/expected/punctuators.js
@@ -0,0 +1 @@
+if(function(){}+function(){})delete[],void[],typeof[],delete{},delete+[],delete+(+[]);(function(){})++
View
3 test/unit/compress/test/issue368.js
@@ -0,0 +1,3 @@
+if ({}.constructor !== Object) {
+ alert('Object was overwritten. Environment is broken!')
+}
View
1 test/unit/compress/test/punctuators.js
@@ -0,0 +1 @@
+if(function(){}+function(){})delete[],void[],typeof[],delete{},delete+[],delete+(+[]);(function(){})++

0 comments on commit b489453

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