From 5d9f1da3abc58bce95dd240bd586bedb4eb04771 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 18 Apr 2017 13:38:42 +0800 Subject: support safe reassignments in `reduce_vars` (#1823) `var a=1;a=2;x(a)` => `x(2)` fix pre-existing issues - reference counting on assignment - walking of anonymous functions - chained assignment --- test/compress/collapse_vars.js | 22 +++++++++ test/compress/reduce_vars.js | 102 ++++++++++++++++++++++++++++++++++------- 2 files changed, 108 insertions(+), 16 deletions(-) (limited to 'test') 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 ad2c90bc..c5f26904 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -66,7 +66,7 @@ modified: { conditionals : true, evaluate : true, reduce_vars : true, - unused : true + unused : true, } input: { function f0() { @@ -136,12 +136,11 @@ modified: { } function f2() { - var b = 2; - b = 3; - console.log(1 + b); - console.log(b + 3); + 3; console.log(4); - console.log(1 + b + 3); + console.log(6); + console.log(4); + console.log(7); } function f3() { @@ -375,12 +374,11 @@ passes: { } expect: { function f() { - var b = 2; - b = 3; - console.log(1 + b); - console.log(b + 3); + 3; console.log(4); - console.log(1 + b + 3); + console.log(6); + console.log(4); + console.log(7); } } } @@ -573,7 +571,7 @@ inner_var_label: { } } -inner_var_for: { +inner_var_for_1: { options = { evaluate: true, reduce_vars: true, @@ -602,6 +600,29 @@ inner_var_for: { } } +inner_var_for_2: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + !function() { + var a = 1; + for (var b = 1; --b;) var a = 2; + console.log(a); + }(); + } + expect: { + !function() { + a = 1; + for (var b = 1; --b;) var a = 2; + console.log(a); + }(); + } + expect_stdout: "1" +} + inner_var_for_in_1: { options = { evaluate: true, @@ -1828,10 +1849,7 @@ redefine_farg_3: { console.log(function(a) { var a; return typeof a; - }([]), "number", function(a) { - var a = void 0; - return typeof a; - }([])); + }([]), "number", "undefined"); } expect_stdout: "object number undefined" } @@ -2115,6 +2133,27 @@ var_assign_5: { expect_stdout: "2 undefined" } +var_assign_6: { + options = { + evaluate: true, + reduce_vars: true, + unused: true, + } + input: { + !function() { + var a = function(){}(a = 1); + console.log(a); + }(); + } + expect: { + !function() { + var a = function(){}(a = 1); + console.log(a); + }(); + } + expect_stdout: "undefined" +} + immutable: { options = { evaluate: true, @@ -2263,3 +2302,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" +} -- cgit v1.2.3