From bf832cde167b6de119761d54302977e14b10c00b Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Tue, 9 Jan 2018 17:55:41 +0800 Subject: [PATCH] improve synergy between `compress` and `rename` (#2755) --- lib/minify.js | 1 - lib/scope.js | 48 ++++++++++++++++++++------------------------ test/mocha/minify.js | 2 +- 3 files changed, 23 insertions(+), 28 deletions(-) diff --git a/lib/minify.js b/lib/minify.js index aad7d226..a68cbf3a 100644 --- a/lib/minify.js +++ b/lib/minify.js @@ -141,7 +141,6 @@ function minify(files, options) { } if (timings) timings.rename = Date.now(); if (options.rename) { - SymbolDef.next_id = 1; toplevel.figure_out_scope(options.mangle); toplevel.expand_names(options.mangle); } 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; }); diff --git a/test/mocha/minify.js b/test/mocha/minify.js index d696a25f..65392ee4 100644 --- a/test/mocha/minify.js +++ b/test/mocha/minify.js @@ -383,7 +383,7 @@ describe("minify", function() { toplevel: true, }, rename: true, - }).code, "var a2;(a2=y)(a2);"); + }).code, "var a;(a=y)(a);"); } }); }); -- 2.34.1