diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-06-24 12:43:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-24 19:43:46 +0800 |
commit | 1a064b6e741f9c3e98f4e2de8084c48f324c0318 (patch) | |
tree | 64c8e372030465ac480464d3d58d99da67bca128 | |
parent | 82772ccb12cfef97480fa8021032a53f90cd1f45 (diff) | |
download | tracifyjs-1a064b6e741f9c3e98f4e2de8084c48f324c0318.tar.gz tracifyjs-1a064b6e741f9c3e98f4e2de8084c48f324c0318.zip |
fix corner case in `functions` (#5035)
fixes #5034
-rw-r--r-- | lib/compress.js | 21 | ||||
-rw-r--r-- | test/compress/awaits.js | 34 | ||||
-rw-r--r-- | test/compress/yields.js | 30 |
3 files changed, 82 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js index 3f9678a7..22f4736a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6562,10 +6562,25 @@ merge(Compressor.prototype, { if (def.orig.length > 1) return null; if (def.assignments > 0) return false; if (def.name == name) return def; - if (name == "await" && is_async(fn)) return false; - if (name == "yield" && is_generator(fn)) return false; + var forbidden; + switch (name) { + case "await": + forbidden = is_async; + break; + case "yield": + forbidden = is_generator; + break; + } return all(def.references, function(ref) { - return ref.scope.find_variable(name) === sym; + var scope = ref.scope; + if (scope.find_variable(name) !== sym) return false; + if (forbidden) { + scope = scope.resolve(); + do { + if (forbidden(scope)) return false; + } while ((scope = scope.parent_scope.resolve()) && scope !== fn); + } + return true; }) && def; } diff --git a/test/compress/awaits.js b/test/compress/awaits.js index a0d744ba..eb808464 100644 --- a/test/compress/awaits.js +++ b/test/compress/awaits.js @@ -1989,3 +1989,37 @@ issue_5032_webkit: { ] node_version: ">=8" } + +issue_5034: { + options = { + functions: true, + reduce_vars: true, + unused: true, + } + input: { + (function() { + var await = function f() { + return async function() { + return f; + }; + }; + await()().then(function(value) { + console.log(value === await ? "PASS" : "FAIL"); + }); + })(); + } + expect: { + (function() { + var await = function f() { + return async function() { + return f; + }; + }; + await()().then(function(value) { + console.log(value === await ? "PASS" : "FAIL"); + }); + })(); + } + expect_stdout: "PASS" + node_version: ">=8" +} diff --git a/test/compress/yields.js b/test/compress/yields.js index 5761d85e..9df533b9 100644 --- a/test/compress/yields.js +++ b/test/compress/yields.js @@ -1216,3 +1216,33 @@ issue_5032_webkit: { ] node_version: ">=4" } + +issue_5034: { + options = { + functions: true, + reduce_vars: true, + unused: true, + } + input: { + console.log(function() { + var yield = function f() { + return function*() { + return f; + }; + }; + return yield()().next().value === yield; + }() ? "PASS" : "FAIL"); + } + expect: { + console.log(function() { + var yield = function f() { + return function*() { + return f; + }; + }; + return yield()().next().value === yield; + }() ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" + node_version: ">=4" +} |