From 304e687264c05a9db8789857a0a7850dbd288ef1 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Mon, 12 Aug 2024 13:42:27 +0300 Subject: [PATCH] fix corner cases in `collapse_vars` & `reduce_vars` fixes #5915 --- lib/compress.js | 10 +--- test/compress/collapse_vars.js | 78 ++++++++++++++++++++++++++++ test/compress/reduce_vars.js | 93 ++++++++++++++++++++++++++++++++++ 3 files changed, 173 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 74e09299b25..1e358599f5f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1378,6 +1378,7 @@ Compressor.prototype.compress = function(node) { def(AST_LambdaDefinition, function(tw, descend, compressor) { var fn = this; var def = fn.name.definition(); + if (!safe_to_trim(fn)) def.fixed = false; var parent = tw.parent(); if (parent instanceof AST_ExportDeclaration || parent instanceof AST_ExportDefault) def.single_use = false; if (!safe_to_visit(tw, fn)) return true; @@ -3545,17 +3546,10 @@ Compressor.prototype.compress = function(node) { if (node instanceof AST_Symbol) { var def = node.definition(); var scope = def.scope.resolve(); - var found = false; - var avoid = def.orig.reduce(function(scopes, sym) { + def.orig.forEach(function(sym) { if (sym instanceof AST_SymbolDefun) { if (sym.scope !== scope) push_uniq(scopes, sym.scope); - } else { - found = true; } - return scopes; - }, []); - if (found) avoid.forEach(function(scope) { - push_uniq(scopes, scope); }); } }); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index df6e1134922..f1f4d61188c 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -10357,3 +10357,81 @@ issue_5869: { } expect_stdout: TypeError("Cannot set properties of undefined") } + +issue_5915_1: { + options = { + collapse_vars: true, + } + input: { + f = void 0; + { + console.log(typeof f); + function f() {} + } + } + expect: { + f = void 0; + { + console.log(typeof f); + function f() {} + } + } + expect_stdout: true +} + +issue_5915_2: { + options = { + collapse_vars: true, + } + input: { + f = void 0; + { + function f() {} + console.log(typeof f); + } + } + expect: { + f = void 0; + { + function f() {} + console.log(typeof f); + } + } + expect_stdout: true +} + +issue_5915_3: { + options = { + collapse_vars: true, + } + input: { + f = void 0; + function f() {} + { + console.log(typeof f); + } + } + expect: { + function f() {} + console.log(typeof (f = void 0)); + } + expect_stdout: "undefined" +} + +issue_5915_4: { + options = { + collapse_vars: true, + } + input: { + { + f = void 0; + function f() {} + console.log(typeof f); + } + } + expect: { + function f() {} + console.log(typeof (f = void 0)); + } + expect_stdout: "undefined" +} diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index edd95bd009e..8108f18d103 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -8493,3 +8493,96 @@ issue_5892: { } expect_stdout: "PASS" } + +issue_5915_1: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + f = void 0; + if (console) { + console.log(typeof f); + function f() {} + } + } + expect: { + f = void 0; + if (console) { + console.log(typeof f); + function f() {} + } + } + expect_stdout: true +} + +issue_5915_2: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + f = void 0; + if (console) { + function f() {} + console.log(typeof f); + } + } + expect: { + f = void 0; + if (console) { + function f() {} + console.log(typeof f); + } + } + expect_stdout: true +} + +issue_5915_3: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + f = void 0; + function f() {} + if (console) { + console.log(typeof f); + } + } + expect: { + void 0; + if (console) + console.log("undefined"); + } + expect_stdout: "undefined" +} + +issue_5915_4: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + if (console) { + f = void 0; + function f() {} + console.log(typeof f); + } + } + expect: { + if (console) { + void 0; + console.log("undefined"); + } + } + expect_stdout: "undefined" +}