Implement `-m sort=true`
authorMihai Bazon <mihai@bazon.net>
Wed, 2 Jan 2013 10:39:00 +0000 (12:39 +0200)
committerMihai Bazon <mihai@bazon.net>
Wed, 2 Jan 2013 10:39:00 +0000 (12:39 +0200)
close #83

README.md
lib/scope.js

index de343c7..4840c74 100644 (file)
--- a/README.md
+++ b/README.md
@@ -130,7 +130,7 @@ input files from the command line.
 ## Mangler options
 
 To enable the mangler you need to pass `--mangle` (`-m`).  Optionally you
-can pass `-m sort` (we'll possibly have other flags in the future) in order
+can pass `-m sort=true` (we'll possibly have other flags in the future) in order
 to assign shorter names to most frequently used variables.  This saves a few
 hundred bytes on jQuery before gzip, but the output is _bigger_ after gzip
 (and seems to happen for other libraries I tried it on) therefore it's not
index 758b61f..34ca5fb 100644 (file)
@@ -340,6 +340,7 @@ AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options){
     return defaults(options, {
         except : [],
         eval   : false,
+        sort   : false
     });
 });
 
@@ -360,12 +361,16 @@ AST_Toplevel.DEFMETHOD("mangle_names", function(options){
             return true;        // don't descend again in TreeWalker
         }
         if (node instanceof AST_Scope) {
-            var p = tw.parent();
+            var p = tw.parent(), a = [];
             node.variables.each(function(symbol){
                 if (options.except.indexOf(symbol.name) < 0) {
-                    to_mangle.push(symbol);
+                    a.push(symbol);
                 }
             });
+            if (options.sort) a.sort(function(a, b){
+                return b.references.length - a.references.length;
+            });
+            to_mangle.push.apply(to_mangle, a);
             return;
         }
         if (node instanceof AST_Label) {