From 79d533a5604e1bf402fd9a25d0bccc7351c904f4 Mon Sep 17 00:00:00 2001 From: alexlamsl Date: Tue, 18 Apr 2017 04:02:44 +0800 Subject: [PATCH] fix pre-existing issues - reference counting on assignment - walking of anonymous functions --- lib/compress.js | 17 ++++++++++------- test/compress/collapse_vars.js | 22 ++++++++++++++++++++++ test/compress/reduce_vars.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/lib/compress.js b/lib/compress.js index 802b28f0..9c918e70 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -298,6 +298,7 @@ merge(Compressor.prototype, { && node.left instanceof AST_SymbolRef) { var d = node.left.definition(); if (HOP(safe_ids, d.id) && safe_to_assign(d, node.right)) { + d.references.push(node.left); d.fixed = function() { return node.right; }; @@ -321,13 +322,11 @@ merge(Compressor.prototype, { safe_ids = save_ids; return true; } - var iife; - if (node instanceof AST_Function - && (iife = tw.parent()) instanceof AST_Call - && iife.expression === node) { - if (node.name) { - node.name.definition().fixed = node; - } else { + if (node instanceof AST_Function) { + var iife; + if (!node.name + && (iife = tw.parent()) instanceof AST_Call + && iife.expression === node) { // Virtually turn IIFE parameters into variable definitions: // (function(a,b) {...})(c,d) => (function() {var a=c,b=d; ...})() // So existing transformation rules can work on them. @@ -339,6 +338,10 @@ merge(Compressor.prototype, { mark(d, true); }); } + push(); + descend(); + pop(); + return true; } if (node instanceof AST_Binary && (node.operator == "&&" || node.operator == "||")) { diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 2264783d..c01572dd 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1592,3 +1592,25 @@ var_side_effects_3: { } expect_stdout: true } + +reduce_vars_assign: { + options = { + collapse_vars: true, + reduce_vars: true, + } + input: { + !function() { + var a = 1; + a = [].length, + console.log(a); + }(); + } + expect: { + !function() { + var a = 1; + a = [].length, + console.log(a); + }(); + } + expect_stdout: "0" +} diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 0eeb80b3..59f49f35 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -2258,3 +2258,34 @@ cond_assign: { } expect_stdout: "undefined" } + +iife_assign: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + !function() { + var a = 1, b = 0; + !function() { + b++; + return; + a = 2; + }(); + console.log(a); + }(); + } + expect: { + !function() { + var a = 1, b = 0; + !function() { + b++; + return; + a = 2; + }(); + console.log(a); + }(); + } + expect_stdout: "1" +}