diff --git a/lib/compress.js b/lib/compress.js index f6d6a9fb..25e46596 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3452,15 +3452,16 @@ Compressor.prototype.compress = function(node) { var parent = compressor.parent(); var self = compressor.self(); var declare_only, jump, merge_jump; - var in_iife = in_lambda && parent && parent.TYPE == "Call" && parent.expression === self; - var chain_if_returns = in_lambda && compressor.option("conditionals") && compressor.option("sequences"); + var in_tail = in_lambda && self instanceof AST_Block && self.body === statements; + var in_iife = in_tail && parent && parent.TYPE == "Call" && parent.expression === self; + var chain_if_returns = in_tail && compressor.option("conditionals") && compressor.option("sequences"); 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); var next = statements[j]; - if (in_lambda && declare_only && !next && stat instanceof AST_Return + if (in_tail && declare_only && !next && stat instanceof AST_Return && !(self instanceof AST_SwitchBranch) && !(in_try && in_try.bfinally && in_async_generator(in_lambda))) { var body = stat.value; @@ -3563,7 +3564,7 @@ Compressor.prototype.compress = function(node) { // if (foo()) return x; [ return ; ] ---> return foo() ? x : undefined; // if (foo()) return bar() ? x : void 0; ---> return foo() && bar() ? x : void 0; // if (foo()) return bar() ? void 0 : x; ---> return !foo() || bar() ? void 0 : x; - if (in_lambda && declare_only && !next && !stat.alternative && (in_bool + if (in_tail && declare_only && !next && !stat.alternative && (in_bool || value && multiple_if_returns || value instanceof AST_Conditional && (is_undefined(value.consequent, compressor) || is_undefined(value.alternative, compressor)))) { @@ -3644,7 +3645,7 @@ Compressor.prototype.compress = function(node) { function can_drop_abort(ab) { if (ab instanceof AST_Exit) { if (merge_jump = match_return(ab)) return true; - if (!in_lambda) return false; + if (!in_tail) return false; if (!(ab instanceof AST_Return)) return false; var value = ab.value; if (value && !is_undefined(value.tail_node())) return false; @@ -3657,7 +3658,7 @@ Compressor.prototype.compress = function(node) { if (!(ab instanceof AST_LoopControl)) return false; if (jump && self instanceof AST_SwitchBranch) { if (jump instanceof AST_Exit) { - if (!in_lambda) return false; + if (!in_tail) return false; if (jump.value) return false; } else if (compressor.loopcontrol_target(jump) !== parent) { return false; @@ -6162,7 +6163,8 @@ Compressor.prototype.compress = function(node) { function opt_arrow(self, compressor) { if (!compressor.option("arrows")) return self; drop_rest_farg(self, compressor); - var body = tighten_body(self.value ? [ self.first_statement() ] : self.body, compressor); + if (self.value) self.body = [ self.first_statement() ]; + var body = tighten_body(self.body, compressor); switch (body.length) { case 1: var stat = body[0]; diff --git a/test/compress/if_return.js b/test/compress/if_return.js index 7b6c6977..40c6f2d8 100644 --- a/test/compress/if_return.js +++ b/test/compress/if_return.js @@ -2253,3 +2253,29 @@ issue_5592_2: { "baz", ] } + +issue_5595: { + options = { + conditionals: true, + if_return: true, + } + input: { + function f(a) { + if (a) { + var b; + if (b++) + return "FAIL"; + } else + return "PASS"; + } + console.log(f()); + } + expect: { + function f(a) { + var b; + return a ? b++ ? "FAIL" : void 0 : "PASS"; + } + console.log(f()); + } + expect_stdout: "PASS" +}