From 8430123e9d2d12442312bbdcdf54004dc6d29c12 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Wed, 17 Aug 2016 11:43:50 +0200 Subject: [PATCH] Fix negate_iife transform to return a correct tree for nested IIFEs Fix for #1256, partially reverts d854523783b4 --- lib/compress.js | 9 ++++++++- lib/output.js | 2 -- test/compress/negate-iife.js | 14 ++++++++++++++ test/compress/new.js | 7 +++++++ 4 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index fd839fa1..f8d9d329 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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 diff --git a/lib/output.js b/lib/output.js index f1e0c2f1..801f7516 100644 --- a/lib/output.js +++ b/lib/output.js @@ -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; diff --git a/test/compress/negate-iife.js b/test/compress/negate-iife.js index aa95d958..0c111604 100644 --- a/test/compress/negate-iife.js +++ b/test/compress/negate-iife.js @@ -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")}}()()()()();' +} diff --git a/test/compress/new.js b/test/compress/new.js index bdf22b0c..83da88e6 100644 --- a/test/compress/new.js +++ b/test/compress/new.js @@ -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);'; +} -- 2.34.1