Skip to content

Commit db94d21

Browse files
authored
fix corner case in side_effects (#5118)
1 parent 9634a9d commit db94d21

3 files changed

Lines changed: 47 additions & 8 deletions

File tree

lib/compress.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,7 +1539,7 @@ merge(Compressor.prototype, {
15391539
AST_Node.DEFMETHOD("match_symbol", function(predicate) {
15401540
return predicate(this);
15411541
});
1542-
AST_Destructured.DEFMETHOD("match_symbol", function(predicate, ignore_side_effects) {
1542+
function match_destructured(predicate, ignore_side_effects) {
15431543
var found = false;
15441544
var tw = new TreeWalker(function(node) {
15451545
if (found) return true;
@@ -1557,7 +1557,9 @@ merge(Compressor.prototype, {
15571557
});
15581558
this.walk(tw);
15591559
return found;
1560-
});
1560+
}
1561+
AST_DefaultValue.DEFMETHOD("match_symbol", match_destructured);
1562+
AST_Destructured.DEFMETHOD("match_symbol", match_destructured);
15611563

15621564
function in_async_generator(scope) {
15631565
return scope instanceof AST_AsyncGeneratorDefun || scope instanceof AST_AsyncGeneratorFunction;
@@ -7924,12 +7926,7 @@ merge(Compressor.prototype, {
79247926
var fn = exp;
79257927
if (fn instanceof AST_SymbolRef) fn = fn.fixed_value();
79267928
if (fn instanceof AST_Lambda) {
7927-
fn.new = true;
7928-
var assign_this_only = all(fn.body, function(stat) {
7929-
return !stat.has_side_effects(compressor);
7930-
});
7931-
delete fn.new;
7932-
if (assign_this_only) {
7929+
if (assign_this_only(fn, compressor)) {
79337930
var exprs = self.args.slice();
79347931
exprs.unshift(exp);
79357932
exprs = trim(exprs, compressor, first_in_statement, array_spread);
@@ -7941,6 +7938,16 @@ merge(Compressor.prototype, {
79417938
self.call_only = true;
79427939
return self;
79437940
});
7941+
function assign_this_only(fn, compressor) {
7942+
fn.new = true;
7943+
var result = all(fn.body, function(stat) {
7944+
return !stat.has_side_effects(compressor);
7945+
}) && all(fn.argnames, function(argname) {
7946+
return !argname.match_symbol(return_false);
7947+
}) && !(fn.rest && fn.rest.match_symbol(return_false));
7948+
delete fn.new;
7949+
return result;
7950+
}
79447951
function drop_class(self, compressor, first_in_statement) {
79457952
var exprs = [], values = [];
79467953
var props = self.properties;

test/compress/default-values.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,20 @@ retain_empty_iife: {
568568
node_version: ">=6"
569569
}
570570

571+
drop_new_function: {
572+
options = {
573+
side_effects: true,
574+
}
575+
input: {
576+
new function(a = console.log("PASS")) {}();
577+
}
578+
expect: {
579+
void console.log("PASS");
580+
}
581+
expect_stdout: "PASS"
582+
node_version: ">=6"
583+
}
584+
571585
retain_fargs: {
572586
options = {
573587
unused: true,

test/compress/rests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,24 @@ keep_rest_lambda_2: {
636636
node_version: ">=6"
637637
}
638638

639+
drop_new_function: {
640+
options = {
641+
side_effects: true,
642+
}
643+
input: {
644+
new function(...{
645+
[console.log("PASS")]: a,
646+
}) {}();
647+
}
648+
expect: {
649+
void ([ ... {
650+
[console.log("PASS")]: [].e,
651+
}] = []);
652+
}
653+
expect_stdout: "PASS"
654+
node_version: ">=6"
655+
}
656+
639657
issue_4525_1: {
640658
options = {
641659
arguments: true,

0 commit comments

Comments
 (0)