Skip to content

Commit

Permalink
Preserve hoisted functions after continue. Closes #833
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Apr 19, 2023
1 parent 374073b commit 7093b30
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions lib/compress/tighten-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -1002,27 +1002,34 @@ export function tighten_body(statements, compressor) {
}

if (stat instanceof AST_If) {
var ab = aborts(stat.body);
if (can_merge_flow(ab)) {
let ab, new_else;

ab = aborts(stat.body);
if (
can_merge_flow(ab)
&& (new_else = as_statement_array_with_return(stat.body, ab))
) {
if (ab.label) {
remove(ab.label.thedef.references, ab);
}
CHANGED = true;
stat = stat.clone();
stat.condition = stat.condition.negate(compressor);
var body = as_statement_array_with_return(stat.body, ab);
stat.body = make_node(AST_BlockStatement, stat, {
body: as_statement_array(stat.alternative).concat(extract_functions())
});
stat.alternative = make_node(AST_BlockStatement, stat, {
body: body
body: new_else
});
statements[i] = stat.transform(compressor);
continue;
}

var ab = aborts(stat.alternative);
if (can_merge_flow(ab)) {
ab = aborts(stat.alternative);
if (
can_merge_flow(ab)
&& (new_else = as_statement_array_with_return(stat.alternative, ab))
) {
if (ab.label) {
remove(ab.label.thedef.references, ab);
}
Expand All @@ -1031,9 +1038,8 @@ export function tighten_body(statements, compressor) {
stat.body = make_node(AST_BlockStatement, stat.body, {
body: as_statement_array(stat.body).concat(extract_functions())
});
var body = as_statement_array_with_return(stat.alternative, ab);
stat.alternative = make_node(AST_BlockStatement, stat.alternative, {
body: body
body: new_else
});
statements[i] = stat.transform(compressor);
continue;
Expand Down Expand Up @@ -1148,7 +1154,11 @@ export function tighten_body(statements, compressor) {
}

function as_statement_array_with_return(node, ab) {
var body = as_statement_array(node).slice(0, -1);
var body = as_statement_array(node);
if (ab !== body[body.length - 1]) {
return undefined;
}
body = body.slice(0, -1);
if (ab.value) {
body.push(make_node(AST_SimpleStatement, ab.value, {
body: ab.value.expression
Expand Down

0 comments on commit 7093b30

Please sign in to comment.