output.js 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936
  1. /***********************************************************************
  2. A JavaScript tokenizer / parser / beautifier / compressor.
  3. https://github.com/mishoo/UglifyJS
  4. -------------------------------- (C) ---------------------------------
  5. Author: Mihai Bazon
  6. <mihai.bazon@gmail.com>
  7. http://mihai.bazon.net/blog
  8. Distributed under the BSD license:
  9. Copyright 2012 (c) Mihai Bazon <mihai.bazon@gmail.com>
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions
  12. are met:
  13. * Redistributions of source code must retain the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer.
  16. * Redistributions in binary form must reproduce the above
  17. copyright notice, this list of conditions and the following
  18. disclaimer in the documentation and/or other materials
  19. provided with the distribution.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
  21. EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  25. OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  29. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. SUCH DAMAGE.
  32. ***********************************************************************/
  33. "use strict";
  34. function is_some_comments(comment) {
  35. // multiline comment
  36. return comment.type == "comment2" && /@preserve|@license|@cc_on/i.test(comment.value);
  37. }
  38. function OutputStream(options) {
  39. options = defaults(options, {
  40. annotations : false,
  41. ascii_only : false,
  42. beautify : false,
  43. braces : false,
  44. comments : false,
  45. galio : false,
  46. ie : 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. v8 : false,
  60. webkit : false,
  61. width : 80,
  62. wrap_iife : false,
  63. }, true);
  64. // Convert comment option to RegExp if neccessary and set up comments filter
  65. var comment_filter = return_false; // Default case, throw all comments away
  66. if (options.comments) {
  67. var comments = options.comments;
  68. if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
  69. var regex_pos = options.comments.lastIndexOf("/");
  70. comments = new RegExp(
  71. options.comments.substr(1, regex_pos - 1),
  72. options.comments.substr(regex_pos + 1)
  73. );
  74. }
  75. if (comments instanceof RegExp) {
  76. comment_filter = function(comment) {
  77. return comment.type != "comment5" && comments.test(comment.value);
  78. };
  79. } else if (typeof comments === "function") {
  80. comment_filter = function(comment) {
  81. return comment.type != "comment5" && comments(this, comment);
  82. };
  83. } else if (comments === "some") {
  84. comment_filter = is_some_comments;
  85. } else { // NOTE includes "all" option
  86. comment_filter = return_true;
  87. }
  88. }
  89. function make_indent(value) {
  90. if (typeof value == "number") return new Array(value + 1).join(" ");
  91. if (!value) return "";
  92. if (!/^\s*$/.test(value)) throw new Error("unsupported indentation: " + JSON.stringify("" + value));
  93. return value;
  94. }
  95. var current_col = 0;
  96. var current_line = 1;
  97. var current_indent = make_indent(options.indent_start);
  98. var full_indent = make_indent(options.indent_level);
  99. var half_indent = full_indent.length + 1 >> 1;
  100. var last;
  101. var line_end = 0;
  102. var line_fixed = true;
  103. var mappings = options.source_map && [];
  104. var mapping_name;
  105. var mapping_token;
  106. var might_need_space;
  107. var might_need_semicolon;
  108. var need_newline_indented = false;
  109. var need_space = false;
  110. var output;
  111. var stack;
  112. var stored = "";
  113. function reset() {
  114. last = "";
  115. might_need_space = false;
  116. might_need_semicolon = false;
  117. stack = [];
  118. var str = output;
  119. output = "";
  120. return str;
  121. }
  122. reset();
  123. var to_utf8 = options.ascii_only ? function(str, identifier) {
  124. if (identifier) str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
  125. return "\\u{" + (ch.charCodeAt(0) - 0xd7c0 << 10 | ch.charCodeAt(1) - 0xdc00).toString(16) + "}";
  126. });
  127. return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
  128. var code = ch.charCodeAt(0).toString(16);
  129. if (code.length <= 2 && !identifier) {
  130. while (code.length < 2) code = "0" + code;
  131. return "\\x" + code;
  132. } else {
  133. while (code.length < 4) code = "0" + code;
  134. return "\\u" + code;
  135. }
  136. });
  137. } : function(str) {
  138. var s = "";
  139. for (var i = 0, j = 0; i < str.length; i++) {
  140. var code = str.charCodeAt(i);
  141. if (is_surrogate_pair_head(code)) {
  142. if (is_surrogate_pair_tail(str.charCodeAt(i + 1))) {
  143. i++;
  144. continue;
  145. }
  146. } else if (!is_surrogate_pair_tail(code)) {
  147. continue;
  148. }
  149. s += str.slice(j, i) + "\\u" + code.toString(16);
  150. j = i + 1;
  151. }
  152. return j == 0 ? str : s + str.slice(j);
  153. };
  154. function quote_single(str) {
  155. return "'" + str.replace(/\x27/g, "\\'") + "'";
  156. }
  157. function quote_double(str) {
  158. return '"' + str.replace(/\x22/g, '\\"') + '"';
  159. }
  160. var quote_string = [
  161. null,
  162. quote_single,
  163. quote_double,
  164. function(str, quote) {
  165. return quote == "'" ? quote_single(str) : quote_double(str);
  166. },
  167. ][options.quote_style] || function(str, quote, dq, sq) {
  168. return dq > sq ? quote_single(str) : quote_double(str);
  169. };
  170. function make_string(str, quote) {
  171. var dq = 0, sq = 0;
  172. str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g, function(s, i) {
  173. switch (s) {
  174. case '"': ++dq; return '"';
  175. case "'": ++sq; return "'";
  176. case "\\": return "\\\\";
  177. case "\n": return "\\n";
  178. case "\r": return "\\r";
  179. case "\t": return "\\t";
  180. case "\b": return "\\b";
  181. case "\f": return "\\f";
  182. case "\x0B": return options.ie ? "\\x0B" : "\\v";
  183. case "\u2028": return "\\u2028";
  184. case "\u2029": return "\\u2029";
  185. case "\ufeff": return "\\ufeff";
  186. case "\0":
  187. return /[0-9]/.test(str.charAt(i+1)) ? "\\x00" : "\\0";
  188. }
  189. return s;
  190. });
  191. return quote_string(to_utf8(str), quote, dq, sq);
  192. }
  193. /* -----[ beautification/minification ]----- */
  194. var adjust_mappings = mappings ? function(line, col) {
  195. mappings.forEach(function(mapping) {
  196. mapping.line += line;
  197. mapping.col += col;
  198. });
  199. } : noop;
  200. var flush_mappings = mappings ? function() {
  201. mappings.forEach(function(mapping) {
  202. options.source_map.add(
  203. mapping.token.file,
  204. mapping.line, mapping.col,
  205. mapping.token.line, mapping.token.col,
  206. !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name
  207. );
  208. });
  209. mappings = [];
  210. } : noop;
  211. function insert_newlines(count) {
  212. stored += output.slice(0, line_end);
  213. output = output.slice(line_end);
  214. var new_col = output.length;
  215. adjust_mappings(count, new_col - current_col);
  216. current_line += count;
  217. current_col = new_col;
  218. while (count--) stored += "\n";
  219. }
  220. var fix_line = options.max_line_len ? function(flush) {
  221. if (line_fixed) {
  222. if (current_col > options.max_line_len) {
  223. AST_Node.warn("Output exceeds {max_line_len} characters", options);
  224. }
  225. return;
  226. }
  227. if (current_col > options.max_line_len) {
  228. insert_newlines(1);
  229. line_fixed = true;
  230. }
  231. if (line_fixed || flush) flush_mappings();
  232. } : noop;
  233. var require_semicolon = makePredicate("( [ + * / - , .");
  234. var print = options.beautify
  235. || options.comments
  236. || options.max_line_len
  237. || options.preserve_line
  238. || options.shebang
  239. || !options.semicolons
  240. || options.source_map
  241. || options.width ? function(str) {
  242. var ch = str.charAt(0);
  243. if (need_newline_indented && ch) {
  244. need_newline_indented = false;
  245. if (ch != "\n") {
  246. print("\n");
  247. indent();
  248. }
  249. }
  250. if (need_space && ch) {
  251. need_space = false;
  252. if (!/[\s;})]/.test(ch)) {
  253. space();
  254. }
  255. }
  256. var prev = last.slice(-1);
  257. if (might_need_semicolon) {
  258. might_need_semicolon = false;
  259. if (prev == ":" && ch == "}" || prev != ";" && (!ch || ";}".indexOf(ch) < 0)) {
  260. var need_semicolon = require_semicolon[ch];
  261. if (need_semicolon || options.semicolons) {
  262. output += ";";
  263. current_col++;
  264. if (!line_fixed) {
  265. fix_line();
  266. if (line_fixed && !need_semicolon && output == ";") {
  267. output = "";
  268. current_col = 0;
  269. }
  270. }
  271. if (line_end == output.length - 1) line_end++;
  272. } else {
  273. fix_line();
  274. output += "\n";
  275. current_line++;
  276. current_col = 0;
  277. // reset the semicolon flag, since we didn't print one
  278. // now and might still have to later
  279. if (/^\s+$/.test(str)) might_need_semicolon = true;
  280. }
  281. if (!options.beautify) might_need_space = false;
  282. }
  283. }
  284. if (might_need_space) {
  285. if (is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\")
  286. || (ch == "/" && ch == prev)
  287. || ((ch == "+" || ch == "-") && ch == last)
  288. || str == "--" && last == "!"
  289. || str == "in" && prev == "/"
  290. || last == "--" && ch == ">") {
  291. output += " ";
  292. current_col++;
  293. }
  294. if (prev != "<" || str != "!") might_need_space = false;
  295. }
  296. if (mapping_token) {
  297. mappings.push({
  298. token: mapping_token,
  299. name: mapping_name,
  300. line: current_line,
  301. col: current_col,
  302. });
  303. mapping_token = false;
  304. if (line_fixed) flush_mappings();
  305. }
  306. output += str;
  307. var a = str.split(/\r?\n/), n = a.length - 1;
  308. current_line += n;
  309. current_col += a[0].length;
  310. if (n > 0) {
  311. fix_line();
  312. current_col = a[n].length;
  313. }
  314. last = str;
  315. } : function(str) {
  316. var ch = str.charAt(0);
  317. var prev = last.slice(-1);
  318. if (might_need_semicolon) {
  319. might_need_semicolon = false;
  320. if (prev == ":" && ch == "}" || (!ch || ";}".indexOf(ch) < 0) && prev != ";") {
  321. output += ";";
  322. might_need_space = false;
  323. }
  324. }
  325. if (might_need_space) {
  326. if (is_identifier_char(prev) && (is_identifier_char(ch) || ch == "\\")
  327. || (ch == "/" && ch == prev)
  328. || ((ch == "+" || ch == "-") && ch == last)
  329. || str == "--" && last == "!"
  330. || str == "in" && prev == "/"
  331. || last == "--" && ch == ">") {
  332. output += " ";
  333. }
  334. if (prev != "<" || str != "!") might_need_space = false;
  335. }
  336. output += str;
  337. last = str;
  338. };
  339. var space = options.beautify ? function() {
  340. print(" ");
  341. } : function() {
  342. might_need_space = true;
  343. };
  344. var indent = options.beautify ? function(half) {
  345. if (need_newline_indented) print("\n");
  346. print(half ? current_indent.slice(0, -half_indent) : current_indent);
  347. } : noop;
  348. var with_indent = options.beautify ? function(cont) {
  349. var save_indentation = current_indent;
  350. current_indent += full_indent;
  351. cont();
  352. current_indent = save_indentation;
  353. } : function(cont) { cont() };
  354. var may_add_newline = options.max_line_len || options.preserve_line ? function() {
  355. fix_line();
  356. line_end = output.length;
  357. line_fixed = false;
  358. } : noop;
  359. var newline = options.beautify ? function() {
  360. print("\n");
  361. line_end = output.length;
  362. } : may_add_newline;
  363. var semicolon = options.beautify ? function() {
  364. print(";");
  365. } : function() {
  366. might_need_semicolon = true;
  367. };
  368. function force_semicolon() {
  369. if (might_need_semicolon) print(";");
  370. print(";");
  371. }
  372. function with_block(cont) {
  373. print("{");
  374. newline();
  375. with_indent(cont);
  376. indent();
  377. print("}");
  378. }
  379. function with_parens(cont) {
  380. print("(");
  381. may_add_newline();
  382. cont();
  383. may_add_newline();
  384. print(")");
  385. }
  386. function with_square(cont) {
  387. print("[");
  388. may_add_newline();
  389. cont();
  390. may_add_newline();
  391. print("]");
  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 (!line_fixed) fix_line(true);
  409. return stored + output;
  410. }
  411. function has_nlb() {
  412. return /(^|\n) *$/.test(output);
  413. }
  414. function pad_comment(token, force) {
  415. if (need_newline_indented) return;
  416. if (token.nlb && (force || !has_nlb())) {
  417. need_newline_indented = true;
  418. } else if (force) {
  419. need_space = true;
  420. }
  421. }
  422. function print_comment(comment) {
  423. var value = comment.value.replace(/[@#]__PURE__/g, " ");
  424. if (/^\s*$/.test(value) && !/^\s*$/.test(comment.value)) return false;
  425. if (/comment[134]/.test(comment.type)) {
  426. print("//" + value);
  427. need_newline_indented = true;
  428. } else if (comment.type == "comment2") {
  429. print("/*" + value + "*/");
  430. }
  431. return true;
  432. }
  433. function should_merge_comments(node, parent) {
  434. if (parent instanceof AST_Binary) return parent.left === node;
  435. if (parent.TYPE == "Call") return parent.expression === node;
  436. if (parent instanceof AST_Conditional) return parent.condition === node;
  437. if (parent instanceof AST_Dot) return parent.expression === node;
  438. if (parent instanceof AST_Exit) return true;
  439. if (parent instanceof AST_Sequence) return parent.expressions[0] === node;
  440. if (parent instanceof AST_Sub) return parent.expression === node;
  441. if (parent instanceof AST_UnaryPostfix) return true;
  442. if (parent instanceof AST_Yield) return true;
  443. }
  444. function prepend_comments(node) {
  445. var self = this;
  446. var scan;
  447. if (node instanceof AST_Exit) {
  448. scan = node.value;
  449. } else if (node instanceof AST_Yield) {
  450. scan = node.expression;
  451. }
  452. var comments = dump(node);
  453. if (!comments) comments = [];
  454. if (scan) {
  455. var tw = new TreeWalker(function(node) {
  456. if (!should_merge_comments(node, tw.parent())) return true;
  457. var before = dump(node);
  458. if (before) comments = comments.concat(before);
  459. });
  460. tw.push(node);
  461. scan.walk(tw);
  462. }
  463. if (current_line == 1 && current_col == 0) {
  464. if (comments.length > 0 && options.shebang && comments[0].type == "comment5") {
  465. print("#!" + comments.shift().value + "\n");
  466. indent();
  467. }
  468. var preamble = options.preamble;
  469. if (preamble) print(preamble.replace(/\r\n?|\u2028|\u2029|(^|\S)\s*$/g, "$1\n"));
  470. }
  471. comments = comments.filter(comment_filter, node);
  472. var printed = false;
  473. comments.forEach(function(comment, index) {
  474. pad_comment(comment, index);
  475. if (print_comment(comment)) printed = true;
  476. });
  477. if (printed) pad_comment(node.start, true);
  478. function dump(node) {
  479. var token = node.start;
  480. if (!token) {
  481. if (!scan) return;
  482. node.start = token = new AST_Token();
  483. }
  484. var comments = token.comments_before;
  485. if (!comments) {
  486. if (!scan) return;
  487. token.comments_before = comments = [];
  488. }
  489. if (comments._dumped === self) return;
  490. comments._dumped = self;
  491. return comments;
  492. }
  493. }
  494. function append_comments(node, tail) {
  495. var self = this;
  496. var token = node.end;
  497. if (!token) return;
  498. var comments = token[tail ? "comments_before" : "comments_after"];
  499. if (!comments || comments._dumped === self) return;
  500. if (!(node instanceof AST_Statement || all(comments, function(c) {
  501. return !/comment[134]/.test(c.type);
  502. }))) return;
  503. comments._dumped = self;
  504. comments.filter(comment_filter, node).forEach(function(comment, index) {
  505. pad_comment(comment, index || !tail);
  506. print_comment(comment);
  507. });
  508. }
  509. return {
  510. get : get,
  511. reset : reset,
  512. indent : indent,
  513. should_break : options.beautify && options.width ? function() {
  514. return current_col >= options.width;
  515. } : return_false,
  516. has_parens : function() { return last.slice(-1) == "(" },
  517. newline : newline,
  518. print : print,
  519. space : space,
  520. comma : comma,
  521. colon : colon,
  522. last : function() { return last },
  523. semicolon : semicolon,
  524. force_semicolon : force_semicolon,
  525. to_utf8 : to_utf8,
  526. print_name : function(name) { print(to_utf8(name.toString(), true)) },
  527. print_string : options.inline_script ? function(str, quote) {
  528. str = make_string(str, quote).replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2");
  529. print(str.replace(/\x3c!--/g, "\\x3c!--").replace(/--\x3e/g, "--\\x3e"));
  530. } : function(str, quote) {
  531. print(make_string(str, quote));
  532. },
  533. with_indent : with_indent,
  534. with_block : with_block,
  535. with_parens : with_parens,
  536. with_square : with_square,
  537. add_mapping : add_mapping,
  538. option : function(opt) { return options[opt] },
  539. prepend_comments: options.comments || options.shebang ? prepend_comments : noop,
  540. append_comments : options.comments ? append_comments : noop,
  541. push_node : function(node) { stack.push(node) },
  542. pop_node : options.preserve_line ? function() {
  543. var node = stack.pop();
  544. if (node.start && node.start.line > current_line) {
  545. insert_newlines(node.start.line - current_line);
  546. }
  547. } : function() {
  548. stack.pop();
  549. },
  550. parent : function(n) {
  551. return stack[stack.length - 2 - (n || 0)];
  552. },
  553. };
  554. }
  555. /* -----[ code generators ]----- */
  556. (function() {
  557. /* -----[ utils ]----- */
  558. function DEFPRINT(nodetype, generator) {
  559. nodetype.DEFMETHOD("_codegen", generator);
  560. }
  561. var use_asm = false;
  562. AST_Node.DEFMETHOD("print", function(stream, force_parens) {
  563. var self = this;
  564. stream.push_node(self);
  565. if (force_parens || self.needs_parens(stream)) {
  566. stream.with_parens(doit);
  567. } else {
  568. doit();
  569. }
  570. stream.pop_node();
  571. function doit() {
  572. stream.prepend_comments(self);
  573. self.add_source_map(stream);
  574. self._codegen(stream);
  575. stream.append_comments(self);
  576. }
  577. });
  578. var readonly = OutputStream({
  579. inline_script: false,
  580. shebang: false,
  581. width: false,
  582. });
  583. AST_Node.DEFMETHOD("print_to_string", function(options) {
  584. if (options) {
  585. var stream = OutputStream(options);
  586. this.print(stream);
  587. return stream.get();
  588. }
  589. this.print(readonly);
  590. return readonly.reset();
  591. });
  592. /* -----[ PARENTHESES ]----- */
  593. function PARENS(nodetype, func) {
  594. nodetype.DEFMETHOD("needs_parens", func);
  595. }
  596. PARENS(AST_Node, return_false);
  597. // a function expression needs parens around it when it's provably
  598. // the first token to appear in a statement.
  599. function needs_parens_function(output) {
  600. var p = output.parent();
  601. if (!output.has_parens() && first_in_statement(output, false, true)) {
  602. // export default function() {}
  603. // export default (function foo() {});
  604. // export default (function() {})(foo);
  605. // export default (function() {})`foo`;
  606. // export default (function() {}) ? foo : bar;
  607. return this.name || !(p instanceof AST_ExportDefault);
  608. }
  609. if (output.option("webkit") && p instanceof AST_PropAccess && p.expression === this) return true;
  610. if (output.option("wrap_iife") && p instanceof AST_Call && p.expression === this) return true;
  611. }
  612. PARENS(AST_AsyncFunction, needs_parens_function);
  613. PARENS(AST_AsyncGeneratorFunction, needs_parens_function);
  614. PARENS(AST_ClassExpression, needs_parens_function);
  615. PARENS(AST_Function, needs_parens_function);
  616. PARENS(AST_GeneratorFunction, needs_parens_function);
  617. // same goes for an object literal, because otherwise it would be
  618. // interpreted as a block of code.
  619. function needs_parens_obj(output) {
  620. return !output.has_parens() && first_in_statement(output, true);
  621. }
  622. PARENS(AST_Object, needs_parens_obj);
  623. function needs_parens_unary(output) {
  624. var p = output.parent();
  625. // (-x) ** y
  626. if (p instanceof AST_Binary) return p.operator == "**" && p.left === this;
  627. // (await x)(y)
  628. // new (await x)
  629. if (p instanceof AST_Call) return p.expression === this;
  630. // class extends (x++) {}
  631. // class x extends (typeof y) {}
  632. if (p instanceof AST_Class) return true;
  633. // (x++)[y]
  634. // (typeof x).y
  635. if (p instanceof AST_PropAccess) return p.expression === this;
  636. // (~x)`foo`
  637. if (p instanceof AST_Template) return p.tag === this;
  638. }
  639. PARENS(AST_Await, needs_parens_unary);
  640. PARENS(AST_Unary, needs_parens_unary);
  641. PARENS(AST_Sequence, function(output) {
  642. var p = output.parent();
  643. // [ 1, (2, 3), 4 ] ---> [ 1, 3, 4 ]
  644. return p instanceof AST_Array
  645. // () ---> (foo, bar)
  646. || is_arrow(p) && p.value === this
  647. // await (foo, bar)
  648. || p instanceof AST_Await
  649. // 1 + (2, 3) + 4 ---> 8
  650. || p instanceof AST_Binary
  651. // new (foo, bar) or foo(1, (2, 3), 4)
  652. || p instanceof AST_Call
  653. // class extends (foo, bar) {}
  654. // class foo extends (bar, baz) {}
  655. || p instanceof AST_Class
  656. // class { foo = (bar, baz) }
  657. // class { [(foo, bar)]() {} }
  658. || p instanceof AST_ClassProperty
  659. // (false, true) ? (a = 10, b = 20) : (c = 30)
  660. // ---> 20 (side effect, set a := 10 and b := 20)
  661. || p instanceof AST_Conditional
  662. // [ a = (1, 2) ] = [] ---> a == 2
  663. || p instanceof AST_DefaultValue
  664. // { [(1, 2)]: foo } = bar
  665. // { 1: (2, foo) } = bar
  666. || p instanceof AST_DestructuredKeyVal
  667. // export default (foo, bar)
  668. || p instanceof AST_ExportDefault
  669. // for (foo of (bar, baz));
  670. || p instanceof AST_ForOf
  671. // { [(1, 2)]: 3 }[2] ---> 3
  672. // { foo: (1, 2) }.foo ---> 2
  673. || p instanceof AST_ObjectProperty
  674. // (1, {foo:2}).foo or (1, {foo:2})["foo"] ---> 2
  675. || p instanceof AST_PropAccess && p.expression === this
  676. // ...(foo, bar, baz)
  677. || p instanceof AST_Spread
  678. // (foo, bar)`baz`
  679. || p instanceof AST_Template && p.tag === this
  680. // !(foo, bar, baz)
  681. || p instanceof AST_Unary
  682. // var a = (1, 2), b = a + a; ---> b == 4
  683. || p instanceof AST_VarDef
  684. // yield (foo, bar)
  685. || p instanceof AST_Yield;
  686. });
  687. PARENS(AST_Binary, function(output) {
  688. var p = output.parent();
  689. // await (foo && bar)
  690. if (p instanceof AST_Await) return true;
  691. // this deals with precedence:
  692. // 3 * (2 + 1)
  693. // 3 - (2 - 1)
  694. // (1 ** 2) ** 3
  695. if (p instanceof AST_Binary) {
  696. var po = p.operator, pp = PRECEDENCE[po];
  697. var so = this.operator, sp = PRECEDENCE[so];
  698. return pp > sp
  699. || po == "??" && (so == "&&" || so == "||")
  700. || (pp == sp && this === p[po == "**" ? "left" : "right"]);
  701. }
  702. // (foo && bar)()
  703. if (p instanceof AST_Call) return p.expression === this;
  704. // class extends (foo && bar) {}
  705. // class foo extends (bar || null) {}
  706. if (p instanceof AST_Class) return true;
  707. // (foo && bar)["prop"], (foo && bar).prop
  708. if (p instanceof AST_PropAccess) return p.expression === this;
  709. // (foo && bar)``
  710. if (p instanceof AST_Template) return p.tag === this;
  711. // typeof (foo && bar)
  712. if (p instanceof AST_Unary) return true;
  713. });
  714. function need_chain_parens(node, parent) {
  715. if (!node.terminal) return false;
  716. if (!(parent instanceof AST_Call || parent instanceof AST_PropAccess)) return false;
  717. return parent.expression === node;
  718. }
  719. PARENS(AST_PropAccess, function(output) {
  720. var node = this;
  721. var p = output.parent();
  722. // i.e. new (foo().bar)
  723. //
  724. // if there's one call into this subtree, then we need
  725. // parens around it too, otherwise the call will be
  726. // interpreted as passing the arguments to the upper New
  727. // expression.
  728. if (p instanceof AST_New && p.expression === node && root_expr(node).TYPE == "Call") return true;
  729. // (foo?.bar)()
  730. // (foo?.bar).baz
  731. // new (foo?.bar)()
  732. return need_chain_parens(node, p);
  733. });
  734. PARENS(AST_Call, function(output) {
  735. var node = this;
  736. var p = output.parent();
  737. if (p instanceof AST_New) return p.expression === node;
  738. // https://bugs.webkit.org/show_bug.cgi?id=123506
  739. if (output.option("webkit")
  740. && node.expression instanceof AST_Function
  741. && p instanceof AST_PropAccess
  742. && p.expression === node) {
  743. var g = output.parent(1);
  744. if (g instanceof AST_Assign && g.left === p) return true;
  745. }
  746. // (foo?.())()
  747. // (foo?.()).bar
  748. // new (foo?.())()
  749. return need_chain_parens(node, p);
  750. });
  751. PARENS(AST_New, function(output) {
  752. if (need_constructor_parens(this, output)) return false;
  753. var p = output.parent();
  754. // (new foo)(bar)
  755. if (p instanceof AST_Call) return p.expression === this;
  756. // (new Date).getTime(), (new Date)["getTime"]()
  757. if (p instanceof AST_PropAccess) return true;
  758. // (new foo)`bar`
  759. if (p instanceof AST_Template) return p.tag === this;
  760. });
  761. PARENS(AST_Number, function(output) {
  762. if (!output.option("galio")) return false;
  763. // https://github.com/mishoo/UglifyJS/pull/1009
  764. var p = output.parent();
  765. return p instanceof AST_PropAccess && p.expression === this && /^0/.test(make_num(this.value));
  766. });
  767. function needs_parens_assign_cond(self, output) {
  768. var p = output.parent();
  769. // await (a = foo)
  770. if (p instanceof AST_Await) return true;
  771. // 1 + (a = 2) + 3 → 6, side effect setting a = 2
  772. if (p instanceof AST_Binary) return !(p instanceof AST_Assign);
  773. // (a = func)() —or— new (a = Object)()
  774. if (p instanceof AST_Call) return p.expression === self;
  775. // class extends (a = foo) {}
  776. // class foo extends (bar ? baz : moo) {}
  777. if (p instanceof AST_Class) return true;
  778. // (a = foo) ? bar : baz
  779. if (p instanceof AST_Conditional) return p.condition === self;
  780. // (a = foo)["prop"] —or— (a = foo).prop
  781. if (p instanceof AST_PropAccess) return p.expression === self;
  782. // (a = foo)`bar`
  783. if (p instanceof AST_Template) return p.tag === self;
  784. // !(a = false) → true
  785. if (p instanceof AST_Unary) return true;
  786. }
  787. PARENS(AST_Arrow, function(output) {
  788. return needs_parens_assign_cond(this, output);
  789. });
  790. PARENS(AST_Assign, function(output) {
  791. if (needs_parens_assign_cond(this, output)) return true;
  792. // v8 parser bug ---> workaround
  793. // f([1], [a] = []) ---> f([1], ([a] = []))
  794. if (output.option("v8")) return this.left instanceof AST_Destructured;
  795. // ({ p: a } = o);
  796. if (this.left instanceof AST_DestructuredObject) return needs_parens_obj(output);
  797. });
  798. PARENS(AST_AsyncArrow, function(output) {
  799. return needs_parens_assign_cond(this, output);
  800. });
  801. PARENS(AST_Conditional, function(output) {
  802. return needs_parens_assign_cond(this, output);
  803. });
  804. PARENS(AST_Yield, function(output) {
  805. return needs_parens_assign_cond(this, output);
  806. });
  807. /* -----[ PRINTERS ]----- */
  808. DEFPRINT(AST_Directive, function(output) {
  809. var quote = this.quote;
  810. var value = this.value;
  811. switch (output.option("quote_style")) {
  812. case 0:
  813. case 2:
  814. if (value.indexOf('"') == -1) quote = '"';
  815. break;
  816. case 1:
  817. if (value.indexOf("'") == -1) quote = "'";
  818. break;
  819. }
  820. output.print(quote + value + quote);
  821. output.semicolon();
  822. });
  823. DEFPRINT(AST_Debugger, function(output) {
  824. output.print("debugger");
  825. output.semicolon();
  826. });
  827. /* -----[ statements ]----- */
  828. function display_body(body, is_toplevel, output, allow_directives) {
  829. var last = body.length - 1;
  830. var in_directive = allow_directives;
  831. var was_asm = use_asm;
  832. body.forEach(function(stmt, i) {
  833. if (in_directive) {
  834. if (stmt instanceof AST_Directive) {
  835. if (stmt.value == "use asm") use_asm = true;
  836. } else if (!(stmt instanceof AST_EmptyStatement)) {
  837. if (stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String) {
  838. output.force_semicolon();
  839. }
  840. in_directive = false;
  841. }
  842. }
  843. if (stmt instanceof AST_EmptyStatement) return;
  844. output.indent();
  845. stmt.print(output);
  846. if (i == last && is_toplevel) return;
  847. output.newline();
  848. if (is_toplevel) output.newline();
  849. });
  850. use_asm = was_asm;
  851. }
  852. DEFPRINT(AST_Toplevel, function(output) {
  853. display_body(this.body, true, output, true);
  854. output.print("");
  855. });
  856. DEFPRINT(AST_LabeledStatement, function(output) {
  857. this.label.print(output);
  858. output.colon();
  859. this.body.print(output);
  860. });
  861. DEFPRINT(AST_SimpleStatement, function(output) {
  862. this.body.print(output);
  863. output.semicolon();
  864. });
  865. function print_braced_empty(self, output) {
  866. output.print("{");
  867. output.with_indent(function() {
  868. output.append_comments(self, true);
  869. });
  870. output.print("}");
  871. }
  872. function print_braced(self, output, allow_directives) {
  873. if (self.body.length > 0) {
  874. output.with_block(function() {
  875. display_body(self.body, false, output, allow_directives);
  876. });
  877. } else print_braced_empty(self, output);
  878. }
  879. DEFPRINT(AST_BlockStatement, function(output) {
  880. print_braced(this, output);
  881. });
  882. DEFPRINT(AST_EmptyStatement, function(output) {
  883. output.semicolon();
  884. });
  885. DEFPRINT(AST_Do, function(output) {
  886. var self = this;
  887. output.print("do");
  888. make_block(self.body, output);
  889. output.space();
  890. output.print("while");
  891. output.space();
  892. output.with_parens(function() {
  893. self.condition.print(output);
  894. });
  895. output.semicolon();
  896. });
  897. DEFPRINT(AST_While, function(output) {
  898. var self = this;
  899. output.print("while");
  900. output.space();
  901. output.with_parens(function() {
  902. self.condition.print(output);
  903. });
  904. force_statement(self.body, output);
  905. });
  906. DEFPRINT(AST_For, function(output) {
  907. var self = this;
  908. output.print("for");
  909. output.space();
  910. output.with_parens(function() {
  911. if (self.init) {
  912. if (self.init instanceof AST_Definitions) {
  913. self.init.print(output);
  914. } else {
  915. parenthesize_for_noin(self.init, output, true);
  916. }
  917. output.print(";");
  918. output.space();
  919. } else {
  920. output.print(";");
  921. }
  922. if (self.condition) {
  923. self.condition.print(output);
  924. output.print(";");
  925. output.space();
  926. } else {
  927. output.print(";");
  928. }
  929. if (self.step) {
  930. self.step.print(output);
  931. }
  932. });
  933. force_statement(self.body, output);
  934. });
  935. function print_for_enum(prefix, infix) {
  936. return function(output) {
  937. var self = this;
  938. output.print(prefix);
  939. output.space();
  940. output.with_parens(function() {
  941. self.init.print(output);
  942. output.space();
  943. output.print(infix);
  944. output.space();
  945. self.object.print(output);
  946. });
  947. force_statement(self.body, output);
  948. };
  949. }
  950. DEFPRINT(AST_ForAwaitOf, print_for_enum("for await", "of"));
  951. DEFPRINT(AST_ForIn, print_for_enum("for", "in"));
  952. DEFPRINT(AST_ForOf, print_for_enum("for", "of"));
  953. DEFPRINT(AST_With, function(output) {
  954. var self = this;
  955. output.print("with");
  956. output.space();
  957. output.with_parens(function() {
  958. self.expression.print(output);
  959. });
  960. force_statement(self.body, output);
  961. });
  962. DEFPRINT(AST_ExportDeclaration, function(output) {
  963. output.print("export");
  964. output.space();
  965. this.body.print(output);
  966. });
  967. DEFPRINT(AST_ExportDefault, function(output) {
  968. output.print("export");
  969. output.space();
  970. output.print("default");
  971. output.space();
  972. var body = this.body;
  973. body.print(output);
  974. if (body instanceof AST_ClassExpression) {
  975. if (!body.name) return;
  976. }
  977. if (body instanceof AST_DefClass) return;
  978. if (body instanceof AST_LambdaDefinition) return;
  979. if (body instanceof AST_LambdaExpression) {
  980. if (!body.name && !is_arrow(body)) return;
  981. }
  982. output.semicolon();
  983. });
  984. DEFPRINT(AST_ExportForeign, function(output) {
  985. var self = this;
  986. output.print("export");
  987. output.space();
  988. var len = self.keys.length;
  989. if (len == 0) {
  990. print_braced_empty(self, output);
  991. } else if (self.keys[0] == "*") {
  992. print_entry(0);
  993. } else output.with_block(function() {
  994. output.indent();
  995. print_entry(0);
  996. for (var i = 1; i < len; i++) {
  997. output.print(",");
  998. output.newline();
  999. output.indent();
  1000. print_entry(i);
  1001. }
  1002. output.newline();
  1003. });
  1004. output.space();
  1005. output.print("from");
  1006. output.space();
  1007. output.print_string(self.path, self.quote);
  1008. output.semicolon();
  1009. function print_entry(index) {
  1010. var alias = self.aliases[index];
  1011. var key = self.keys[index];
  1012. output.print_name(key);
  1013. if (alias != key) {
  1014. output.space();
  1015. output.print("as");
  1016. output.space();
  1017. output.print_name(alias);
  1018. }
  1019. }
  1020. });
  1021. DEFPRINT(AST_ExportReferences, function(output) {
  1022. var self = this;
  1023. output.print("export");
  1024. output.space();
  1025. print_properties(self, output);
  1026. output.semicolon();
  1027. });
  1028. DEFPRINT(AST_Import, function(output) {
  1029. var self = this;
  1030. output.print("import");
  1031. output.space();
  1032. if (self.default) self.default.print(output);
  1033. if (self.all) {
  1034. if (self.default) output.comma();
  1035. self.all.print(output);
  1036. }
  1037. if (self.properties) {
  1038. if (self.default) output.comma();
  1039. print_properties(self, output);
  1040. }
  1041. if (self.all || self.default || self.properties) {
  1042. output.space();
  1043. output.print("from");
  1044. output.space();
  1045. }
  1046. output.print_string(self.path, self.quote);
  1047. output.semicolon();
  1048. });
  1049. /* -----[ functions ]----- */
  1050. function print_funargs(self, output) {
  1051. output.with_parens(function() {
  1052. self.argnames.forEach(function(arg, i) {
  1053. if (i) output.comma();
  1054. arg.print(output);
  1055. });
  1056. if (self.rest) {
  1057. if (self.argnames.length) output.comma();
  1058. output.print("...");
  1059. self.rest.print(output);
  1060. }
  1061. });
  1062. }
  1063. function print_arrow(self, output) {
  1064. var argname = self.argnames.length == 1 && !self.rest && self.argnames[0];
  1065. if (argname instanceof AST_SymbolFunarg && argname.name != "yield") {
  1066. argname.print(output);
  1067. } else {
  1068. print_funargs(self, output);
  1069. }
  1070. output.space();
  1071. output.print("=>");
  1072. output.space();
  1073. if (self.value) {
  1074. self.value.print(output);
  1075. } else {
  1076. print_braced(self, output, true);
  1077. }
  1078. }
  1079. DEFPRINT(AST_Arrow, function(output) {
  1080. print_arrow(this, output);
  1081. });
  1082. DEFPRINT(AST_AsyncArrow, function(output) {
  1083. output.print("async");
  1084. output.space();
  1085. print_arrow(this, output);
  1086. });
  1087. function print_lambda(self, output) {
  1088. if (self.name) {
  1089. output.space();
  1090. self.name.print(output);
  1091. }
  1092. print_funargs(self, output);
  1093. output.space();
  1094. print_braced(self, output, true);
  1095. }
  1096. DEFPRINT(AST_Lambda, function(output) {
  1097. output.print("function");
  1098. print_lambda(this, output);
  1099. });
  1100. function print_async(output) {
  1101. output.print("async");
  1102. output.space();
  1103. output.print("function");
  1104. print_lambda(this, output);
  1105. }
  1106. DEFPRINT(AST_AsyncDefun, print_async);
  1107. DEFPRINT(AST_AsyncFunction, print_async);
  1108. function print_async_generator(output) {
  1109. output.print("async");
  1110. output.space();
  1111. output.print("function*");
  1112. print_lambda(this, output);
  1113. }
  1114. DEFPRINT(AST_AsyncGeneratorDefun, print_async_generator);
  1115. DEFPRINT(AST_AsyncGeneratorFunction, print_async_generator);
  1116. function print_generator(output) {
  1117. output.print("function*");
  1118. print_lambda(this, output);
  1119. }
  1120. DEFPRINT(AST_GeneratorDefun, print_generator);
  1121. DEFPRINT(AST_GeneratorFunction, print_generator);
  1122. /* -----[ classes ]----- */
  1123. DEFPRINT(AST_Class, function(output) {
  1124. var self = this;
  1125. output.print("class");
  1126. if (self.name) {
  1127. output.space();
  1128. self.name.print(output);
  1129. }
  1130. if (self.extends) {
  1131. output.space();
  1132. output.print("extends");
  1133. output.space();
  1134. self.extends.print(output);
  1135. }
  1136. output.space();
  1137. print_properties(self, output, true);
  1138. });
  1139. DEFPRINT(AST_ClassField, function(output) {
  1140. var self = this;
  1141. if (self.static) {
  1142. output.print("static");
  1143. output.space();
  1144. }
  1145. print_property_key(self, output);
  1146. if (self.value) {
  1147. output.space();
  1148. output.print("=");
  1149. output.space();
  1150. self.value.print(output);
  1151. }
  1152. output.semicolon();
  1153. });
  1154. DEFPRINT(AST_ClassGetter, print_accessor("get"));
  1155. DEFPRINT(AST_ClassSetter, print_accessor("set"));
  1156. function print_method(self, output) {
  1157. var fn = self.value;
  1158. if (is_async(fn)) {
  1159. output.print("async");
  1160. output.space();
  1161. }
  1162. if (is_generator(fn)) output.print("*");
  1163. print_property_key(self, output);
  1164. print_lambda(self.value, output);
  1165. }
  1166. DEFPRINT(AST_ClassMethod, function(output) {
  1167. var self = this;
  1168. if (self.static) {
  1169. output.print("static");
  1170. output.space();
  1171. }
  1172. print_method(self, output);
  1173. });
  1174. /* -----[ jumps ]----- */
  1175. function print_jump(kind, prop) {
  1176. return function(output) {
  1177. output.print(kind);
  1178. var target = this[prop];
  1179. if (target) {
  1180. output.space();
  1181. target.print(output);
  1182. }
  1183. output.semicolon();
  1184. };
  1185. }
  1186. DEFPRINT(AST_Return, print_jump("return", "value"));
  1187. DEFPRINT(AST_Throw, print_jump("throw", "value"));
  1188. DEFPRINT(AST_Break, print_jump("break", "label"));
  1189. DEFPRINT(AST_Continue, print_jump("continue", "label"));
  1190. /* -----[ if ]----- */
  1191. function make_then(self, output) {
  1192. var b = self.body;
  1193. if (output.option("braces") && !(b instanceof AST_Const || b instanceof AST_Let)
  1194. || output.option("ie") && b instanceof AST_Do)
  1195. return make_block(b, output);
  1196. // The squeezer replaces "block"-s that contain only a single
  1197. // statement with the statement itself; technically, the AST
  1198. // is correct, but this can create problems when we output an
  1199. // IF having an ELSE clause where the THEN clause ends in an
  1200. // IF *without* an ELSE block (then the outer ELSE would refer
  1201. // to the inner IF). This function checks for this case and
  1202. // adds the block braces if needed.
  1203. if (!b) return output.force_semicolon();
  1204. while (true) {
  1205. if (b instanceof AST_If) {
  1206. if (!b.alternative) {
  1207. make_block(self.body, output);
  1208. return;
  1209. }
  1210. b = b.alternative;
  1211. } else if (b instanceof AST_StatementWithBody) {
  1212. b = b.body;
  1213. } else break;
  1214. }
  1215. force_statement(self.body, output);
  1216. }
  1217. DEFPRINT(AST_If, function(output) {
  1218. var self = this;
  1219. output.print("if");
  1220. output.space();
  1221. output.with_parens(function() {
  1222. self.condition.print(output);
  1223. });
  1224. if (self.alternative) {
  1225. make_then(self, output);
  1226. output.space();
  1227. output.print("else");
  1228. if (self.alternative instanceof AST_If) {
  1229. output.space();
  1230. self.alternative.print(output);
  1231. } else {
  1232. force_statement(self.alternative, output);
  1233. }
  1234. } else {
  1235. force_statement(self.body, output);
  1236. }
  1237. });
  1238. /* -----[ switch ]----- */
  1239. DEFPRINT(AST_Switch, function(output) {
  1240. var self = this;
  1241. output.print("switch");
  1242. output.space();
  1243. output.with_parens(function() {
  1244. self.expression.print(output);
  1245. });
  1246. output.space();
  1247. var last = self.body.length - 1;
  1248. if (last < 0) print_braced_empty(self, output);
  1249. else output.with_block(function() {
  1250. self.body.forEach(function(branch, i) {
  1251. output.indent(true);
  1252. branch.print(output);
  1253. if (i < last && branch.body.length > 0)
  1254. output.newline();
  1255. });
  1256. });
  1257. });
  1258. function print_branch_body(self, output) {
  1259. output.newline();
  1260. self.body.forEach(function(stmt) {
  1261. output.indent();
  1262. stmt.print(output);
  1263. output.newline();
  1264. });
  1265. }
  1266. DEFPRINT(AST_Default, function(output) {
  1267. output.print("default:");
  1268. print_branch_body(this, output);
  1269. });
  1270. DEFPRINT(AST_Case, function(output) {
  1271. var self = this;
  1272. output.print("case");
  1273. output.space();
  1274. self.expression.print(output);
  1275. output.print(":");
  1276. print_branch_body(self, output);
  1277. });
  1278. /* -----[ exceptions ]----- */
  1279. DEFPRINT(AST_Try, function(output) {
  1280. var self = this;
  1281. output.print("try");
  1282. output.space();
  1283. print_braced(self, output);
  1284. if (self.bcatch) {
  1285. output.space();
  1286. self.bcatch.print(output);
  1287. }
  1288. if (self.bfinally) {
  1289. output.space();
  1290. self.bfinally.print(output);
  1291. }
  1292. });
  1293. DEFPRINT(AST_Catch, function(output) {
  1294. var self = this;
  1295. output.print("catch");
  1296. if (self.argname) {
  1297. output.space();
  1298. output.with_parens(function() {
  1299. self.argname.print(output);
  1300. });
  1301. }
  1302. output.space();
  1303. print_braced(self, output);
  1304. });
  1305. DEFPRINT(AST_Finally, function(output) {
  1306. output.print("finally");
  1307. output.space();
  1308. print_braced(this, output);
  1309. });
  1310. function print_definitinos(type) {
  1311. return function(output) {
  1312. var self = this;
  1313. output.print(type);
  1314. output.space();
  1315. self.definitions.forEach(function(def, i) {
  1316. if (i) output.comma();
  1317. def.print(output);
  1318. });
  1319. var p = output.parent();
  1320. if (!(p instanceof AST_IterationStatement && p.init === self)) output.semicolon();
  1321. };
  1322. }
  1323. DEFPRINT(AST_Const, print_definitinos("const"));
  1324. DEFPRINT(AST_Let, print_definitinos("let"));
  1325. DEFPRINT(AST_Var, print_definitinos("var"));
  1326. function parenthesize_for_noin(node, output, noin) {
  1327. var parens = false;
  1328. // need to take some precautions here:
  1329. // https://github.com/mishoo/UglifyJS/issues/60
  1330. if (noin) node.walk(new TreeWalker(function(node) {
  1331. if (parens) return true;
  1332. if (node instanceof AST_Binary && node.operator == "in") return parens = true;
  1333. if (node instanceof AST_Scope && !(is_arrow(node) && node.value)) return true;
  1334. }));
  1335. node.print(output, parens);
  1336. }
  1337. DEFPRINT(AST_VarDef, function(output) {
  1338. var self = this;
  1339. self.name.print(output);
  1340. if (self.value) {
  1341. output.space();
  1342. output.print("=");
  1343. output.space();
  1344. var p = output.parent(1);
  1345. var noin = p instanceof AST_For || p instanceof AST_ForEnumeration;
  1346. parenthesize_for_noin(self.value, output, noin);
  1347. }
  1348. });
  1349. DEFPRINT(AST_DefaultValue, function(output) {
  1350. var self = this;
  1351. self.name.print(output);
  1352. output.space();
  1353. output.print("=");
  1354. output.space();
  1355. self.value.print(output);
  1356. });
  1357. /* -----[ other expressions ]----- */
  1358. function print_annotation(self, output) {
  1359. if (!output.option("annotations")) return;
  1360. if (!self.pure) return;
  1361. var level = 0, parent = self, node;
  1362. do {
  1363. node = parent;
  1364. parent = output.parent(level++);
  1365. if (parent instanceof AST_Call && parent.expression === node) return;
  1366. } while (parent instanceof AST_PropAccess && parent.expression === node);
  1367. output.print("/*@__PURE__*/");
  1368. }
  1369. function print_call_args(self, output) {
  1370. if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
  1371. output.add_mapping(self.start);
  1372. }
  1373. output.with_parens(function() {
  1374. self.args.forEach(function(expr, i) {
  1375. if (i) output.comma();
  1376. expr.print(output);
  1377. });
  1378. });
  1379. }
  1380. DEFPRINT(AST_Call, function(output) {
  1381. var self = this;
  1382. print_annotation(self, output);
  1383. self.expression.print(output);
  1384. if (self.optional) output.print("?.");
  1385. print_call_args(self, output);
  1386. });
  1387. DEFPRINT(AST_New, function(output) {
  1388. var self = this;
  1389. print_annotation(self, output);
  1390. output.print("new");
  1391. output.space();
  1392. self.expression.print(output);
  1393. if (need_constructor_parens(self, output)) print_call_args(self, output);
  1394. });
  1395. DEFPRINT(AST_Sequence, function(output) {
  1396. this.expressions.forEach(function(node, index) {
  1397. if (index > 0) {
  1398. output.comma();
  1399. if (output.should_break()) {
  1400. output.newline();
  1401. output.indent();
  1402. }
  1403. }
  1404. node.print(output);
  1405. });
  1406. });
  1407. DEFPRINT(AST_Dot, function(output) {
  1408. var self = this;
  1409. var expr = self.expression;
  1410. expr.print(output);
  1411. var prop = self.property;
  1412. if (output.option("ie") && RESERVED_WORDS[prop]) {
  1413. output.print(self.optional ? "?.[" : "[");
  1414. output.add_mapping(self.end);
  1415. output.print_string(prop);
  1416. output.print("]");
  1417. } else {
  1418. if (expr instanceof AST_Number && !/[ex.)]/i.test(output.last())) output.print(".");
  1419. output.print(self.optional ? "?." : ".");
  1420. // the name after dot would be mapped about here.
  1421. output.add_mapping(self.end);
  1422. output.print_name(prop);
  1423. }
  1424. });
  1425. DEFPRINT(AST_Sub, function(output) {
  1426. var self = this;
  1427. self.expression.print(output);
  1428. output.print(self.optional ? "?.[" : "[");
  1429. self.property.print(output);
  1430. output.print("]");
  1431. });
  1432. DEFPRINT(AST_Spread, function(output) {
  1433. output.print("...");
  1434. this.expression.print(output);
  1435. });
  1436. DEFPRINT(AST_UnaryPrefix, function(output) {
  1437. var op = this.operator;
  1438. var exp = this.expression;
  1439. output.print(op);
  1440. if (/^[a-z]/i.test(op)
  1441. || (/[+-]$/.test(op)
  1442. && exp instanceof AST_UnaryPrefix
  1443. && /^[+-]/.test(exp.operator))) {
  1444. output.space();
  1445. }
  1446. exp.print(output);
  1447. });
  1448. DEFPRINT(AST_UnaryPostfix, function(output) {
  1449. this.expression.print(output);
  1450. output.print(this.operator);
  1451. });
  1452. DEFPRINT(AST_Binary, function(output) {
  1453. var self = this;
  1454. self.left.print(output);
  1455. output.space();
  1456. output.print(self.operator);
  1457. output.space();
  1458. self.right.print(output);
  1459. });
  1460. DEFPRINT(AST_Conditional, function(output) {
  1461. var self = this;
  1462. self.condition.print(output);
  1463. output.space();
  1464. output.print("?");
  1465. output.space();
  1466. self.consequent.print(output);
  1467. output.space();
  1468. output.colon();
  1469. self.alternative.print(output);
  1470. });
  1471. DEFPRINT(AST_Await, function(output) {
  1472. output.print("await");
  1473. output.space();
  1474. this.expression.print(output);
  1475. });
  1476. DEFPRINT(AST_Yield, function(output) {
  1477. output.print(this.nested ? "yield*" : "yield");
  1478. if (this.expression) {
  1479. output.space();
  1480. this.expression.print(output);
  1481. }
  1482. });
  1483. /* -----[ literals ]----- */
  1484. DEFPRINT(AST_Array, function(output) {
  1485. var a = this.elements, len = a.length;
  1486. output.with_square(len > 0 ? function() {
  1487. output.space();
  1488. a.forEach(function(exp, i) {
  1489. if (i) output.comma();
  1490. exp.print(output);
  1491. // If the final element is a hole, we need to make sure it
  1492. // doesn't look like a trailing comma, by inserting an actual
  1493. // trailing comma.
  1494. if (i === len - 1 && exp instanceof AST_Hole)
  1495. output.comma();
  1496. });
  1497. output.space();
  1498. } : noop);
  1499. });
  1500. DEFPRINT(AST_DestructuredArray, function(output) {
  1501. var a = this.elements, len = a.length, rest = this.rest;
  1502. output.with_square(len || rest ? function() {
  1503. output.space();
  1504. a.forEach(function(exp, i) {
  1505. if (i) output.comma();
  1506. exp.print(output);
  1507. });
  1508. if (rest) {
  1509. if (len) output.comma();
  1510. output.print("...");
  1511. rest.print(output);
  1512. } else if (a[len - 1] instanceof AST_Hole) {
  1513. // If the final element is a hole, we need to make sure it
  1514. // doesn't look like a trailing comma, by inserting an actual
  1515. // trailing comma.
  1516. output.comma();
  1517. }
  1518. output.space();
  1519. } : noop);
  1520. });
  1521. DEFPRINT(AST_DestructuredKeyVal, function(output) {
  1522. var self = this;
  1523. var key = print_property_key(self, output);
  1524. var value = self.value;
  1525. if (key) {
  1526. if (value instanceof AST_DefaultValue) {
  1527. if (value.name instanceof AST_Symbol && key == get_symbol_name(value.name)) {
  1528. output.space();
  1529. output.print("=");
  1530. output.space();
  1531. value.value.print(output);
  1532. return;
  1533. }
  1534. } else if (value instanceof AST_Symbol) {
  1535. if (key == get_symbol_name(value)) return;
  1536. }
  1537. }
  1538. output.colon();
  1539. value.print(output);
  1540. });
  1541. DEFPRINT(AST_DestructuredObject, function(output) {
  1542. var props = this.properties, len = props.length, rest = this.rest;
  1543. if (len || rest) output.with_block(function() {
  1544. props.forEach(function(prop, i) {
  1545. if (i) {
  1546. output.print(",");
  1547. output.newline();
  1548. }
  1549. output.indent();
  1550. prop.print(output);
  1551. });
  1552. if (rest) {
  1553. if (len) {
  1554. output.print(",");
  1555. output.newline();
  1556. }
  1557. output.indent();
  1558. output.print("...");
  1559. rest.print(output);
  1560. }
  1561. output.newline();
  1562. });
  1563. else print_braced_empty(this, output);
  1564. });
  1565. function print_properties(self, output, no_comma) {
  1566. var props = self.properties;
  1567. if (props.length > 0) output.with_block(function() {
  1568. props.forEach(function(prop, i) {
  1569. if (i) {
  1570. if (!no_comma) output.print(",");
  1571. output.newline();
  1572. }
  1573. output.indent();
  1574. prop.print(output);
  1575. });
  1576. output.newline();
  1577. });
  1578. else print_braced_empty(self, output);
  1579. }
  1580. DEFPRINT(AST_Object, function(output) {
  1581. print_properties(this, output);
  1582. });
  1583. function print_property_key(self, output) {
  1584. var key = self.key;
  1585. if (key instanceof AST_Node) return output.with_square(function() {
  1586. key.print(output);
  1587. });
  1588. var quote = self.start && self.start.quote;
  1589. if (output.option("quote_keys") || quote && output.option("keep_quoted_props")) {
  1590. output.print_string(key, quote);
  1591. } else if ("" + +key == key && key >= 0) {
  1592. output.print(make_num(key));
  1593. } else if (self.private) {
  1594. output.print_name(key);
  1595. } else if (RESERVED_WORDS[key] ? !output.option("ie") : is_identifier_string(key)) {
  1596. output.print_name(key);
  1597. return key;
  1598. } else {
  1599. output.print_string(key, quote);
  1600. }
  1601. }
  1602. DEFPRINT(AST_ObjectKeyVal, function(output) {
  1603. var self = this;
  1604. print_property_key(self, output);
  1605. output.colon();
  1606. self.value.print(output);
  1607. });
  1608. DEFPRINT(AST_ObjectMethod, function(output) {
  1609. print_method(this, output);
  1610. });
  1611. function print_accessor(type) {
  1612. return function(output) {
  1613. var self = this;
  1614. if (self.static) {
  1615. output.print("static");
  1616. output.space();
  1617. }
  1618. output.print(type);
  1619. output.space();
  1620. print_property_key(self, output);
  1621. print_lambda(self.value, output);
  1622. };
  1623. }
  1624. DEFPRINT(AST_ObjectGetter, print_accessor("get"));
  1625. DEFPRINT(AST_ObjectSetter, print_accessor("set"));
  1626. function get_symbol_name(sym) {
  1627. var def = sym.definition();
  1628. return def && def.mangled_name || sym.name;
  1629. }
  1630. DEFPRINT(AST_Symbol, function(output) {
  1631. output.print_name(get_symbol_name(this));
  1632. });
  1633. DEFPRINT(AST_SymbolExport, function(output) {
  1634. var self = this;
  1635. var name = get_symbol_name(self);
  1636. output.print_name(name);
  1637. var alias = self.alias;
  1638. if (alias != name) {
  1639. output.space();
  1640. output.print("as");
  1641. output.space();
  1642. output.print_name(alias);
  1643. }
  1644. });
  1645. DEFPRINT(AST_SymbolImport, function(output) {
  1646. var self = this;
  1647. var name = get_symbol_name(self);
  1648. var key = self.key;
  1649. if (key && key != name) {
  1650. output.print_name(key);
  1651. output.space();
  1652. output.print("as");
  1653. output.space();
  1654. }
  1655. output.print_name(name);
  1656. });
  1657. DEFPRINT(AST_Hole, noop);
  1658. DEFPRINT(AST_Template, function(output) {
  1659. var self = this;
  1660. if (self.tag) self.tag.print(output);
  1661. output.print("`");
  1662. for (var i = 0; i < self.expressions.length; i++) {
  1663. output.print(self.strings[i]);
  1664. output.print("${");
  1665. self.expressions[i].print(output);
  1666. output.print("}");
  1667. }
  1668. output.print(self.strings[i]);
  1669. output.print("`");
  1670. });
  1671. DEFPRINT(AST_Constant, function(output) {
  1672. output.print("" + this.value);
  1673. });
  1674. DEFPRINT(AST_String, function(output) {
  1675. output.print_string(this.value, this.quote);
  1676. });
  1677. DEFPRINT(AST_Number, function(output) {
  1678. var start = this.start;
  1679. if (use_asm && start && start.raw != null) {
  1680. output.print(start.raw);
  1681. } else {
  1682. output.print(make_num(this.value));
  1683. }
  1684. });
  1685. DEFPRINT(AST_RegExp, function(output) {
  1686. var regexp = this.value;
  1687. var str = regexp.toString();
  1688. var end = str.lastIndexOf("/");
  1689. if (regexp.raw_source) {
  1690. str = "/" + regexp.raw_source + str.slice(end);
  1691. } else if (end == 1) {
  1692. str = "/(?:)" + str.slice(end);
  1693. } else if (str.indexOf("/", 1) < end) {
  1694. str = "/" + str.slice(1, end).replace(/\\\\|[^/]?\//g, function(match) {
  1695. return match[0] == "\\" ? match : match.slice(0, -1) + "\\/";
  1696. }) + str.slice(end);
  1697. }
  1698. output.print(output.to_utf8(str).replace(/\\(?:\0(?![0-9])|[^\0])/g, function(match) {
  1699. switch (match[1]) {
  1700. case "\n": return "\\n";
  1701. case "\r": return "\\r";
  1702. case "\t": return "\t";
  1703. case "\b": return "\b";
  1704. case "\f": return "\f";
  1705. case "\0": return "\0";
  1706. case "\x0B": return "\v";
  1707. case "\u2028": return "\\u2028";
  1708. case "\u2029": return "\\u2029";
  1709. default: return match;
  1710. }
  1711. }).replace(/[\n\r\u2028\u2029]/g, function(c) {
  1712. switch (c) {
  1713. case "\n": return "\\n";
  1714. case "\r": return "\\r";
  1715. case "\u2028": return "\\u2028";
  1716. case "\u2029": return "\\u2029";
  1717. }
  1718. }));
  1719. var p = output.parent();
  1720. if (p instanceof AST_Binary && /^in/.test(p.operator) && p.left === this)
  1721. output.print(" ");
  1722. });
  1723. function force_statement(stat, output) {
  1724. if (output.option("braces") && !(stat instanceof AST_Const || stat instanceof AST_Let)) {
  1725. make_block(stat, output);
  1726. } else if (stat instanceof AST_EmptyStatement) {
  1727. output.force_semicolon();
  1728. } else {
  1729. output.space();
  1730. stat.print(output);
  1731. }
  1732. }
  1733. // self should be AST_New. decide if we want to show parens or not.
  1734. function need_constructor_parens(self, output) {
  1735. // Always print parentheses with arguments
  1736. if (self.args.length > 0) return true;
  1737. return output.option("beautify");
  1738. }
  1739. function best_of(a) {
  1740. var best = a[0], len = best.length;
  1741. for (var i = 1; i < a.length; ++i) {
  1742. if (a[i].length < len) {
  1743. best = a[i];
  1744. len = best.length;
  1745. }
  1746. }
  1747. return best;
  1748. }
  1749. function make_num(num) {
  1750. var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
  1751. var candidates = [ str ];
  1752. if (Math.floor(num) === num) {
  1753. if (num < 0) {
  1754. candidates.push("-0x" + (-num).toString(16).toLowerCase());
  1755. } else {
  1756. candidates.push("0x" + num.toString(16).toLowerCase());
  1757. }
  1758. }
  1759. var match, len, digits;
  1760. if (match = /^\.0+/.exec(str)) {
  1761. len = match[0].length;
  1762. digits = str.slice(len);
  1763. candidates.push(digits + "e-" + (digits.length + len - 1));
  1764. } else if (match = /[^0]0+$/.exec(str)) {
  1765. len = match[0].length - 1;
  1766. candidates.push(str.slice(0, -len) + "e" + len);
  1767. } else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
  1768. candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
  1769. }
  1770. return best_of(candidates);
  1771. }
  1772. function make_block(stmt, output) {
  1773. output.space();
  1774. if (stmt instanceof AST_EmptyStatement) {
  1775. print_braced_empty(stmt, output);
  1776. } else if (stmt instanceof AST_BlockStatement) {
  1777. stmt.print(output);
  1778. } else output.with_block(function() {
  1779. output.indent();
  1780. stmt.print(output);
  1781. output.newline();
  1782. });
  1783. }
  1784. /* -----[ source map generators ]----- */
  1785. function DEFMAP(nodetype, generator) {
  1786. nodetype.forEach(function(nodetype) {
  1787. nodetype.DEFMETHOD("add_source_map", generator);
  1788. });
  1789. }
  1790. DEFMAP([
  1791. // We could easily add info for ALL nodes, but it seems to me that
  1792. // would be quite wasteful, hence this noop in the base class.
  1793. AST_Node,
  1794. // since the label symbol will mark it
  1795. AST_LabeledStatement,
  1796. ], noop);
  1797. // XXX: I'm not exactly sure if we need it for all of these nodes,
  1798. // or if we should add even more.
  1799. DEFMAP([
  1800. AST_Array,
  1801. AST_BlockStatement,
  1802. AST_Catch,
  1803. AST_Constant,
  1804. AST_Debugger,
  1805. AST_Definitions,
  1806. AST_Destructured,
  1807. AST_Finally,
  1808. AST_Jump,
  1809. AST_Lambda,
  1810. AST_New,
  1811. AST_Object,
  1812. AST_StatementWithBody,
  1813. AST_Symbol,
  1814. AST_Switch,
  1815. AST_SwitchBranch,
  1816. AST_Try,
  1817. ], function(output) {
  1818. output.add_mapping(this.start);
  1819. });
  1820. DEFMAP([
  1821. AST_ClassProperty,
  1822. AST_DestructuredKeyVal,
  1823. AST_ObjectProperty,
  1824. ], function(output) {
  1825. if (typeof this.key == "string") output.add_mapping(this.start, this.key);
  1826. });
  1827. })();