Fix negate_iife transform to return a correct tree for nested IIFEs
authorRichard van Velzen <rvanvelzen@experty.com>
Wed, 17 Aug 2016 09:43:50 +0000 (11:43 +0200)
committerRichard van Velzen <rvanvelzen@experty.com>
Wed, 17 Aug 2016 09:55:59 +0000 (11:55 +0200)
Fix for #1256, partially reverts d854523783b4

lib/compress.js
lib/output.js
test/compress/negate-iife.js
test/compress/new.js

index fd839fa..f8d9d32 100644 (file)
@@ -831,6 +831,13 @@ merge(Compressor.prototype, {
         };
 
         function negate_iifes(statements, compressor) {
+            function is_iife_call(node) {
+                if (node instanceof AST_Call) {
+                    return node.expression instanceof AST_Function || is_iife_call(node.expression);
+                }
+                return false;
+            }
+
             statements.forEach(function(stat){
                 if (stat instanceof AST_SimpleStatement) {
                     stat.body = (function transform(thing) {
@@ -838,7 +845,7 @@ merge(Compressor.prototype, {
                             if (node instanceof AST_New) {
                                 return node;
                             }
-                            if (node instanceof AST_Call && node.expression instanceof AST_Function) {
+                            if (is_iife_call(node)) {
                                 return make_node(AST_UnaryPrefix, node, {
                                     operator: "!",
                                     expression: node
index f1e0c2f..801f751 100644 (file)
@@ -531,8 +531,6 @@ function OutputStream(options) {
     });
 
     PARENS([ AST_Unary, AST_Undefined ], function(output){
-        if (this.expression instanceof AST_Call)
-            return false;
         var p = output.parent();
         return p instanceof AST_PropAccess && p.expression === this
             || p instanceof AST_Call && p.expression === this;
index aa95d95..0c11160 100644 (file)
@@ -158,3 +158,17 @@ issue_1254_negate_iife_true: {
     }
     expect_exact: '!function(){return function(){console.log("test")}}()();'
 }
+
+issue_1254_negate_iife_nested: {
+    options = {
+        negate_iife: true,
+    }
+    input: {
+        (function() {
+            return function() {
+                console.log('test')
+            };
+        })()()()()();
+    }
+    expect_exact: '!function(){return function(){console.log("test")}}()()()()();'
+}
index bdf22b0..83da88e 100644 (file)
@@ -75,3 +75,10 @@ call_with_unary_arguments: {
     }
     expect_exact: "x();x(-1);x(-1,-2);x(void 1,+2,-3,~4,!5,--a,++b,c--,d++,typeof e,delete f);(-1)();(-1)(-2);"
 }
+
+new_with_unary_prefix: {
+    input: {
+        var bar = (+new Date()).toString(32);
+    }
+    expect_exact: 'var bar=(+new Date).toString(32);';
+}