diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2019-11-13 21:44:44 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-11-13 21:44:44 +0800 |
commit | 4bd36dc8da23ad31eba45376e861784ba8568379 (patch) | |
tree | d7dfeab526456574d565849790a16305b2349684 /lib | |
parent | ab15c40770410e0dbd5ebb382aa711f3bf5aeb38 (diff) | |
download | tracifyjs-4bd36dc8da23ad31eba45376e861784ba8568379.tar.gz tracifyjs-4bd36dc8da23ad31eba45376e861784ba8568379.zip |
enhance `unused` (#3584)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/compress.js | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/lib/compress.js b/lib/compress.js index 92e8d3a3..e97427bc 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3727,10 +3727,20 @@ merge(Compressor.prototype, { }); } if (value) { - props.push(value); - return maintain_this_binding(compressor, parent, node, make_sequence(node, props.map(function(prop) { - return prop.transform(tt); - }))); + if (parent instanceof AST_Sequence && parent.tail_node() !== node) { + value = value.drop_side_effect_free(compressor); + } + if (value) props.push(value); + switch (props.length) { + case 0: + return MAP.skip; + case 1: + return maintain_this_binding(compressor, parent, node, props[0].transform(tt)); + default: + return make_sequence(node, props.map(function(prop) { + return prop.transform(tt); + })); + } } } } @@ -3874,37 +3884,10 @@ merge(Compressor.prototype, { }); } } - // certain combination of unused name + side effect leads to: - // https://github.com/mishoo/UglifyJS2/issues/44 - // https://github.com/mishoo/UglifyJS2/issues/1830 - // https://github.com/mishoo/UglifyJS2/issues/1838 - // https://github.com/mishoo/UglifyJS2/issues/3371 - // that's an invalid AST. - // We fix it at this stage by moving the `var` outside the `for`. - if (node instanceof AST_For) { - descend(node, this); - var block; - if (node.init instanceof AST_BlockStatement) { - block = node.init; - node.init = block.body.pop(); - block.body.push(node); - } - if (node.init instanceof AST_Defun) { - if (!block) { - block = make_node(AST_BlockStatement, node, { - body: [ node ] - }); - } - block.body.splice(-1, 0, node.init); - node.init = null; - } else if (node.init instanceof AST_SimpleStatement) { - node.init = node.init.body; - } else if (is_empty(node.init)) { - node.init = null; - } - return !block ? node : in_list ? MAP.splice(block.body) : block; - } if (node instanceof AST_LabeledStatement && node.body instanceof AST_For) { + // Certain combination of unused name + side effect leads to invalid AST: + // https://github.com/mishoo/UglifyJS2/issues/1830 + // We fix it at this stage by moving the label inwards, back to the `for`. descend(node, this); if (node.body instanceof AST_BlockStatement) { var block = node.body; @@ -3934,6 +3917,36 @@ merge(Compressor.prototype, { col : sym.start.col }; } + }, function(node, in_list) { + if (node instanceof AST_For) { + // Certain combination of unused name + side effect leads to invalid AST: + // https://github.com/mishoo/UglifyJS2/issues/44 + // https://github.com/mishoo/UglifyJS2/issues/1838 + // https://github.com/mishoo/UglifyJS2/issues/3371 + // We fix it at this stage by moving the `var` outside the `for`. + var block; + if (node.init instanceof AST_BlockStatement) { + block = node.init; + node.init = block.body.pop(); + block.body.push(node); + } + if (node.init instanceof AST_Defun) { + if (!block) { + block = make_node(AST_BlockStatement, node, { + body: [ node ] + }); + } + block.body.splice(-1, 0, node.init); + node.init = null; + } else if (node.init instanceof AST_SimpleStatement) { + node.init = node.init.body; + } else if (is_empty(node.init)) { + node.init = null; + } + return !block ? node : in_list ? MAP.splice(block.body) : block; + } else if (node instanceof AST_Sequence) { + if (node.expressions.length == 1) return node.expressions[0]; + } }); tt.push(compressor.parent()); self.transform(tt); |