diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-01-09 17:55:41 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-09 17:55:41 +0800 |
commit | bf832cde167b6de119761d54302977e14b10c00b (patch) | |
tree | d4afa36570c29d34e13a73d27d66027aa8bc5a8b /lib/scope.js | |
parent | 2972d58dbb7cdb918267b278368550bf36c3cb08 (diff) | |
download | tracifyjs-bf832cde167b6de119761d54302977e14b10c00b.tar.gz tracifyjs-bf832cde167b6de119761d54302977e14b10c00b.zip |
improve synergy between `compress` and `rename` (#2755)
Diffstat (limited to 'lib/scope.js')
-rw-r--r-- | lib/scope.js | 48 |
1 files changed, 22 insertions, 26 deletions
diff --git a/lib/scope.js b/lib/scope.js index a18e92a3..af852bb1 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -464,59 +464,55 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){ } }); -AST_Toplevel.DEFMETHOD("find_unique_prefix", function(options) { - var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_"; +AST_Toplevel.DEFMETHOD("find_colliding_names", function(options) { var cache = options.cache && options.cache.props; - var prefixes = Object.create(null); - options.reserved.forEach(add_prefix); + var avoid = Object.create(null); + options.reserved.forEach(to_avoid); this.globals.each(add_def); this.walk(new TreeWalker(function(node) { if (node instanceof AST_Scope) node.variables.each(add_def); if (node instanceof AST_SymbolCatch) add_def(node.definition()); })); - var prefix, i = 0; - do { - prefix = create_name(i++); - } while (prefixes[prefix]); - return prefix; + return avoid; - function add_prefix(name) { - if (/[0-9]$/.test(name)) { - prefixes[name.replace(/[0-9]+$/, "")] = true; - } + function to_avoid(name) { + avoid[name] = true; } function add_def(def) { var name = def.name; if (def.global && cache && cache.has(name)) name = cache.get(name); else if (!def.unmangleable(options)) return; - add_prefix(name); - } - - function create_name(num) { - var name = ""; - do { - name += letters[num % letters.length]; - num = Math.floor(num / letters.length); - } while (num); - return name; + to_avoid(name); } }); AST_Toplevel.DEFMETHOD("expand_names", function(options) { + base54.reset(); + base54.sort(); options = this._default_mangler_options(options); - var prefix = this.find_unique_prefix(options); + var avoid = this.find_colliding_names(options); + var cname = 0; this.globals.each(rename); this.walk(new TreeWalker(function(node) { if (node instanceof AST_Scope) node.variables.each(rename); if (node instanceof AST_SymbolCatch) rename(node.definition()); })); + function next_name() { + var name; + do { + name = base54(cname++); + } while (avoid[name] || !is_identifier(name)); + return name; + } + function rename(def) { - if (def.global || def.unmangleable(options)) return; + if (def.global && options.cache) return; + if (def.unmangleable(options)) return; if (member(def.name, options.reserved)) return; var d = def.redefined(); - def.name = d ? d.name : prefix + def.id; + def.name = d ? d.name : next_name(); def.orig.forEach(function(sym) { sym.name = def.name; }); |