aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Lam S.L <alexlamsl@gmail.com>2020-10-13 08:52:03 +0100
committerGitHub <noreply@github.com>2020-10-13 15:52:03 +0800
commit0e234a25c52bcfc873fc1ac287995be08a6756aa (patch)
treeb4ef544ffce2da43bff90c073a9bd220f2d174f2
parent3096f6fdad2d92b822ab75bbe78903c152e6b844 (diff)
downloadtracifyjs-0e234a25c52bcfc873fc1ac287995be08a6756aa.tar.gz
tracifyjs-0e234a25c52bcfc873fc1ac287995be08a6756aa.zip
fix corner case in `reduce_vars` (#4211)
fixes #4210
-rw-r--r--lib/compress.js2
-rw-r--r--lib/scope.js11
-rw-r--r--test/compress/const.js33
3 files changed, 41 insertions, 5 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 2307abc7..e36fcaa7 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -6651,7 +6651,7 @@ merge(Compressor.prototype, {
var s = def.scope;
do {
s = s.parent_scope;
- if (s.variables.has(node.name)) return false;
+ if (s.var_names()[node.name]) return false;
} while (s !== scope);
return true;
}) ? make_node(AST_Var, self, {
diff --git a/lib/scope.js b/lib/scope.js
index abb50c4f..ef19a530 100644
--- a/lib/scope.js
+++ b/lib/scope.js
@@ -82,10 +82,13 @@ SymbolDef.prototype = {
redefined: function() {
var scope = this.defun;
if (!scope) return;
- var def = scope.variables.get(this.name);
- if (!def && scope instanceof AST_Toplevel) def = scope.globals.get(this.name);
- if (!def || def === this) return;
- return def.redefined() || def;
+ var name = this.name;
+ var def = scope.variables.get(name)
+ || scope instanceof AST_Toplevel && scope.globals.get(name)
+ || this.orig[0] instanceof AST_SymbolConst && find_if(function(def) {
+ return def.name == name;
+ }, scope.enclosed);
+ if (def && def !== this) return def.redefined() || def;
},
unmangleable: function(options) {
return this.global && !options.toplevel
diff --git a/test/compress/const.js b/test/compress/const.js
index aedfdbc3..b161e876 100644
--- a/test/compress/const.js
+++ b/test/compress/const.js
@@ -930,3 +930,36 @@ issue_4207: {
}
expect_stdout: "0"
}
+
+issue_4210: {
+ options = {
+ reduce_vars: true,
+ }
+ input: {
+ (function() {
+ try {
+ throw 42;
+ } catch (e) {
+ const a = typeof e;
+ console.log(a);
+ } finally {
+ return a = "foo";
+ }
+ })();
+ console.log(typeof a);
+ }
+ expect: {
+ (function() {
+ try {
+ throw 42;
+ } catch (e) {
+ const a = typeof e;
+ console.log(a);
+ } finally {
+ return a = "foo";
+ }
+ })();
+ console.log(typeof a);
+ }
+ expect_stdout: true
+}