diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-12-08 01:15:31 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-12-08 01:15:31 +0800 |
commit | 3e34f62a1c48ab45db34cfb08d8dd2118c5780f0 (patch) | |
tree | 4dda97562e2cdd858c00686d5e2a7c5406caf87b /test | |
parent | d21cb84696f47f5cba25c88527d5575b7954191a (diff) | |
download | tracifyjs-3e34f62a1c48ab45db34cfb08d8dd2118c5780f0.tar.gz tracifyjs-3e34f62a1c48ab45db34cfb08d8dd2118c5780f0.zip |
account for side-effects in conditional call inversion (#2562)
fixes #2560
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/conditionals.js | 128 |
1 files changed, 108 insertions, 20 deletions
diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index 89c05263..143ece4a 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -166,22 +166,24 @@ cond_1: { conditionals: true }; input: { - var do_something; // if undeclared it's assumed to have side-effects - if (some_condition()) { - do_something(x); - } else { - do_something(y); - } - if (some_condition()) { - side_effects(x); - } else { - side_effects(y); + function foo(do_something, some_condition) { + if (some_condition) { + do_something(x); + } else { + do_something(y); + } + if (some_condition) { + side_effects(x); + } else { + side_effects(y); + } } } expect: { - var do_something; - do_something(some_condition() ? x : y); - some_condition() ? side_effects(x) : side_effects(y); + function foo(do_something, some_condition) { + do_something(some_condition ? x : y); + some_condition ? side_effects(x) : side_effects(y); + } } } @@ -190,16 +192,18 @@ cond_2: { conditionals: true }; input: { - var x, FooBar; - if (some_condition()) { - x = new FooBar(1); - } else { - x = new FooBar(2); + function foo(x, FooBar, some_condition) { + if (some_condition) { + x = new FooBar(1); + } else { + x = new FooBar(2); + } } } expect: { - var x, FooBar; - x = new FooBar(some_condition() ? 1 : 2); + function foo(x, FooBar, some_condition) { + x = new FooBar(some_condition ? 1 : 2); + } } } @@ -605,6 +609,42 @@ cond_8c: { } } +cond_9: { + options = { + conditionals: true, + } + input: { + function f(x, y) { + g() ? x(1) : x(2); + x ? (y || x)() : (y || x)(); + x ? y(a, b) : y(d, b, c); + x ? y(a, b, c) : y(a, b, c); + x ? y(a, b, c) : y(a, b, f); + x ? y(a, b, c) : y(a, e, c); + x ? y(a, b, c) : y(a, e, f); + x ? y(a, b, c) : y(d, b, c); + x ? y(a, b, c) : y(d, b, f); + x ? y(a, b, c) : y(d, e, c); + x ? y(a, b, c) : y(d, e, f); + } + } + expect: { + function f(x, y) { + g() ? x(1) : x(2); + x, (y || x)(); + x ? y(a, b) : y(d, b, c); + x, y(a, b, c); + y(a, b, x ? c : f); + y(a, x ? b : e, c); + x ? y(a, b, c) : y(a, e, f); + y(x ? a : d, b, c); + x ? y(a, b, c) : y(d, b, f); + x ? y(a, b, c) : y(d, e, c); + x ? y(a, b, c) : y(d, e, f); + } + } +} + ternary_boolean_consequent: { options = { collapse_vars:true, sequences:true, properties:true, dead_code:true, conditionals:true, @@ -1115,3 +1155,51 @@ issue_2535_2: { "false", ] } + +issue_2560: { + options = { + conditionals: true, + inline: true, + reduce_funcs: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function log(x) { + console.log(x); + } + function foo() { + return log; + } + function bar() { + if (x !== (x = foo())) { + x(1); + } else { + x(2); + } + } + var x = function() { + console.log("init"); + }; + bar(); + bar(); + } + expect: { + function log(x) { + console.log(x); + } + function bar() { + x !== (x = log) ? x(1) : x(2); + } + var x = function() { + console.log("init"); + }; + bar(); + bar(); + } + expect_stdout: [ + "1", + "2", + ] +} |