output.js 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  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. var EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
  35. function is_some_comments(comment) {
  36. // multiline comment
  37. return comment.type == "comment2" && /@preserve|@license|@cc_on/i.test(comment.value);
  38. }
  39. function OutputStream(options) {
  40. var readonly = !options;
  41. options = defaults(options, {
  42. ascii_only : false,
  43. beautify : false,
  44. braces : false,
  45. comments : false,
  46. ie8 : false,
  47. indent_level : 4,
  48. indent_start : 0,
  49. inline_script : true,
  50. keep_quoted_props: false,
  51. max_line_len : false,
  52. preamble : null,
  53. preserve_line : false,
  54. quote_keys : false,
  55. quote_style : 0,
  56. semicolons : true,
  57. shebang : true,
  58. source_map : null,
  59. webkit : false,
  60. width : 80,
  61. wrap_iife : false,
  62. }, true);
  63. // Convert comment option to RegExp if neccessary and set up comments filter
  64. var comment_filter = return_false; // Default case, throw all comments away
  65. if (options.comments) {
  66. var comments = options.comments;
  67. if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
  68. var regex_pos = options.comments.lastIndexOf("/");
  69. comments = new RegExp(
  70. options.comments.substr(1, regex_pos - 1),
  71. options.comments.substr(regex_pos + 1)
  72. );
  73. }
  74. if (comments instanceof RegExp) {
  75. comment_filter = function(comment) {
  76. return comment.type != "comment5" && comments.test(comment.value);
  77. };
  78. }
  79. else if (typeof comments === "function") {
  80. comment_filter = function(comment) {
  81. return comment.type != "comment5" && comments(this, comment);
  82. };
  83. }
  84. else if (comments === "some") {
  85. comment_filter = is_some_comments;
  86. } else { // NOTE includes "all" option
  87. comment_filter = return_true;
  88. }
  89. }
  90. var indentation = 0;
  91. var current_col = 0;
  92. var current_line = 1;
  93. var current_pos = 0;
  94. var OUTPUT = "";
  95. var to_utf8 = options.ascii_only ? function(str, identifier) {
  96. return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
  97. var code = ch.charCodeAt(0).toString(16);
  98. if (code.length <= 2 && !identifier) {
  99. while (code.length < 2) code = "0" + code;
  100. return "\\x" + code;
  101. } else {
  102. while (code.length < 4) code = "0" + code;
  103. return "\\u" + code;
  104. }
  105. });
  106. } : function(str) {
  107. var s = "";
  108. for (var i = 0, len = str.length; i < len; i++) {
  109. if (is_surrogate_pair_head(str[i]) && !is_surrogate_pair_tail(str[i + 1])
  110. || is_surrogate_pair_tail(str[i]) && !is_surrogate_pair_head(str[i - 1])) {
  111. s += "\\u" + str.charCodeAt(i).toString(16);
  112. } else {
  113. s += str[i];
  114. }
  115. }
  116. return s;
  117. };
  118. function make_string(str, quote) {
  119. var dq = 0, sq = 0;
  120. str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
  121. function(s, i){
  122. switch (s) {
  123. case '"': ++dq; return '"';
  124. case "'": ++sq; return "'";
  125. case "\\": return "\\\\";
  126. case "\n": return "\\n";
  127. case "\r": return "\\r";
  128. case "\t": return "\\t";
  129. case "\b": return "\\b";
  130. case "\f": return "\\f";
  131. case "\x0B": return options.ie8 ? "\\x0B" : "\\v";
  132. case "\u2028": return "\\u2028";
  133. case "\u2029": return "\\u2029";
  134. case "\ufeff": return "\\ufeff";
  135. case "\0":
  136. return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
  137. }
  138. return s;
  139. });
  140. function quote_single() {
  141. return "'" + str.replace(/\x27/g, "\\'") + "'";
  142. }
  143. function quote_double() {
  144. return '"' + str.replace(/\x22/g, '\\"') + '"';
  145. }
  146. str = to_utf8(str);
  147. switch (options.quote_style) {
  148. case 1:
  149. return quote_single();
  150. case 2:
  151. return quote_double();
  152. case 3:
  153. return quote == "'" ? quote_single() : quote_double();
  154. default:
  155. return dq > sq ? quote_single() : quote_double();
  156. }
  157. }
  158. function encode_string(str, quote) {
  159. var ret = make_string(str, quote);
  160. if (options.inline_script) {
  161. ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2");
  162. ret = ret.replace(/\x3c!--/g, "\\x3c!--");
  163. ret = ret.replace(/--\x3e/g, "--\\x3e");
  164. }
  165. return ret;
  166. }
  167. function make_name(name) {
  168. name = name.toString();
  169. name = to_utf8(name, true);
  170. return name;
  171. }
  172. function make_indent(back) {
  173. return repeat_string(" ", options.indent_start + indentation - back * options.indent_level);
  174. }
  175. /* -----[ beautification/minification ]----- */
  176. var has_parens = false;
  177. var might_need_space = false;
  178. var might_need_semicolon = false;
  179. var might_add_newline = 0;
  180. var need_newline_indented = false;
  181. var need_space = false;
  182. var newline_insert = -1;
  183. var last = "";
  184. var mapping_token, mapping_name, mappings = options.source_map && [];
  185. var do_add_mapping = mappings ? function() {
  186. mappings.forEach(function(mapping) {
  187. try {
  188. options.source_map.add(
  189. mapping.token.file,
  190. mapping.line, mapping.col,
  191. mapping.token.line, mapping.token.col,
  192. !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name
  193. );
  194. } catch(ex) {
  195. AST_Node.warn("Couldn't figure out mapping for {file}:{line},{col} → {cline},{ccol} [{name}]", {
  196. file: mapping.token.file,
  197. line: mapping.token.line,
  198. col: mapping.token.col,
  199. cline: mapping.line,
  200. ccol: mapping.col,
  201. name: mapping.name || ""
  202. })
  203. }
  204. });
  205. mappings = [];
  206. } : noop;
  207. var ensure_line_len = options.max_line_len ? function() {
  208. if (current_col > options.max_line_len) {
  209. if (might_add_newline) {
  210. var left = OUTPUT.slice(0, might_add_newline);
  211. var right = OUTPUT.slice(might_add_newline);
  212. if (mappings) {
  213. var delta = right.length - current_col;
  214. mappings.forEach(function(mapping) {
  215. mapping.line++;
  216. mapping.col += delta;
  217. });
  218. }
  219. OUTPUT = left + "\n" + right;
  220. current_line++;
  221. current_pos++;
  222. current_col = right.length;
  223. }
  224. if (current_col > options.max_line_len) {
  225. AST_Node.warn("Output exceeds {max_line_len} characters", options);
  226. }
  227. }
  228. if (might_add_newline) {
  229. might_add_newline = 0;
  230. do_add_mapping();
  231. }
  232. } : noop;
  233. var requireSemicolonChars = makePredicate("( [ + * / - , .");
  234. function print(str) {
  235. str = String(str);
  236. var ch = str.charAt(0);
  237. if (need_newline_indented && ch) {
  238. need_newline_indented = false;
  239. if (ch != "\n") {
  240. print("\n");
  241. indent();
  242. }
  243. }
  244. if (need_space && ch) {
  245. need_space = false;
  246. if (!/[\s;})]/.test(ch)) {
  247. space();
  248. }
  249. }
  250. newline_insert = -1;
  251. var prev = last.charAt(last.length - 1);
  252. if (might_need_semicolon) {
  253. might_need_semicolon = false;
  254. if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
  255. if (options.semicolons || requireSemicolonChars[ch]) {
  256. OUTPUT += ";";
  257. current_col++;
  258. current_pos++;
  259. } else {
  260. ensure_line_len();
  261. OUTPUT += "\n";
  262. current_pos++;
  263. current_line++;
  264. current_col = 0;
  265. if (/^\s+$/.test(str)) {
  266. // reset the semicolon flag, since we didn't print one
  267. // now and might still have to later
  268. might_need_semicolon = true;
  269. }
  270. }
  271. if (!options.beautify)
  272. might_need_space = false;
  273. }
  274. }
  275. if (!options.beautify && options.preserve_line && stack[stack.length - 1]) {
  276. var target_line = stack[stack.length - 1].start.line;
  277. while (current_line < target_line) {
  278. ensure_line_len();
  279. OUTPUT += "\n";
  280. current_pos++;
  281. current_line++;
  282. current_col = 0;
  283. might_need_space = false;
  284. }
  285. }
  286. if (might_need_space) {
  287. if ((is_identifier_char(prev)
  288. && (is_identifier_char(ch) || ch == "\\"))
  289. || (ch == "/" && ch == prev)
  290. || ((ch == "+" || ch == "-") && ch == last))
  291. {
  292. OUTPUT += " ";
  293. current_col++;
  294. current_pos++;
  295. }
  296. might_need_space = false;
  297. }
  298. if (mapping_token) {
  299. mappings.push({
  300. token: mapping_token,
  301. name: mapping_name,
  302. line: current_line,
  303. col: current_col
  304. });
  305. mapping_token = false;
  306. if (!might_add_newline) do_add_mapping();
  307. }
  308. OUTPUT += str;
  309. has_parens = str[str.length - 1] == "(";
  310. current_pos += str.length;
  311. var a = str.split(/\r?\n/), n = a.length - 1;
  312. current_line += n;
  313. current_col += a[0].length;
  314. if (n > 0) {
  315. ensure_line_len();
  316. current_col = a[n].length;
  317. }
  318. last = str;
  319. }
  320. var space = options.beautify ? function() {
  321. print(" ");
  322. } : function() {
  323. might_need_space = true;
  324. };
  325. var indent = options.beautify ? function(half) {
  326. if (options.beautify) {
  327. print(make_indent(half ? 0.5 : 0));
  328. }
  329. } : noop;
  330. var with_indent = options.beautify ? function(col, cont) {
  331. if (col === true) col = next_indent();
  332. var save_indentation = indentation;
  333. indentation = col;
  334. var ret = cont();
  335. indentation = save_indentation;
  336. return ret;
  337. } : function(col, cont) { return cont() };
  338. var may_add_newline = options.max_line_len ? function() {
  339. ensure_line_len();
  340. might_add_newline = OUTPUT.length;
  341. } : noop;
  342. var newline = options.beautify ? function() {
  343. if (newline_insert < 0) return print("\n");
  344. if (OUTPUT[newline_insert] != "\n") {
  345. OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
  346. current_pos++;
  347. current_line++;
  348. }
  349. newline_insert++;
  350. } : may_add_newline;
  351. var semicolon = options.beautify ? function() {
  352. print(";");
  353. } : function() {
  354. might_need_semicolon = true;
  355. };
  356. function force_semicolon() {
  357. might_need_semicolon = false;
  358. print(";");
  359. }
  360. function next_indent() {
  361. return indentation + options.indent_level;
  362. }
  363. function with_block(cont) {
  364. var ret;
  365. print("{");
  366. newline();
  367. with_indent(next_indent(), function(){
  368. ret = cont();
  369. });
  370. indent();
  371. print("}");
  372. return ret;
  373. }
  374. function with_parens(cont) {
  375. print("(");
  376. may_add_newline();
  377. //XXX: still nice to have that for argument lists
  378. //var ret = with_indent(current_col, cont);
  379. var ret = cont();
  380. may_add_newline();
  381. print(")");
  382. return ret;
  383. }
  384. function with_square(cont) {
  385. print("[");
  386. may_add_newline();
  387. //var ret = with_indent(current_col, cont);
  388. var ret = cont();
  389. may_add_newline();
  390. print("]");
  391. return ret;
  392. }
  393. function comma() {
  394. may_add_newline();
  395. print(",");
  396. may_add_newline();
  397. space();
  398. }
  399. function colon() {
  400. print(":");
  401. space();
  402. }
  403. var add_mapping = mappings ? function(token, name) {
  404. mapping_token = token;
  405. mapping_name = name;
  406. } : noop;
  407. function get() {
  408. if (might_add_newline) {
  409. ensure_line_len();
  410. }
  411. return OUTPUT;
  412. }
  413. function has_nlb() {
  414. var index = OUTPUT.lastIndexOf("\n");
  415. return /^ *$/.test(OUTPUT.slice(index + 1));
  416. }
  417. function prepend_comments(node) {
  418. var self = this;
  419. var start = node.start;
  420. if (!start) return;
  421. if (start.comments_before && start.comments_before._dumped === self) return;
  422. var comments = start.comments_before;
  423. if (!comments) {
  424. comments = start.comments_before = [];
  425. }
  426. comments._dumped = self;
  427. if (node instanceof AST_Exit && node.value) {
  428. var tw = new TreeWalker(function(node) {
  429. var parent = tw.parent();
  430. if (parent instanceof AST_Exit
  431. || parent instanceof AST_Binary && parent.left === node
  432. || parent.TYPE == "Call" && parent.expression === node
  433. || parent instanceof AST_Conditional && parent.condition === node
  434. || parent instanceof AST_Dot && parent.expression === node
  435. || parent instanceof AST_Sequence && parent.expressions[0] === node
  436. || parent instanceof AST_Sub && parent.expression === node
  437. || parent instanceof AST_UnaryPostfix) {
  438. var text = node.start.comments_before;
  439. if (text && text._dumped !== self) {
  440. text._dumped = self;
  441. comments = comments.concat(text);
  442. }
  443. } else {
  444. return true;
  445. }
  446. });
  447. tw.push(node);
  448. node.value.walk(tw);
  449. }
  450. if (current_pos == 0) {
  451. if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
  452. print("#!" + comments.shift().value + "\n");
  453. indent();
  454. }
  455. var preamble = options.preamble;
  456. if (preamble) {
  457. print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
  458. }
  459. }
  460. comments = comments.filter(comment_filter, node);
  461. if (comments.length == 0) return;
  462. var last_nlb = has_nlb();
  463. comments.forEach(function(c, i) {
  464. if (!last_nlb) {
  465. if (c.nlb) {
  466. print("\n");
  467. indent();
  468. last_nlb = true;
  469. } else if (i > 0) {
  470. space();
  471. }
  472. }
  473. if (/comment[134]/.test(c.type)) {
  474. print("//" + c.value.replace(/[@#]__PURE__/g, ' ') + "\n");
  475. indent();
  476. last_nlb = true;
  477. } else if (c.type == "comment2") {
  478. print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
  479. last_nlb = false;
  480. }
  481. });
  482. if (!last_nlb) {
  483. if (start.nlb) {
  484. print("\n");
  485. indent();
  486. } else {
  487. space();
  488. }
  489. }
  490. }
  491. function append_comments(node, tail) {
  492. var self = this;
  493. var token = node.end;
  494. if (!token) return;
  495. var comments = token[tail ? "comments_before" : "comments_after"];
  496. if (!comments || comments._dumped === self) return;
  497. if (!(node instanceof AST_Statement || all(comments, function(c) {
  498. return !/comment[134]/.test(c.type);
  499. }))) return;
  500. comments._dumped = self;
  501. var insert = OUTPUT.length;
  502. comments.filter(comment_filter, node).forEach(function(c, i) {
  503. need_space = false;
  504. if (need_newline_indented) {
  505. print("\n");
  506. indent();
  507. need_newline_indented = false;
  508. } else if (c.nlb && (i > 0 || !has_nlb())) {
  509. print("\n");
  510. indent();
  511. } else if (i > 0 || !tail) {
  512. space();
  513. }
  514. if (/comment[134]/.test(c.type)) {
  515. print("//" + c.value.replace(/[@#]__PURE__/g, ' '));
  516. need_newline_indented = true;
  517. } else if (c.type == "comment2") {
  518. print("/*" + c.value.replace(/[@#]__PURE__/g, ' ') + "*/");
  519. need_space = true;
  520. }
  521. });
  522. if (OUTPUT.length > insert) newline_insert = insert;
  523. }
  524. var stack = [];
  525. return {
  526. get : get,
  527. toString : get,
  528. indent : indent,
  529. indentation : function() { return indentation },
  530. current_width : function() { return current_col - indentation },
  531. should_break : function() { return options.width && this.current_width() >= options.width },
  532. has_parens : function() { return has_parens },
  533. newline : newline,
  534. print : print,
  535. space : space,
  536. comma : comma,
  537. colon : colon,
  538. last : function() { return last },
  539. semicolon : semicolon,
  540. force_semicolon : force_semicolon,
  541. to_utf8 : to_utf8,
  542. print_name : function(name) { print(make_name(name)) },
  543. print_string : function(str, quote, escape_directive) {
  544. var encoded = encode_string(str, quote);
  545. if (escape_directive === true && encoded.indexOf("\\") === -1) {
  546. // Insert semicolons to break directive prologue
  547. if (!EXPECT_DIRECTIVE.test(OUTPUT)) {
  548. force_semicolon();
  549. }
  550. force_semicolon();
  551. }
  552. print(encoded);
  553. },
  554. encode_string : encode_string,
  555. next_indent : next_indent,
  556. with_indent : with_indent,
  557. with_block : with_block,
  558. with_parens : with_parens,
  559. with_square : with_square,
  560. add_mapping : add_mapping,
  561. option : function(opt) { return options[opt] },
  562. prepend_comments: readonly ? noop : prepend_comments,
  563. append_comments : readonly || comment_filter === return_false ? noop : append_comments,
  564. line : function() { return current_line },
  565. col : function() { return current_col },
  566. pos : function() { return current_pos },
  567. push_node : function(node) { stack.push(node) },
  568. pop_node : function() { return stack.pop() },
  569. parent : function(n) {
  570. return stack[stack.length - 2 - (n || 0)];
  571. }
  572. };
  573. }
  574. /* -----[ code generators ]----- */
  575. (function(){
  576. /* -----[ utils ]----- */
  577. function DEFPRINT(nodetype, generator) {
  578. nodetype.DEFMETHOD("_codegen", generator);
  579. }
  580. var in_directive = false;
  581. var active_scope = null;
  582. var use_asm = null;
  583. AST_Node.DEFMETHOD("print", function(stream, force_parens){
  584. var self = this, generator = self._codegen;
  585. if (self instanceof AST_Scope) {
  586. active_scope = self;
  587. }
  588. else if (!use_asm && self instanceof AST_Directive && self.value == "use asm") {
  589. use_asm = active_scope;
  590. }
  591. function doit() {
  592. stream.prepend_comments(self);
  593. self.add_source_map(stream);
  594. generator(self, stream);
  595. stream.append_comments(self);
  596. }
  597. stream.push_node(self);
  598. if (force_parens || self.needs_parens(stream)) {
  599. stream.with_parens(doit);
  600. } else {
  601. doit();
  602. }
  603. stream.pop_node();
  604. if (self === use_asm) {
  605. use_asm = null;
  606. }
  607. });
  608. AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
  609. AST_Node.DEFMETHOD("print_to_string", function(options){
  610. var s = OutputStream(options);
  611. this.print(s);
  612. return s.get();
  613. });
  614. /* -----[ PARENTHESES ]----- */
  615. function PARENS(nodetype, func) {
  616. if (Array.isArray(nodetype)) {
  617. nodetype.forEach(function(nodetype){
  618. PARENS(nodetype, func);
  619. });
  620. } else {
  621. nodetype.DEFMETHOD("needs_parens", func);
  622. }
  623. }
  624. PARENS(AST_Node, return_false);
  625. // a function expression needs parens around it when it's provably
  626. // the first token to appear in a statement.
  627. PARENS(AST_Function, function(output){
  628. if (!output.has_parens() && first_in_statement(output)) {
  629. return true;
  630. }
  631. if (output.option('webkit')) {
  632. var p = output.parent();
  633. if (p instanceof AST_PropAccess && p.expression === this) {
  634. return true;
  635. }
  636. }
  637. if (output.option('wrap_iife')) {
  638. var p = output.parent();
  639. return p instanceof AST_Call && p.expression === this;
  640. }
  641. return false;
  642. });
  643. // same goes for an object literal, because otherwise it would be
  644. // interpreted as a block of code.
  645. PARENS(AST_Object, function(output){
  646. return !output.has_parens() && first_in_statement(output);
  647. });
  648. PARENS(AST_Unary, function(output){
  649. var p = output.parent();
  650. return p instanceof AST_PropAccess && p.expression === this
  651. || p instanceof AST_Call && p.expression === this;
  652. });
  653. PARENS(AST_Sequence, function(output){
  654. var p = output.parent();
  655. return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
  656. || p instanceof AST_Unary // !(foo, bar, baz)
  657. || p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 8
  658. || p instanceof AST_VarDef // var a = (1, 2), b = a + a; ==> b == 4
  659. || p instanceof AST_PropAccess // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
  660. || p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
  661. || p instanceof AST_ObjectProperty // { foo: (1, 2) }.foo ==> 2
  662. || p instanceof AST_Conditional /* (false, true) ? (a = 10, b = 20) : (c = 30)
  663. * ==> 20 (side effect, set a := 10 and b := 20) */
  664. ;
  665. });
  666. PARENS(AST_Binary, function(output){
  667. var p = output.parent();
  668. // (foo && bar)()
  669. if (p instanceof AST_Call && p.expression === this)
  670. return true;
  671. // typeof (foo && bar)
  672. if (p instanceof AST_Unary)
  673. return true;
  674. // (foo && bar)["prop"], (foo && bar).prop
  675. if (p instanceof AST_PropAccess && p.expression === this)
  676. return true;
  677. // this deals with precedence: 3 * (2 + 1)
  678. if (p instanceof AST_Binary) {
  679. var po = p.operator, pp = PRECEDENCE[po];
  680. var so = this.operator, sp = PRECEDENCE[so];
  681. if (pp > sp
  682. || (pp == sp
  683. && this === p.right)) {
  684. return true;
  685. }
  686. }
  687. });
  688. PARENS(AST_PropAccess, function(output){
  689. var p = output.parent();
  690. if (p instanceof AST_New && p.expression === this) {
  691. // i.e. new (foo.bar().baz)
  692. //
  693. // if there's one call into this subtree, then we need
  694. // parens around it too, otherwise the call will be
  695. // interpreted as passing the arguments to the upper New
  696. // expression.
  697. var parens = false;
  698. this.walk(new TreeWalker(function(node) {
  699. if (parens || node instanceof AST_Scope) return true;
  700. if (node instanceof AST_Call) {
  701. parens = true;
  702. return true;
  703. }
  704. }));
  705. return parens;
  706. }
  707. });
  708. PARENS(AST_Call, function(output){
  709. var p = output.parent(), p1;
  710. if (p instanceof AST_New && p.expression === this)
  711. return true;
  712. // workaround for Safari bug.
  713. // https://bugs.webkit.org/show_bug.cgi?id=123506
  714. return this.expression instanceof AST_Function
  715. && p instanceof AST_PropAccess
  716. && p.expression === this
  717. && (p1 = output.parent(1)) instanceof AST_Assign
  718. && p1.left === p;
  719. });
  720. PARENS(AST_New, function(output){
  721. var p = output.parent();
  722. if (!need_constructor_parens(this, output)
  723. && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
  724. || p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
  725. return true;
  726. });
  727. PARENS(AST_Number, function(output){
  728. var p = output.parent();
  729. if (p instanceof AST_PropAccess && p.expression === this) {
  730. var value = this.getValue();
  731. if (value < 0 || /^0/.test(make_num(value))) {
  732. return true;
  733. }
  734. }
  735. });
  736. PARENS([ AST_Assign, AST_Conditional ], function(output){
  737. var p = output.parent();
  738. // !(a = false) → true
  739. if (p instanceof AST_Unary)
  740. return true;
  741. // 1 + (a = 2) + 3 → 6, side effect setting a = 2
  742. if (p instanceof AST_Binary && !(p instanceof AST_Assign))
  743. return true;
  744. // (a = func)() —or— new (a = Object)()
  745. if (p instanceof AST_Call && p.expression === this)
  746. return true;
  747. // (a = foo) ? bar : baz
  748. if (p instanceof AST_Conditional && p.condition === this)
  749. return true;
  750. // (a = foo)["prop"] —or— (a = foo).prop
  751. if (p instanceof AST_PropAccess && p.expression === this)
  752. return true;
  753. });
  754. /* -----[ PRINTERS ]----- */
  755. DEFPRINT(AST_Directive, function(self, output){
  756. output.print_string(self.value, self.quote);
  757. output.semicolon();
  758. });
  759. DEFPRINT(AST_Debugger, function(self, output){
  760. output.print("debugger");
  761. output.semicolon();
  762. });
  763. /* -----[ statements ]----- */
  764. function display_body(body, is_toplevel, output, allow_directives) {
  765. var last = body.length - 1;
  766. in_directive = allow_directives;
  767. body.forEach(function(stmt, i){
  768. if (in_directive === true && !(stmt instanceof AST_Directive ||
  769. stmt instanceof AST_EmptyStatement ||
  770. (stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
  771. )) {
  772. in_directive = false;
  773. }
  774. if (!(stmt instanceof AST_EmptyStatement)) {
  775. output.indent();
  776. stmt.print(output);
  777. if (!(i == last && is_toplevel)) {
  778. output.newline();
  779. if (is_toplevel) output.newline();
  780. }
  781. }
  782. if (in_directive === true &&
  783. stmt instanceof AST_SimpleStatement &&
  784. stmt.body instanceof AST_String
  785. ) {
  786. in_directive = false;
  787. }
  788. });
  789. in_directive = false;
  790. }
  791. AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output){
  792. force_statement(this.body, output);
  793. });
  794. DEFPRINT(AST_Statement, function(self, output){
  795. self.body.print(output);
  796. output.semicolon();
  797. });
  798. DEFPRINT(AST_Toplevel, function(self, output){
  799. display_body(self.body, true, output, true);
  800. output.print("");
  801. });
  802. DEFPRINT(AST_LabeledStatement, function(self, output){
  803. self.label.print(output);
  804. output.colon();
  805. self.body.print(output);
  806. });
  807. DEFPRINT(AST_SimpleStatement, function(self, output){
  808. self.body.print(output);
  809. output.semicolon();
  810. });
  811. function print_braced_empty(self, output) {
  812. output.print("{");
  813. output.with_indent(output.next_indent(), function() {
  814. output.append_comments(self, true);
  815. });
  816. output.print("}");
  817. }
  818. function print_braced(self, output, allow_directives) {
  819. if (self.body.length > 0) {
  820. output.with_block(function() {
  821. display_body(self.body, false, output, allow_directives);
  822. });
  823. } else print_braced_empty(self, output);
  824. }
  825. DEFPRINT(AST_BlockStatement, function(self, output){
  826. print_braced(self, output);
  827. });
  828. DEFPRINT(AST_EmptyStatement, function(self, output){
  829. output.semicolon();
  830. });
  831. DEFPRINT(AST_Do, function(self, output){
  832. output.print("do");
  833. output.space();
  834. make_block(self.body, output);
  835. output.space();
  836. output.print("while");
  837. output.space();
  838. output.with_parens(function(){
  839. self.condition.print(output);
  840. });
  841. output.semicolon();
  842. });
  843. DEFPRINT(AST_While, function(self, output){
  844. output.print("while");
  845. output.space();
  846. output.with_parens(function(){
  847. self.condition.print(output);
  848. });
  849. output.space();
  850. self._do_print_body(output);
  851. });
  852. DEFPRINT(AST_For, function(self, output){
  853. output.print("for");
  854. output.space();
  855. output.with_parens(function(){
  856. if (self.init) {
  857. if (self.init instanceof AST_Definitions) {
  858. self.init.print(output);
  859. } else {
  860. parenthesize_for_noin(self.init, output, true);
  861. }
  862. output.print(";");
  863. output.space();
  864. } else {
  865. output.print(";");
  866. }
  867. if (self.condition) {
  868. self.condition.print(output);
  869. output.print(";");
  870. output.space();
  871. } else {
  872. output.print(";");
  873. }
  874. if (self.step) {
  875. self.step.print(output);
  876. }
  877. });
  878. output.space();
  879. self._do_print_body(output);
  880. });
  881. DEFPRINT(AST_ForIn, function(self, output){
  882. output.print("for");
  883. output.space();
  884. output.with_parens(function(){
  885. self.init.print(output);
  886. output.space();
  887. output.print("in");
  888. output.space();
  889. self.object.print(output);
  890. });
  891. output.space();
  892. self._do_print_body(output);
  893. });
  894. DEFPRINT(AST_With, function(self, output){
  895. output.print("with");
  896. output.space();
  897. output.with_parens(function(){
  898. self.expression.print(output);
  899. });
  900. output.space();
  901. self._do_print_body(output);
  902. });
  903. /* -----[ functions ]----- */
  904. AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword){
  905. var self = this;
  906. if (!nokeyword) {
  907. output.print("function");
  908. }
  909. if (self.name) {
  910. output.space();
  911. self.name.print(output);
  912. }
  913. output.with_parens(function(){
  914. self.argnames.forEach(function(arg, i){
  915. if (i) output.comma();
  916. arg.print(output);
  917. });
  918. });
  919. output.space();
  920. print_braced(self, output, true);
  921. });
  922. DEFPRINT(AST_Lambda, function(self, output){
  923. self._do_print(output);
  924. });
  925. /* -----[ exits ]----- */
  926. AST_Exit.DEFMETHOD("_do_print", function(output, kind){
  927. output.print(kind);
  928. if (this.value) {
  929. output.space();
  930. this.value.print(output);
  931. }
  932. output.semicolon();
  933. });
  934. DEFPRINT(AST_Return, function(self, output){
  935. self._do_print(output, "return");
  936. });
  937. DEFPRINT(AST_Throw, function(self, output){
  938. self._do_print(output, "throw");
  939. });
  940. /* -----[ loop control ]----- */
  941. AST_LoopControl.DEFMETHOD("_do_print", function(output, kind){
  942. output.print(kind);
  943. if (this.label) {
  944. output.space();
  945. this.label.print(output);
  946. }
  947. output.semicolon();
  948. });
  949. DEFPRINT(AST_Break, function(self, output){
  950. self._do_print(output, "break");
  951. });
  952. DEFPRINT(AST_Continue, function(self, output){
  953. self._do_print(output, "continue");
  954. });
  955. /* -----[ if ]----- */
  956. function make_then(self, output) {
  957. var b = self.body;
  958. if (output.option("braces")
  959. || output.option("ie8") && b instanceof AST_Do)
  960. return make_block(b, output);
  961. // The squeezer replaces "block"-s that contain only a single
  962. // statement with the statement itself; technically, the AST
  963. // is correct, but this can create problems when we output an
  964. // IF having an ELSE clause where the THEN clause ends in an
  965. // IF *without* an ELSE block (then the outer ELSE would refer
  966. // to the inner IF). This function checks for this case and
  967. // adds the block braces if needed.
  968. if (!b) return output.force_semicolon();
  969. while (true) {
  970. if (b instanceof AST_If) {
  971. if (!b.alternative) {
  972. make_block(self.body, output);
  973. return;
  974. }
  975. b = b.alternative;
  976. }
  977. else if (b instanceof AST_StatementWithBody) {
  978. b = b.body;
  979. }
  980. else break;
  981. }
  982. force_statement(self.body, output);
  983. }
  984. DEFPRINT(AST_If, function(self, output){
  985. output.print("if");
  986. output.space();
  987. output.with_parens(function(){
  988. self.condition.print(output);
  989. });
  990. output.space();
  991. if (self.alternative) {
  992. make_then(self, output);
  993. output.space();
  994. output.print("else");
  995. output.space();
  996. if (self.alternative instanceof AST_If)
  997. self.alternative.print(output);
  998. else
  999. force_statement(self.alternative, output);
  1000. } else {
  1001. self._do_print_body(output);
  1002. }
  1003. });
  1004. /* -----[ switch ]----- */
  1005. DEFPRINT(AST_Switch, function(self, output){
  1006. output.print("switch");
  1007. output.space();
  1008. output.with_parens(function(){
  1009. self.expression.print(output);
  1010. });
  1011. output.space();
  1012. var last = self.body.length - 1;
  1013. if (last < 0) print_braced_empty(self, output);
  1014. else output.with_block(function(){
  1015. self.body.forEach(function(branch, i){
  1016. output.indent(true);
  1017. branch.print(output);
  1018. if (i < last && branch.body.length > 0)
  1019. output.newline();
  1020. });
  1021. });
  1022. });
  1023. AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output){
  1024. output.newline();
  1025. this.body.forEach(function(stmt){
  1026. output.indent();
  1027. stmt.print(output);
  1028. output.newline();
  1029. });
  1030. });
  1031. DEFPRINT(AST_Default, function(self, output){
  1032. output.print("default:");
  1033. self._do_print_body(output);
  1034. });
  1035. DEFPRINT(AST_Case, function(self, output){
  1036. output.print("case");
  1037. output.space();
  1038. self.expression.print(output);
  1039. output.print(":");
  1040. self._do_print_body(output);
  1041. });
  1042. /* -----[ exceptions ]----- */
  1043. DEFPRINT(AST_Try, function(self, output){
  1044. output.print("try");
  1045. output.space();
  1046. print_braced(self, output);
  1047. if (self.bcatch) {
  1048. output.space();
  1049. self.bcatch.print(output);
  1050. }
  1051. if (self.bfinally) {
  1052. output.space();
  1053. self.bfinally.print(output);
  1054. }
  1055. });
  1056. DEFPRINT(AST_Catch, function(self, output){
  1057. output.print("catch");
  1058. output.space();
  1059. output.with_parens(function(){
  1060. self.argname.print(output);
  1061. });
  1062. output.space();
  1063. print_braced(self, output);
  1064. });
  1065. DEFPRINT(AST_Finally, function(self, output){
  1066. output.print("finally");
  1067. output.space();
  1068. print_braced(self, output);
  1069. });
  1070. /* -----[ var/const ]----- */
  1071. AST_Definitions.DEFMETHOD("_do_print", function(output, kind){
  1072. output.print(kind);
  1073. output.space();
  1074. this.definitions.forEach(function(def, i){
  1075. if (i) output.comma();
  1076. def.print(output);
  1077. });
  1078. var p = output.parent();
  1079. var in_for = p instanceof AST_For || p instanceof AST_ForIn;
  1080. var avoid_semicolon = in_for && p.init === this;
  1081. if (!avoid_semicolon)
  1082. output.semicolon();
  1083. });
  1084. DEFPRINT(AST_Var, function(self, output){
  1085. self._do_print(output, "var");
  1086. });
  1087. function parenthesize_for_noin(node, output, noin) {
  1088. var parens = false;
  1089. // need to take some precautions here:
  1090. // https://github.com/mishoo/UglifyJS2/issues/60
  1091. if (noin) node.walk(new TreeWalker(function(node) {
  1092. if (parens || node instanceof AST_Scope) return true;
  1093. if (node instanceof AST_Binary && node.operator == "in") {
  1094. parens = true;
  1095. return true;
  1096. }
  1097. }));
  1098. node.print(output, parens);
  1099. }
  1100. DEFPRINT(AST_VarDef, function(self, output){
  1101. self.name.print(output);
  1102. if (self.value) {
  1103. output.space();
  1104. output.print("=");
  1105. output.space();
  1106. var p = output.parent(1);
  1107. var noin = p instanceof AST_For || p instanceof AST_ForIn;
  1108. parenthesize_for_noin(self.value, output, noin);
  1109. }
  1110. });
  1111. /* -----[ other expressions ]----- */
  1112. DEFPRINT(AST_Call, function(self, output){
  1113. self.expression.print(output);
  1114. if (self instanceof AST_New && !need_constructor_parens(self, output))
  1115. return;
  1116. if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
  1117. output.add_mapping(self.start);
  1118. }
  1119. output.with_parens(function(){
  1120. self.args.forEach(function(expr, i){
  1121. if (i) output.comma();
  1122. expr.print(output);
  1123. });
  1124. });
  1125. });
  1126. DEFPRINT(AST_New, function(self, output){
  1127. output.print("new");
  1128. output.space();
  1129. AST_Call.prototype._codegen(self, output);
  1130. });
  1131. AST_Sequence.DEFMETHOD("_do_print", function(output){
  1132. this.expressions.forEach(function(node, index) {
  1133. if (index > 0) {
  1134. output.comma();
  1135. if (output.should_break()) {
  1136. output.newline();
  1137. output.indent();
  1138. }
  1139. }
  1140. node.print(output);
  1141. });
  1142. });
  1143. DEFPRINT(AST_Sequence, function(self, output){
  1144. self._do_print(output);
  1145. // var p = output.parent();
  1146. // if (p instanceof AST_Statement) {
  1147. // output.with_indent(output.next_indent(), function(){
  1148. // self._do_print(output);
  1149. // });
  1150. // } else {
  1151. // self._do_print(output);
  1152. // }
  1153. });
  1154. DEFPRINT(AST_Dot, function(self, output){
  1155. var expr = self.expression;
  1156. expr.print(output);
  1157. var prop = self.property;
  1158. if (output.option("ie8") && RESERVED_WORDS[prop]) {
  1159. output.print("[");
  1160. output.add_mapping(self.end);
  1161. output.print_string(prop);
  1162. output.print("]");
  1163. } else {
  1164. if (expr instanceof AST_Number && expr.getValue() >= 0) {
  1165. if (!/[xa-f.)]/i.test(output.last())) {
  1166. output.print(".");
  1167. }
  1168. }
  1169. output.print(".");
  1170. // the name after dot would be mapped about here.
  1171. output.add_mapping(self.end);
  1172. output.print_name(prop);
  1173. }
  1174. });
  1175. DEFPRINT(AST_Sub, function(self, output){
  1176. self.expression.print(output);
  1177. output.print("[");
  1178. self.property.print(output);
  1179. output.print("]");
  1180. });
  1181. DEFPRINT(AST_UnaryPrefix, function(self, output){
  1182. var op = self.operator;
  1183. output.print(op);
  1184. if (/^[a-z]/i.test(op)
  1185. || (/[+-]$/.test(op)
  1186. && self.expression instanceof AST_UnaryPrefix
  1187. && /^[+-]/.test(self.expression.operator))) {
  1188. output.space();
  1189. }
  1190. self.expression.print(output);
  1191. });
  1192. DEFPRINT(AST_UnaryPostfix, function(self, output){
  1193. self.expression.print(output);
  1194. output.print(self.operator);
  1195. });
  1196. DEFPRINT(AST_Binary, function(self, output){
  1197. var op = self.operator;
  1198. self.left.print(output);
  1199. if (op[0] == ">" /* ">>" ">>>" ">" ">=" */
  1200. && self.left instanceof AST_UnaryPostfix
  1201. && self.left.operator == "--") {
  1202. // space is mandatory to avoid outputting -->
  1203. output.print(" ");
  1204. } else {
  1205. // the space is optional depending on "beautify"
  1206. output.space();
  1207. }
  1208. output.print(op);
  1209. if ((op == "<" || op == "<<")
  1210. && self.right instanceof AST_UnaryPrefix
  1211. && self.right.operator == "!"
  1212. && self.right.expression instanceof AST_UnaryPrefix
  1213. && self.right.expression.operator == "--") {
  1214. // space is mandatory to avoid outputting <!--
  1215. output.print(" ");
  1216. } else {
  1217. // the space is optional depending on "beautify"
  1218. output.space();
  1219. }
  1220. self.right.print(output);
  1221. });
  1222. DEFPRINT(AST_Conditional, function(self, output){
  1223. self.condition.print(output);
  1224. output.space();
  1225. output.print("?");
  1226. output.space();
  1227. self.consequent.print(output);
  1228. output.space();
  1229. output.colon();
  1230. self.alternative.print(output);
  1231. });
  1232. /* -----[ literals ]----- */
  1233. DEFPRINT(AST_Array, function(self, output){
  1234. output.with_square(function(){
  1235. var a = self.elements, len = a.length;
  1236. if (len > 0) output.space();
  1237. a.forEach(function(exp, i){
  1238. if (i) output.comma();
  1239. exp.print(output);
  1240. // If the final element is a hole, we need to make sure it
  1241. // doesn't look like a trailing comma, by inserting an actual
  1242. // trailing comma.
  1243. if (i === len - 1 && exp instanceof AST_Hole)
  1244. output.comma();
  1245. });
  1246. if (len > 0) output.space();
  1247. });
  1248. });
  1249. DEFPRINT(AST_Object, function(self, output){
  1250. if (self.properties.length > 0) output.with_block(function(){
  1251. self.properties.forEach(function(prop, i){
  1252. if (i) {
  1253. output.print(",");
  1254. output.newline();
  1255. }
  1256. output.indent();
  1257. prop.print(output);
  1258. });
  1259. output.newline();
  1260. });
  1261. else print_braced_empty(self, output);
  1262. });
  1263. function print_property_name(key, quote, output) {
  1264. if (output.option("quote_keys")) {
  1265. output.print_string(key);
  1266. } else if ("" + +key == key && key >= 0) {
  1267. output.print(make_num(key));
  1268. } else if (RESERVED_WORDS[key] ? !output.option("ie8") : is_identifier_string(key)) {
  1269. if (quote && output.option("keep_quoted_props")) {
  1270. output.print_string(key, quote);
  1271. } else {
  1272. output.print_name(key);
  1273. }
  1274. } else {
  1275. output.print_string(key, quote);
  1276. }
  1277. }
  1278. DEFPRINT(AST_ObjectKeyVal, function(self, output){
  1279. print_property_name(self.key, self.quote, output);
  1280. output.colon();
  1281. self.value.print(output);
  1282. });
  1283. AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
  1284. output.print(type);
  1285. output.space();
  1286. print_property_name(this.key.name, this.quote, output);
  1287. this.value._do_print(output, true);
  1288. });
  1289. DEFPRINT(AST_ObjectSetter, function(self, output){
  1290. self._print_getter_setter("set", output);
  1291. });
  1292. DEFPRINT(AST_ObjectGetter, function(self, output){
  1293. self._print_getter_setter("get", output);
  1294. });
  1295. DEFPRINT(AST_Symbol, function(self, output){
  1296. var def = self.definition();
  1297. output.print_name(def ? def.mangled_name || def.name : self.name);
  1298. });
  1299. DEFPRINT(AST_Hole, noop);
  1300. DEFPRINT(AST_This, function(self, output){
  1301. output.print("this");
  1302. });
  1303. DEFPRINT(AST_Constant, function(self, output){
  1304. output.print(self.getValue());
  1305. });
  1306. DEFPRINT(AST_String, function(self, output){
  1307. output.print_string(self.getValue(), self.quote, in_directive);
  1308. });
  1309. DEFPRINT(AST_Number, function(self, output){
  1310. if (use_asm && self.start && self.start.raw != null) {
  1311. output.print(self.start.raw);
  1312. } else {
  1313. output.print(make_num(self.getValue()));
  1314. }
  1315. });
  1316. DEFPRINT(AST_RegExp, function(self, output){
  1317. var regexp = self.getValue();
  1318. var str = regexp.toString();
  1319. if (regexp.raw_source) {
  1320. str = "/" + regexp.raw_source + str.slice(str.lastIndexOf("/"));
  1321. }
  1322. str = output.to_utf8(str);
  1323. output.print(str);
  1324. var p = output.parent();
  1325. if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === self)
  1326. output.print(" ");
  1327. });
  1328. function force_statement(stat, output) {
  1329. if (output.option("braces")) {
  1330. make_block(stat, output);
  1331. } else {
  1332. if (!stat || stat instanceof AST_EmptyStatement)
  1333. output.force_semicolon();
  1334. else
  1335. stat.print(output);
  1336. }
  1337. }
  1338. // self should be AST_New. decide if we want to show parens or not.
  1339. function need_constructor_parens(self, output) {
  1340. // Always print parentheses with arguments
  1341. if (self.args.length > 0) return true;
  1342. return output.option("beautify");
  1343. }
  1344. function best_of(a) {
  1345. var best = a[0], len = best.length;
  1346. for (var i = 1; i < a.length; ++i) {
  1347. if (a[i].length < len) {
  1348. best = a[i];
  1349. len = best.length;
  1350. }
  1351. }
  1352. return best;
  1353. }
  1354. function make_num(num) {
  1355. var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
  1356. var candidates = [ str ];
  1357. if (Math.floor(num) === num) {
  1358. if (num < 0) {
  1359. candidates.push("-0x" + (-num).toString(16).toLowerCase());
  1360. } else {
  1361. candidates.push("0x" + num.toString(16).toLowerCase());
  1362. }
  1363. }
  1364. var match, len, digits;
  1365. if (match = /^\.0+/.exec(str)) {
  1366. len = match[0].length;
  1367. digits = str.slice(len);
  1368. candidates.push(digits + "e-" + (digits.length + len - 1));
  1369. } else if (match = /0+$/.exec(str)) {
  1370. len = match[0].length;
  1371. candidates.push(str.slice(0, -len) + "e" + len);
  1372. } else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
  1373. candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
  1374. }
  1375. return best_of(candidates);
  1376. }
  1377. function make_block(stmt, output) {
  1378. if (!stmt || stmt instanceof AST_EmptyStatement)
  1379. output.print("{}");
  1380. else if (stmt instanceof AST_BlockStatement)
  1381. stmt.print(output);
  1382. else output.with_block(function(){
  1383. output.indent();
  1384. stmt.print(output);
  1385. output.newline();
  1386. });
  1387. }
  1388. /* -----[ source map generators ]----- */
  1389. function DEFMAP(nodetype, generator) {
  1390. nodetype.forEach(function(nodetype) {
  1391. nodetype.DEFMETHOD("add_source_map", generator);
  1392. });
  1393. }
  1394. DEFMAP([
  1395. // We could easily add info for ALL nodes, but it seems to me that
  1396. // would be quite wasteful, hence this noop in the base class.
  1397. AST_Node,
  1398. // since the label symbol will mark it
  1399. AST_LabeledStatement,
  1400. AST_Toplevel,
  1401. ], noop);
  1402. // XXX: I'm not exactly sure if we need it for all of these nodes,
  1403. // or if we should add even more.
  1404. DEFMAP([
  1405. AST_Array,
  1406. AST_BlockStatement,
  1407. AST_Catch,
  1408. AST_Constant,
  1409. AST_Debugger,
  1410. AST_Definitions,
  1411. AST_Directive,
  1412. AST_Finally,
  1413. AST_Jump,
  1414. AST_Lambda,
  1415. AST_New,
  1416. AST_Object,
  1417. AST_StatementWithBody,
  1418. AST_Symbol,
  1419. AST_Switch,
  1420. AST_SwitchBranch,
  1421. AST_Try,
  1422. ], function(output) {
  1423. output.add_mapping(this.start);
  1424. });
  1425. DEFMAP([
  1426. AST_ObjectGetter,
  1427. AST_ObjectSetter,
  1428. ], function(output) {
  1429. output.add_mapping(this.start, this.key.name);
  1430. });
  1431. DEFMAP([ AST_ObjectProperty ], function(output) {
  1432. output.add_mapping(this.start, this.key);
  1433. });
  1434. })();