From 4240fba9b831c6d78fde5aac6855410bca34c30e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 23 Oct 2019 06:46:05 +0800 Subject: [PATCH] fix corner cases in `unused` (#3519) --- lib/compress.js | 3 +- test/compress/drop-unused.js | 61 +++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index f7fedeb2..7202219a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3738,7 +3738,7 @@ merge(Compressor.prototype, { head.push(def); } else { var value = def.value - && (sym.references.length != 1 || !sym.replaced) + && !def.value.single_use && def.value.drop_side_effect_free(compressor); if (value) { AST_Node.warn("Side effects in initialization of unused variable {name} [{file}:{line},{col}]", template(def.name)); @@ -6158,6 +6158,7 @@ merge(Compressor.prototype, { if (single_use && fixed) { def.single_use = false; fixed._squeezed = true; + fixed.single_use = true; if (fixed instanceof AST_Defun) { fixed = make_node(AST_Function, fixed, fixed); fixed.name = make_node(AST_SymbolLambda, fixed.name, fixed.name); diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 833b8c61..dfe33cec 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -2103,7 +2103,7 @@ issue_3497: { expect_stdout: "undefined" } -issue_3515: { +issue_3515_1: { options = { collapse_vars: true, reduce_vars: true, @@ -2127,3 +2127,62 @@ issue_3515: { } expect_stdout: "1" } + +issue_3515_2: { + options = { + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a = "FAIL"; + function f() { + typeof b === "number"; + delete a; + } + var b = f(a = "PASS"); + console.log(a); + } + expect: { + var a = "FAIL"; + function f() { + delete a; + } + f(a = "PASS"); + console.log(a); + } + expect_stdout: "PASS" +} + +issue_3515_3: { + options = { + collapse_vars: true, + unused: true, + } + input: { + var c = "FAIL"; + (function() { + function f() { + c = "PASS"; + } + var a = f(); + var a = function g(b) { + b && (b.p = this); + }(a); + })(); + console.log(c); + } + expect: { + var c = "FAIL"; + (function() { + function f() { + c = "PASS"; + } + (function(b) { + b && (b.p = this); + })(f()); + })(); + console.log(c); + } + expect_stdout: "PASS" +} -- 2.34.1