From 722465b2a032b64ad24bb1822559dd4885332d04 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L." Date: Tue, 16 Jul 2024 01:09:28 +0300 Subject: [PATCH] fix corner case in `inline` (#5886) fixes #5884 --- lib/compress.js | 2 ++ test/compress/functions.js | 5 ++- test/compress/hoist_vars.js | 68 +++++++++++++++++++++++++++++++++++++ test/compress/join_vars.js | 54 +++++++++++++++++++++++++++-- 4 files changed, 124 insertions(+), 5 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index d8dc58e2355..275e9ee034b 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 2fa172fcf8f..26d9d00c308 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 fc75af3717c..8314212fa5f 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 cbf8137a8a1..df0445c68cc 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,