avoid emitting try-block with no catch- & finally-blocks

This commit is contained in:
alexlamsl 2017-03-30 02:17:29 +08:00
parent 2a2c6badd7
commit ff0c1116b6
3 changed files with 13 additions and 7 deletions

View File

@ -2606,15 +2606,14 @@ merge(Compressor.prototype, {
OPT(AST_Try, function(self, compressor){
self.body = tighten_body(self.body, compressor);
if (self.bcatch && all(self.bcatch.body, is_empty)) self.bcatch = null;
if (self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null;
if (self.bcatch && self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null;
if (all(self.body, is_empty)) {
var body = [];
if (self.bcatch) extract_declarations_from_unreachable_code(compressor, self.bcatch, body);
if (self.bfinally) body = body.concat(self.bfinally.body);
return body.length > 0 ? make_node(AST_BlockStatement, self, {
body: body
}) : make_node(AST_EmptyStatement, self);
}).optimize(compressor) : make_node(AST_EmptyStatement, self);
}
return self;
});

View File

@ -234,7 +234,10 @@ try_catch_finally: {
console.log("PASS");
}
}();
console.log(a);
try {
console.log(a);
} finally {
}
}
expect: {
var a = 1;
@ -243,7 +246,10 @@ try_catch_finally: {
a = 3;
console.log("PASS");
}();
console.log(a);
try {
console.log(a);
} finally {
}
}
expect_stdout: [
"PASS",

View File

@ -70,7 +70,7 @@ side_effects_finally: {
function f() {
function g() {
try {
if (f);
x();
} catch (e) {
} finally {
console.log("PASS");
@ -84,7 +84,8 @@ side_effects_finally: {
function f() {
(function() {
try {
if (f);
x();
} catch (e) {
} finally {
console.log("PASS");
}