From 8a83c8dd46dc8e446cd7231116a06c8b217291b7 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 17 Nov 2020 08:23:50 +0000 Subject: [PATCH] fix corner cases in `collapse_vars` & `dead_code` (#4285) fixes #4284 --- lib/compress.js | 10 +++--- test/compress/destructured.js | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index b8725b32..e4426fa3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -845,7 +845,7 @@ merge(Compressor.prototype, { if (init instanceof AST_Definitions) { init.definitions[0].name.match_symbol(function(node) { if (node instanceof AST_SymbolDeclaration) node.definition().fixed = false; - }); + }, true); } else if (init instanceof AST_SymbolRef) { init.definition().fixed = false; } @@ -1188,11 +1188,12 @@ merge(Compressor.prototype, { AST_Node.DEFMETHOD("match_symbol", function(predicate) { return predicate(this); }); - AST_Destructured.DEFMETHOD("match_symbol", function(predicate) { + AST_Destructured.DEFMETHOD("match_symbol", function(predicate, allow_computed_keys) { var found = false; var tw = new TreeWalker(function(node) { if (found) return true; if (node instanceof AST_DestructuredKeyVal) { + if (!allow_computed_keys && node.key instanceof AST_Node) return found = true; node.value.walk(tw); return true; } @@ -7045,7 +7046,7 @@ merge(Compressor.prototype, { name: node, value: make_value(compressor, node) })); - }); + }, true); }); return dropped; }; @@ -7107,7 +7108,7 @@ merge(Compressor.prototype, { if (node instanceof AST_SymbolDeclaration) { return !node.fixed_value() || may_overlap(compressor, node.definition()); } - }); + }, true); }) ? to_var(self) : self; } @@ -9061,6 +9062,7 @@ merge(Compressor.prototype, { def.fixed = false; return strip_assignment(); } else if (parent instanceof AST_VarDef) { + if (!(parent.name instanceof AST_SymbolDeclaration)) continue; if (parent.name.definition() !== def) continue; if (in_try(level, parent)) break; def.fixed = false; diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 65429b92..1555e3e3 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -1341,3 +1341,65 @@ issue_4282: { expect_stdout: true node_version: ">=6" } + +issue_4284_1: { + options = { + dead_code: true, + } + input: { + var a, { + 0: b, + } = a = "foo"; + console.log(a, b); + } + expect: { + var a, { + 0: b, + } = a = "foo"; + console.log(a, b); + } + expect_stdout: "foo f" + node_version: ">=6" +} + +issue_4284_2: { + options = { + collapse_vars: true, + } + input: { + var a, { + [console.log(a)]: b, + } = (a = "PASS", 0); + var c = a; + } + expect: { + var a, { + [console.log(a)]: b, + } = (a = "PASS", 0); + var c = a; + } + expect_stdout: "PASS" + node_version: ">=6" +} + +issue_4284_3: { + options = { + collapse_vars: true, + } + input: { + var a, b; + ({ + [console.log(a)]: b, + } = (a = "PASS", 0)); + var c = a; + } + expect: { + var a, b; + ({ + [console.log(a)]: b, + } = (a = "PASS", 0)); + var c = a; + } + expect_stdout: "PASS" + node_version: ">=6" +} -- 2.34.1