From c76ee4b868f2b2cdf87e7ab4a000997c475bdbf3 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 10 May 2020 21:29:55 +0100 Subject: [PATCH] enhance `if_return` (#3875) --- lib/compress.js | 6 ++++-- test/compress/if_return.js | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index fcbbffb3..4f942429 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2027,8 +2027,10 @@ merge(Compressor.prototype, { function handle_if_return(statements, compressor) { var self = compressor.self(); - var multiple_if_returns = has_multiple_if_returns(statements); + var parent = compressor.parent(); var in_lambda = self instanceof AST_Lambda; + var in_iife = in_lambda && parent && parent.TYPE == "Call"; + var multiple_if_returns = has_multiple_if_returns(statements); for (var i = statements.length; --i >= 0;) { var stat = statements[i]; var j = next_index(i); @@ -2156,7 +2158,7 @@ merge(Compressor.prototype, { // the example code. var prev = statements[prev_index(i)]; if (compressor.option("sequences") && in_lambda && !stat.alternative - && prev instanceof AST_If && prev.body instanceof AST_Return + && (!prev && in_iife || prev instanceof AST_If && prev.body instanceof AST_Return) && next_index(j) == statements.length && next instanceof AST_SimpleStatement) { CHANGED = true; stat = stat.clone(); diff --git a/test/compress/if_return.js b/test/compress/if_return.js index 82df1cea..836fab17 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -573,3 +573,24 @@ issue_3600: { } expect_stdout: "1" } + +iife_if_return_simple: { + options = { + conditionals: true, + if_return: true, + inline: true, + sequences: true, + side_effects: true, + } + input: { + (function() { + if (console) + return console.log("PASS"); + console.log("FAIL"); + })(); + } + expect: { + console ? console.log("PASS") : console.log("FAIL"); + } + expect_stdout: "PASS" +} -- 2.34.1