Skip to content

Commit

Permalink
enhance if_return (#5330)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Feb 2, 2022
1 parent 77552d9 commit aad5d6e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
11 changes: 4 additions & 7 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -3294,7 +3294,8 @@ Compressor.prototype.compress = function(node) {
function handle_if_return(statements, compressor) {
var changed = false;
var parent = compressor.parent();
var in_iife = in_lambda && parent && parent.TYPE == "Call";
var self = compressor.self();
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 multiple_if_returns = has_multiple_if_returns(statements);
for (var i = statements.length; --i >= 0;) {
Expand Down Expand Up @@ -3371,15 +3372,11 @@ Compressor.prototype.compress = function(node) {

if (compressor.option("typeofs")) {
if (ab && !alt) {
var stats = make_node(AST_BlockStatement, compressor.self(), {
body: statements.slice(i + 1),
});
var stats = make_node(AST_BlockStatement, self, { body: statements.slice(i + 1) });
mark_locally_defined(stat.condition, null, stats);
}
if (!ab && alt) {
var stats = make_node(AST_BlockStatement, compressor.self(), {
body: statements.slice(i + 1),
});
var stats = make_node(AST_BlockStatement, self, { body: statements.slice(i + 1) });
mark_locally_defined(stat.condition, stats);
}
}
Expand Down
26 changes: 25 additions & 1 deletion test/compress/if_return.js
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,31 @@ if_var_return_2: {
}
}

if_var_return_3: {
if_var_retrn_3: {
options = {
conditionals: true,
if_return: true,
sequences: true,
}
input: {
f(function() {
var a = w();
if (x())
return y(a);
z();
});
}
expect: {
f(function() {
var a = w();
if (x())
return y(a);
z();
});
}
}

if_var_return_4: {
options = {
conditionals: true,
if_return: true,
Expand Down

0 comments on commit aad5d6e

Please sign in to comment.