Fix parens for AST_Undefined
authorMihai Bazon <mihai@bazon.net>
Fri, 18 Jul 2014 08:31:41 +0000 (11:31 +0300)
committerMihai Bazon <mihai@bazon.net>
Fri, 18 Jul 2014 08:31:41 +0000 (11:31 +0300)
Do the same as for AST_Unary, since we output undefined as `void 0`.

Reported at https://github.com/mishoo/UglifyJS2/issues/338#issuecomment-48858341

lib/output.js

index 6c8f15a..7fe61af 100644 (file)
@@ -434,7 +434,13 @@ function OutputStream(options) {
     /* -----[ PARENTHESES ]----- */
 
     function PARENS(nodetype, func) {
-        nodetype.DEFMETHOD("needs_parens", func);
+        if (Array.isArray(nodetype)) {
+            nodetype.forEach(function(nodetype){
+                PARENS(nodetype, func);
+            });
+        } else {
+            nodetype.DEFMETHOD("needs_parens", func);
+        }
     };
 
     PARENS(AST_Node, function(){
@@ -453,7 +459,7 @@ function OutputStream(options) {
         return first_in_statement(output);
     });
 
-    PARENS(AST_Unary, function(output){
+    PARENS([ AST_Unary, AST_Undefined ], function(output){
         var p = output.parent();
         return p instanceof AST_PropAccess && p.expression === this;
     });
@@ -549,7 +555,7 @@ function OutputStream(options) {
             return true;
     });
 
-    function assign_and_conditional_paren_rules(output) {
+    PARENS([ AST_Assign, AST_Conditional ], function (output){
         var p = output.parent();
         // !(a = false) β†’ true
         if (p instanceof AST_Unary)
@@ -566,10 +572,7 @@ function OutputStream(options) {
         // (a = foo)["prop"] β€”orβ€” (a = foo).prop
         if (p instanceof AST_PropAccess && p.expression === this)
             return true;
-    };
-
-    PARENS(AST_Assign, assign_and_conditional_paren_rules);
-    PARENS(AST_Conditional, assign_and_conditional_paren_rules);
+    });
 
     /* -----[ PRINTERS ]----- */