diff --git a/lib/compress.js b/lib/compress.js index e939044c..b73c3d66 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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) { diff --git a/test/compress/yields.js b/test/compress/yields.js index 835eff1c..b0721b6d 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -896,7 +896,7 @@ dont_inline_nested: { node_version: ">=4" } -drop_body: { +drop_body_1: { options = { side_effects: true, yields: true, @@ -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") ] ]; } @@ -2019,7 +2040,7 @@ issue_5684: { node_version: ">=10" } -issue_5707: { +issue_5707_1: { options = { hoist_props: true, reduce_vars: true, @@ -2033,6 +2054,28 @@ issue_5707: { 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, + unused: true, + yields: true, + } + input: { + var a, b; + function* f(c = (b = 42, console.log("PASS"))) {} + b = f(); + } expect: { console.log("PASS"); } @@ -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" +}