diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-03-16 12:58:51 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-16 20:58:51 +0800 |
commit | 9a9543013c9d56cfc9593893fb9bfdff2aaa0307 (patch) | |
tree | 51c9711bb30e47ae6582b1209222a43cb9fe31d5 | |
parent | b98ce6c84fcd7cc5203aa0657b216e52103ca9e4 (diff) | |
download | tracifyjs-9a9543013c9d56cfc9593893fb9bfdff2aaa0307.tar.gz tracifyjs-9a9543013c9d56cfc9593893fb9bfdff2aaa0307.zip |
fix corner case in `functions` (#4789)
fixes #4788
-rw-r--r-- | lib/compress.js | 10 | ||||
-rw-r--r-- | test/compress/functions.js | 37 | ||||
-rw-r--r-- | test/compress/hoist_vars.js | 2 | ||||
-rw-r--r-- | test/ufuzz/index.js | 6 |
4 files changed, 50 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js index 2dc6fabb..ac66ce6a 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6279,11 +6279,15 @@ merge(Compressor.prototype, { function can_rename(fn, name) { if (!fn.name) return !fn.variables.get(name); old_def = fn.name.definition(); - if (old_def.assignments > 0) return false; - if (old_def.name == name) return true; + if (old_def.orig.length > 1) { + old_def = null; + } else { + if (old_def.assignments > 0) return false; + if (old_def.name == name) return true; + } if (name == "await" && is_async(fn)) return false; if (name == "yield" && is_generator(fn)) return false; - return all(old_def.references, function(ref) { + return !old_def || all(old_def.references, function(ref) { return ref.scope.find_variable(name) === sym; }); } diff --git a/test/compress/functions.js b/test/compress/functions.js index 68ff709b..e43efcea 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -5912,3 +5912,40 @@ issue_4753_2: { } expect_stdout: "PASS" } + +issue_4788: { + options = { + evaluate: true, + functions: true, + keep_fnames: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f() { + var a = function g() { + if (0) { + var g = 42; + f(); + } + g || console.log("PASS"); + }; + a(a); + } + f(); + } + expect: { + (function f() { + function a() { + if (0) { + var g = 42; + f(); + } + g || console.log("PASS"); + } + a(); + })(); + } + expect_stdout: "PASS" +} diff --git a/test/compress/hoist_vars.js b/test/compress/hoist_vars.js index 305f98c6..4118f765 100644 --- a/test/compress/hoist_vars.js +++ b/test/compress/hoist_vars.js @@ -153,7 +153,7 @@ issue_4487: { } expect: { function a() { - var a = console.log(typeof a); + var f = console.log(typeof f); } a(); } diff --git a/test/ufuzz/index.js b/test/ufuzz/index.js index bc9e62a0..f7ad29ad 100644 --- a/test/ufuzz/index.js +++ b/test/ufuzz/index.js @@ -1947,7 +1947,11 @@ function createTypeofExpr(recurmax, stmtDepth, canThrow) { } function createValue() { - return VALUES[rng(VALUES.length)]; + var v; + do { + v = VALUES[rng(VALUES.length)]; + } while (v == "new.target" && rng(200)); + return v; } function createBinaryOp(noComma, canThrow) { |