Skip to content

Commit 436a293

Browse files
authored
enhance dead_code (#5130)
1 parent 55418fd commit 436a293

2 files changed

Lines changed: 31 additions & 19 deletions

File tree

lib/compress.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,14 +3331,11 @@ merge(Compressor.prototype, {
33313331
var stat = statements[i];
33323332
if (stat instanceof AST_LoopControl) {
33333333
var lct = compressor.loopcontrol_target(stat);
3334-
if (stat instanceof AST_Break
3335-
&& !(lct instanceof AST_IterationStatement)
3336-
&& loop_body(lct) === self
3337-
|| stat instanceof AST_Continue
3338-
&& loop_body(lct) === self) {
3339-
if (stat.label) remove(stat.label.thedef.references, stat);
3340-
} else {
3334+
if (loop_body(lct) !== self
3335+
|| stat instanceof AST_Break && lct instanceof AST_IterationStatement) {
33413336
statements[n++] = stat;
3337+
} else if (stat.label) {
3338+
remove(stat.label.thedef.references, stat);
33423339
}
33433340
} else {
33443341
statements[n++] = stat;
@@ -5485,6 +5482,21 @@ merge(Compressor.prototype, {
54855482
return compressor.option("unused") && self.label.references.length == 0 ? self.body : self;
54865483
});
54875484

5485+
OPT(AST_LoopControl, function(self, compressor) {
5486+
if (!compressor.option("dead_code")) return self;
5487+
var label = self.label;
5488+
if (label) {
5489+
var lct = compressor.loopcontrol_target(self);
5490+
self.label = null;
5491+
if (compressor.loopcontrol_target(self) === lct) {
5492+
remove(label.thedef.references, self);
5493+
} else {
5494+
self.label = label;
5495+
}
5496+
}
5497+
return self;
5498+
});
5499+
54885500
OPT(AST_Block, function(self, compressor) {
54895501
self.body = tighten_body(self.body, compressor);
54905502
return self;

test/compress/labels.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,9 @@ labels_5: {
8383
conditionals: true,
8484
dead_code: true,
8585
if_return: true,
86+
unused: true,
8687
}
87-
// should keep the break-s in the following
88+
// should keep `break`s below
8889
input: {
8990
while (foo) {
9091
if (bar) break;
@@ -100,8 +101,8 @@ labels_5: {
100101
if (bar) break;
101102
console.log("foo");
102103
}
103-
out: while (foo) {
104-
if (bar) break out;
104+
while (foo) {
105+
if (bar) break;
105106
console.log("foo");
106107
}
107108
}
@@ -189,23 +190,22 @@ labels_10: {
189190
conditionals: true,
190191
dead_code: true,
191192
if_return: true,
193+
unused: true,
192194
}
193195
input: {
194-
out: while (foo) {
195-
x();
196-
y();
196+
out: while (42) {
197+
console.log("PASS");
197198
break out;
198-
z();
199-
k();
199+
console.log("FAIL");
200200
}
201201
};
202202
expect: {
203-
out: while (foo) {
204-
x();
205-
y();
206-
break out;
203+
while (42) {
204+
console.log("PASS");
205+
break;
207206
}
208207
}
208+
expect_stdout: "PASS"
209209
}
210210

211211
issue_4466_1: {

0 commit comments

Comments
 (0)