From 1e8fa1aa1db199835e328d7a042155ce859df596 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Sun, 22 Nov 2020 23:05:20 +0000 Subject: fix corner case in `passes` & `reduce_vars` (#4316) fixes #4315 --- lib/compress.js | 19 +++++++++++-------- test/compress/destructured.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 1161f3b6..0e585e17 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -597,9 +597,10 @@ merge(Compressor.prototype, { function scan_declaration(tw, lhs, fixed, visit) { var scanner = new TreeWalker(function(node) { if (node instanceof AST_DestructuredArray) { + reset_flags(node); var save = fixed; node.elements.forEach(function(node, index) { - if (node instanceof AST_Hole) return; + if (node instanceof AST_Hole) return reset_flags(node); fixed = function() { return make_node(AST_Sub, node, { expression: save(), @@ -614,8 +615,10 @@ merge(Compressor.prototype, { return true; } if (node instanceof AST_DestructuredObject) { + reset_flags(node); var save = fixed; node.properties.forEach(function(node) { + reset_flags(node); if (node.key instanceof AST_Node) { push(tw); node.key.walk(tw); @@ -1112,6 +1115,13 @@ merge(Compressor.prototype, { node.DEFMETHOD("reduce_vars", func); }); + function reset_flags(node) { + node._squeezed = false; + node._optimized = false; + delete node.fixed; + if (node instanceof AST_Scope) delete node._var_names; + } + AST_Toplevel.DEFMETHOD("reset_opt_flags", function(compressor) { var tw = new TreeWalker(compressor.option("reduce_vars") ? function(node, descend) { reset_flags(node); @@ -1129,13 +1139,6 @@ merge(Compressor.prototype, { // - backup & restore via `save_ids` when visiting out-of-order sections tw.safe_ids = Object.create(null); this.walk(tw); - - function reset_flags(node) { - node._squeezed = false; - node._optimized = false; - delete node.fixed; - if (node instanceof AST_Scope) delete node._var_names; - } }); AST_Symbol.DEFMETHOD("fixed_value", function() { diff --git a/test/compress/destructured.js b/test/compress/destructured.js index e654847a..6eea10d2 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -1647,3 +1647,40 @@ issue_4312: { expect_stdout: "PASS" node_version: ">=6" } + +issue_4315: { + options = { + conditionals: true, + dead_code: true, + evaluate: true, + inline: true, + passes: 2, + reduce_funcs: true, + reduce_vars: true, + side_effects: true, + toplevel: true, + unused: true, + } + input: { + function f() { + console; + } + var a = function() { + if ([ 0[f && f] ] = []) + return this; + }(), b; + do { + console.log("PASS"); + } while (0 && (b = 0), b && a); + } + expect: { + [ 0[function() { + console + }] ] = []; + do { + console.log("PASS"); + } while (void 0); + } + expect_stdout: "PASS" + node_version: ">=6" +} -- cgit v1.2.3