Skip to content

Commit 8b46433

Browse files
authored
enhance dead_code & if_return (#5520)
1 parent 9f57920 commit 8b46433

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

lib/compress.js

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,13 +1951,26 @@ Compressor.prototype.compress = function(node) {
19511951
return statements;
19521952

19531953
function last_of(compressor, predicate) {
1954-
var block = compressor.self(), stat, level = 0;
1954+
var block = compressor.self(), level = 0;
19551955
do {
1956-
do {
1956+
if (block instanceof AST_Catch || block instanceof AST_Finally) {
1957+
block = compressor.parent(level++);
1958+
} else if (block instanceof AST_LabeledStatement) {
1959+
block = block.body;
1960+
}
1961+
var stat = null;
1962+
while (true) {
19571963
if (predicate(block)) return true;
19581964
block = compressor.parent(level++);
1959-
} while (block instanceof AST_If && (stat = block));
1960-
} while ((block instanceof AST_BlockStatement || block instanceof AST_Scope)
1965+
if (!(block instanceof AST_If)) break;
1966+
stat = block;
1967+
}
1968+
} while (stat
1969+
&& (block instanceof AST_BlockStatement
1970+
|| block instanceof AST_Catch
1971+
|| block instanceof AST_Finally
1972+
|| block instanceof AST_Scope
1973+
|| block instanceof AST_Try)
19611974
&& is_last_statement(block.body, stat));
19621975
}
19631976

@@ -3677,6 +3690,11 @@ Compressor.prototype.compress = function(node) {
36773690
function eliminate_dead_code(statements, compressor) {
36783691
var has_quit;
36793692
var self = compressor.self();
3693+
if (self instanceof AST_Catch || self instanceof AST_Finally) {
3694+
self = compressor.parent();
3695+
} else if (self instanceof AST_LabeledStatement) {
3696+
self = self.body;
3697+
}
36803698
for (var i = 0, n = 0, len = statements.length; i < len; i++) {
36813699
var stat = statements[i];
36823700
if (stat instanceof AST_LoopControl) {
@@ -5965,10 +5983,19 @@ Compressor.prototype.compress = function(node) {
59655983
});
59665984

59675985
OPT(AST_LabeledStatement, function(self, compressor) {
5968-
if (compressor.option("dead_code")
5969-
&& self.body instanceof AST_Break
5970-
&& compressor.loopcontrol_target(self.body) === self.body) {
5971-
return make_node(AST_EmptyStatement, self);
5986+
if (self.body instanceof AST_If || self.body instanceof AST_Break) {
5987+
var body = tighten_body([ self.body ], compressor);
5988+
switch (body.length) {
5989+
case 0:
5990+
self.body = make_node(AST_EmptyStatement, self);
5991+
break;
5992+
case 1:
5993+
self.body = body[0];
5994+
break;
5995+
default:
5996+
self.body = make_node(AST_BlockStatement, self, { body: body });
5997+
break;
5998+
}
59725999
}
59736000
return compressor.option("unused") && self.label.references.length == 0 ? self.body : self;
59746001
});

test/compress/labels.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ labels_5: {
111111
labels_6: {
112112
options = {
113113
dead_code: true,
114+
unused: true,
114115
}
115116
input: {
116117
out: break out;
@@ -208,6 +209,59 @@ labels_10: {
208209
expect_stdout: "PASS"
209210
}
210211

212+
labels_11: {
213+
options = {
214+
conditionals: true,
215+
if_return: true,
216+
unused: true,
217+
}
218+
input: {
219+
L: if (console.log("PASS"))
220+
break L;
221+
}
222+
expect: {
223+
console.log("PASS");
224+
}
225+
expect_stdout: "PASS"
226+
}
227+
228+
labels_12: {
229+
options = {
230+
conditionals: true,
231+
dead_code: true,
232+
if_return: true,
233+
unused: true,
234+
}
235+
input: {
236+
L: try {
237+
if (console.log("foo"))
238+
break L;
239+
throw "bar";
240+
} catch (e) {
241+
console.log(e);
242+
break L;
243+
} finally {
244+
if (console.log("baz"))
245+
break L;
246+
}
247+
}
248+
expect: {
249+
try {
250+
if (!console.log("foo"))
251+
throw "bar";
252+
} catch (e) {
253+
console.log(e);
254+
} finally {
255+
console.log("baz")
256+
}
257+
}
258+
expect_stdout: [
259+
"foo",
260+
"bar",
261+
"baz",
262+
]
263+
}
264+
211265
issue_4466_1: {
212266
mangle = {
213267
v8: false,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy