From f0120e90b6eae20e46eb38e815654f16d8a5d896 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Mon, 18 Jul 2022 02:04:51 +0100 Subject: [PATCH] fix corner case `collapse_vars` (#5569) fixes #5568 --- lib/ast.js | 3 +++ lib/compress.js | 2 +- test/compress/collapse_vars.js | 22 ++++++++++++++++++++++ test/reduce.js | 10 ++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/lib/ast.js b/lib/ast.js index 0c93edb4..28fcd079 100644 --- a/lib/ast.js +++ b/lib/ast.js @@ -141,6 +141,7 @@ var AST_Node = DEFNODE("Node", "start end", { }, null); DEF_BITPROPS(AST_Node, [ + // AST_Node "_optimized", "_squeezed", // AST_Call @@ -175,6 +176,8 @@ DEF_BITPROPS(AST_Node, [ "pure", // AST_Assign "redundant", + // AST_Node + "single_use", // AST_ClassProperty "static", // AST_Call diff --git a/lib/compress.js b/lib/compress.js index 4e89eaf4..9d51fad8 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2080,7 +2080,7 @@ Compressor.prototype.compress = function(node) { return node; } // Skip transient nodes caused by single-use variable replacement - if (node.single_use && parent instanceof AST_VarDef && parent.value === node) return node; + if (node.single_use) return node; // Replace variable with assignment when found var hit_rhs; if (!(node instanceof AST_SymbolDeclaration) diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index b16450d7..0e19d579 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -9979,3 +9979,25 @@ issue_5396: { } expect_stdout: "PASS" } + +issue_5568: { + options = { + collapse_vars: true, + evaluate: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + A = "FAIL"; + var a = (A = "PASS", !1); + for (var b in a); + console.log(A); + } + expect: { + A = "FAIL"; + for (var b in !(A = "PASS")); + console.log(A); + } + expect_stdout: "PASS" +} diff --git a/test/reduce.js b/test/reduce.js index 4437e321..5da2b152 100644 --- a/test/reduce.js +++ b/test/reduce.js @@ -460,6 +460,16 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options) return to_statement(node.definitions[0].value); } } + else if (node instanceof U.AST_VarDef) { + if (node.value) { + node.start._permute++; + CHANGED = true; + return new U.AST_VarDef({ + name: node.name, + start: {}, + }); + } + } if (in_list) { // drop switch branches