diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-02-28 15:19:32 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-02-28 15:19:32 +0800 |
commit | 36bca6934de494a79a1c1af952653eaaf9c15ed8 (patch) | |
tree | b7f98d8ef621eef6289a233c40f3cea0073bfc06 /test | |
parent | ace581169134d9b6d4cdf6b24880f7ce122fc88e (diff) | |
download | tracifyjs-36bca6934de494a79a1c1af952653eaaf9c15ed8.tar.gz tracifyjs-36bca6934de494a79a1c1af952653eaaf9c15ed8.zip |
enhance `collapse_vars` (#2952)
- `a = b, b` => `a = b`
- `a.b = c, c()` => `(a.b = c)()`
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/collapse_vars.js | 389 | ||||
-rw-r--r-- | test/compress/pure_getters.js | 331 | ||||
-rw-r--r-- | test/compress/sequences.js | 3 |
3 files changed, 721 insertions, 2 deletions
diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 60505509..d4ab444e 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -4770,3 +4770,392 @@ issue_2954_3: { } expect_stdout: Error("PASS") } + +collapse_rhs_conditional_1: { + options = { + collapse_vars: true, + } + input: { + var a = "PASS", b = "FAIL"; + b = a; + "function" == typeof f && f(a); + console.log(a, b); + } + expect: { + var a = "PASS", b = "FAIL"; + b = a; + "function" == typeof f && f(a); + console.log(a, b); + } + expect_stdout: "PASS PASS" +} + +collapse_rhs_conditional_2: { + options = { + collapse_vars: true, + } + input: { + var a = "FAIL", b; + while ((a = "PASS", --b) && "PASS" == b); + console.log(a, b); + } + expect: { + var a = "FAIL", b; + while ((a = "PASS", --b) && "PASS" == b); + console.log(a, b); + } + expect_stdout: "PASS NaN" +} + +collapse_rhs_lhs_1: { + options = { + collapse_vars: true, + } + input: { + var c = 0; + new function() { + this[c++] = 1; + c += 1; + }(); + console.log(c); + } + expect: { + var c = 0; + new function() { + this[c++] = 1; + c += 1; + }(); + console.log(c); + } + expect_stdout: "2" +} + +collapse_rhs_lhs_2: { + options = { + collapse_vars: true, + } + input: { + var b = 1; + (function f(f) { + f = b; + f[b] = 0; + })(); + console.log("PASS"); + } + expect: { + var b = 1; + (function f(f) { + f = b; + f[b] = 0; + })(); + console.log("PASS"); + } + expect_stdout: "PASS" +} + +collapse_rhs_side_effects: { + options = { + collapse_vars: true, + } + input: { + var a = 1, c = 0; + new function f() { + this[a-- && f()] = 1; + c += 1; + }(); + console.log(c); + } + expect: { + var a = 1, c = 0; + new function f() { + this[a-- && f()] = 1; + c += 1; + }(); + console.log(c); + } + expect_stdout: "2" +} + +collapse_rhs_vardef: { + options = { + collapse_vars: true, + } + input: { + var a, b = 1; + a = --b + function c() { + var b; + c[--b] = 1; + }(); + b |= a; + console.log(a, b); + } + expect: { + var a, b = 1; + a = --b + function c() { + var b; + c[--b] = 1; + }(); + b |= a; + console.log(a, b); + } + expect_stdout: "NaN 0" +} + +collapse_rhs_array: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = []; + b = []; + return []; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + a = []; + b = []; + return []; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "false false false" +} + +collapse_rhs_boolean: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = !0; + b = !0; + return !0; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + return b = a = !0; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} + +collapse_rhs_function: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = function() {}; + b = function() {}; + return function() {}; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + a = function() {}; + b = function() {}; + return function() {}; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "false false false" +} + +collapse_rhs_number: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = 42; + b = 42; + return 42; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + return b = a = 42; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} + +collapse_rhs_object: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = {}; + b = {}; + return {}; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + a = {}; + b = {}; + return {}; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "false false false" +} + +collapse_rhs_regexp: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = /bar/; + b = /bar/; + return /bar/; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + a = /bar/; + b = /bar/; + return /bar/; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "false false false" +} + +collapse_rhs_string: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = "foo"; + b = "foo"; + return "foo"; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + return b = a = "foo"; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} + +collapse_rhs_var: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = f; + b = f; + return f; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + return b = a = f; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} + +collapse_rhs_this: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = this; + b = this; + return this; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + return b = a = this; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} + +collapse_rhs_undefined: { + options = { + collapse_vars: true, + } + input: { + var a, b; + function f() { + a = void 0; + b = void 0; + return void 0; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect: { + var a, b; + function f() { + b = a = void 0; + return; + } + var c = f(); + console.log(a === b, b === c, c === a); + } + expect_stdout: "true true true" +} diff --git a/test/compress/pure_getters.js b/test/compress/pure_getters.js index e2662868..f9edcc8d 100644 --- a/test/compress/pure_getters.js +++ b/test/compress/pure_getters.js @@ -830,3 +830,334 @@ issue_2938_4: { } expect_stdout: "PASS" } + +collapse_vars_1_true: { + options = { + collapse_vars: true, + pure_getters: true, + unused: true, + } + input: { + function f(a, b) { + for (;;) { + var c = a.g(); + var d = b.p; + if (c || d) break; + } + } + } + expect: { + function f(a, b) { + for (;;) { + if (a.g() || b.p) break; + } + } + } +} + +collapse_vars_1_false: { + options = { + collapse_vars: true, + pure_getters: false, + unused: true, + } + input: { + function f(a, b) { + for (;;) { + var c = a.g(); + var d = b.p; + if (c || d) break; + } + } + } + expect: { + function f(a, b) { + for (;;) { + var c = a.g(); + var d = b.p; + if (c || d) break; + } + } + } +} + +collapse_vars_1_strict: { + options = { + collapse_vars: true, + pure_getters: "strict", + unused: true, + } + input: { + function f(a, b) { + for (;;) { + var c = a.g(); + var d = b.p; + if (c || d) break; + } + } + } + expect: { + function f(a, b) { + for (;;) { + var c = a.g(); + var d = b.p; + if (c || d) break; + } + } + } +} + +collapse_vars_2_true: { + options = { + collapse_vars: true, + pure_getters: true, + reduce_vars: true, + } + input: { + function f() { + function g() {} + g.a = function() {}; + g.b = g.a; + return g; + } + } + expect: { + function f() { + function g() {} + g.b = g.a = function() {}; + return g; + } + } +} + +collapse_vars_2_false: { + options = { + collapse_vars: true, + pure_getters: false, + reduce_vars: true, + } + input: { + function f() { + function g() {} + g.a = function() {}; + g.b = g.a; + return g; + } + } + expect: { + function f() { + function g() {} + g.a = function() {}; + g.b = g.a; + return g; + } + } +} + +collapse_vars_2_strict: { + options = { + collapse_vars: true, + pure_getters: "strict", + reduce_vars: true, + } + input: { + function f() { + function g() {} + g.a = function() {}; + g.b = g.a; + return g; + } + } + expect: { + function f() { + function g() {} + g.b = g.a = function() {}; + return g; + } + } +} + +collapse_rhs_true: { + options = { + collapse_vars: true, + pure_getters: true, + } + input: { + console.log((42..length = "PASS", "PASS")); + console.log(("foo".length = "PASS", "PASS")); + console.log((false.length = "PASS", "PASS")); + console.log((function() {}.length = "PASS", "PASS")); + console.log(({ + get length() { + return "FAIL"; + } + }.length = "PASS", "PASS")); + } + expect: { + console.log(42..length = "PASS"); + console.log("foo".length = "PASS"); + console.log(false.length = "PASS"); + console.log(function() {}.length = "PASS"); + console.log({ + get length() { + return "FAIL"; + } + }.length = "PASS"); + } + expect_stdout: [ + "PASS", + "PASS", + "PASS", + "PASS", + "PASS", + ] +} + +collapse_rhs_false: { + options = { + collapse_vars: true, + pure_getters: false, + } + input: { + console.log((42..length = "PASS", "PASS")); + console.log(("foo".length = "PASS", "PASS")); + console.log((false.length = "PASS", "PASS")); + console.log((function() {}.length = "PASS", "PASS")); + console.log(({ + get length() { + return "FAIL"; + } + }.length = "PASS", "PASS")); + } + expect: { + console.log(42..length = "PASS"); + console.log("foo".length = "PASS"); + console.log(false.length = "PASS"); + console.log(function() {}.length = "PASS"); + console.log({ + get length() { + return "FAIL"; + } + }.length = "PASS"); + } + expect_stdout: [ + "PASS", + "PASS", + "PASS", + "PASS", + "PASS", + ] +} + +collapse_rhs_strict: { + options = { + collapse_vars: true, + pure_getters: "strict", + } + input: { + console.log((42..length = "PASS", "PASS")); + console.log(("foo".length = "PASS", "PASS")); + console.log((false.length = "PASS", "PASS")); + console.log((function() {}.length = "PASS", "PASS")); + console.log(({ + get length() { + return "FAIL"; + } + }.length = "PASS", "PASS")); + } + expect: { + console.log(42..length = "PASS"); + console.log("foo".length = "PASS"); + console.log(false.length = "PASS"); + console.log(function() {}.length = "PASS"); + console.log({ + get length() { + return "FAIL"; + } + }.length = "PASS"); + } + expect_stdout: [ + "PASS", + "PASS", + "PASS", + "PASS", + "PASS", + ] +} + +collapse_rhs_setter: { + options = { + collapse_vars: true, + pure_getters: "strict", + } + input: { + try { + console.log(({ + set length(v) { + throw "PASS"; + } + }.length = "FAIL", "FAIL")); + } catch (e) { + console.log(e); + } + } + expect: { + try { + console.log({ + set length(v) { + throw "PASS"; + } + }.length = "FAIL"); + } catch (e) { + console.log(e); + } + } + expect_stdout: "PASS" +} + +collapse_rhs_call: { + options = { + collapse_vars: true, + passes: 2, + pure_getters: "strict", + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + var o = {}; + function f() { + console.log("PASS"); + } + o.f = f; + f(); + } + expect: { + ({}.f = function() { + console.log("PASS"); + })(); + } + expect_stdout: "PASS" +} + +collapse_rhs_lhs: { + options = { + collapse_vars: true, + pure_getters: true, + } + input: { + function f(a, b) { + a.b = b, b += 2; + console.log(a.b, b); + } + f({}, 1); + } + expect: { + function f(a, b) { + a.b = b, b += 2; + console.log(a.b, b); + } + f({}, 1); + } + expect_stdout: "1 3" +} diff --git a/test/compress/sequences.js b/test/compress/sequences.js index 03075bf1..12acbcf7 100644 --- a/test/compress/sequences.js +++ b/test/compress/sequences.js @@ -668,8 +668,7 @@ side_effects_cascade_2: { } expect: { function f(a, b) { - b = a, - !a + (b += a) || (b += a), + !(b = a) + (b += a) || (b += a), b = a; } } |