From 02a6ce07eba11223518a22bce18e209371f78f39 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 14 Dec 2017 15:32:13 +0800 Subject: [PATCH] improve `reduce_vars` (#2592) - account for hoisting nature of `var` --- lib/compress.js | 11 +++++--- test/compress/reduce_vars.js | 54 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 05e0e1aa..4166909f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -381,7 +381,10 @@ merge(Compressor.prototype, { && node.operator == "=" && node.left instanceof AST_SymbolRef) { var d = node.left.definition(); - if (safe_to_assign(d, node.right)) { + if (safe_to_assign(d, node.right) + || d.fixed === undefined && all(d.orig, function(sym) { + return sym instanceof AST_SymbolVar; + })) { d.references.push(node.left); d.fixed = function() { return node.right; @@ -561,9 +564,9 @@ merge(Compressor.prototype, { if (!safe_to_read(def)) return false; if (def.fixed === false) return false; if (def.fixed != null && (!value || def.references.length > 0)) return false; - return !def.orig.some(function(sym) { - return sym instanceof AST_SymbolDefun - || sym instanceof AST_SymbolLambda; + return all(def.orig, function(sym) { + return !(sym instanceof AST_SymbolDefun + || sym instanceof AST_SymbolLambda); }); } diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 504ce6f0..108dc0e9 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -4784,3 +4784,57 @@ escape_local_throw: { } expect_stdout: "PASS" } + +inverted_var: { + options = { + evaluate: true, + inline: true, + passes: 3, + reduce_vars: true, + side_effects: true, + unused: true, + } + input: { + console.log(function() { + var a = 1; + return a; + }(), function() { + var b; + b = 2; + return b; + }(), function() { + c = 3; + return c; + var c; + }(), function(c) { + c = 4; + return c; + }(), function (c) { + c = 5; + return c; + var c; + }(), function c() { + c = 6; + return c; + }(), function c() { + c = 7; + return c; + var c; + }(), function() { + c = 8; + return c; + var c = "foo"; + }()); + } + expect: { + console.log(1, 2, 3, 4, 5, function c() { + c = 6; + return c; + }(), 7, function() { + c = 8; + return c; + var c = "foo"; + }()); + } + expect_stdout: true +} -- 2.34.1