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

View File

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