diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-11-21 00:05:40 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-21 08:05:40 +0800 |
commit | 8d30902ba9e8a08945c8ae7cac0cb2feb27bb93c (patch) | |
tree | 9ba07d329d8996dac374700afe7195efd7946827 /lib/scope.js | |
parent | 02459cddf98428427dd1b089cbb469f465cd5b3b (diff) | |
download | tracifyjs-8d30902ba9e8a08945c8ae7cac0cb2feb27bb93c.tar.gz tracifyjs-8d30902ba9e8a08945c8ae7cac0cb2feb27bb93c.zip |
fix corner case in `mangle` (#4311)
Diffstat (limited to 'lib/scope.js')
-rw-r--r-- | lib/scope.js | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/lib/scope.js b/lib/scope.js index 0e44280f..4cc65c75 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -72,7 +72,7 @@ SymbolDef.prototype = { if (def) { this.mangled_name = def.mangled_name || def.name; } else { - this.mangled_name = next_mangled_name(this.scope, options, this); + this.mangled_name = next_mangled_name(this, options); } if (this.global && cache) { cache.set(this.name, this.mangled_name); @@ -432,7 +432,8 @@ function names_in_use(scope, options) { return names; } -function next_mangled_name(scope, options, def) { +function next_mangled_name(def, options) { + var scope = def.scope; var in_use = names_in_use(scope, options); var holes = scope.cname_holes; var names = Object.create(null); @@ -519,9 +520,9 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { return true; } if (node instanceof AST_BlockScope) { - var to_mangle = []; + node.to_mangle = []; node.variables.each(function(def) { - if (!defer_redef(def)) to_mangle.push(def); + if (!defer_redef(def)) node.to_mangle.push(def); }); descend(); if (options.cache && node instanceof AST_Toplevel) { @@ -532,7 +533,7 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { sym.scope = node; sym.reference(options); } - to_mangle.forEach(mangle); + node.to_mangle.forEach(mangle); return true; } if (node instanceof AST_Label) { @@ -552,13 +553,19 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options) { def.mangle(options); } - function defer_redef(def, node) { + function defer_redef(def) { + var sym = def.orig[0]; var redef = def.redefined(); - if (!redef) return false; + if (!redef) { + if (!(sym instanceof AST_SymbolConst)) return false; + var scope = def.scope.resolve(); + if (def.scope === scope) return false; + redef = scope.def_variable(sym); + scope.to_mangle.push(redef); + } redefined.push(def); def.references.forEach(reference); - var node = def.orig[0]; - if (node instanceof AST_SymbolCatch || node instanceof AST_SymbolConst) reference(node); + if (sym instanceof AST_SymbolCatch || sym instanceof AST_SymbolConst) reference(sym); return true; function reference(sym) { |