fix corner case in side_effects (#5118)

This commit is contained in:
Alex Lam S.L
2021-08-23 01:51:59 +01:00
committed by GitHub
parent 9634a9d1fd
commit db94d21980
3 changed files with 47 additions and 8 deletions

View File

@@ -1539,7 +1539,7 @@ merge(Compressor.prototype, {
AST_Node.DEFMETHOD("match_symbol", function(predicate) {
return predicate(this);
});
AST_Destructured.DEFMETHOD("match_symbol", function(predicate, ignore_side_effects) {
function match_destructured(predicate, ignore_side_effects) {
var found = false;
var tw = new TreeWalker(function(node) {
if (found) return true;
@@ -1557,7 +1557,9 @@ merge(Compressor.prototype, {
});
this.walk(tw);
return found;
});
}
AST_DefaultValue.DEFMETHOD("match_symbol", match_destructured);
AST_Destructured.DEFMETHOD("match_symbol", match_destructured);
function in_async_generator(scope) {
return scope instanceof AST_AsyncGeneratorDefun || scope instanceof AST_AsyncGeneratorFunction;
@@ -7924,12 +7926,7 @@ merge(Compressor.prototype, {
var fn = exp;
if (fn instanceof AST_SymbolRef) fn = fn.fixed_value();
if (fn instanceof AST_Lambda) {
fn.new = true;
var assign_this_only = all(fn.body, function(stat) {
return !stat.has_side_effects(compressor);
});
delete fn.new;
if (assign_this_only) {
if (assign_this_only(fn, compressor)) {
var exprs = self.args.slice();
exprs.unshift(exp);
exprs = trim(exprs, compressor, first_in_statement, array_spread);
@@ -7941,6 +7938,16 @@ merge(Compressor.prototype, {
self.call_only = true;
return self;
});
function assign_this_only(fn, compressor) {
fn.new = true;
var result = all(fn.body, function(stat) {
return !stat.has_side_effects(compressor);
}) && all(fn.argnames, function(argname) {
return !argname.match_symbol(return_false);
}) && !(fn.rest && fn.rest.match_symbol(return_false));
delete fn.new;
return result;
}
function drop_class(self, compressor, first_in_statement) {
var exprs = [], values = [];
var props = self.properties;