From 1dbf7d4a3af7bf870d61b029120078c0ef499350 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Wed, 10 Jun 2020 12:30:37 +0100 Subject: [PATCH] fix corner case in `side_effects` (#3984) fixes #3983 --- lib/compress.js | 6 +++--- test/compress/side_effects.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 3 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 55645286..03432bee 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5279,7 +5279,8 @@ merge(Compressor.prototype, { }); def(AST_SymbolRef, function(compressor) { if (!this.is_declared(compressor)) return this; - this.definition().replaced++; + var def = this.definition(); + if (member(this, def.references)) def.replaced++; return null; }); def(AST_This, return_null); @@ -7575,9 +7576,8 @@ merge(Compressor.prototype, { function recursive_ref(compressor, def) { var level = 0, node = compressor.self(); do { - if (node instanceof AST_Lambda && node.name && node.name.definition() === def) break; + if (node instanceof AST_Lambda && node.name && node.name.definition() === def) return node; } while (node = compressor.parent(level++)); - return node; } OPT(AST_SymbolRef, function(self, compressor) { diff --git a/test/compress/side_effects.js b/test/compress/side_effects.js index b7ffe47a..412a38cb 100644 --- a/test/compress/side_effects.js +++ b/test/compress/side_effects.js @@ -297,3 +297,33 @@ operator_in: { } expect_stdout: "PASS" } + +issue_3983: { + options = { + collapse_vars: true, + conditionals: true, + evaluate: true, + inline: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + var a = "PASS"; + function f() { + g && g(); + } + f(); + function g() { + 0 ? a : 0; + } + var b = a; + console.log(a); + } + expect: { + var a = "PASS"; + console.log(a); + } + expect_stdout: "PASS" +} -- 2.34.1