enhance switches (#3583)
This commit is contained in:
@@ -4828,10 +4828,12 @@ merge(Compressor.prototype, {
|
|||||||
for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
|
for (var i = 0, len = self.body.length; i < len && !exact_match; i++) {
|
||||||
branch = self.body[i];
|
branch = self.body[i];
|
||||||
if (branch instanceof AST_Default) {
|
if (branch instanceof AST_Default) {
|
||||||
if (!default_branch) {
|
var prev = body[body.length - 1];
|
||||||
default_branch = branch;
|
if (default_branch || is_break(branch.body[0], compressor) && (!prev || aborts(prev))) {
|
||||||
|
eliminate_branch(branch, prev);
|
||||||
|
continue;
|
||||||
} else {
|
} else {
|
||||||
eliminate_branch(branch, body[body.length - 1]);
|
default_branch = branch;
|
||||||
}
|
}
|
||||||
} else if (!(value instanceof AST_Node)) {
|
} else if (!(value instanceof AST_Node)) {
|
||||||
var exp = branch.expression.evaluate(compressor);
|
var exp = branch.expression.evaluate(compressor);
|
||||||
@@ -4857,8 +4859,15 @@ merge(Compressor.prototype, {
|
|||||||
prev.body = [];
|
prev.body = [];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (default_branch
|
||||||
|
&& default_branch.body.length == 0
|
||||||
|
&& body[body.length - 1] === default_branch
|
||||||
|
&& !branch.expression.has_side_effects(compressor)) {
|
||||||
|
default_branch.body = branch.body.slice();
|
||||||
|
} else {
|
||||||
body.push(branch);
|
body.push(branch);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
|
while (i < len) eliminate_branch(self.body[i++], body[body.length - 1]);
|
||||||
if (body.length > 0) {
|
if (body.length > 0) {
|
||||||
body[0].body = decl.concat(body[0].body);
|
body[0].body = decl.concat(body[0].body);
|
||||||
@@ -4866,8 +4875,7 @@ merge(Compressor.prototype, {
|
|||||||
self.body = body;
|
self.body = body;
|
||||||
while (branch = body[body.length - 1]) {
|
while (branch = body[body.length - 1]) {
|
||||||
var stat = branch.body[branch.body.length - 1];
|
var stat = branch.body[branch.body.length - 1];
|
||||||
if (stat instanceof AST_Break && compressor.loopcontrol_target(stat) === self)
|
if (is_break(stat, compressor)) branch.body.pop();
|
||||||
branch.body.pop();
|
|
||||||
if (branch.body.length || branch instanceof AST_Case
|
if (branch.body.length || branch instanceof AST_Case
|
||||||
&& (default_branch || branch.expression.has_side_effects(compressor))) break;
|
&& (default_branch || branch.expression.has_side_effects(compressor))) break;
|
||||||
if (body.pop() === default_branch) default_branch = null;
|
if (body.pop() === default_branch) default_branch = null;
|
||||||
@@ -4885,8 +4893,7 @@ merge(Compressor.prototype, {
|
|||||||
if (has_break
|
if (has_break
|
||||||
|| node instanceof AST_Lambda
|
|| node instanceof AST_Lambda
|
||||||
|| node instanceof AST_SimpleStatement) return true;
|
|| node instanceof AST_SimpleStatement) return true;
|
||||||
if (node instanceof AST_Break && tw.loopcontrol_target(node) === self)
|
if (is_break(node, tw)) has_break = true;
|
||||||
has_break = true;
|
|
||||||
});
|
});
|
||||||
self.walk(tw);
|
self.walk(tw);
|
||||||
if (!has_break) {
|
if (!has_break) {
|
||||||
@@ -4905,6 +4912,10 @@ merge(Compressor.prototype, {
|
|||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
|
|
||||||
|
function is_break(node, tw) {
|
||||||
|
return node instanceof AST_Break && tw.loopcontrol_target(node) === self;
|
||||||
|
}
|
||||||
|
|
||||||
function eliminate_branch(branch, prev) {
|
function eliminate_branch(branch, prev) {
|
||||||
if (prev && !aborts(prev)) {
|
if (prev && !aborts(prev)) {
|
||||||
prev.body = prev.body.concat(branch.body);
|
prev.body = prev.body.concat(branch.body);
|
||||||
|
|||||||
@@ -261,13 +261,13 @@ drop_default_1: {
|
|||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -279,14 +279,14 @@ drop_default_2: {
|
|||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz(); break;
|
case "bar": baz(); break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -298,7 +298,7 @@ keep_default: {
|
|||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
default:
|
default:
|
||||||
something();
|
something();
|
||||||
break;
|
break;
|
||||||
@@ -306,7 +306,7 @@ keep_default: {
|
|||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
default:
|
default:
|
||||||
something();
|
something();
|
||||||
}
|
}
|
||||||
@@ -347,21 +347,48 @@ issue_1663: {
|
|||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
drop_case: {
|
drop_case_1: {
|
||||||
options = {
|
options = {
|
||||||
dead_code: true,
|
dead_code: true,
|
||||||
switches: true,
|
switches: true,
|
||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz(); break;
|
case "bar": baz(); break;
|
||||||
case 'moo':
|
case "moo":
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz();
|
case "bar": baz();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drop_case_2: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (foo) {
|
||||||
|
case "bar":
|
||||||
|
bar();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
case "moo":
|
||||||
|
moo();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
switch (foo) {
|
||||||
|
case "bar":
|
||||||
|
bar();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
moo();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -373,14 +400,14 @@ keep_case: {
|
|||||||
}
|
}
|
||||||
input: {
|
input: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz(); break;
|
case "bar": baz(); break;
|
||||||
case moo:
|
case moo:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect: {
|
expect: {
|
||||||
switch (foo) {
|
switch (foo) {
|
||||||
case 'bar': baz(); break;
|
case "bar": baz(); break;
|
||||||
case moo:
|
case moo:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -494,7 +521,7 @@ issue_1674: {
|
|||||||
expect_stdout: "PASS"
|
expect_stdout: "PASS"
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_1679: {
|
issue_1679_1: {
|
||||||
options = {
|
options = {
|
||||||
dead_code: true,
|
dead_code: true,
|
||||||
evaluate: true,
|
evaluate: true,
|
||||||
@@ -525,7 +552,6 @@ issue_1679: {
|
|||||||
function f() {
|
function f() {
|
||||||
switch (--b) {
|
switch (--b) {
|
||||||
default:
|
default:
|
||||||
case !function x() {}:
|
|
||||||
break;
|
break;
|
||||||
case b--:
|
case b--:
|
||||||
switch (0) {
|
switch (0) {
|
||||||
@@ -539,7 +565,53 @@ issue_1679: {
|
|||||||
f();
|
f();
|
||||||
console.log(a, b);
|
console.log(a, b);
|
||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: "99 8"
|
||||||
|
}
|
||||||
|
|
||||||
|
issue_1679_2: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
evaluate: true,
|
||||||
|
passes: 2,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 100, b = 10;
|
||||||
|
function f() {
|
||||||
|
switch (--b) {
|
||||||
|
default:
|
||||||
|
case !function x() {}:
|
||||||
|
break;
|
||||||
|
case b--:
|
||||||
|
switch (0) {
|
||||||
|
default:
|
||||||
|
case a--:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case (a++):
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 100, b = 10;
|
||||||
|
function f() {
|
||||||
|
switch (--b) {
|
||||||
|
case b--:
|
||||||
|
switch (0) {
|
||||||
|
default:
|
||||||
|
case a--:
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case (a++):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f();
|
||||||
|
console.log(a, b);
|
||||||
|
}
|
||||||
|
expect_stdout: "99 8"
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_1680_1: {
|
issue_1680_1: {
|
||||||
@@ -864,3 +936,65 @@ issue_1750: {
|
|||||||
}
|
}
|
||||||
expect_stdout: "0 2"
|
expect_stdout: "0 2"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drop_switch_1: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (foo) {
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
case "bar":
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
foo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drop_switch_2: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
switch (foo) {
|
||||||
|
default:
|
||||||
|
case "bar":
|
||||||
|
baz();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
foo;
|
||||||
|
baz();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
drop_switch_3: {
|
||||||
|
options = {
|
||||||
|
dead_code: true,
|
||||||
|
switches: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
console.log(function() {
|
||||||
|
switch (0) {
|
||||||
|
default:
|
||||||
|
return "PASS";
|
||||||
|
case 1:
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
console.log(function() {
|
||||||
|
switch (0) {
|
||||||
|
default:
|
||||||
|
return "PASS";
|
||||||
|
case 1:
|
||||||
|
}
|
||||||
|
}());
|
||||||
|
}
|
||||||
|
expect_stdout: "PASS"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user