diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2017-03-02 00:20:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-03-02 00:20:53 +0800 |
commit | 7aa69117e1a38e4aeead13100ac952ca99dbb07f (patch) | |
tree | 6b957de603843f936905ff76caceb22a2e625a4c /test | |
parent | bff7ad67bbab6ce0792292fba66d3a6cf8d1836f (diff) | |
download | tracifyjs-7aa69117e1a38e4aeead13100ac952ca99dbb07f.tar.gz tracifyjs-7aa69117e1a38e4aeead13100ac952ca99dbb07f.zip |
fix corner cases in `reduce_vars` (#1524)
Avoid variable substitution in the following cases:
- use of variable before declaration
- declaration within conditional code blocks
- declaration within loop body
fixes #1518
fixes #1525
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/reduce_vars.js | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js index 0ee201c0..e38c317b 100644 --- a/test/compress/reduce_vars.js +++ b/test/compress/reduce_vars.js @@ -470,3 +470,122 @@ multi_def_2: { var repeatLength = this.getBits(bitsLength) + bitsOffset; } } + +use_before_var: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + console.log(t); + var t = 1; + } + expect: { + console.log(t); + var t = 1; + } +} + +inner_var_if: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + function f(){ + return 0; + } + if (f()) + var t = 1; + if (!t) + console.log(t); + } + expect: { + function f(){ + return 0; + } + if (f()) + var t = 1; + if (!t) + console.log(t); + } +} + +inner_var_label: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + function f(){ + return 1; + } + l: { + if (f()) break l; + var t = 1; + } + console.log(t); + } + expect: { + function f(){ + return 1; + } + l: { + if (f()) break l; + var t = 1; + } + console.log(t); + } +} + +inner_var_for: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + var a = 1; + x(a, b, d); + for (var b = 2, c = 3; x(a, b, c, d); x(a, b, c, d)) { + var d = 4, e = 5; + x(a, b, c, d, e); + } + x(a, b, c, d, e) + } + expect: { + var a = 1; + x(1, b, d); + for (var b = 2, c = 3; x(1, b, 3, d); x(1, b, 3, d)) { + var d = 4, e = 5; + x(1, b, 3, d, e); + } + x(1, b, 3, d, e); + } +} + +inner_var_for_in: { + options = { + evaluate: true, + reduce_vars: true, + } + input: { + var a = 1, b = 2; + for (b in (function() { + return x(a, b, c); + })()) { + var c = 3, d = 4; + x(a, b, c, d); + } + x(a, b, c, d); + } + expect: { + var a = 1, b = 2; + for (b in (function() { + return x(1, b, c); + })()) { + var c = 3, d = 4; + x(1, b, c, d); + } + x(1, b, c, d); + } +} |