diff --git a/lib/compress.js b/lib/compress.js index d8dc58e235..275e9ee034 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1653,6 +1653,7 @@ Compressor.prototype.compress = function(node) { function reset_flags(node) { node._squeezed = false; node._optimized = false; + node.single_use = false; if (node instanceof AST_BlockScope) node._var_names = undefined; if (node instanceof AST_SymbolRef) node.fixed = undefined; } @@ -14178,6 +14179,7 @@ Compressor.prototype.compress = function(node) { var fn = call.expression; if (!(fn instanceof AST_LambdaExpression)) return; if (fn.name) return; + if (fn.single_use) return; if (fn.uses_arguments) return; if (fn.pinned()) return; if (is_generator(fn)) return; diff --git a/test/compress/functions.js b/test/compress/functions.js index 2fa172fcf8..26d9d00c30 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -8706,10 +8706,9 @@ single_use_inline_collision: { expect: { var a = "PASS"; (function() { - (function() { + void function() { while (console.log(a)); - return; - })(); + }(); (function(a) { a || a("FAIL"); })(console.log); diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index fc75af3717..8314212fa5 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -801,3 +801,71 @@ issue_5638_4: { } expect_stdout: "foo 42" } + +issue_5884_1: { + options = { + hoist_vars: true, + inline: true, + join_vars: true, + reduce_vars: true, + sequences: true, + toplevel: true, + unused: true, + } + input: { + try { + var f = function() { + var a = [ "PASS" ]; + for (b in a) + console.log(a[b]); + }; + f(); + } finally { + var b; + } + } + expect: { + var b; + try { + (function() { + var a = [ "PASS" ]; + for (b in a) + console.log(a[b]); + })(); + } finally {} + } + expect_stdout: "PASS" +} + +issue_5884_2: { + options = { + hoist_vars: true, + inline: true, + join_vars: true, + passes: 2, + reduce_vars: true, + sequences: true, + toplevel: true, + unused: true, + } + input: { + try { + var f = function() { + var a = [ "PASS" ]; + for (b in a) + console.log(a[b]); + }; + f(); + } finally { + var b; + } + } + expect: { + try { + var a = [ "PASS" ]; + for (var b in a) + console.log(a[b]); + } finally {} + } + expect_stdout: "PASS" +} diff --git a/test/compress/join_vars.js b/test/compress/join_vars.js index cbf8137a8a..df0445c68c 100644 --- a/test/compress/join_vars.js +++ b/test/compress/join_vars.js @@ -669,7 +669,7 @@ inlined_assignments: { expect_stdout: "PASS" } -inline_for: { +single_use_for: { options = { inline: true, join_vars: true, @@ -683,16 +683,66 @@ inline_for: { }; a(); } + expect: { + (function() { + for (; console.log("PASS");); + })(); + } + expect_stdout: "PASS" +} + +single_use_for_inline: { + options = { + inline: true, + join_vars: true, + passes: 2, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var a = function() { + for (; console.log("PASS");); + }; + a(); + } expect: { for (; console.log("PASS");); } expect_stdout: "PASS" } -inline_var: { +single_use_var: { + options = { + inline: true, + join_vars: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + A = "PASS"; + var a = function() { + var b = A; + for (b in console.log(b)); + }; + a(); + } + expect: { + A = "PASS"; + (function() { + var b = A; + for (b in console.log(b)); + })(); + } + expect_stdout: "PASS" +} + +single_use_var_inline: { options = { inline: true, join_vars: true, + passes: 2, reduce_vars: true, toplevel: true, unused: true,