optimize try-catch-finally (#1731)
- eliminate empty blocks - flatten out if try-block does not throw
This commit is contained in:
@@ -2606,6 +2606,15 @@ merge(Compressor.prototype, {
|
|||||||
|
|
||||||
OPT(AST_Try, function(self, compressor){
|
OPT(AST_Try, function(self, compressor){
|
||||||
self.body = tighten_body(self.body, compressor);
|
self.body = tighten_body(self.body, compressor);
|
||||||
|
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
|
||||||
|
}).optimize(compressor) : make_node(AST_EmptyStatement, self);
|
||||||
|
}
|
||||||
return self;
|
return self;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -214,3 +214,45 @@ dead_code_const_annotation_complex_scope: {
|
|||||||
}
|
}
|
||||||
expect_stdout: true
|
expect_stdout: true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try_catch_finally: {
|
||||||
|
options = {
|
||||||
|
conditionals: true,
|
||||||
|
dead_code: true,
|
||||||
|
evaluate: true,
|
||||||
|
}
|
||||||
|
input: {
|
||||||
|
var a = 1;
|
||||||
|
!function() {
|
||||||
|
try {
|
||||||
|
if (false) throw x;
|
||||||
|
} catch (a) {
|
||||||
|
var a = 2;
|
||||||
|
console.log("FAIL");
|
||||||
|
} finally {
|
||||||
|
a = 3;
|
||||||
|
console.log("PASS");
|
||||||
|
}
|
||||||
|
}();
|
||||||
|
try {
|
||||||
|
console.log(a);
|
||||||
|
} finally {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect: {
|
||||||
|
var a = 1;
|
||||||
|
!function() {
|
||||||
|
var a;
|
||||||
|
a = 3;
|
||||||
|
console.log("PASS");
|
||||||
|
}();
|
||||||
|
try {
|
||||||
|
console.log(a);
|
||||||
|
} finally {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
expect_stdout: [
|
||||||
|
"PASS",
|
||||||
|
"1",
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|||||||
@@ -853,7 +853,9 @@ issue_1715_1: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -864,7 +866,9 @@ issue_1715_1: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -882,7 +886,9 @@ issue_1715_2: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a = 2;
|
var a = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -893,7 +899,9 @@ issue_1715_2: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -911,7 +919,9 @@ issue_1715_3: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
console;
|
||||||
|
} catch (a) {
|
||||||
var a = 2 + x();
|
var a = 2 + x();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -922,7 +932,9 @@ issue_1715_3: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
function f() {
|
function f() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
console;
|
||||||
|
} catch (a) {
|
||||||
var a = x();
|
var a = x();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -940,7 +952,9 @@ issue_1715_4: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
!function a() {
|
!function a() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
@@ -950,7 +964,9 @@ issue_1715_4: {
|
|||||||
var a = 1;
|
var a = 1;
|
||||||
!function() {
|
!function() {
|
||||||
a++;
|
a++;
|
||||||
try {} catch (a) {
|
try {
|
||||||
|
x();
|
||||||
|
} catch (a) {
|
||||||
var a;
|
var a;
|
||||||
}
|
}
|
||||||
}();
|
}();
|
||||||
|
|||||||
@@ -70,6 +70,7 @@ side_effects_finally: {
|
|||||||
function f() {
|
function f() {
|
||||||
function g() {
|
function g() {
|
||||||
try {
|
try {
|
||||||
|
x();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
} finally {
|
} finally {
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
@@ -83,6 +84,7 @@ side_effects_finally: {
|
|||||||
function f() {
|
function f() {
|
||||||
(function() {
|
(function() {
|
||||||
try {
|
try {
|
||||||
|
x();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
} finally {
|
} finally {
|
||||||
console.log("PASS");
|
console.log("PASS");
|
||||||
|
|||||||
@@ -204,13 +204,13 @@ issue_1586_1: {
|
|||||||
input: {
|
input: {
|
||||||
function f() {
|
function f() {
|
||||||
try {
|
try {
|
||||||
|
x();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err.message);
|
console.log(err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
|
expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}"
|
||||||
expect_stdout: true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
issue_1586_2: {
|
issue_1586_2: {
|
||||||
@@ -223,11 +223,11 @@ issue_1586_2: {
|
|||||||
input: {
|
input: {
|
||||||
function f() {
|
function f() {
|
||||||
try {
|
try {
|
||||||
|
x();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.log(err.message);
|
console.log(err.message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
expect_exact: "function f(){try{}catch(c){console.log(c.message)}}"
|
expect_exact: "function f(){try{x()}catch(c){console.log(c.message)}}"
|
||||||
expect_stdout: true
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user