scope.js 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS2
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function SymbolDef(scope, orig) {
  35. this.name = orig.name;
  36. this.orig = [ orig ];
  37. this.eliminated = 0;
  38. this.scope = scope;
  39. this.references = [];
  40. this.replaced = 0;
  41. this.global = false;
  42. this.export = false;
  43. this.mangled_name = null;
  44. this.undeclared = false;
  45. this.id = SymbolDef.next_id++;
  46. };
  47. SymbolDef.next_id = 1;
  48. SymbolDef.prototype = {
  49. unmangleable: function(options) {
  50. if (!options) options = {};
  51. return (this.global && !options.toplevel)
  52. || this.export
  53. || this.undeclared
  54. || (!options.eval && (this.scope.uses_eval || this.scope.uses_with))
  55. || (options.keep_fnames
  56. && (this.orig[0] instanceof AST_SymbolLambda
  57. || this.orig[0] instanceof AST_SymbolDefun))
  58. || this.orig[0] instanceof AST_SymbolMethod
  59. || (options.keep_classnames
  60. && (this.orig[0] instanceof AST_SymbolClass
  61. || this.orig[0] instanceof AST_SymbolDefClass));
  62. },
  63. mangle: function(options) {
  64. var cache = options.cache && options.cache.props;
  65. if (this.global && cache && cache.has(this.name)) {
  66. this.mangled_name = cache.get(this.name);
  67. }
  68. else if (!this.mangled_name && !this.unmangleable(options)) {
  69. var s = this.scope;
  70. var sym = this.orig[0];
  71. if (options.ie8 && sym instanceof AST_SymbolLambda)
  72. s = s.parent_scope;
  73. var def;
  74. if (def = this.redefined()) {
  75. this.mangled_name = def.mangled_name || def.name;
  76. } else
  77. this.mangled_name = s.next_mangled(options, this);
  78. if (this.global && cache) {
  79. cache.set(this.name, this.mangled_name);
  80. }
  81. }
  82. },
  83. redefined: function() {
  84. return this.defun && this.defun.variables.get(this.name);
  85. }
  86. };
  87. AST_Toplevel.DEFMETHOD("figure_out_scope", function(options){
  88. options = defaults(options, {
  89. cache: null,
  90. ie8: false,
  91. safari10: false,
  92. });
  93. // pass 1: setup scope chaining and handle definitions
  94. var self = this;
  95. var scope = self.parent_scope = null;
  96. var labels = new Dictionary();
  97. var defun = null;
  98. var in_destructuring = null;
  99. var for_scopes = [];
  100. var tw = new TreeWalker(function(node, descend){
  101. if (node.is_block_scope()) {
  102. var save_scope = scope;
  103. node.block_scope = scope = new AST_Scope(node);
  104. scope.init_scope_vars(save_scope);
  105. if (!(node instanceof AST_Scope)) {
  106. scope.uses_with = save_scope.uses_with;
  107. scope.uses_eval = save_scope.uses_eval;
  108. scope.directives = save_scope.directives;
  109. }
  110. if (options.safari10) {
  111. if (node instanceof AST_For || node instanceof AST_ForIn) {
  112. for_scopes.push(scope);
  113. }
  114. }
  115. descend();
  116. scope = save_scope;
  117. return true;
  118. }
  119. if (node instanceof AST_Destructuring) {
  120. in_destructuring = node; // These don't nest
  121. descend();
  122. in_destructuring = null;
  123. return true;
  124. }
  125. if (node instanceof AST_Scope) {
  126. node.init_scope_vars(scope);
  127. var save_scope = scope;
  128. var save_defun = defun;
  129. var save_labels = labels;
  130. defun = scope = node;
  131. labels = new Dictionary();
  132. descend();
  133. scope = save_scope;
  134. defun = save_defun;
  135. labels = save_labels;
  136. return true; // don't descend again in TreeWalker
  137. }
  138. if (node instanceof AST_LabeledStatement) {
  139. var l = node.label;
  140. if (labels.has(l.name)) {
  141. throw new Error(string_template("Label {name} defined twice", l));
  142. }
  143. labels.set(l.name, l);
  144. descend();
  145. labels.del(l.name);
  146. return true; // no descend again
  147. }
  148. if (node instanceof AST_With) {
  149. for (var s = scope; s; s = s.parent_scope)
  150. s.uses_with = true;
  151. return;
  152. }
  153. if (node instanceof AST_Symbol) {
  154. node.scope = scope;
  155. }
  156. if (node instanceof AST_Label) {
  157. node.thedef = node;
  158. node.references = [];
  159. }
  160. if (node instanceof AST_SymbolLambda) {
  161. defun.def_function(node);
  162. }
  163. else if (node instanceof AST_SymbolDefun) {
  164. // Careful here, the scope where this should be defined is
  165. // the parent scope. The reason is that we enter a new
  166. // scope when we encounter the AST_Defun node (which is
  167. // instanceof AST_Scope) but we get to the symbol a bit
  168. // later.
  169. mark_export((node.scope = defun.parent_scope.get_defun_scope()).def_function(node), 1);
  170. }
  171. else if (node instanceof AST_SymbolClass) {
  172. mark_export(defun.def_variable(node), 1);
  173. }
  174. else if (node instanceof AST_SymbolImport) {
  175. scope.def_variable(node);
  176. }
  177. else if (node instanceof AST_SymbolDefClass) {
  178. // This deals with the name of the class being available
  179. // inside the class.
  180. mark_export((node.scope = defun.parent_scope).def_function(node), 1);
  181. }
  182. else if (node instanceof AST_SymbolVar
  183. || node instanceof AST_SymbolLet
  184. || node instanceof AST_SymbolConst) {
  185. var def = ((node instanceof AST_SymbolBlockDeclaration) ? scope : defun).def_variable(node);
  186. if (!all(def.orig, function(sym) {
  187. if (sym === node) return true;
  188. if (node instanceof AST_SymbolBlockDeclaration) {
  189. return sym instanceof AST_SymbolLambda;
  190. }
  191. return !(sym instanceof AST_SymbolLet || sym instanceof AST_SymbolConst);
  192. })) {
  193. js_error(
  194. node.name + " redeclared",
  195. node.start.file,
  196. node.start.line,
  197. node.start.col,
  198. node.start.pos
  199. );
  200. }
  201. if (!(node instanceof AST_SymbolFunarg)) mark_export(def, 2);
  202. def.destructuring = in_destructuring;
  203. if (defun !== scope) {
  204. node.mark_enclosed(options);
  205. var def = scope.find_variable(node);
  206. if (node.thedef !== def) {
  207. node.thedef = def;
  208. node.reference(options);
  209. }
  210. }
  211. }
  212. else if (node instanceof AST_SymbolCatch) {
  213. scope.def_variable(node).defun = defun;
  214. }
  215. else if (node instanceof AST_LabelRef) {
  216. var sym = labels.get(node.name);
  217. if (!sym) throw new Error(string_template("Undefined label {name} [{line},{col}]", {
  218. name: node.name,
  219. line: node.start.line,
  220. col: node.start.col
  221. }));
  222. node.thedef = sym;
  223. }
  224. if (!(scope instanceof AST_Toplevel) && (node instanceof AST_Export || node instanceof AST_Import)) {
  225. js_error(
  226. node.TYPE + " statement may only appear at top level",
  227. node.start.file,
  228. node.start.line,
  229. node.start.col,
  230. node.start.pos
  231. );
  232. }
  233. function mark_export(def, level) {
  234. if (in_destructuring) {
  235. var i = 0;
  236. do {
  237. level++;
  238. } while (tw.parent(i++) !== in_destructuring);
  239. }
  240. var node = tw.parent(level);
  241. def.export = node instanceof AST_Export;
  242. }
  243. });
  244. self.walk(tw);
  245. // pass 2: find back references and eval
  246. self.globals = new Dictionary();
  247. var tw = new TreeWalker(function(node, descend){
  248. if (node instanceof AST_LoopControl && node.label) {
  249. node.label.thedef.references.push(node);
  250. return true;
  251. }
  252. if (node instanceof AST_SymbolRef) {
  253. var name = node.name;
  254. if (name == "eval" && tw.parent() instanceof AST_Call) {
  255. for (var s = node.scope; s && !s.uses_eval; s = s.parent_scope) {
  256. s.uses_eval = true;
  257. }
  258. }
  259. var sym;
  260. if (tw.parent() instanceof AST_NameMapping && tw.parent(1).module_name
  261. || !(sym = node.scope.find_variable(name))) {
  262. sym = self.def_global(node);
  263. } else if (sym.scope instanceof AST_Lambda && name == "arguments") {
  264. sym.scope.uses_arguments = true;
  265. }
  266. node.thedef = sym;
  267. node.reference(options);
  268. return true;
  269. }
  270. // ensure mangling works if catch reuses a scope variable
  271. var def;
  272. if (node instanceof AST_SymbolCatch && (def = node.definition().redefined())) {
  273. var s = node.scope;
  274. while (s) {
  275. push_uniq(s.enclosed, def);
  276. if (s === def.scope) break;
  277. s = s.parent_scope;
  278. }
  279. }
  280. });
  281. self.walk(tw);
  282. // pass 3: fix up any scoping issue with IE8
  283. if (options.ie8) {
  284. self.walk(new TreeWalker(function(node, descend) {
  285. if (node instanceof AST_SymbolCatch) {
  286. var name = node.name;
  287. var refs = node.thedef.references;
  288. var scope = node.thedef.defun;
  289. var def = scope.find_variable(name) || self.globals.get(name) || scope.def_variable(node);
  290. refs.forEach(function(ref) {
  291. ref.thedef = def;
  292. ref.reference(options);
  293. });
  294. node.thedef = def;
  295. node.reference(options);
  296. return true;
  297. }
  298. }));
  299. }
  300. // pass 4: add symbol definitions to loop scopes
  301. // Safari/Webkit bug workaround - loop init let variable shadowing argument.
  302. // https://github.com/mishoo/UglifyJS2/issues/1753
  303. // https://bugs.webkit.org/show_bug.cgi?id=171041
  304. if (options.safari10) {
  305. for (var i = 0; i < for_scopes.length; i++) {
  306. var scope = for_scopes[i];
  307. scope.parent_scope.variables.each(function(def) {
  308. push_uniq(scope.enclosed, def);
  309. });
  310. }
  311. }
  312. if (options.cache) {
  313. this.cname = options.cache.cname;
  314. }
  315. });
  316. AST_Toplevel.DEFMETHOD("def_global", function(node){
  317. var globals = this.globals, name = node.name;
  318. if (globals.has(name)) {
  319. return globals.get(name);
  320. } else {
  321. var g = new SymbolDef(this, node);
  322. g.undeclared = true;
  323. g.global = true;
  324. globals.set(name, g);
  325. return g;
  326. }
  327. });
  328. AST_Scope.DEFMETHOD("init_scope_vars", function(parent_scope){
  329. this.variables = new Dictionary(); // map name to AST_SymbolVar (variables defined in this scope; includes functions)
  330. this.functions = new Dictionary(); // map name to AST_SymbolDefun (functions defined in this scope)
  331. this.uses_with = false; // will be set to true if this or some nested scope uses the `with` statement
  332. this.uses_eval = false; // will be set to true if this or nested scope uses the global `eval`
  333. this.parent_scope = parent_scope; // the parent scope
  334. this.enclosed = []; // a list of variables from this or outer scope(s) that are referenced from this or inner scopes
  335. this.cname = -1; // the current index for mangling functions/variables
  336. });
  337. AST_Node.DEFMETHOD("is_block_scope", return_false);
  338. AST_Class.DEFMETHOD("is_block_scope", return_false);
  339. AST_Lambda.DEFMETHOD("is_block_scope", return_false);
  340. AST_Toplevel.DEFMETHOD("is_block_scope", return_false);
  341. AST_SwitchBranch.DEFMETHOD("is_block_scope", return_false);
  342. AST_Block.DEFMETHOD("is_block_scope", return_true);
  343. AST_IterationStatement.DEFMETHOD("is_block_scope", return_true);
  344. AST_Lambda.DEFMETHOD("init_scope_vars", function(){
  345. AST_Scope.prototype.init_scope_vars.apply(this, arguments);
  346. this.uses_arguments = false;
  347. this.def_variable(new AST_SymbolFunarg({
  348. name: "arguments",
  349. start: this.start,
  350. end: this.end
  351. }));
  352. });
  353. AST_Symbol.DEFMETHOD("mark_enclosed", function(options) {
  354. var def = this.definition();
  355. var s = this.scope;
  356. while (s) {
  357. push_uniq(s.enclosed, def);
  358. if (options.keep_fnames) {
  359. s.functions.each(function(d) {
  360. push_uniq(def.scope.enclosed, d);
  361. });
  362. }
  363. if (s === def.scope) break;
  364. s = s.parent_scope;
  365. }
  366. });
  367. AST_Symbol.DEFMETHOD("reference", function(options) {
  368. this.definition().references.push(this);
  369. this.mark_enclosed(options);
  370. });
  371. AST_Scope.DEFMETHOD("find_variable", function(name){
  372. if (name instanceof AST_Symbol) name = name.name;
  373. return this.variables.get(name)
  374. || (this.parent_scope && this.parent_scope.find_variable(name));
  375. });
  376. AST_Scope.DEFMETHOD("def_function", function(symbol){
  377. var def = this.def_variable(symbol);
  378. this.functions.set(symbol.name, def);
  379. return def;
  380. });
  381. AST_Scope.DEFMETHOD("def_variable", function(symbol){
  382. var def;
  383. if (!this.variables.has(symbol.name)) {
  384. def = new SymbolDef(this, symbol);
  385. this.variables.set(symbol.name, def);
  386. def.global = !this.parent_scope;
  387. } else {
  388. def = this.variables.get(symbol.name);
  389. def.orig.push(symbol);
  390. }
  391. return symbol.thedef = def;
  392. });
  393. AST_Scope.DEFMETHOD("next_mangled", function(options){
  394. var ext = this.enclosed;
  395. out: while (true) {
  396. var m = base54(++this.cname);
  397. if (!is_identifier(m)) continue; // skip over "do"
  398. // https://github.com/mishoo/UglifyJS2/issues/242 -- do not
  399. // shadow a name reserved from mangling.
  400. if (member(m, options.reserved)) continue;
  401. // we must ensure that the mangled name does not shadow a name
  402. // from some parent scope that is referenced in this or in
  403. // inner scopes.
  404. for (var i = ext.length; --i >= 0;) {
  405. var sym = ext[i];
  406. var name = sym.mangled_name || (sym.unmangleable(options) && sym.name);
  407. if (m == name) continue out;
  408. }
  409. return m;
  410. }
  411. });
  412. AST_Function.DEFMETHOD("next_mangled", function(options, def){
  413. // #179, #326
  414. // in Safari strict mode, something like (function x(x){...}) is a syntax error;
  415. // a function expression's argument cannot shadow the function expression's name
  416. var tricky_def = def.orig[0] instanceof AST_SymbolFunarg && this.name && this.name.definition();
  417. // the function's mangled_name is null when keep_fnames is true
  418. var tricky_name = tricky_def ? tricky_def.mangled_name || tricky_def.name : null;
  419. while (true) {
  420. var name = AST_Lambda.prototype.next_mangled.call(this, options, def);
  421. if (!tricky_name || tricky_name != name)
  422. return name;
  423. }
  424. });
  425. AST_Symbol.DEFMETHOD("unmangleable", function(options){
  426. var def = this.definition();
  427. return !def || def.unmangleable(options);
  428. });
  429. // labels are always mangleable
  430. AST_Label.DEFMETHOD("unmangleable", return_false);
  431. AST_Symbol.DEFMETHOD("unreferenced", function(){
  432. return this.definition().references.length == 0
  433. && !(this.scope.uses_eval || this.scope.uses_with);
  434. });
  435. AST_Symbol.DEFMETHOD("definition", function(){
  436. return this.thedef;
  437. });
  438. AST_Symbol.DEFMETHOD("global", function(){
  439. return this.definition().global;
  440. });
  441. AST_Toplevel.DEFMETHOD("_default_mangler_options", function(options) {
  442. options = defaults(options, {
  443. eval : false,
  444. ie8 : false,
  445. keep_classnames: false,
  446. keep_fnames : false,
  447. reserved : [],
  448. toplevel : false,
  449. });
  450. if (!Array.isArray(options.reserved)) options.reserved = [];
  451. // Never mangle arguments
  452. push_uniq(options.reserved, "arguments");
  453. return options;
  454. });
  455. AST_Toplevel.DEFMETHOD("mangle_names", function(options){
  456. options = this._default_mangler_options(options);
  457. // We only need to mangle declaration nodes. Special logic wired
  458. // into the code generator will display the mangled name if it's
  459. // present (and for AST_SymbolRef-s it'll use the mangled name of
  460. // the AST_SymbolDeclaration that it points to).
  461. var lname = -1;
  462. var to_mangle = [];
  463. if (options.cache) {
  464. this.globals.each(collect);
  465. }
  466. var tw = new TreeWalker(function(node, descend){
  467. if (node instanceof AST_LabeledStatement) {
  468. // lname is incremented when we get to the AST_Label
  469. var save_nesting = lname;
  470. descend();
  471. lname = save_nesting;
  472. return true; // don't descend again in TreeWalker
  473. }
  474. if (node instanceof AST_Scope) {
  475. node.variables.each(collect);
  476. return;
  477. }
  478. if (node instanceof AST_Label) {
  479. var name;
  480. do name = base54(++lname); while (!is_identifier(name));
  481. node.mangled_name = name;
  482. return true;
  483. }
  484. var mangle_with_block_scope =
  485. (!options.ie8 && node instanceof AST_SymbolCatch) ||
  486. node instanceof AST_SymbolBlockDeclaration;
  487. if (mangle_with_block_scope && options.reserved.indexOf(node.name) < 0) {
  488. to_mangle.push(node.definition());
  489. return;
  490. }
  491. });
  492. this.walk(tw);
  493. to_mangle.forEach(function(def){
  494. def.mangle(options);
  495. });
  496. if (options.cache) {
  497. options.cache.cname = this.cname;
  498. }
  499. function collect(symbol) {
  500. if (!member(symbol.name, options.reserved)) {
  501. to_mangle.push(symbol);
  502. }
  503. }
  504. });
  505. AST_Toplevel.DEFMETHOD("find_unique_prefix", function(options) {
  506. var letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_";
  507. var cache = options.cache && options.cache.props;
  508. var prefixes = Object.create(null);
  509. options.reserved.forEach(add_prefix);
  510. this.globals.each(add_def);
  511. this.walk(new TreeWalker(function(node) {
  512. if (node instanceof AST_Scope) node.variables.each(add_def);
  513. if (node instanceof AST_SymbolCatch) add_def(node.definition());
  514. }));
  515. var prefix, i = 0;
  516. do {
  517. prefix = create_name(i++);
  518. } while (prefixes[prefix]);
  519. return prefix;
  520. function add_prefix(name) {
  521. if (/[0-9]$/.test(name)) {
  522. prefixes[name.replace(/[0-9]+$/, "")] = true;
  523. }
  524. }
  525. function add_def(def) {
  526. var name = def.name;
  527. if (def.global && cache && cache.has(name)) name = cache.get(name);
  528. else if (!def.unmangleable(options)) return;
  529. add_prefix(name);
  530. }
  531. function create_name(num) {
  532. var name = "";
  533. do {
  534. name += letters[num % letters.length];
  535. num = Math.floor(num / letters.length);
  536. } while (num);
  537. return name;
  538. }
  539. });
  540. AST_Toplevel.DEFMETHOD("expand_names", function(options) {
  541. options = this._default_mangler_options(options);
  542. var prefix = this.find_unique_prefix(options);
  543. this.globals.each(rename);
  544. this.walk(new TreeWalker(function(node) {
  545. if (node instanceof AST_Scope) node.variables.each(rename);
  546. if (node instanceof AST_SymbolCatch) rename(node.definition());
  547. }));
  548. function rename(def) {
  549. if (def.global || def.unmangleable(options)) return;
  550. if (member(def.name, options.reserved)) return;
  551. var d = def.redefined();
  552. def.name = d ? d.name : prefix + def.id;
  553. def.orig.forEach(function(sym) {
  554. sym.name = def.name;
  555. });
  556. def.references.forEach(function(sym) {
  557. sym.name = def.name;
  558. });
  559. }
  560. });
  561. AST_Node.DEFMETHOD("tail_node", return_this);
  562. AST_Sequence.DEFMETHOD("tail_node", function() {
  563. return this.expressions[this.expressions.length - 1];
  564. });
  565. AST_Toplevel.DEFMETHOD("compute_char_frequency", function(options){
  566. options = this._default_mangler_options(options);
  567. try {
  568. AST_Node.prototype.print = function(stream, force_parens) {
  569. this._print(stream, force_parens);
  570. if (this instanceof AST_Symbol && !this.unmangleable(options)) {
  571. base54.consider(this.name, -1);
  572. } else if (options.properties) {
  573. if (this instanceof AST_Dot) {
  574. base54.consider(this.property, -1);
  575. } else if (this instanceof AST_Sub) {
  576. skip_string(this.property);
  577. }
  578. }
  579. };
  580. base54.consider(this.print_to_string(), 1);
  581. } finally {
  582. AST_Node.prototype.print = AST_Node.prototype._print;
  583. }
  584. base54.sort();
  585. function skip_string(node) {
  586. if (node instanceof AST_String) {
  587. base54.consider(node.value, -1);
  588. } else if (node instanceof AST_Conditional) {
  589. skip_string(node.consequent);
  590. skip_string(node.alternative);
  591. } else if (node instanceof AST_Sequence) {
  592. skip_string(node.tail_node());
  593. }
  594. }
  595. });
  596. var base54 = (function() {
  597. var leading = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$_".split("");
  598. var digits = "0123456789".split("");
  599. var chars, frequency;
  600. function reset() {
  601. frequency = Object.create(null);
  602. leading.forEach(function(ch) {
  603. frequency[ch] = 0;
  604. });
  605. digits.forEach(function(ch) {
  606. frequency[ch] = 0;
  607. });
  608. }
  609. base54.consider = function(str, delta) {
  610. for (var i = str.length; --i >= 0;) {
  611. frequency[str[i]] += delta;
  612. }
  613. };
  614. function compare(a, b) {
  615. return frequency[b] - frequency[a];
  616. }
  617. base54.sort = function() {
  618. chars = mergeSort(leading, compare).concat(mergeSort(digits, compare));
  619. };
  620. base54.reset = reset;
  621. reset();
  622. function base54(num) {
  623. var ret = "", base = 54;
  624. num++;
  625. do {
  626. num--;
  627. ret += chars[num % base];
  628. num = Math.floor(num / base);
  629. base = 64;
  630. } while (num > 0);
  631. return ret;
  632. };
  633. return base54;
  634. })();