scope.js 25 KB

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