diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-02 15:33:58 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-02 23:33:58 +0800 |
commit | adcafce04852bb4730e3e699dfdd656dc018cd0f (patch) | |
tree | 3f30430a010b2967022755f58bee570a9876bc3c /test | |
parent | b1e05fd48afd9aceaa00cc621a8f6ac22c097398 (diff) | |
download | tracifyjs-adcafce04852bb4730e3e699dfdd656dc018cd0f.tar.gz tracifyjs-adcafce04852bb4730e3e699dfdd656dc018cd0f.zip |
fix corner cases in `varify` (#4719)
Diffstat (limited to 'test')
-rw-r--r-- | test/compress/varify.js | 86 | ||||
-rw-r--r-- | test/ufuzz/index.js | 23 |
2 files changed, 103 insertions, 6 deletions
diff --git a/test/compress/varify.js b/test/compress/varify.js index ab212842..f60c5474 100644 --- a/test/compress/varify.js +++ b/test/compress/varify.js @@ -354,6 +354,92 @@ forin_let_2: { node_version: ">=6" } +loop_scope_1: { + options = { + toplevel: true, + varify: true, + } + input: { + "use strict"; + var o = { foo: 1, bar: 2 }; + for (let i in o) { + console.log(i); + } + for (const j in o) + setTimeout(() => console.log(j), 0); + for (let k in o) + setTimeout(function() { + console.log(k); + }, 0); + } + expect: { + "use strict"; + var o = { foo: 1, bar: 2 }; + for (var i in o) + console.log(i); + for (const j in o) + setTimeout(() => console.log(j), 0); + for (let k in o) + setTimeout(function() { + console.log(k); + }, 0); + } + expect_stdout: [ + "foo", + "bar", + "foo", + "bar", + "foo", + "bar", + ] + node_version: ">=4" +} + +loop_scope_2: { + options = { + reduce_vars: true, + toplevel: true, + varify: true, + } + input: { + "use strict"; + var a = [ "foo", "bar" ]; + for (var i = 0; i < a.length; i++) { + const x = a[i]; + console.log(x); + let y = a[i]; + setTimeout(() => console.log(y), 0); + const z = a[i]; + setTimeout(function() { + console.log(z); + }, 0); + } + } + expect: { + "use strict"; + var a = [ "foo", "bar" ]; + for (var i = 0; i < a.length; i++) { + var x = a[i]; + console.log(x); + let y = a[i]; + setTimeout(() => console.log(y), 0); + const z = a[i]; + setTimeout(function() { + console.log(z); + }, 0); + } + } + expect_stdout: [ + "foo", + "bar", + "foo", + "foo", + "bar", + "bar", + ] + node_version: ">=4" +} + issue_4290_1_const: { options = { reduce_vars: true, diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index f38cdd96..191814c1 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -919,6 +919,12 @@ function getLabel(label) { return label && " L" + label; } +function declareVarName(name, no_var) { + if (!SUPPORT.let || !no_var && rng(10)) return "var "; + block_vars.push(name); + return rng(2) ? "let " : "const "; +} + function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth, target) { ++stmtDepth; var loop = ++loops; @@ -955,6 +961,8 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); return label.target + "for (var brake" + loop + " = 5; " + createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + " && brake" + loop + " > 0; --brake" + loop + ")" + createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth); case STMT_FOR_ENUM: + var block_len = block_vars.length; + var nameLenBefore = VAR_NAMES.length; var label = createLabel(canBreak, canContinue); canBreak = label.break || enableLoopControl(canBreak, CAN_BREAK); canContinue = label.continue || enableLoopControl(canContinue, CAN_CONTINUE); @@ -963,12 +971,8 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn var init = ""; if (!/^key/.test(key)) { if (!(of && bug_for_of_var) && rng(10) == 0) init = "var "; - } else if (!SUPPORT.let || !(of && bug_for_of_var) && rng(10)) { - init = "var "; - } else if (rng(2)) { - init = "let "; } else { - init = "const "; + init = declareVarName(key, of && bug_for_of_var); } if (!SUPPORT.destructuring || of && !(canThrow && rng(20) == 0) || rng(10)) { init += key; @@ -1003,8 +1007,15 @@ function createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn s += createExpression(recurmax, COMMA_OK, stmtDepth, canThrow) + "; "; s += label.target + " for (" + init + " in expr" + loop + ") {"; } - if (rng(3)) s += "c = 1 + c; var " + createVarName(MANDATORY) + " = expr" + loop + "[" + key + "]; "; + if (/^key/.test(key)) VAR_NAMES.push(key); + if (rng(3)) { + s += "c = 1 + c; "; + var name = createVarName(MANDATORY); + s += declareVarName(name) + name + " = expr" + loop + "[" + key + "]; "; + } s += createStatement(recurmax, canThrow, canBreak, canContinue, cannotReturn, stmtDepth) + "}"; + VAR_NAMES.length = nameLenBefore; + block_vars.length = block_len; return "{" + s + "}"; case STMT_SEMI: return use_strict && rng(20) === 0 ? '"use strict";' : ";"; |