diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 70 |
1 files changed, 40 insertions, 30 deletions
diff --git a/lib/compress.js b/lib/compress.js index d055132d..6926b357 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1034,6 +1034,16 @@ merge(Compressor.prototype, { return false; } + function has_declarations_only(block) { + return all(block.body, function(stat) { + return is_empty(stat) + || stat instanceof AST_Defun + || stat instanceof AST_Var && all(stat.definitions, function(var_def) { + return !var_def.value; + }); + }); + } + function loop_body(x) { if (x instanceof AST_IterationStatement) { return x.body instanceof AST_BlockStatement ? x.body : x; @@ -5493,15 +5503,15 @@ merge(Compressor.prototype, { var stat = branch.body[branch.body.length - 1]; if (is_break(stat, compressor)) branch.body.pop(); if (branch === default_branch) { - if (!is_body_empty(branch)) break; + if (!has_declarations_only(branch)) break; } else if (branch.expression.has_side_effects(compressor)) { break; } else if (default_branch) { - if (!is_body_empty(default_branch)) break; + if (!has_declarations_only(default_branch)) break; if (body[body.length - 2] !== default_branch) break; default_branch.body = default_branch.body.concat(branch.body); branch.body = []; - } else if (!is_body_empty(branch)) break; + } else if (!has_declarations_only(branch)) break; eliminate_branch(branch); if (body.pop() === default_branch) default_branch = null; } @@ -5543,16 +5553,6 @@ merge(Compressor.prototype, { return node instanceof AST_Break && tw.loopcontrol_target(node) === self; } - function is_body_empty(branch) { - return all(branch.body, function(stat) { - return is_empty(stat) - || stat instanceof AST_Defun - || stat instanceof AST_Var && all(stat.definitions, function(var_def) { - return !var_def.value; - }); - }); - } - function eliminate_branch(branch, prev) { if (prev && !aborts(prev)) { prev.body = prev.body.concat(branch.body); @@ -5564,25 +5564,35 @@ merge(Compressor.prototype, { OPT(AST_Try, function(self, compressor) { self.body = tighten_body(self.body, compressor); - if (self.bcatch && self.bfinally && all(self.bfinally.body, is_empty)) self.bfinally = null; - if (compressor.option("dead_code") && all(self.body, is_empty)) { - var body = []; - if (self.bcatch) { - extract_declarations_from_unreachable_code(self.bcatch, body); - body.forEach(function(stat) { - if (!(stat instanceof AST_Definitions)) return; - stat.definitions.forEach(function(var_def) { - var def = var_def.name.definition().redefined(); - if (!def) return; - var_def.name = var_def.name.clone(); - var_def.name.thedef = def; + if (compressor.option("dead_code")) { + if (has_declarations_only(self)) { + var body = []; + if (self.bcatch) { + extract_declarations_from_unreachable_code(self.bcatch, body); + body.forEach(function(stat) { + if (!(stat instanceof AST_Definitions)) return; + stat.definitions.forEach(function(var_def) { + var def = var_def.name.definition().redefined(); + if (!def) return; + var_def.name = var_def.name.clone(); + var_def.name.thedef = def; + }); }); - }); + } + [].unshift.apply(body, self.body); + if (self.bfinally) [].push.apply(body, self.bfinally.body); + return make_node(AST_BlockStatement, self, { + body: body + }).optimize(compressor); + } + if (self.bfinally && has_declarations_only(self.bfinally)) { + var body = self.body.concat(self.bfinally.body); + if (!self.bcatch) return make_node(AST_BlockStatement, self, { + body: body + }).optimize(compressor); + self.body = body; + self.bfinally = null; } - if (self.bfinally) body = body.concat(self.bfinally.body); - return make_node(AST_BlockStatement, self, { - body: body - }).optimize(compressor); } return self; }); |