enhance dead_code (#5130)

This commit is contained in:
Alex Lam S.L
2021-09-21 20:11:45 +01:00
committed by GitHub
parent 55418fd460
commit 436a29367c
2 changed files with 31 additions and 19 deletions

View File

@@ -3331,14 +3331,11 @@ merge(Compressor.prototype, {
var stat = statements[i];
if (stat instanceof AST_LoopControl) {
var lct = compressor.loopcontrol_target(stat);
if (stat instanceof AST_Break
&& !(lct instanceof AST_IterationStatement)
&& loop_body(lct) === self
|| stat instanceof AST_Continue
&& loop_body(lct) === self) {
if (stat.label) remove(stat.label.thedef.references, stat);
} else {
if (loop_body(lct) !== self
|| stat instanceof AST_Break && lct instanceof AST_IterationStatement) {
statements[n++] = stat;
} else if (stat.label) {
remove(stat.label.thedef.references, stat);
}
} else {
statements[n++] = stat;
@@ -5485,6 +5482,21 @@ merge(Compressor.prototype, {
return compressor.option("unused") && self.label.references.length == 0 ? self.body : self;
});
OPT(AST_LoopControl, function(self, compressor) {
if (!compressor.option("dead_code")) return self;
var label = self.label;
if (label) {
var lct = compressor.loopcontrol_target(self);
self.label = null;
if (compressor.loopcontrol_target(self) === lct) {
remove(label.thedef.references, self);
} else {
self.label = label;
}
}
return self;
});
OPT(AST_Block, function(self, compressor) {
self.body = tighten_body(self.body, compressor);
return self;

View File

@@ -83,8 +83,9 @@ labels_5: {
conditionals: true,
dead_code: true,
if_return: true,
unused: true,
}
// should keep the break-s in the following
// should keep `break`s below
input: {
while (foo) {
if (bar) break;
@@ -100,8 +101,8 @@ labels_5: {
if (bar) break;
console.log("foo");
}
out: while (foo) {
if (bar) break out;
while (foo) {
if (bar) break;
console.log("foo");
}
}
@@ -189,23 +190,22 @@ labels_10: {
conditionals: true,
dead_code: true,
if_return: true,
unused: true,
}
input: {
out: while (foo) {
x();
y();
out: while (42) {
console.log("PASS");
break out;
z();
k();
console.log("FAIL");
}
};
expect: {
out: while (foo) {
x();
y();
break out;
while (42) {
console.log("PASS");
break;
}
}
expect_stdout: "PASS"
}
issue_4466_1: {