From 1b61a81b5d6c9489466fe4707c1b01810a01d03a Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 29 Nov 2019 17:45:49 +0800 Subject: enhance `collapse_vars` (#3613) --- test/compress/collapse_vars.js | 180 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 170 insertions(+), 10 deletions(-) (limited to 'test') diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 175ab9c2..20bbfea2 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -1348,8 +1348,7 @@ collapse_vars_array_3: { } expect: { function f(a) { - var b; - return [ b = a, b, b ]; + return [ a, a, a ]; } console.log(f().length); } @@ -1487,11 +1486,10 @@ collapse_vars_object_3: { } expect: { function f(a) { - var b; return { - p: b = a, - q: b, - r: b, + p: a, + q: a, + r: a, }; } console.log(f("PASS").r); @@ -6304,7 +6302,7 @@ assign_left: { } expect: { console.log(function(a, b) { - (b = a).p.q = "PASS"; + a.p.q = "PASS"; return a.p.q; }({p: {}})); } @@ -6317,12 +6315,12 @@ sub_property: { } input: { console.log(function(a, b) { - return a[(b = a, b.length - 1)]; + return a[b = a, b.length - 1]; }([ "FAIL", "PASS" ])); } expect: { console.log(function(a, b) { - return a[(b = a).length - 1]; + return a[a.length - 1]; }([ "FAIL", "PASS" ])); } expect_stdout: "PASS" @@ -6754,7 +6752,7 @@ local_value_replacement: { } expect: { function f(a, b) { - (a = b) && g(a); + b && g(b); } function g(c) { console.log(c); @@ -6805,3 +6803,165 @@ array_in_object_2: { } expect_stdout: "1 1" } + +array_in_conditional: { + options = { + collapse_vars: true, + } + input: { + var a = 1, b = 2, c; + console.log(c && [ b = a ], a, b); + } + expect: { + var a = 1, b = 2, c; + console.log(c && [ b = a ], a, b); + } + expect_stdout: "undefined 1 2" +} + +object_in_conditional: { + options = { + collapse_vars: true, + } + input: { + var a = 1, b = 2, c; + console.log(c && { + p: b = a + }, a, b); + } + expect: { + var a = 1, b = 2, c; + console.log(c && { + p: b = a + }, a, b); + } + expect_stdout: "undefined 1 2" +} + +sequence_in_iife_1: { + options = { + collapse_vars: true, + } + input: { + var a = "foo", b = 42; + (function() { + var c = (b = a, b); + })(); + console.log(a, b); + } + expect: { + var a = "foo", b = 42; + (function() { + var c = b = a; + })(); + console.log(a, b); + } + expect_stdout: "foo foo" +} + +sequence_in_iife_2: { + options = { + collapse_vars: true, + inline: true, + passes: 2, + side_effects: true, + unused: true, + } + input: { + var a = "foo", b = 42; + (function() { + var c = (b = a, b); + })(); + console.log(a, b); + } + expect: { + var a = "foo", b = 42; + console.log(a, a); + } + expect_stdout: "foo foo" +} + +retain_assign: { + options = { + collapse_vars: true, + } + input: { + var a = 42, b, c = "FAIL"; + b = a; + b++ && (c = "PASS"); + console.log(c); + } + expect: { + var a = 42, b, c = "FAIL"; + b = a; + b++ && (c = "PASS"); + console.log(c); + } + expect_stdout: "PASS" +} + +getter_side_effect: { + options = { + collapse_vars: true, + } + input: { + var c = "FAIL"; + (function(a) { + var b; + (b = a) && { + get foo() { + a = 0; + } + }.foo; + b && (c = "PASS"); + })(42); + console.log(c); + } + expect: { + var c = "FAIL"; + (function(a) { + var b; + (b = a) && { + get foo() { + a = 0; + } + }.foo; + b && (c = "PASS"); + })(42); + console.log(c); + } + expect_stdout: "PASS" +} + +setter_side_effect: { + options = { + collapse_vars: true, + } + input: { + var c = "FAIL"; + (function(a) { + var b; + (b = a) && ({ + set foo(v) { + a = v; + } + }.foo = 0); + b && (c = "PASS"); + })(42); + console.log(c); + } + expect: { + var c = "FAIL"; + (function(a) { + var b; + (b = a) && ({ + set foo(v) { + a = v; + } + }.foo = 0); + b && (c = "PASS"); + })(42); + console.log(c); + } + expect_stdout: "PASS" +} -- cgit v1.2.3