enhance unused (#3584)

This commit is contained in:
Alex Lam S.L
2019-11-13 21:44:44 +08:00
committed by GitHub
parent ab15c40770
commit 4bd36dc8da
3 changed files with 78 additions and 51 deletions

View File

@@ -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);