Skip to content

Commit

Permalink
fix corner case in yields (#5750)
Browse files Browse the repository at this point in the history
fixes #5749
  • Loading branch information
alexlamsl authored Nov 30, 2022
1 parent 548f093 commit 574ca47
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 5 deletions.
5 changes: 2 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -8901,12 +8901,11 @@ Compressor.prototype.compress = function(node) {
exprs = trim(exprs, compressor, first_in_statement, array_spread);
return exprs && make_sequence(self, exprs.map(convert_spread));
}
if (compressor.option("yields") && is_generator(exp)) {
if (compressor.option("yields") && is_generator(exp) && fn_name_unused(exp, compressor)) {
var call = self.clone();
call.expression = make_node(AST_Function, exp);
call.expression.body = [];
var opt = call.transform(compressor);
if (opt !== call) return opt.drop_side_effect_free(compressor, first_in_statement);
return call;
}
var dropped = drop_returns(compressor, exp);
if (dropped) {
Expand Down
97 changes: 95 additions & 2 deletions test/compress/yields.js
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ dont_inline_nested: {
node_version: ">=4"
}

drop_body: {
drop_body_1: {
options = {
side_effects: true,
yields: true,
Expand All @@ -906,6 +906,27 @@ drop_body: {
console.log("bar");
})([ console.log("baz") ]);
}
expect: {
void ([ [ , [][0] = console.log("foo") ] ] = [ [ console.log("baz") ] ]);
}
expect_stdout: [
"baz",
"foo",
]
node_version: ">=6"
}

drop_body_2: {
options = {
passes: 2,
side_effects: true,
yields: true,
}
input: {
(function*([ , a = console.log("foo") ]) {
console.log("bar");
})([ console.log("baz") ]);
}
expect: {
[ [ , [][0] = console.log("foo") ] ] = [ [ console.log("baz") ] ];
}
Expand Down Expand Up @@ -2019,9 +2040,31 @@ issue_5684: {
node_version: ">=10"
}

issue_5707: {
issue_5707_1: {
options = {
hoist_props: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
yields: true,
}
input: {
var a, b;
function* f(c = (b = 42, console.log("PASS"))) {}
b = f();
}
expect: {
(function(c = console.log("PASS")) {})();
}
expect_stdout: "PASS"
node_version: ">=6"
}

issue_5707_2: {
options = {
hoist_props: true,
passes: 2,
reduce_vars: true,
side_effects: true,
toplevel: true,
Expand Down Expand Up @@ -2076,3 +2119,53 @@ issue_5710: {
expect_stdout: "PASS"
node_version: ">=10"
}

issue_5749_1: {
options = {
collapse_vars: true,
reduce_vars: true,
side_effects: true,
toplevel: true,
unused: true,
yields: true,
}
input: {
var a;
function* f() {}
a = f(new function() {
var b = a |= 0, c = a += console.log("PASS");
}());
}
expect: {
(function() {})(function() {
console.log("PASS");
}());
}
expect_stdout: "PASS"
node_version: ">=4"
}

issue_5749_2: {
options = {
collapse_vars: true,
inline: true,
reduce_vars: true,
sequences: true,
side_effects: true,
toplevel: true,
unused: true,
yields: true,
}
input: {
var a;
function* f() {}
a = f(new function() {
var b = a |= 0, c = a += console.log("PASS");
}());
}
expect: {
console.log("PASS");
}
expect_stdout: "PASS"
node_version: ">=4"
}

0 comments on commit 574ca47

Please sign in to comment.