improve switch case compression (#2547)
This commit is contained in:
@@ -3312,6 +3312,11 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
} else if (!(value instanceof AST_Node)) {
|
} else if (!(value instanceof AST_Node)) {
|
||||||
var exp = branch.expression.evaluate(compressor);
|
var exp = branch.expression.evaluate(compressor);
|
||||||
|
if (!(exp instanceof AST_Node) && exp !== value) {
|
||||||
|
eliminate_branch(branch, body[body.length - 1]);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (exp instanceof AST_Node) exp = branch.expression.tail_node().evaluate(compressor);
|
||||||
if (exp === value) {
|
if (exp === value) {
|
||||||
exact_match = branch;
|
exact_match = branch;
|
||||||
if (default_branch) {
|
if (default_branch) {
|
||||||
@@ -3320,9 +3325,6 @@ merge(Compressor.prototype, {
|
|||||||
eliminate_branch(default_branch, body[default_index - 1]);
|
eliminate_branch(default_branch, body[default_index - 1]);
|
||||||
default_branch = null;
|
default_branch = null;
|
||||||
}
|
}
|
||||||
} else if (exp !== branch.expression) {
|
|
||||||
eliminate_branch(branch, body[body.length - 1]);
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (aborts(branch)) {
|
if (aborts(branch)) {
|
||||||
@@ -3365,12 +3367,16 @@ merge(Compressor.prototype, {
|
|||||||
});
|
});
|
||||||
self.walk(tw);
|
self.walk(tw);
|
||||||
if (!has_break) {
|
if (!has_break) {
|
||||||
body = body[0].body.slice();
|
var statements = body[0].body.slice();
|
||||||
body.unshift(make_node(AST_SimpleStatement, self.expression, {
|
var exp = body[0].expression;
|
||||||
body: self.expression
|
if (exp) statements.unshift(make_node(AST_SimpleStatement, exp, {
|
||||||
|
body: exp
|
||||||
|
}));
|
||||||
|
statements.unshift(make_node(AST_SimpleStatement, self.expression, {
|
||||||
|
body:self.expression
|
||||||
}));
|
}));
|
||||||
return make_node(AST_BlockStatement, self, {
|
return make_node(AST_BlockStatement, self, {
|
||||||
body: body
|
body: statements
|
||||||
}).optimize(compressor);
|
}).optimize(compressor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ case_1: {
|
|||||||
input: {
|
input: {
|
||||||
var a = 0, b = 1;
|
var a = 0, b = 1;
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case a, true:
|
case a || true:
|
||||||
default:
|
default:
|
||||||
b = 2;
|
b = 2;
|
||||||
case true:
|
case true:
|
||||||
@@ -17,7 +17,7 @@ case_1: {
|
|||||||
expect: {
|
expect: {
|
||||||
var a = 0, b = 1;
|
var a = 0, b = 1;
|
||||||
switch (true) {
|
switch (true) {
|
||||||
case a, true:
|
case a || true:
|
||||||
b = 2;
|
b = 2;
|
||||||
}
|
}
|
||||||
console.log(a, b);
|
console.log(a, b);
|
||||||
|
|||||||
@@ -833,7 +833,34 @@ issue_2535: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
w(), 42;
|
w(), 42;
|
||||||
|
42;
|
||||||
y();
|
y();
|
||||||
z();
|
z();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
issue_1750: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
evaluate: true,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 0, b = 1;
|
||||||
|
switch (true) {
|
||||||
|
case a, true:
|
||||||
|
default:
|
||||||
|
b = 2;
|
||||||
|
case true:
|
||||||
|
}
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 0, b = 1;
|
||||||
|
true;
|
||||||
|
a, true;
|
||||||
|
b = 2;
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect_stdout: "0 2"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user