From ff0c1116b6616f5c21f6d4ee1c0e811a0c769cd3 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Thu, 30 Mar 2017 02:17:29 +0800 Subject: [PATCH] avoid emitting try-block with no catch- & finally-blocks --- lib/compress.js | 5 ++--- test/compress/dead-code.js | 10 ++++++++-- test/compress/issue-1673.js | 5 +++-- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 693682a0..be760152 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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; }); diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index e172442d..bb72451c 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -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", diff --git a/test/compress/issue-1673.js b/test/compress/issue-1673.js index 15ff236f..081b0e5f 100644 --- a/test/compress/issue-1673.js +++ b/test/compress/issue-1673.js @@ -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"); }