diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-12-10 12:57:47 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-12-10 12:57:47 +0000 |
commit | 036bca980caad83edbabca860c69515d8007bd11 (patch) | |
tree | 2232cc3d6fa3c72916f244a5f6440f733f868f3e /lib | |
parent | 18c2b1841bf0bfa6c827eed53238d0499c143b23 (diff) | |
download | tracifyjs-036bca980caad83edbabca860c69515d8007bd11.tar.gz tracifyjs-036bca980caad83edbabca860c69515d8007bd11.zip |
enhance `loops` (#3633)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 45 |
1 files changed, 34 insertions, 11 deletions
diff --git a/lib/compress.js b/lib/compress.js index 6d398de2..fb7e66de 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4718,8 +4718,8 @@ merge(Compressor.prototype, { }); function if_break_in_loop(self, compressor) { - var first = self.body instanceof AST_BlockStatement ? self.body.body[0] : self.body; - if (compressor.option("dead_code") && is_break(first)) { + var first = first_statement(self.body); + if (compressor.option("dead_code") && (breaks(first) || first instanceof AST_Exit)) { var body = []; if (self.init instanceof AST_Statement) { body.push(self.init); @@ -4728,10 +4728,19 @@ merge(Compressor.prototype, { body: self.init })); } - if (self.condition) { + var retain = external_target(first) || first instanceof AST_Exit; + if (self.condition && retain) { + body.push(make_node(AST_If, self, { + condition: self.condition, + body: first, + alternative: null + })); + } else if (self.condition) { body.push(make_node(AST_SimpleStatement, self.condition, { body: self.condition })); + } else if (retain) { + body.push(first); } extract_declarations_from_unreachable_code(self.body, body); return make_node(AST_BlockStatement, self, { @@ -4739,7 +4748,8 @@ merge(Compressor.prototype, { }); } if (first instanceof AST_If) { - if (is_break(first.body)) { + var ab = first_statement(first.body); + if (breaks(ab)) { if (self.condition) { self.condition = make_node(AST_Binary, self.condition, { left: self.condition, @@ -4749,8 +4759,12 @@ merge(Compressor.prototype, { } else { self.condition = first.condition.negate(compressor); } - drop_it(first.alternative); - } else if (is_break(first.alternative)) { + var body = as_statement_array(first.alternative); + extract_declarations_from_unreachable_code(first.body, body); + return drop_it(body); + } + ab = first_statement(first.alternative); + if (breaks(ab)) { if (self.condition) { self.condition = make_node(AST_Binary, self.condition, { left: self.condition, @@ -4760,18 +4774,27 @@ merge(Compressor.prototype, { } else { self.condition = first.condition; } - drop_it(first.body); + var body = as_statement_array(first.body); + extract_declarations_from_unreachable_code(first.alternative, body); + return drop_it(body); } } return self; - function is_break(node) { + function first_statement(body) { + return body instanceof AST_BlockStatement ? body.body[0] : body; + } + + function external_target(node) { + return compressor.loopcontrol_target(node) !== compressor.self(); + } + + function breaks(node) { return node instanceof AST_Break - && compressor.loopcontrol_target(node) === compressor.self(); + || node instanceof AST_Continue && external_target(node); } function drop_it(rest) { - rest = as_statement_array(rest); if (self.body instanceof AST_BlockStatement) { self.body = self.body.clone(); self.body.body = rest.concat(self.body.body.slice(1)); @@ -4781,7 +4804,7 @@ merge(Compressor.prototype, { body: rest }).transform(compressor); } - self = if_break_in_loop(self, compressor); + return if_break_in_loop(self, compressor); } } |