diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-02-26 20:16:14 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-27 04:16:14 +0800 |
commit | ac26993b5a1546e790d93603d0d7a05740566b07 (patch) | |
tree | 1aa4e1a945a847db8314a5966764c357134cd1d1 /test | |
parent | ea52339502ac0436532e55a6980df0785736c8fb (diff) | |
download | tracifyjs-ac26993b5a1546e790d93603d0d7a05740566b07.tar.gz tracifyjs-ac26993b5a1546e790d93603d0d7a05740566b07.zip |
fix corner cases with block-scoped functions (#4695)
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/const.js | 44 | ||||
-rw-r--r-- | test/compress/dead-code.js | 13 | ||||
-rw-r--r-- | test/compress/functions.js | 144 | ||||
-rw-r--r-- | test/compress/let.js | 47 |
4 files changed, 241 insertions, 7 deletions
diff --git a/test/compress/const.js b/test/compress/const.js index be49e845..4306fff2 100644 --- a/test/compress/const.js +++ b/test/compress/const.js @@ -1454,3 +1454,47 @@ issue_4689: { expect_stdout: "PASS" node_version: ">=4" } + +issue_4691: { + options = { + if_return: true, + toplevel: true, + } + input: { + function A() {} + A.prototype.f = function() { + if (!this) + return; + const a = "PA"; + function g(b) { + h(a + b); + } + [ "SS" ].forEach(function(c) { + g(c); + }); + }; + function h(d) { + console.log(d); + } + new A().f(); + } + expect: { + function A() {} + A.prototype.f = function() { + if (this) { + const a = "PA"; + [ "SS" ].forEach(function(c) { + g(c); + }); + function g(b) { + h(a + b); + } + } + }; + function h(d) { + console.log(d); + } + new A().f(); + } + expect_stdout: "PASS" +} diff --git a/test/compress/dead-code.js b/test/compress/dead-code.js index 1720a0dc..9101869d 100644 --- a/test/compress/dead-code.js +++ b/test/compress/dead-code.js @@ -53,8 +53,10 @@ dead_code_2_should_warn: { g(); x = 10; throw new Error("foo"); - var x; - function g(){}; + { + var x; + function g(){}; + } } f(); } @@ -62,7 +64,6 @@ dead_code_2_should_warn: { expect_warnings: [ "WARN: Dropping unreachable code [test/compress/dead-code.js:8,12]", ] - node_version: "<=4" } dead_code_constant_boolean_should_warn_more: { @@ -88,8 +89,10 @@ dead_code_constant_boolean_should_warn_more: { bar(); } expect: { - var foo; - function bar() {} + { + var foo; + function bar() {} + } // nothing for the while // as for the for, it should keep: var x = 10, y; diff --git a/test/compress/functions.js b/test/compress/functions.js index fe9307ca..44e41dff 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -5018,9 +5018,12 @@ catch_no_argname: { try { throw a; } catch { - console.log(a, a, a); + function g() { + return a; + } + console.log(a, a, g()); } - console.log(a, a, a); + console.log(a, a, g()); } expect_stdout: [ "PASS PASS PASS", @@ -5558,3 +5561,140 @@ issue_4659_3: { } expect_stdout: "1" } + +block_scope_1: { + input: { + console.log(typeof f); + function f() {} + } + expect: { + console.log(typeof f); + function f() {} + } + expect_stdout: "function" +} + +block_scope_1_compress: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + typeofs: true, + unused: true, + } + input: { + console.log(typeof f); + function f() {} + } + expect: { + console.log("function"); + } + expect_stdout: "function" +} + +block_scope_2: { + input: { + { + console.log(typeof f); + } + function f() {} + } + expect: { + console.log(typeof f); + function f() {} + } + expect_stdout: "function" +} + +block_scope_2_compress: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + typeofs: true, + unused: true, + } + input: { + { + console.log(typeof f); + } + function f() {} + } + expect: { + console.log("function"); + } + expect_stdout: "function" +} + +block_scope_3: { + input: { + console.log(typeof f); + { + function f() {} + } + } + expect: { + console.log(typeof f); + { + function f() {} + } + } + expect_stdout: true +} + +block_scope_3_compress: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + typeofs: true, + unused: true, + } + input: { + console.log(typeof f); + { + function f() {} + } + } + expect: { + console.log(typeof f); + { + function f() {} + } + } + expect_stdout: true +} + +block_scope_4: { + input: { + { + console.log(typeof f); + function f() {} + } + } + expect: { + console.log(typeof f); + function f() {} + } + expect_stdout: "function" +} + +block_scope_4_compress: { + options = { + evaluate: true, + reduce_vars: true, + toplevel: true, + typeofs: true, + unused: true, + } + input: { + { + console.log(typeof f); + function f() {} + } + } + expect: { + console.log("function"); + } + expect_stdout: "function" +} diff --git a/test/compress/let.js b/test/compress/let.js index b5005ebf..04002e9a 100644 --- a/test/compress/let.js +++ b/test/compress/let.js @@ -1357,3 +1357,50 @@ issue_4689: { expect_stdout: "PASS" node_version: ">=4" } + +issue_4691: { + options = { + if_return: true, + toplevel: true, + } + input: { + "use strict"; + function A() {} + A.prototype.f = function() { + if (!this) + return; + let a = "PA"; + function g(b) { + h(a + b); + } + [ "SS" ].forEach(function(c) { + g(c); + }); + }; + function h(d) { + console.log(d); + } + new A().f(); + } + expect: { + "use strict"; + function A() {} + A.prototype.f = function() { + if (this) { + let a = "PA"; + [ "SS" ].forEach(function(c) { + g(c); + }); + function g(b) { + h(a + b); + } + } + }; + function h(d) { + console.log(d); + } + new A().f(); + } + expect_stdout: "PASS" + node_version: ">=4" +} |