output.js 49 KB

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