From afbcebddf63c7ffa5b0df9b3712ee3b560918f1e Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 5 Jan 2018 05:08:09 +0800 Subject: fix `mangle` name collision across files (#2722) --- lib/scope.js | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'lib/scope.js') diff --git a/lib/scope.js b/lib/scope.js index de92fc94..801c888c 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -242,10 +242,6 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){ } })); } - - if (options.cache) { - this.cname = options.cache.cname; - } }); AST_Toplevel.DEFMETHOD("def_global", function(node){ @@ -329,10 +325,10 @@ AST_Scope.DEFMETHOD("def_variable", function(symbol, init){ return symbol.thedef = def; }); -AST_Scope.DEFMETHOD("next_mangled", function(options){ - var ext = this.enclosed; +function next_mangled(scope, options) { + var ext = scope.enclosed; out: while (true) { - var m = base54(++this.cname); + var m = base54(++scope.cname); if (!is_identifier(m)) continue; // skip over "do" // https://github.com/mishoo/UglifyJS2/issues/242 -- do not @@ -349,6 +345,18 @@ AST_Scope.DEFMETHOD("next_mangled", function(options){ } return m; } +} + +AST_Scope.DEFMETHOD("next_mangled", function(options){ + return next_mangled(this, options); +}); + +AST_Toplevel.DEFMETHOD("next_mangled", function(options){ + var name; + do { + name = next_mangled(this, options); + } while (member(name, this.mangled_names)); + return name; }); AST_Function.DEFMETHOD("next_mangled", function(options, def){ @@ -362,7 +370,7 @@ AST_Function.DEFMETHOD("next_mangled", function(options, def){ var tricky_name = tricky_def ? tricky_def.mangled_name || tricky_def.name : null; while (true) { - var name = AST_Lambda.prototype.next_mangled.call(this, options, def); + var name = next_mangled(this, options); if (!tricky_name || tricky_name != name) return name; } @@ -413,8 +421,14 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ var lname = -1; var to_mangle = []; + var mangled_names = this.mangled_names = []; if (options.cache) { this.globals.each(collect); + if (options.cache.props) { + options.cache.props.each(function(mangled_name) { + push_uniq(mangled_names, mangled_name); + }); + } } var tw = new TreeWalker(function(node, descend){ @@ -443,10 +457,6 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ this.walk(tw); to_mangle.forEach(function(def){ def.mangle(options) }); - if (options.cache) { - options.cache.cname = this.cname; - } - function collect(symbol) { if (!member(symbol.name, options.reserved)) { to_mangle.push(symbol); -- cgit v1.2.3