Skip to content

Commit

Permalink
enhance conditionals (#5575)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Jul 25, 2022
1 parent fc7678c commit 996836b
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 4 deletions.
29 changes: 25 additions & 4 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -9662,10 +9662,20 @@ Compressor.prototype.compress = function(node) {
var alt_index = last_index(alt_stats);
for (var stats = []; body_index >= 0 && alt_index >= 0;) {
var stat = body_stats[body_index];
if (!stat.equals(alt_stats[alt_index])) break;
body_stats.splice(body_index--, 1);
alt_stats.splice(alt_index--, 1);
stats.unshift(stat);
var alt_stat = alt_stats[alt_index];
if (stat.equals(alt_stat)) {
body_stats.splice(body_index--, 1);
alt_stats.splice(alt_index--, 1);
stats.unshift(stat);
} else {
if (!(stat instanceof AST_SimpleStatement)) break;
if (!(alt_stat instanceof AST_SimpleStatement)) break;
var expr = stat.body.tail_node();
if (!expr.equals(alt_stat.body.tail_node())) break;
body_index = pop_expr(body_stats, stat.body, body_index);
alt_index = pop_expr(alt_stats, alt_stat.body, alt_index);
stats.unshift(make_node(AST_SimpleStatement, expr, { body: expr }));
}
}
if (stats.length > 0) {
self.body = body_stats.length > 0 ? make_node(AST_BlockStatement, self, {
Expand All @@ -9692,6 +9702,17 @@ Compressor.prototype.compress = function(node) {
return index;
}

function pop_expr(stats, body, index) {
if (body instanceof AST_Sequence) {
stats[index] = make_node(AST_SimpleStatement, body, {
body: make_sequence(body, body.expressions.slice(0, -1)),
});
} else {
stats.splice(index--, 1);
}
return index;
}

function sequencesize(stat, defuns, var_defs, refs) {
if (stat == null) return [];
if (stat instanceof AST_BlockStatement) {
Expand Down
90 changes: 90 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,96 @@ merge_tail_2: {
]
}

merge_tail_sequence_1: {
options = {
conditionals: true,
}
input: {
function f(a) {
var b = "foo";
if (a) {
while (console.log("bar"));
console.log(b);
} else {
c = "baz";
while (console.log(c));
console.log("bar"),
console.log(b);
var c;
}
}
f();
f(42);
}
expect: {
function f(a) {
var b = "foo";
if (a)
while (console.log("bar"));
else {
c = "baz";
while (console.log(c));
console.log("bar");
var c;
}
console.log(b);
}
f();
f(42);
}
expect_stdout: [
"baz",
"bar",
"foo",
"bar",
"foo",
]
}

merge_tail_sequence_2: {
options = {
conditionals: true,
}
input: {
function f(a) {
var b = "foo";
if (a) {
console.log("bar");
console.log(b);
} else {
c = "baz";
while (console.log(c));
console.log("bar"),
console.log(b);
var c;
}
}
f();
f(42);
}
expect: {
function f(a) {
var b = "foo";
if (!a) {
c = "baz";
while (console.log(c));
var c;
}
console.log("bar");
console.log(b);
}
f();
f(42);
}
expect_stdout: [
"baz",
"bar",
"foo",
"bar",
"foo",
]
}

cond_1: {
options = {
conditionals: true,
Expand Down

0 comments on commit 996836b

Please sign in to comment.