diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-09-15 03:01:48 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-15 10:01:48 +0800 |
commit | 3ac575f2e81630d560b6353831761a7f11037d93 (patch) | |
tree | 468f29a0d5a4ad0ca36b67f4b7164e6deca1f219 /test/compress | |
parent | d33a3a3253e2370f6874b2e5b01e94d78406162d (diff) | |
download | tracifyjs-3ac575f2e81630d560b6353831761a7f11037d93.tar.gz tracifyjs-3ac575f2e81630d560b6353831761a7f11037d93.zip |
introduce `merge_vars` (#4100)
Diffstat (limited to 'test/compress')
-rw-r--r-- | test/compress/merge_vars.js | 233 |
1 files changed, 233 insertions, 0 deletions
diff --git a/test/compress/merge_vars.js b/test/compress/merge_vars.js new file mode 100644 index 00000000..8ef186cf --- /dev/null +++ b/test/compress/merge_vars.js @@ -0,0 +1,233 @@ +merge: { + options = { + merge_vars: true, + toplevel: false, + } + input: { + var a = "foo"; + console.log(a); + function f(b) { + var c; + console.log(b); + c = "bar"; + console.log(c); + } + f("baz"); + var d = "moo"; + console.log(d); + } + expect: { + var a = "foo"; + console.log(a); + function f(c) { + var c; + console.log(c); + c = "bar"; + console.log(c); + } + f("baz"); + var d = "moo"; + console.log(d); + } + expect_stdout: [ + "foo", + "baz", + "bar", + "moo", + ] +} + +merge_toplevel: { + options = { + merge_vars: true, + toplevel: true, + } + input: { + var a = "foo"; + console.log(a); + function f(b) { + var c; + console.log(b); + c = "bar"; + console.log(c); + } + f("baz"); + var d = "moo"; + console.log(d); + } + expect: { + var d = "foo"; + console.log(d); + function f(c) { + var c; + console.log(c); + c = "bar"; + console.log(c); + } + f("baz"); + var d = "moo"; + console.log(d); + } + expect_stdout: [ + "foo", + "baz", + "bar", + "moo", + ] +} + +init_scope_vars: { + options = { + merge_vars: true, + unsafe_proto: true, + } + input: { + Function.prototype.call(); + } + expect: { + (function() {}).call(); + } + expect_stdout: true +} + +binary_branch: { + options = { + merge_vars: true, + } + input: { + console.log(function(a) { + var b = "FAIL", c; + a && (c = b); + return c || "PASS"; + }()); + } + expect: { + console.log(function(a) { + var b = "FAIL", c; + a && (c = b); + return c || "PASS"; + }()); + } + expect_stdout: "PASS" +} + +conditional_branch: { + options = { + merge_vars: true, + } + input: { + console.log(function(a) { + var b = "FAIL", c; + a ? (c = b) : void 0; + return c || "PASS"; + }()); + } + expect: { + console.log(function(a) { + var b = "FAIL", c; + a ? (c = b) : void 0; + return c || "PASS"; + }()); + } + expect_stdout: "PASS" +} + +if_branch: { + options = { + merge_vars: true, + } + input: { + console.log(function(a) { + var b = "FAIL", c; + if (a) c = b; + return c || "PASS"; + }()); + } + expect: { + console.log(function(a) { + var b = "FAIL", c; + if (a) c = b; + return c || "PASS"; + }()); + } + expect_stdout: "PASS" +} + +switch_branch: { + options = { + merge_vars: true, + } + input: { + console.log(function(a) { + var b = "FAIL", c; + switch (a) { + case 1: + c = b; + break; + } + return c || "PASS"; + }()); + } + expect: { + console.log(function(a) { + var b = "FAIL", c; + switch (a) { + case 1: + c = b; + break; + } + return c || "PASS"; + }()); + } + expect_stdout: "PASS" +} + +read_before_assign_1: { + options = { + inline: true, + merge_vars: true, + sequences: true, + toplevel: true, + } + input: { + var c = 0; + c = 0; + (function() { + var a = console.log(++a); + a; + })(); + c; + } + expect: { + var c = 0; + var a; + c = 0, + a = console.log(++a); + } + expect_stdout: "NaN" +} + +read_before_assign_2: { + options = { + dead_code: true, + loops: true, + merge_vars: true, + } + input: { + console.log(function(a, a) { + while (b) + return "FAIL"; + var b = 1; + return "PASS"; + }(0, [])); + } + expect: { + console.log(function(a, a) { + if (b) + return "FAIL"; + var b = 1; + return "PASS"; + }(0, [])); + } + expect_stdout: "PASS" +} |