From 29e1ff0981f80370450511f77aa4bec4cb098bb9 Mon Sep 17 00:00:00 2001 From: Richard van Velzen Date: Fri, 16 Sep 2016 12:45:29 +0200 Subject: [PATCH] Preserve program completion value with enable_iife enabled Fix #640 --- lib/compress.js | 10 ++++++++++ test/compress/negate-iife.js | 18 +++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 8a08572f..9e55aa49 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -838,6 +838,14 @@ merge(Compressor.prototype, { return false; } + function is_completion_statement(node) { + if (compressor.parent()) { + return false; + } + + return statements[statements.length - 1].body === node; + } + statements.forEach(function(stat){ if (stat instanceof AST_SimpleStatement) { stat.body = (function transform(thing) { @@ -846,6 +854,8 @@ merge(Compressor.prototype, { return node; } if (is_iife_call(node)) { + if (is_completion_statement(node)) return node; + return make_node(AST_UnaryPrefix, node, { operator: "!", expression: node diff --git a/test/compress/negate-iife.js b/test/compress/negate-iife.js index 0c111604..faae1d36 100644 --- a/test/compress/negate-iife.js +++ b/test/compress/negate-iife.js @@ -6,7 +6,7 @@ negate_iife_1: { (function(){ stuff() })(); } expect: { - !function(){ stuff() }(); + (function(){ stuff() })(); } } @@ -156,7 +156,7 @@ issue_1254_negate_iife_true: { }; })()(); } - expect_exact: '!function(){return function(){console.log("test")}}()();' + expect_exact: '(function(){return function(){console.log("test")}})()();' } issue_1254_negate_iife_nested: { @@ -170,5 +170,17 @@ issue_1254_negate_iife_nested: { }; })()()()()(); } - expect_exact: '!function(){return function(){console.log("test")}}()()()()();' + expect_exact: '(function(){return function(){console.log("test")}})()()()()();' +} + +preserve_completion_value: { + options = { + negate_iife: true, + } + input: { + (function () { return true; })(); + (function () { return true; })(); + (function () { return true; })(); + } + expect_exact: '!function(){return true}();!function(){return true}();(function(){return true})();' }