output.js 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219
  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. import {
  35. defaults,
  36. makePredicate,
  37. noop,
  38. regexp_source_fix,
  39. sort_regexp_flags,
  40. return_false,
  41. return_true,
  42. } from "./utils/index.js";
  43. import { first_in_statement, left_is_object } from "./utils/first_in_statement.js";
  44. import {
  45. AST_Array,
  46. AST_Arrow,
  47. AST_Assign,
  48. AST_Await,
  49. AST_BigInt,
  50. AST_Binary,
  51. AST_BlockStatement,
  52. AST_Break,
  53. AST_Call,
  54. AST_Case,
  55. AST_Catch,
  56. AST_Chain,
  57. AST_Class,
  58. AST_ClassExpression,
  59. AST_ClassProperty,
  60. AST_ConciseMethod,
  61. AST_Conditional,
  62. AST_Const,
  63. AST_Constant,
  64. AST_Continue,
  65. AST_Debugger,
  66. AST_Default,
  67. AST_DefaultAssign,
  68. AST_Definitions,
  69. AST_Defun,
  70. AST_Destructuring,
  71. AST_Directive,
  72. AST_Do,
  73. AST_Dot,
  74. AST_EmptyStatement,
  75. AST_Exit,
  76. AST_Expansion,
  77. AST_Export,
  78. AST_Finally,
  79. AST_For,
  80. AST_ForIn,
  81. AST_ForOf,
  82. AST_Function,
  83. AST_Hole,
  84. AST_If,
  85. AST_Import,
  86. AST_ImportMeta,
  87. AST_Jump,
  88. AST_LabeledStatement,
  89. AST_Lambda,
  90. AST_Let,
  91. AST_LoopControl,
  92. AST_NameMapping,
  93. AST_New,
  94. AST_NewTarget,
  95. AST_Node,
  96. AST_Number,
  97. AST_Object,
  98. AST_ObjectGetter,
  99. AST_ObjectKeyVal,
  100. AST_ObjectProperty,
  101. AST_ObjectSetter,
  102. AST_PrefixedTemplateString,
  103. AST_PropAccess,
  104. AST_RegExp,
  105. AST_Return,
  106. AST_Scope,
  107. AST_Sequence,
  108. AST_SimpleStatement,
  109. AST_Statement,
  110. AST_StatementWithBody,
  111. AST_String,
  112. AST_Sub,
  113. AST_Super,
  114. AST_Switch,
  115. AST_SwitchBranch,
  116. AST_Symbol,
  117. AST_SymbolClassProperty,
  118. AST_SymbolMethod,
  119. AST_SymbolRef,
  120. AST_TemplateSegment,
  121. AST_TemplateString,
  122. AST_This,
  123. AST_Throw,
  124. AST_Toplevel,
  125. AST_Try,
  126. AST_Unary,
  127. AST_UnaryPostfix,
  128. AST_UnaryPrefix,
  129. AST_Var,
  130. AST_VarDef,
  131. AST_While,
  132. AST_With,
  133. AST_Yield,
  134. TreeWalker,
  135. walk,
  136. walk_abort
  137. } from "./ast.js";
  138. import {
  139. get_full_char_code,
  140. get_full_char,
  141. is_identifier_char,
  142. is_basic_identifier_string,
  143. is_identifier_string,
  144. PRECEDENCE,
  145. RESERVED_WORDS,
  146. } from "./parse.js";
  147. const EXPECT_DIRECTIVE = /^$|[;{][\s\n]*$/;
  148. const CODE_LINE_BREAK = 10;
  149. const CODE_SPACE = 32;
  150. const r_annotation = /[@#]__(PURE|INLINE|NOINLINE)__/g;
  151. function is_some_comments(comment) {
  152. // multiline comment
  153. return (
  154. (comment.type === "comment2" || comment.type === "comment1")
  155. && /@preserve|@lic|@cc_on|^\**!/i.test(comment.value)
  156. );
  157. }
  158. function OutputStream(options) {
  159. var readonly = !options;
  160. options = defaults(options, {
  161. ascii_only : false,
  162. beautify : false,
  163. braces : false,
  164. comments : "some",
  165. ecma : 5,
  166. ie8 : false,
  167. indent_level : 4,
  168. indent_start : 0,
  169. inline_script : true,
  170. keep_numbers : false,
  171. keep_quoted_props : false,
  172. max_line_len : false,
  173. preamble : null,
  174. preserve_annotations : false,
  175. quote_keys : false,
  176. quote_style : 0,
  177. safari10 : false,
  178. semicolons : true,
  179. shebang : true,
  180. shorthand : undefined,
  181. source_map : null,
  182. webkit : false,
  183. width : 80,
  184. wrap_iife : false,
  185. wrap_func_args : true,
  186. }, true);
  187. if (options.shorthand === undefined)
  188. options.shorthand = options.ecma > 5;
  189. // Convert comment option to RegExp if neccessary and set up comments filter
  190. var comment_filter = return_false; // Default case, throw all comments away
  191. if (options.comments) {
  192. let comments = options.comments;
  193. if (typeof options.comments === "string" && /^\/.*\/[a-zA-Z]*$/.test(options.comments)) {
  194. var regex_pos = options.comments.lastIndexOf("/");
  195. comments = new RegExp(
  196. options.comments.substr(1, regex_pos - 1),
  197. options.comments.substr(regex_pos + 1)
  198. );
  199. }
  200. if (comments instanceof RegExp) {
  201. comment_filter = function(comment) {
  202. return comment.type != "comment5" && comments.test(comment.value);
  203. };
  204. } else if (typeof comments === "function") {
  205. comment_filter = function(comment) {
  206. return comment.type != "comment5" && comments(this, comment);
  207. };
  208. } else if (comments === "some") {
  209. comment_filter = is_some_comments;
  210. } else { // NOTE includes "all" option
  211. comment_filter = return_true;
  212. }
  213. }
  214. var indentation = 0;
  215. var current_col = 0;
  216. var current_line = 1;
  217. var current_pos = 0;
  218. var OUTPUT = "";
  219. let printed_comments = new Set();
  220. var to_utf8 = options.ascii_only ? function(str, identifier) {
  221. if (options.ecma >= 2015) {
  222. str = str.replace(/[\ud800-\udbff][\udc00-\udfff]/g, function(ch) {
  223. var code = get_full_char_code(ch, 0).toString(16);
  224. return "\\u{" + code + "}";
  225. });
  226. }
  227. return str.replace(/[\u0000-\u001f\u007f-\uffff]/g, function(ch) {
  228. var code = ch.charCodeAt(0).toString(16);
  229. if (code.length <= 2 && !identifier) {
  230. while (code.length < 2) code = "0" + code;
  231. return "\\x" + code;
  232. } else {
  233. while (code.length < 4) code = "0" + code;
  234. return "\\u" + code;
  235. }
  236. });
  237. } : function(str) {
  238. return str.replace(/[\ud800-\udbff][\udc00-\udfff]|([\ud800-\udbff]|[\udc00-\udfff])/g, function(match, lone) {
  239. if (lone) {
  240. return "\\u" + lone.charCodeAt(0).toString(16);
  241. }
  242. return match;
  243. });
  244. };
  245. function make_string(str, quote) {
  246. var dq = 0, sq = 0;
  247. str = str.replace(/[\\\b\f\n\r\v\t\x22\x27\u2028\u2029\0\ufeff]/g,
  248. function(s, i) {
  249. switch (s) {
  250. case '"': ++dq; return '"';
  251. case "'": ++sq; return "'";
  252. case "\\": return "\\\\";
  253. case "\n": return "\\n";
  254. case "\r": return "\\r";
  255. case "\t": return "\\t";
  256. case "\b": return "\\b";
  257. case "\f": return "\\f";
  258. case "\x0B": return options.ie8 ? "\\x0B" : "\\v";
  259. case "\u2028": return "\\u2028";
  260. case "\u2029": return "\\u2029";
  261. case "\ufeff": return "\\ufeff";
  262. case "\0":
  263. return /[0-9]/.test(get_full_char(str, i+1)) ? "\\x00" : "\\0";
  264. }
  265. return s;
  266. });
  267. function quote_single() {
  268. return "'" + str.replace(/\x27/g, "\\'") + "'";
  269. }
  270. function quote_double() {
  271. return '"' + str.replace(/\x22/g, '\\"') + '"';
  272. }
  273. function quote_template() {
  274. return "`" + str.replace(/`/g, "\\`") + "`";
  275. }
  276. str = to_utf8(str);
  277. if (quote === "`") return quote_template();
  278. switch (options.quote_style) {
  279. case 1:
  280. return quote_single();
  281. case 2:
  282. return quote_double();
  283. case 3:
  284. return quote == "'" ? quote_single() : quote_double();
  285. default:
  286. return dq > sq ? quote_single() : quote_double();
  287. }
  288. }
  289. function encode_string(str, quote) {
  290. var ret = make_string(str, quote);
  291. if (options.inline_script) {
  292. ret = ret.replace(/<\x2f(script)([>\/\t\n\f\r ])/gi, "<\\/$1$2");
  293. ret = ret.replace(/\x3c!--/g, "\\x3c!--");
  294. ret = ret.replace(/--\x3e/g, "--\\x3e");
  295. }
  296. return ret;
  297. }
  298. function make_name(name) {
  299. name = name.toString();
  300. name = to_utf8(name, true);
  301. return name;
  302. }
  303. function make_indent(back) {
  304. return " ".repeat(options.indent_start + indentation - back * options.indent_level);
  305. }
  306. /* -----[ beautification/minification ]----- */
  307. var has_parens = false;
  308. var might_need_space = false;
  309. var might_need_semicolon = false;
  310. var might_add_newline = 0;
  311. var need_newline_indented = false;
  312. var need_space = false;
  313. var newline_insert = -1;
  314. var last = "";
  315. var mapping_token, mapping_name, mappings = options.source_map && [];
  316. var do_add_mapping = mappings ? function() {
  317. mappings.forEach(function(mapping) {
  318. try {
  319. options.source_map.add(
  320. mapping.token.file,
  321. mapping.line, mapping.col,
  322. mapping.token.line, mapping.token.col,
  323. !mapping.name && mapping.token.type == "name" ? mapping.token.value : mapping.name
  324. );
  325. } catch(ex) {
  326. // Ignore bad mapping
  327. }
  328. });
  329. mappings = [];
  330. } : noop;
  331. var ensure_line_len = options.max_line_len ? function() {
  332. if (current_col > options.max_line_len) {
  333. if (might_add_newline) {
  334. var left = OUTPUT.slice(0, might_add_newline);
  335. var right = OUTPUT.slice(might_add_newline);
  336. if (mappings) {
  337. var delta = right.length - current_col;
  338. mappings.forEach(function(mapping) {
  339. mapping.line++;
  340. mapping.col += delta;
  341. });
  342. }
  343. OUTPUT = left + "\n" + right;
  344. current_line++;
  345. current_pos++;
  346. current_col = right.length;
  347. }
  348. }
  349. if (might_add_newline) {
  350. might_add_newline = 0;
  351. do_add_mapping();
  352. }
  353. } : noop;
  354. var requireSemicolonChars = makePredicate("( [ + * / - , . `");
  355. function print(str) {
  356. str = String(str);
  357. var ch = get_full_char(str, 0);
  358. if (need_newline_indented && ch) {
  359. need_newline_indented = false;
  360. if (ch !== "\n") {
  361. print("\n");
  362. indent();
  363. }
  364. }
  365. if (need_space && ch) {
  366. need_space = false;
  367. if (!/[\s;})]/.test(ch)) {
  368. space();
  369. }
  370. }
  371. newline_insert = -1;
  372. var prev = last.charAt(last.length - 1);
  373. if (might_need_semicolon) {
  374. might_need_semicolon = false;
  375. if (prev === ":" && ch === "}" || (!ch || !";}".includes(ch)) && prev !== ";") {
  376. if (options.semicolons || requireSemicolonChars.has(ch)) {
  377. OUTPUT += ";";
  378. current_col++;
  379. current_pos++;
  380. } else {
  381. ensure_line_len();
  382. if (current_col > 0) {
  383. OUTPUT += "\n";
  384. current_pos++;
  385. current_line++;
  386. current_col = 0;
  387. }
  388. if (/^\s+$/.test(str)) {
  389. // reset the semicolon flag, since we didn't print one
  390. // now and might still have to later
  391. might_need_semicolon = true;
  392. }
  393. }
  394. if (!options.beautify)
  395. might_need_space = false;
  396. }
  397. }
  398. if (might_need_space) {
  399. if ((is_identifier_char(prev)
  400. && (is_identifier_char(ch) || ch == "\\"))
  401. || (ch == "/" && ch == prev)
  402. || ((ch == "+" || ch == "-") && ch == last)
  403. ) {
  404. OUTPUT += " ";
  405. current_col++;
  406. current_pos++;
  407. }
  408. might_need_space = false;
  409. }
  410. if (mapping_token) {
  411. mappings.push({
  412. token: mapping_token,
  413. name: mapping_name,
  414. line: current_line,
  415. col: current_col
  416. });
  417. mapping_token = false;
  418. if (!might_add_newline) do_add_mapping();
  419. }
  420. OUTPUT += str;
  421. has_parens = str[str.length - 1] == "(";
  422. current_pos += str.length;
  423. var a = str.split(/\r?\n/), n = a.length - 1;
  424. current_line += n;
  425. current_col += a[0].length;
  426. if (n > 0) {
  427. ensure_line_len();
  428. current_col = a[n].length;
  429. }
  430. last = str;
  431. }
  432. var star = function() {
  433. print("*");
  434. };
  435. var space = options.beautify ? function() {
  436. print(" ");
  437. } : function() {
  438. might_need_space = true;
  439. };
  440. var indent = options.beautify ? function(half) {
  441. if (options.beautify) {
  442. print(make_indent(half ? 0.5 : 0));
  443. }
  444. } : noop;
  445. var with_indent = options.beautify ? function(col, cont) {
  446. if (col === true) col = next_indent();
  447. var save_indentation = indentation;
  448. indentation = col;
  449. var ret = cont();
  450. indentation = save_indentation;
  451. return ret;
  452. } : function(col, cont) { return cont(); };
  453. var newline = options.beautify ? function() {
  454. if (newline_insert < 0) return print("\n");
  455. if (OUTPUT[newline_insert] != "\n") {
  456. OUTPUT = OUTPUT.slice(0, newline_insert) + "\n" + OUTPUT.slice(newline_insert);
  457. current_pos++;
  458. current_line++;
  459. }
  460. newline_insert++;
  461. } : options.max_line_len ? function() {
  462. ensure_line_len();
  463. might_add_newline = OUTPUT.length;
  464. } : noop;
  465. var semicolon = options.beautify ? function() {
  466. print(";");
  467. } : function() {
  468. might_need_semicolon = true;
  469. };
  470. function force_semicolon() {
  471. might_need_semicolon = false;
  472. print(";");
  473. }
  474. function next_indent() {
  475. return indentation + options.indent_level;
  476. }
  477. function with_block(cont) {
  478. var ret;
  479. print("{");
  480. newline();
  481. with_indent(next_indent(), function() {
  482. ret = cont();
  483. });
  484. indent();
  485. print("}");
  486. return ret;
  487. }
  488. function with_parens(cont) {
  489. print("(");
  490. //XXX: still nice to have that for argument lists
  491. //var ret = with_indent(current_col, cont);
  492. var ret = cont();
  493. print(")");
  494. return ret;
  495. }
  496. function with_square(cont) {
  497. print("[");
  498. //var ret = with_indent(current_col, cont);
  499. var ret = cont();
  500. print("]");
  501. return ret;
  502. }
  503. function comma() {
  504. print(",");
  505. space();
  506. }
  507. function colon() {
  508. print(":");
  509. space();
  510. }
  511. var add_mapping = mappings ? function(token, name) {
  512. mapping_token = token;
  513. mapping_name = name;
  514. } : noop;
  515. function get() {
  516. if (might_add_newline) {
  517. ensure_line_len();
  518. }
  519. return OUTPUT;
  520. }
  521. function has_nlb() {
  522. let n = OUTPUT.length - 1;
  523. while (n >= 0) {
  524. const code = OUTPUT.charCodeAt(n);
  525. if (code === CODE_LINE_BREAK) {
  526. return true;
  527. }
  528. if (code !== CODE_SPACE) {
  529. return false;
  530. }
  531. n--;
  532. }
  533. return true;
  534. }
  535. function filter_comment(comment) {
  536. if (!options.preserve_annotations) {
  537. comment = comment.replace(r_annotation, " ");
  538. }
  539. if (/^\s*$/.test(comment)) {
  540. return "";
  541. }
  542. return comment.replace(/(<\s*\/\s*)(script)/i, "<\\/$2");
  543. }
  544. function prepend_comments(node) {
  545. var self = this;
  546. var start = node.start;
  547. if (!start) return;
  548. var printed_comments = self.printed_comments;
  549. // There cannot be a newline between return and its value.
  550. const return_with_value = node instanceof AST_Exit && node.value;
  551. if (
  552. start.comments_before
  553. && printed_comments.has(start.comments_before)
  554. ) {
  555. if (return_with_value) {
  556. start.comments_before = [];
  557. } else {
  558. return;
  559. }
  560. }
  561. var comments = start.comments_before;
  562. if (!comments) {
  563. comments = start.comments_before = [];
  564. }
  565. printed_comments.add(comments);
  566. if (return_with_value) {
  567. var tw = new TreeWalker(function(node) {
  568. var parent = tw.parent();
  569. if (parent instanceof AST_Exit
  570. || parent instanceof AST_Binary && parent.left === node
  571. || parent.TYPE == "Call" && parent.expression === node
  572. || parent instanceof AST_Conditional && parent.condition === node
  573. || parent instanceof AST_Dot && parent.expression === node
  574. || parent instanceof AST_Sequence && parent.expressions[0] === node
  575. || parent instanceof AST_Sub && parent.expression === node
  576. || parent instanceof AST_UnaryPostfix) {
  577. if (!node.start) return;
  578. var text = node.start.comments_before;
  579. if (text && !printed_comments.has(text)) {
  580. printed_comments.add(text);
  581. comments = comments.concat(text);
  582. }
  583. } else {
  584. return true;
  585. }
  586. });
  587. tw.push(node);
  588. node.value.walk(tw);
  589. }
  590. if (current_pos == 0) {
  591. if (comments.length > 0 && options.shebang && comments[0].type === "comment5"
  592. && !printed_comments.has(comments[0])) {
  593. print("#!" + comments.shift().value + "\n");
  594. indent();
  595. }
  596. var preamble = options.preamble;
  597. if (preamble) {
  598. print(preamble.replace(/\r\n?|[\n\u2028\u2029]|\s*$/g, "\n"));
  599. }
  600. }
  601. comments = comments.filter(comment_filter, node).filter(c => !printed_comments.has(c));
  602. if (comments.length == 0) return;
  603. var last_nlb = has_nlb();
  604. comments.forEach(function(c, i) {
  605. printed_comments.add(c);
  606. if (!last_nlb) {
  607. if (c.nlb) {
  608. print("\n");
  609. indent();
  610. last_nlb = true;
  611. } else if (i > 0) {
  612. space();
  613. }
  614. }
  615. if (/comment[134]/.test(c.type)) {
  616. var value = filter_comment(c.value);
  617. if (value) {
  618. print("//" + value + "\n");
  619. indent();
  620. }
  621. last_nlb = true;
  622. } else if (c.type == "comment2") {
  623. var value = filter_comment(c.value);
  624. if (value) {
  625. print("/*" + value + "*/");
  626. }
  627. last_nlb = false;
  628. }
  629. });
  630. if (!last_nlb) {
  631. if (start.nlb) {
  632. print("\n");
  633. indent();
  634. } else {
  635. space();
  636. }
  637. }
  638. }
  639. function append_comments(node, tail) {
  640. var self = this;
  641. var token = node.end;
  642. if (!token) return;
  643. var printed_comments = self.printed_comments;
  644. var comments = token[tail ? "comments_before" : "comments_after"];
  645. if (!comments || printed_comments.has(comments)) return;
  646. if (!(node instanceof AST_Statement || comments.every((c) =>
  647. !/comment[134]/.test(c.type)
  648. ))) return;
  649. printed_comments.add(comments);
  650. var insert = OUTPUT.length;
  651. comments.filter(comment_filter, node).forEach(function(c, i) {
  652. if (printed_comments.has(c)) return;
  653. printed_comments.add(c);
  654. need_space = false;
  655. if (need_newline_indented) {
  656. print("\n");
  657. indent();
  658. need_newline_indented = false;
  659. } else if (c.nlb && (i > 0 || !has_nlb())) {
  660. print("\n");
  661. indent();
  662. } else if (i > 0 || !tail) {
  663. space();
  664. }
  665. if (/comment[134]/.test(c.type)) {
  666. const value = filter_comment(c.value);
  667. if (value) {
  668. print("//" + value);
  669. }
  670. need_newline_indented = true;
  671. } else if (c.type == "comment2") {
  672. const value = filter_comment(c.value);
  673. if (value) {
  674. print("/*" + value + "*/");
  675. }
  676. need_space = true;
  677. }
  678. });
  679. if (OUTPUT.length > insert) newline_insert = insert;
  680. }
  681. var stack = [];
  682. return {
  683. get : get,
  684. toString : get,
  685. indent : indent,
  686. in_directive : false,
  687. use_asm : null,
  688. active_scope : null,
  689. indentation : function() { return indentation; },
  690. current_width : function() { return current_col - indentation; },
  691. should_break : function() { return options.width && this.current_width() >= options.width; },
  692. has_parens : function() { return has_parens; },
  693. newline : newline,
  694. print : print,
  695. star : star,
  696. space : space,
  697. comma : comma,
  698. colon : colon,
  699. last : function() { return last; },
  700. semicolon : semicolon,
  701. force_semicolon : force_semicolon,
  702. to_utf8 : to_utf8,
  703. print_name : function(name) { print(make_name(name)); },
  704. print_string : function(str, quote, escape_directive) {
  705. var encoded = encode_string(str, quote);
  706. if (escape_directive === true && !encoded.includes("\\")) {
  707. // Insert semicolons to break directive prologue
  708. if (!EXPECT_DIRECTIVE.test(OUTPUT)) {
  709. force_semicolon();
  710. }
  711. force_semicolon();
  712. }
  713. print(encoded);
  714. },
  715. print_template_string_chars: function(str) {
  716. var encoded = encode_string(str, "`").replace(/\${/g, "\\${");
  717. return print(encoded.substr(1, encoded.length - 2));
  718. },
  719. encode_string : encode_string,
  720. next_indent : next_indent,
  721. with_indent : with_indent,
  722. with_block : with_block,
  723. with_parens : with_parens,
  724. with_square : with_square,
  725. add_mapping : add_mapping,
  726. option : function(opt) { return options[opt]; },
  727. printed_comments: printed_comments,
  728. prepend_comments: readonly ? noop : prepend_comments,
  729. append_comments : readonly || comment_filter === return_false ? noop : append_comments,
  730. line : function() { return current_line; },
  731. col : function() { return current_col; },
  732. pos : function() { return current_pos; },
  733. push_node : function(node) { stack.push(node); },
  734. pop_node : function() { return stack.pop(); },
  735. parent : function(n) {
  736. return stack[stack.length - 2 - (n || 0)];
  737. }
  738. };
  739. }
  740. /* -----[ code generators ]----- */
  741. (function() {
  742. /* -----[ utils ]----- */
  743. function DEFPRINT(nodetype, generator) {
  744. nodetype.DEFMETHOD("_codegen", generator);
  745. }
  746. AST_Node.DEFMETHOD("print", function(output, force_parens) {
  747. var self = this, generator = self._codegen;
  748. if (self instanceof AST_Scope) {
  749. output.active_scope = self;
  750. } else if (!output.use_asm && self instanceof AST_Directive && self.value == "use asm") {
  751. output.use_asm = output.active_scope;
  752. }
  753. function doit() {
  754. output.prepend_comments(self);
  755. self.add_source_map(output);
  756. generator(self, output);
  757. output.append_comments(self);
  758. }
  759. output.push_node(self);
  760. if (force_parens || self.needs_parens(output)) {
  761. output.with_parens(doit);
  762. } else {
  763. doit();
  764. }
  765. output.pop_node();
  766. if (self === output.use_asm) {
  767. output.use_asm = null;
  768. }
  769. });
  770. AST_Node.DEFMETHOD("_print", AST_Node.prototype.print);
  771. AST_Node.DEFMETHOD("print_to_string", function(options) {
  772. var output = OutputStream(options);
  773. this.print(output);
  774. return output.get();
  775. });
  776. /* -----[ PARENTHESES ]----- */
  777. function PARENS(nodetype, func) {
  778. if (Array.isArray(nodetype)) {
  779. nodetype.forEach(function(nodetype) {
  780. PARENS(nodetype, func);
  781. });
  782. } else {
  783. nodetype.DEFMETHOD("needs_parens", func);
  784. }
  785. }
  786. PARENS(AST_Node, return_false);
  787. // a function expression needs parens around it when it's provably
  788. // the first token to appear in a statement.
  789. PARENS(AST_Function, function(output) {
  790. if (!output.has_parens() && first_in_statement(output)) {
  791. return true;
  792. }
  793. if (output.option("webkit")) {
  794. var p = output.parent();
  795. if (p instanceof AST_PropAccess && p.expression === this) {
  796. return true;
  797. }
  798. }
  799. if (output.option("wrap_iife")) {
  800. var p = output.parent();
  801. if (p instanceof AST_Call && p.expression === this) {
  802. return true;
  803. }
  804. }
  805. if (output.option("wrap_func_args")) {
  806. var p = output.parent();
  807. if (p instanceof AST_Call && p.args.includes(this)) {
  808. return true;
  809. }
  810. }
  811. return false;
  812. });
  813. PARENS(AST_Arrow, function(output) {
  814. var p = output.parent();
  815. if (
  816. output.option("wrap_func_args")
  817. && p instanceof AST_Call
  818. && p.args.includes(this)
  819. ) {
  820. return true;
  821. }
  822. return p instanceof AST_PropAccess && p.expression === this;
  823. });
  824. // same goes for an object literal (as in AST_Function), because
  825. // otherwise {...} would be interpreted as a block of code.
  826. PARENS(AST_Object, function(output) {
  827. return !output.has_parens() && first_in_statement(output);
  828. });
  829. PARENS(AST_ClassExpression, first_in_statement);
  830. PARENS(AST_Unary, function(output) {
  831. var p = output.parent();
  832. return p instanceof AST_PropAccess && p.expression === this
  833. || p instanceof AST_Call && p.expression === this
  834. || p instanceof AST_Binary
  835. && p.operator === "**"
  836. && this instanceof AST_UnaryPrefix
  837. && p.left === this
  838. && this.operator !== "++"
  839. && this.operator !== "--";
  840. });
  841. PARENS(AST_Await, function(output) {
  842. var p = output.parent();
  843. return p instanceof AST_PropAccess && p.expression === this
  844. || p instanceof AST_Call && p.expression === this
  845. || output.option("safari10") && p instanceof AST_UnaryPrefix;
  846. });
  847. PARENS(AST_Sequence, function(output) {
  848. var p = output.parent();
  849. return p instanceof AST_Call // (foo, bar)() or foo(1, (2, 3), 4)
  850. || p instanceof AST_Unary // !(foo, bar, baz)
  851. || p instanceof AST_Binary // 1 + (2, 3) + 4 ==> 8
  852. || p instanceof AST_VarDef // var a = (1, 2), b = a + a; ==> b == 4
  853. || p instanceof AST_PropAccess // (1, {foo:2}).foo or (1, {foo:2})["foo"] ==> 2
  854. || p instanceof AST_Array // [ 1, (2, 3), 4 ] ==> [ 1, 3, 4 ]
  855. || p instanceof AST_ObjectProperty // { foo: (1, 2) }.foo ==> 2
  856. || p instanceof AST_Conditional /* (false, true) ? (a = 10, b = 20) : (c = 30)
  857. * ==> 20 (side effect, set a := 10 and b := 20) */
  858. || p instanceof AST_Arrow // x => (x, x)
  859. || p instanceof AST_DefaultAssign // x => (x = (0, function(){}))
  860. || p instanceof AST_Expansion // [...(a, b)]
  861. || p instanceof AST_ForOf && this === p.object // for (e of (foo, bar)) {}
  862. || p instanceof AST_Yield // yield (foo, bar)
  863. || p instanceof AST_Export // export default (foo, bar)
  864. ;
  865. });
  866. PARENS(AST_Binary, function(output) {
  867. var p = output.parent();
  868. // (foo && bar)()
  869. if (p instanceof AST_Call && p.expression === this)
  870. return true;
  871. // typeof (foo && bar)
  872. if (p instanceof AST_Unary)
  873. return true;
  874. // (foo && bar)["prop"], (foo && bar).prop
  875. if (p instanceof AST_PropAccess && p.expression === this)
  876. return true;
  877. // this deals with precedence: 3 * (2 + 1)
  878. if (p instanceof AST_Binary) {
  879. const po = p.operator;
  880. const so = this.operator;
  881. if (so === "??" && (po === "||" || po === "&&")) {
  882. return true;
  883. }
  884. if (po === "??" && (so === "||" || so === "&&")) {
  885. return true;
  886. }
  887. const pp = PRECEDENCE[po];
  888. const sp = PRECEDENCE[so];
  889. if (pp > sp
  890. || (pp == sp
  891. && (this === p.right || po == "**"))) {
  892. return true;
  893. }
  894. }
  895. });
  896. PARENS(AST_Yield, function(output) {
  897. var p = output.parent();
  898. // (yield 1) + (yield 2)
  899. // a = yield 3
  900. if (p instanceof AST_Binary && p.operator !== "=")
  901. return true;
  902. // (yield 1)()
  903. // new (yield 1)()
  904. if (p instanceof AST_Call && p.expression === this)
  905. return true;
  906. // (yield 1) ? yield 2 : yield 3
  907. if (p instanceof AST_Conditional && p.condition === this)
  908. return true;
  909. // -(yield 4)
  910. if (p instanceof AST_Unary)
  911. return true;
  912. // (yield x).foo
  913. // (yield x)['foo']
  914. if (p instanceof AST_PropAccess && p.expression === this)
  915. return true;
  916. });
  917. PARENS(AST_PropAccess, function(output) {
  918. var p = output.parent();
  919. if (p instanceof AST_New && p.expression === this) {
  920. // i.e. new (foo.bar().baz)
  921. //
  922. // if there's one call into this subtree, then we need
  923. // parens around it too, otherwise the call will be
  924. // interpreted as passing the arguments to the upper New
  925. // expression.
  926. return walk(this, node => {
  927. if (node instanceof AST_Scope) return true;
  928. if (node instanceof AST_Call) {
  929. return walk_abort; // makes walk() return true.
  930. }
  931. });
  932. }
  933. });
  934. PARENS(AST_Call, function(output) {
  935. var p = output.parent(), p1;
  936. if (p instanceof AST_New && p.expression === this
  937. || p instanceof AST_Export && p.is_default && this.expression instanceof AST_Function)
  938. return true;
  939. // workaround for Safari bug.
  940. // https://bugs.webkit.org/show_bug.cgi?id=123506
  941. return this.expression instanceof AST_Function
  942. && p instanceof AST_PropAccess
  943. && p.expression === this
  944. && (p1 = output.parent(1)) instanceof AST_Assign
  945. && p1.left === p;
  946. });
  947. PARENS(AST_New, function(output) {
  948. var p = output.parent();
  949. if (this.args.length === 0
  950. && (p instanceof AST_PropAccess // (new Date).getTime(), (new Date)["getTime"]()
  951. || p instanceof AST_Call && p.expression === this)) // (new foo)(bar)
  952. return true;
  953. });
  954. PARENS(AST_Number, function(output) {
  955. var p = output.parent();
  956. if (p instanceof AST_PropAccess && p.expression === this) {
  957. var value = this.getValue();
  958. if (value < 0 || /^0/.test(make_num(value))) {
  959. return true;
  960. }
  961. }
  962. });
  963. PARENS(AST_BigInt, function(output) {
  964. var p = output.parent();
  965. if (p instanceof AST_PropAccess && p.expression === this) {
  966. var value = this.getValue();
  967. if (value.startsWith("-")) {
  968. return true;
  969. }
  970. }
  971. });
  972. PARENS([ AST_Assign, AST_Conditional ], function(output) {
  973. var p = output.parent();
  974. // !(a = false) → true
  975. if (p instanceof AST_Unary)
  976. return true;
  977. // 1 + (a = 2) + 3 → 6, side effect setting a = 2
  978. if (p instanceof AST_Binary && !(p instanceof AST_Assign))
  979. return true;
  980. // (a = func)() —or— new (a = Object)()
  981. if (p instanceof AST_Call && p.expression === this)
  982. return true;
  983. // (a = foo) ? bar : baz
  984. if (p instanceof AST_Conditional && p.condition === this)
  985. return true;
  986. // (a = foo)["prop"] —or— (a = foo).prop
  987. if (p instanceof AST_PropAccess && p.expression === this)
  988. return true;
  989. // ({a, b} = {a: 1, b: 2}), a destructuring assignment
  990. if (this instanceof AST_Assign && this.left instanceof AST_Destructuring && this.left.is_array === false)
  991. return true;
  992. });
  993. /* -----[ PRINTERS ]----- */
  994. DEFPRINT(AST_Directive, function(self, output) {
  995. output.print_string(self.value, self.quote);
  996. output.semicolon();
  997. });
  998. DEFPRINT(AST_Expansion, function (self, output) {
  999. output.print("...");
  1000. self.expression.print(output);
  1001. });
  1002. DEFPRINT(AST_Destructuring, function (self, output) {
  1003. output.print(self.is_array ? "[" : "{");
  1004. var len = self.names.length;
  1005. self.names.forEach(function (name, i) {
  1006. if (i > 0) output.comma();
  1007. name.print(output);
  1008. // If the final element is a hole, we need to make sure it
  1009. // doesn't look like a trailing comma, by inserting an actual
  1010. // trailing comma.
  1011. if (i == len - 1 && name instanceof AST_Hole) output.comma();
  1012. });
  1013. output.print(self.is_array ? "]" : "}");
  1014. });
  1015. DEFPRINT(AST_Debugger, function(self, output) {
  1016. output.print("debugger");
  1017. output.semicolon();
  1018. });
  1019. /* -----[ statements ]----- */
  1020. function display_body(body, is_toplevel, output, allow_directives) {
  1021. var last = body.length - 1;
  1022. output.in_directive = allow_directives;
  1023. body.forEach(function(stmt, i) {
  1024. if (output.in_directive === true && !(stmt instanceof AST_Directive ||
  1025. stmt instanceof AST_EmptyStatement ||
  1026. (stmt instanceof AST_SimpleStatement && stmt.body instanceof AST_String)
  1027. )) {
  1028. output.in_directive = false;
  1029. }
  1030. if (!(stmt instanceof AST_EmptyStatement)) {
  1031. output.indent();
  1032. stmt.print(output);
  1033. if (!(i == last && is_toplevel)) {
  1034. output.newline();
  1035. if (is_toplevel) output.newline();
  1036. }
  1037. }
  1038. if (output.in_directive === true &&
  1039. stmt instanceof AST_SimpleStatement &&
  1040. stmt.body instanceof AST_String
  1041. ) {
  1042. output.in_directive = false;
  1043. }
  1044. });
  1045. output.in_directive = false;
  1046. }
  1047. AST_StatementWithBody.DEFMETHOD("_do_print_body", function(output) {
  1048. force_statement(this.body, output);
  1049. });
  1050. DEFPRINT(AST_Statement, function(self, output) {
  1051. self.body.print(output);
  1052. output.semicolon();
  1053. });
  1054. DEFPRINT(AST_Toplevel, function(self, output) {
  1055. display_body(self.body, true, output, true);
  1056. output.print("");
  1057. });
  1058. DEFPRINT(AST_LabeledStatement, function(self, output) {
  1059. self.label.print(output);
  1060. output.colon();
  1061. self.body.print(output);
  1062. });
  1063. DEFPRINT(AST_SimpleStatement, function(self, output) {
  1064. self.body.print(output);
  1065. output.semicolon();
  1066. });
  1067. function print_braced_empty(self, output) {
  1068. output.print("{");
  1069. output.with_indent(output.next_indent(), function() {
  1070. output.append_comments(self, true);
  1071. });
  1072. output.print("}");
  1073. }
  1074. function print_braced(self, output, allow_directives) {
  1075. if (self.body.length > 0) {
  1076. output.with_block(function() {
  1077. display_body(self.body, false, output, allow_directives);
  1078. });
  1079. } else print_braced_empty(self, output);
  1080. }
  1081. DEFPRINT(AST_BlockStatement, function(self, output) {
  1082. print_braced(self, output);
  1083. });
  1084. DEFPRINT(AST_EmptyStatement, function(self, output) {
  1085. output.semicolon();
  1086. });
  1087. DEFPRINT(AST_Do, function(self, output) {
  1088. output.print("do");
  1089. output.space();
  1090. make_block(self.body, output);
  1091. output.space();
  1092. output.print("while");
  1093. output.space();
  1094. output.with_parens(function() {
  1095. self.condition.print(output);
  1096. });
  1097. output.semicolon();
  1098. });
  1099. DEFPRINT(AST_While, function(self, output) {
  1100. output.print("while");
  1101. output.space();
  1102. output.with_parens(function() {
  1103. self.condition.print(output);
  1104. });
  1105. output.space();
  1106. self._do_print_body(output);
  1107. });
  1108. DEFPRINT(AST_For, function(self, output) {
  1109. output.print("for");
  1110. output.space();
  1111. output.with_parens(function() {
  1112. if (self.init) {
  1113. if (self.init instanceof AST_Definitions) {
  1114. self.init.print(output);
  1115. } else {
  1116. parenthesize_for_noin(self.init, output, true);
  1117. }
  1118. output.print(";");
  1119. output.space();
  1120. } else {
  1121. output.print(";");
  1122. }
  1123. if (self.condition) {
  1124. self.condition.print(output);
  1125. output.print(";");
  1126. output.space();
  1127. } else {
  1128. output.print(";");
  1129. }
  1130. if (self.step) {
  1131. self.step.print(output);
  1132. }
  1133. });
  1134. output.space();
  1135. self._do_print_body(output);
  1136. });
  1137. DEFPRINT(AST_ForIn, function(self, output) {
  1138. output.print("for");
  1139. if (self.await) {
  1140. output.space();
  1141. output.print("await");
  1142. }
  1143. output.space();
  1144. output.with_parens(function() {
  1145. self.init.print(output);
  1146. output.space();
  1147. output.print(self instanceof AST_ForOf ? "of" : "in");
  1148. output.space();
  1149. self.object.print(output);
  1150. });
  1151. output.space();
  1152. self._do_print_body(output);
  1153. });
  1154. DEFPRINT(AST_With, function(self, output) {
  1155. output.print("with");
  1156. output.space();
  1157. output.with_parens(function() {
  1158. self.expression.print(output);
  1159. });
  1160. output.space();
  1161. self._do_print_body(output);
  1162. });
  1163. /* -----[ functions ]----- */
  1164. AST_Lambda.DEFMETHOD("_do_print", function(output, nokeyword) {
  1165. var self = this;
  1166. if (!nokeyword) {
  1167. if (self.async) {
  1168. output.print("async");
  1169. output.space();
  1170. }
  1171. output.print("function");
  1172. if (self.is_generator) {
  1173. output.star();
  1174. }
  1175. if (self.name) {
  1176. output.space();
  1177. }
  1178. }
  1179. if (self.name instanceof AST_Symbol) {
  1180. self.name.print(output);
  1181. } else if (nokeyword && self.name instanceof AST_Node) {
  1182. output.with_square(function() {
  1183. self.name.print(output); // Computed method name
  1184. });
  1185. }
  1186. output.with_parens(function() {
  1187. self.argnames.forEach(function(arg, i) {
  1188. if (i) output.comma();
  1189. arg.print(output);
  1190. });
  1191. });
  1192. output.space();
  1193. print_braced(self, output, true);
  1194. });
  1195. DEFPRINT(AST_Lambda, function(self, output) {
  1196. self._do_print(output);
  1197. });
  1198. DEFPRINT(AST_PrefixedTemplateString, function(self, output) {
  1199. var tag = self.prefix;
  1200. var parenthesize_tag = tag instanceof AST_Lambda
  1201. || tag instanceof AST_Binary
  1202. || tag instanceof AST_Conditional
  1203. || tag instanceof AST_Sequence
  1204. || tag instanceof AST_Unary
  1205. || tag instanceof AST_Dot && tag.expression instanceof AST_Object;
  1206. if (parenthesize_tag) output.print("(");
  1207. self.prefix.print(output);
  1208. if (parenthesize_tag) output.print(")");
  1209. self.template_string.print(output);
  1210. });
  1211. DEFPRINT(AST_TemplateString, function(self, output) {
  1212. var is_tagged = output.parent() instanceof AST_PrefixedTemplateString;
  1213. output.print("`");
  1214. for (var i = 0; i < self.segments.length; i++) {
  1215. if (!(self.segments[i] instanceof AST_TemplateSegment)) {
  1216. output.print("${");
  1217. self.segments[i].print(output);
  1218. output.print("}");
  1219. } else if (is_tagged) {
  1220. output.print(self.segments[i].raw);
  1221. } else {
  1222. output.print_template_string_chars(self.segments[i].value);
  1223. }
  1224. }
  1225. output.print("`");
  1226. });
  1227. AST_Arrow.DEFMETHOD("_do_print", function(output) {
  1228. var self = this;
  1229. var parent = output.parent();
  1230. var needs_parens = (parent instanceof AST_Binary && !(parent instanceof AST_Assign)) ||
  1231. parent instanceof AST_Unary ||
  1232. (parent instanceof AST_Call && self === parent.expression);
  1233. if (needs_parens) { output.print("("); }
  1234. if (self.async) {
  1235. output.print("async");
  1236. output.space();
  1237. }
  1238. if (self.argnames.length === 1 && self.argnames[0] instanceof AST_Symbol) {
  1239. self.argnames[0].print(output);
  1240. } else {
  1241. output.with_parens(function() {
  1242. self.argnames.forEach(function(arg, i) {
  1243. if (i) output.comma();
  1244. arg.print(output);
  1245. });
  1246. });
  1247. }
  1248. output.space();
  1249. output.print("=>");
  1250. output.space();
  1251. const first_statement = self.body[0];
  1252. if (
  1253. self.body.length === 1
  1254. && first_statement instanceof AST_Return
  1255. ) {
  1256. const returned = first_statement.value;
  1257. if (!returned) {
  1258. output.print("{}");
  1259. } else if (left_is_object(returned)) {
  1260. output.print("(");
  1261. returned.print(output);
  1262. output.print(")");
  1263. } else {
  1264. returned.print(output);
  1265. }
  1266. } else {
  1267. print_braced(self, output);
  1268. }
  1269. if (needs_parens) { output.print(")"); }
  1270. });
  1271. /* -----[ exits ]----- */
  1272. AST_Exit.DEFMETHOD("_do_print", function(output, kind) {
  1273. output.print(kind);
  1274. if (this.value) {
  1275. output.space();
  1276. const comments = this.value.start.comments_before;
  1277. if (comments && comments.length && !output.printed_comments.has(comments)) {
  1278. output.print("(");
  1279. this.value.print(output);
  1280. output.print(")");
  1281. } else {
  1282. this.value.print(output);
  1283. }
  1284. }
  1285. output.semicolon();
  1286. });
  1287. DEFPRINT(AST_Return, function(self, output) {
  1288. self._do_print(output, "return");
  1289. });
  1290. DEFPRINT(AST_Throw, function(self, output) {
  1291. self._do_print(output, "throw");
  1292. });
  1293. /* -----[ yield ]----- */
  1294. DEFPRINT(AST_Yield, function(self, output) {
  1295. var star = self.is_star ? "*" : "";
  1296. output.print("yield" + star);
  1297. if (self.expression) {
  1298. output.space();
  1299. self.expression.print(output);
  1300. }
  1301. });
  1302. DEFPRINT(AST_Await, function(self, output) {
  1303. output.print("await");
  1304. output.space();
  1305. var e = self.expression;
  1306. var parens = !(
  1307. e instanceof AST_Call
  1308. || e instanceof AST_SymbolRef
  1309. || e instanceof AST_PropAccess
  1310. || e instanceof AST_Unary
  1311. || e instanceof AST_Constant
  1312. || e instanceof AST_Await
  1313. || e instanceof AST_Object
  1314. );
  1315. if (parens) output.print("(");
  1316. self.expression.print(output);
  1317. if (parens) output.print(")");
  1318. });
  1319. /* -----[ loop control ]----- */
  1320. AST_LoopControl.DEFMETHOD("_do_print", function(output, kind) {
  1321. output.print(kind);
  1322. if (this.label) {
  1323. output.space();
  1324. this.label.print(output);
  1325. }
  1326. output.semicolon();
  1327. });
  1328. DEFPRINT(AST_Break, function(self, output) {
  1329. self._do_print(output, "break");
  1330. });
  1331. DEFPRINT(AST_Continue, function(self, output) {
  1332. self._do_print(output, "continue");
  1333. });
  1334. /* -----[ if ]----- */
  1335. function make_then(self, output) {
  1336. var b = self.body;
  1337. if (output.option("braces")
  1338. || output.option("ie8") && b instanceof AST_Do)
  1339. return make_block(b, output);
  1340. // The squeezer replaces "block"-s that contain only a single
  1341. // statement with the statement itself; technically, the AST
  1342. // is correct, but this can create problems when we output an
  1343. // IF having an ELSE clause where the THEN clause ends in an
  1344. // IF *without* an ELSE block (then the outer ELSE would refer
  1345. // to the inner IF). This function checks for this case and
  1346. // adds the block braces if needed.
  1347. if (!b) return output.force_semicolon();
  1348. while (true) {
  1349. if (b instanceof AST_If) {
  1350. if (!b.alternative) {
  1351. make_block(self.body, output);
  1352. return;
  1353. }
  1354. b = b.alternative;
  1355. } else if (b instanceof AST_StatementWithBody) {
  1356. b = b.body;
  1357. } else break;
  1358. }
  1359. force_statement(self.body, output);
  1360. }
  1361. DEFPRINT(AST_If, function(self, output) {
  1362. output.print("if");
  1363. output.space();
  1364. output.with_parens(function() {
  1365. self.condition.print(output);
  1366. });
  1367. output.space();
  1368. if (self.alternative) {
  1369. make_then(self, output);
  1370. output.space();
  1371. output.print("else");
  1372. output.space();
  1373. if (self.alternative instanceof AST_If)
  1374. self.alternative.print(output);
  1375. else
  1376. force_statement(self.alternative, output);
  1377. } else {
  1378. self._do_print_body(output);
  1379. }
  1380. });
  1381. /* -----[ switch ]----- */
  1382. DEFPRINT(AST_Switch, function(self, output) {
  1383. output.print("switch");
  1384. output.space();
  1385. output.with_parens(function() {
  1386. self.expression.print(output);
  1387. });
  1388. output.space();
  1389. var last = self.body.length - 1;
  1390. if (last < 0) print_braced_empty(self, output);
  1391. else output.with_block(function() {
  1392. self.body.forEach(function(branch, i) {
  1393. output.indent(true);
  1394. branch.print(output);
  1395. if (i < last && branch.body.length > 0)
  1396. output.newline();
  1397. });
  1398. });
  1399. });
  1400. AST_SwitchBranch.DEFMETHOD("_do_print_body", function(output) {
  1401. output.newline();
  1402. this.body.forEach(function(stmt) {
  1403. output.indent();
  1404. stmt.print(output);
  1405. output.newline();
  1406. });
  1407. });
  1408. DEFPRINT(AST_Default, function(self, output) {
  1409. output.print("default:");
  1410. self._do_print_body(output);
  1411. });
  1412. DEFPRINT(AST_Case, function(self, output) {
  1413. output.print("case");
  1414. output.space();
  1415. self.expression.print(output);
  1416. output.print(":");
  1417. self._do_print_body(output);
  1418. });
  1419. /* -----[ exceptions ]----- */
  1420. DEFPRINT(AST_Try, function(self, output) {
  1421. output.print("try");
  1422. output.space();
  1423. print_braced(self, output);
  1424. if (self.bcatch) {
  1425. output.space();
  1426. self.bcatch.print(output);
  1427. }
  1428. if (self.bfinally) {
  1429. output.space();
  1430. self.bfinally.print(output);
  1431. }
  1432. });
  1433. DEFPRINT(AST_Catch, function(self, output) {
  1434. output.print("catch");
  1435. if (self.argname) {
  1436. output.space();
  1437. output.with_parens(function() {
  1438. self.argname.print(output);
  1439. });
  1440. }
  1441. output.space();
  1442. print_braced(self, output);
  1443. });
  1444. DEFPRINT(AST_Finally, function(self, output) {
  1445. output.print("finally");
  1446. output.space();
  1447. print_braced(self, output);
  1448. });
  1449. /* -----[ var/const ]----- */
  1450. AST_Definitions.DEFMETHOD("_do_print", function(output, kind) {
  1451. output.print(kind);
  1452. output.space();
  1453. this.definitions.forEach(function(def, i) {
  1454. if (i) output.comma();
  1455. def.print(output);
  1456. });
  1457. var p = output.parent();
  1458. var in_for = p instanceof AST_For || p instanceof AST_ForIn;
  1459. var output_semicolon = !in_for || p && p.init !== this;
  1460. if (output_semicolon)
  1461. output.semicolon();
  1462. });
  1463. DEFPRINT(AST_Let, function(self, output) {
  1464. self._do_print(output, "let");
  1465. });
  1466. DEFPRINT(AST_Var, function(self, output) {
  1467. self._do_print(output, "var");
  1468. });
  1469. DEFPRINT(AST_Const, function(self, output) {
  1470. self._do_print(output, "const");
  1471. });
  1472. DEFPRINT(AST_Import, function(self, output) {
  1473. output.print("import");
  1474. output.space();
  1475. if (self.imported_name) {
  1476. self.imported_name.print(output);
  1477. }
  1478. if (self.imported_name && self.imported_names) {
  1479. output.print(",");
  1480. output.space();
  1481. }
  1482. if (self.imported_names) {
  1483. if (self.imported_names.length === 1 && self.imported_names[0].foreign_name.name === "*") {
  1484. self.imported_names[0].print(output);
  1485. } else {
  1486. output.print("{");
  1487. self.imported_names.forEach(function (name_import, i) {
  1488. output.space();
  1489. name_import.print(output);
  1490. if (i < self.imported_names.length - 1) {
  1491. output.print(",");
  1492. }
  1493. });
  1494. output.space();
  1495. output.print("}");
  1496. }
  1497. }
  1498. if (self.imported_name || self.imported_names) {
  1499. output.space();
  1500. output.print("from");
  1501. output.space();
  1502. }
  1503. self.module_name.print(output);
  1504. output.semicolon();
  1505. });
  1506. DEFPRINT(AST_ImportMeta, function(self, output) {
  1507. output.print("import.meta");
  1508. });
  1509. DEFPRINT(AST_NameMapping, function(self, output) {
  1510. var is_import = output.parent() instanceof AST_Import;
  1511. var definition = self.name.definition();
  1512. var names_are_different =
  1513. (definition && definition.mangled_name || self.name.name) !==
  1514. self.foreign_name.name;
  1515. if (names_are_different) {
  1516. if (is_import) {
  1517. output.print(self.foreign_name.name);
  1518. } else {
  1519. self.name.print(output);
  1520. }
  1521. output.space();
  1522. output.print("as");
  1523. output.space();
  1524. if (is_import) {
  1525. self.name.print(output);
  1526. } else {
  1527. output.print(self.foreign_name.name);
  1528. }
  1529. } else {
  1530. self.name.print(output);
  1531. }
  1532. });
  1533. DEFPRINT(AST_Export, function(self, output) {
  1534. output.print("export");
  1535. output.space();
  1536. if (self.is_default) {
  1537. output.print("default");
  1538. output.space();
  1539. }
  1540. if (self.exported_names) {
  1541. if (self.exported_names.length === 1 && self.exported_names[0].name.name === "*") {
  1542. self.exported_names[0].print(output);
  1543. } else {
  1544. output.print("{");
  1545. self.exported_names.forEach(function(name_export, i) {
  1546. output.space();
  1547. name_export.print(output);
  1548. if (i < self.exported_names.length - 1) {
  1549. output.print(",");
  1550. }
  1551. });
  1552. output.space();
  1553. output.print("}");
  1554. }
  1555. } else if (self.exported_value) {
  1556. self.exported_value.print(output);
  1557. } else if (self.exported_definition) {
  1558. self.exported_definition.print(output);
  1559. if (self.exported_definition instanceof AST_Definitions) return;
  1560. }
  1561. if (self.module_name) {
  1562. output.space();
  1563. output.print("from");
  1564. output.space();
  1565. self.module_name.print(output);
  1566. }
  1567. if (self.exported_value
  1568. && !(self.exported_value instanceof AST_Defun ||
  1569. self.exported_value instanceof AST_Function ||
  1570. self.exported_value instanceof AST_Class)
  1571. || self.module_name
  1572. || self.exported_names
  1573. ) {
  1574. output.semicolon();
  1575. }
  1576. });
  1577. function parenthesize_for_noin(node, output, noin) {
  1578. var parens = false;
  1579. // need to take some precautions here:
  1580. // https://github.com/mishoo/UglifyJS2/issues/60
  1581. if (noin) {
  1582. parens = walk(node, node => {
  1583. if (node instanceof AST_Scope) return true;
  1584. if (node instanceof AST_Binary && node.operator == "in") {
  1585. return walk_abort; // makes walk() return true
  1586. }
  1587. });
  1588. }
  1589. node.print(output, parens);
  1590. }
  1591. DEFPRINT(AST_VarDef, function(self, output) {
  1592. self.name.print(output);
  1593. if (self.value) {
  1594. output.space();
  1595. output.print("=");
  1596. output.space();
  1597. var p = output.parent(1);
  1598. var noin = p instanceof AST_For || p instanceof AST_ForIn;
  1599. parenthesize_for_noin(self.value, output, noin);
  1600. }
  1601. });
  1602. /* -----[ other expressions ]----- */
  1603. DEFPRINT(AST_Call, function(self, output) {
  1604. self.expression.print(output);
  1605. if (self instanceof AST_New && self.args.length === 0)
  1606. return;
  1607. if (self.expression instanceof AST_Call || self.expression instanceof AST_Lambda) {
  1608. output.add_mapping(self.start);
  1609. }
  1610. if (self.optional) output.print("?.");
  1611. output.with_parens(function() {
  1612. self.args.forEach(function(expr, i) {
  1613. if (i) output.comma();
  1614. expr.print(output);
  1615. });
  1616. });
  1617. });
  1618. DEFPRINT(AST_New, function(self, output) {
  1619. output.print("new");
  1620. output.space();
  1621. AST_Call.prototype._codegen(self, output);
  1622. });
  1623. AST_Sequence.DEFMETHOD("_do_print", function(output) {
  1624. this.expressions.forEach(function(node, index) {
  1625. if (index > 0) {
  1626. output.comma();
  1627. if (output.should_break()) {
  1628. output.newline();
  1629. output.indent();
  1630. }
  1631. }
  1632. node.print(output);
  1633. });
  1634. });
  1635. DEFPRINT(AST_Sequence, function(self, output) {
  1636. self._do_print(output);
  1637. // var p = output.parent();
  1638. // if (p instanceof AST_Statement) {
  1639. // output.with_indent(output.next_indent(), function(){
  1640. // self._do_print(output);
  1641. // });
  1642. // } else {
  1643. // self._do_print(output);
  1644. // }
  1645. });
  1646. DEFPRINT(AST_Dot, function(self, output) {
  1647. var expr = self.expression;
  1648. expr.print(output);
  1649. var prop = self.property;
  1650. var print_computed = RESERVED_WORDS.has(prop)
  1651. ? output.option("ie8")
  1652. : !is_identifier_string(prop, output.option("ecma") >= 2015);
  1653. if (self.optional) output.print("?.");
  1654. if (print_computed) {
  1655. output.print("[");
  1656. output.add_mapping(self.end);
  1657. output.print_string(prop);
  1658. output.print("]");
  1659. } else {
  1660. if (expr instanceof AST_Number && expr.getValue() >= 0) {
  1661. if (!/[xa-f.)]/i.test(output.last())) {
  1662. output.print(".");
  1663. }
  1664. }
  1665. if (!self.optional) output.print(".");
  1666. // the name after dot would be mapped about here.
  1667. output.add_mapping(self.end);
  1668. output.print_name(prop);
  1669. }
  1670. });
  1671. DEFPRINT(AST_Sub, function(self, output) {
  1672. self.expression.print(output);
  1673. if (self.optional) output.print("?.");
  1674. output.print("[");
  1675. self.property.print(output);
  1676. output.print("]");
  1677. });
  1678. DEFPRINT(AST_Chain, function(self, output) {
  1679. self.expression.print(output);
  1680. });
  1681. DEFPRINT(AST_UnaryPrefix, function(self, output) {
  1682. var op = self.operator;
  1683. output.print(op);
  1684. if (/^[a-z]/i.test(op)
  1685. || (/[+-]$/.test(op)
  1686. && self.expression instanceof AST_UnaryPrefix
  1687. && /^[+-]/.test(self.expression.operator))) {
  1688. output.space();
  1689. }
  1690. self.expression.print(output);
  1691. });
  1692. DEFPRINT(AST_UnaryPostfix, function(self, output) {
  1693. self.expression.print(output);
  1694. output.print(self.operator);
  1695. });
  1696. DEFPRINT(AST_Binary, function(self, output) {
  1697. var op = self.operator;
  1698. self.left.print(output);
  1699. if (op[0] == ">" /* ">>" ">>>" ">" ">=" */
  1700. && self.left instanceof AST_UnaryPostfix
  1701. && self.left.operator == "--") {
  1702. // space is mandatory to avoid outputting -->
  1703. output.print(" ");
  1704. } else {
  1705. // the space is optional depending on "beautify"
  1706. output.space();
  1707. }
  1708. output.print(op);
  1709. if ((op == "<" || op == "<<")
  1710. && self.right instanceof AST_UnaryPrefix
  1711. && self.right.operator == "!"
  1712. && self.right.expression instanceof AST_UnaryPrefix
  1713. && self.right.expression.operator == "--") {
  1714. // space is mandatory to avoid outputting <!--
  1715. output.print(" ");
  1716. } else {
  1717. // the space is optional depending on "beautify"
  1718. output.space();
  1719. }
  1720. self.right.print(output);
  1721. });
  1722. DEFPRINT(AST_Conditional, function(self, output) {
  1723. self.condition.print(output);
  1724. output.space();
  1725. output.print("?");
  1726. output.space();
  1727. self.consequent.print(output);
  1728. output.space();
  1729. output.colon();
  1730. self.alternative.print(output);
  1731. });
  1732. /* -----[ literals ]----- */
  1733. DEFPRINT(AST_Array, function(self, output) {
  1734. output.with_square(function() {
  1735. var a = self.elements, len = a.length;
  1736. if (len > 0) output.space();
  1737. a.forEach(function(exp, i) {
  1738. if (i) output.comma();
  1739. exp.print(output);
  1740. // If the final element is a hole, we need to make sure it
  1741. // doesn't look like a trailing comma, by inserting an actual
  1742. // trailing comma.
  1743. if (i === len - 1 && exp instanceof AST_Hole)
  1744. output.comma();
  1745. });
  1746. if (len > 0) output.space();
  1747. });
  1748. });
  1749. DEFPRINT(AST_Object, function(self, output) {
  1750. if (self.properties.length > 0) output.with_block(function() {
  1751. self.properties.forEach(function(prop, i) {
  1752. if (i) {
  1753. output.print(",");
  1754. output.newline();
  1755. }
  1756. output.indent();
  1757. prop.print(output);
  1758. });
  1759. output.newline();
  1760. });
  1761. else print_braced_empty(self, output);
  1762. });
  1763. DEFPRINT(AST_Class, function(self, output) {
  1764. output.print("class");
  1765. output.space();
  1766. if (self.name) {
  1767. self.name.print(output);
  1768. output.space();
  1769. }
  1770. if (self.extends) {
  1771. var parens = (
  1772. !(self.extends instanceof AST_SymbolRef)
  1773. && !(self.extends instanceof AST_PropAccess)
  1774. && !(self.extends instanceof AST_ClassExpression)
  1775. && !(self.extends instanceof AST_Function)
  1776. );
  1777. output.print("extends");
  1778. if (parens) {
  1779. output.print("(");
  1780. } else {
  1781. output.space();
  1782. }
  1783. self.extends.print(output);
  1784. if (parens) {
  1785. output.print(")");
  1786. } else {
  1787. output.space();
  1788. }
  1789. }
  1790. if (self.properties.length > 0) output.with_block(function() {
  1791. self.properties.forEach(function(prop, i) {
  1792. if (i) {
  1793. output.newline();
  1794. }
  1795. output.indent();
  1796. prop.print(output);
  1797. });
  1798. output.newline();
  1799. });
  1800. else output.print("{}");
  1801. });
  1802. DEFPRINT(AST_NewTarget, function(self, output) {
  1803. output.print("new.target");
  1804. });
  1805. function print_property_name(key, quote, output) {
  1806. if (output.option("quote_keys")) {
  1807. return output.print_string(key);
  1808. }
  1809. if ("" + +key == key && key >= 0) {
  1810. if (output.option("keep_numbers")) {
  1811. return output.print(key);
  1812. }
  1813. return output.print(make_num(key));
  1814. }
  1815. var print_string = RESERVED_WORDS.has(key)
  1816. ? output.option("ie8")
  1817. : (
  1818. output.option("ecma") < 2015
  1819. ? !is_basic_identifier_string(key)
  1820. : !is_identifier_string(key, true)
  1821. );
  1822. if (print_string || (quote && output.option("keep_quoted_props"))) {
  1823. return output.print_string(key, quote);
  1824. }
  1825. return output.print_name(key);
  1826. }
  1827. DEFPRINT(AST_ObjectKeyVal, function(self, output) {
  1828. function get_name(self) {
  1829. var def = self.definition();
  1830. return def ? def.mangled_name || def.name : self.name;
  1831. }
  1832. var allowShortHand = output.option("shorthand");
  1833. if (allowShortHand &&
  1834. self.value instanceof AST_Symbol &&
  1835. is_identifier_string(self.key, output.option("ecma") >= 2015) &&
  1836. get_name(self.value) === self.key &&
  1837. !RESERVED_WORDS.has(self.key)
  1838. ) {
  1839. print_property_name(self.key, self.quote, output);
  1840. } else if (allowShortHand &&
  1841. self.value instanceof AST_DefaultAssign &&
  1842. self.value.left instanceof AST_Symbol &&
  1843. is_identifier_string(self.key, output.option("ecma") >= 2015) &&
  1844. get_name(self.value.left) === self.key
  1845. ) {
  1846. print_property_name(self.key, self.quote, output);
  1847. output.space();
  1848. output.print("=");
  1849. output.space();
  1850. self.value.right.print(output);
  1851. } else {
  1852. if (!(self.key instanceof AST_Node)) {
  1853. print_property_name(self.key, self.quote, output);
  1854. } else {
  1855. output.with_square(function() {
  1856. self.key.print(output);
  1857. });
  1858. }
  1859. output.colon();
  1860. self.value.print(output);
  1861. }
  1862. });
  1863. DEFPRINT(AST_ClassProperty, (self, output) => {
  1864. if (self.static) {
  1865. output.print("static");
  1866. output.space();
  1867. }
  1868. if (self.key instanceof AST_SymbolClassProperty) {
  1869. print_property_name(self.key.name, self.quote, output);
  1870. } else {
  1871. output.print("[");
  1872. self.key.print(output);
  1873. output.print("]");
  1874. }
  1875. if (self.value) {
  1876. output.print("=");
  1877. self.value.print(output);
  1878. }
  1879. output.semicolon();
  1880. });
  1881. AST_ObjectProperty.DEFMETHOD("_print_getter_setter", function(type, output) {
  1882. var self = this;
  1883. if (self.static) {
  1884. output.print("static");
  1885. output.space();
  1886. }
  1887. if (type) {
  1888. output.print(type);
  1889. output.space();
  1890. }
  1891. if (self.key instanceof AST_SymbolMethod) {
  1892. print_property_name(self.key.name, self.quote, output);
  1893. } else {
  1894. output.with_square(function() {
  1895. self.key.print(output);
  1896. });
  1897. }
  1898. self.value._do_print(output, true);
  1899. });
  1900. DEFPRINT(AST_ObjectSetter, function(self, output) {
  1901. self._print_getter_setter("set", output);
  1902. });
  1903. DEFPRINT(AST_ObjectGetter, function(self, output) {
  1904. self._print_getter_setter("get", output);
  1905. });
  1906. DEFPRINT(AST_ConciseMethod, function(self, output) {
  1907. var type;
  1908. if (self.is_generator && self.async) {
  1909. type = "async*";
  1910. } else if (self.is_generator) {
  1911. type = "*";
  1912. } else if (self.async) {
  1913. type = "async";
  1914. }
  1915. self._print_getter_setter(type, output);
  1916. });
  1917. AST_Symbol.DEFMETHOD("_do_print", function(output) {
  1918. var def = this.definition();
  1919. output.print_name(def ? def.mangled_name || def.name : this.name);
  1920. });
  1921. DEFPRINT(AST_Symbol, function (self, output) {
  1922. self._do_print(output);
  1923. });
  1924. DEFPRINT(AST_Hole, noop);
  1925. DEFPRINT(AST_This, function(self, output) {
  1926. output.print("this");
  1927. });
  1928. DEFPRINT(AST_Super, function(self, output) {
  1929. output.print("super");
  1930. });
  1931. DEFPRINT(AST_Constant, function(self, output) {
  1932. output.print(self.getValue());
  1933. });
  1934. DEFPRINT(AST_String, function(self, output) {
  1935. output.print_string(self.getValue(), self.quote, output.in_directive);
  1936. });
  1937. DEFPRINT(AST_Number, function(self, output) {
  1938. if ((output.option("keep_numbers") || output.use_asm) && self.start && self.start.raw != null) {
  1939. output.print(self.start.raw);
  1940. } else {
  1941. output.print(make_num(self.getValue()));
  1942. }
  1943. });
  1944. DEFPRINT(AST_BigInt, function(self, output) {
  1945. output.print(self.getValue() + "n");
  1946. });
  1947. const r_slash_script = /(<\s*\/\s*script)/i;
  1948. const slash_script_replace = (_, $1) => $1.replace("/", "\\/");
  1949. DEFPRINT(AST_RegExp, function(self, output) {
  1950. let { source, flags } = self.getValue();
  1951. source = regexp_source_fix(source);
  1952. flags = flags ? sort_regexp_flags(flags) : "";
  1953. source = source.replace(r_slash_script, slash_script_replace);
  1954. output.print(output.to_utf8(`/${source}/${flags}`));
  1955. const parent = output.parent();
  1956. if (
  1957. parent instanceof AST_Binary
  1958. && /^\w/.test(parent.operator)
  1959. && parent.left === self
  1960. ) {
  1961. output.print(" ");
  1962. }
  1963. });
  1964. function force_statement(stat, output) {
  1965. if (output.option("braces")) {
  1966. make_block(stat, output);
  1967. } else {
  1968. if (!stat || stat instanceof AST_EmptyStatement)
  1969. output.force_semicolon();
  1970. else
  1971. stat.print(output);
  1972. }
  1973. }
  1974. function best_of(a) {
  1975. var best = a[0], len = best.length;
  1976. for (var i = 1; i < a.length; ++i) {
  1977. if (a[i].length < len) {
  1978. best = a[i];
  1979. len = best.length;
  1980. }
  1981. }
  1982. return best;
  1983. }
  1984. function make_num(num) {
  1985. var str = num.toString(10).replace(/^0\./, ".").replace("e+", "e");
  1986. var candidates = [ str ];
  1987. if (Math.floor(num) === num) {
  1988. if (num < 0) {
  1989. candidates.push("-0x" + (-num).toString(16).toLowerCase());
  1990. } else {
  1991. candidates.push("0x" + num.toString(16).toLowerCase());
  1992. }
  1993. }
  1994. var match, len, digits;
  1995. if (match = /^\.0+/.exec(str)) {
  1996. len = match[0].length;
  1997. digits = str.slice(len);
  1998. candidates.push(digits + "e-" + (digits.length + len - 1));
  1999. } else if (match = /0+$/.exec(str)) {
  2000. len = match[0].length;
  2001. candidates.push(str.slice(0, -len) + "e" + len);
  2002. } else if (match = /^(\d)\.(\d+)e(-?\d+)$/.exec(str)) {
  2003. candidates.push(match[1] + match[2] + "e" + (match[3] - match[2].length));
  2004. }
  2005. return best_of(candidates);
  2006. }
  2007. function make_block(stmt, output) {
  2008. if (!stmt || stmt instanceof AST_EmptyStatement)
  2009. output.print("{}");
  2010. else if (stmt instanceof AST_BlockStatement)
  2011. stmt.print(output);
  2012. else output.with_block(function() {
  2013. output.indent();
  2014. stmt.print(output);
  2015. output.newline();
  2016. });
  2017. }
  2018. /* -----[ source map generators ]----- */
  2019. function DEFMAP(nodetype, generator) {
  2020. nodetype.forEach(function(nodetype) {
  2021. nodetype.DEFMETHOD("add_source_map", generator);
  2022. });
  2023. }
  2024. DEFMAP([
  2025. // We could easily add info for ALL nodes, but it seems to me that
  2026. // would be quite wasteful, hence this noop in the base class.
  2027. AST_Node,
  2028. // since the label symbol will mark it
  2029. AST_LabeledStatement,
  2030. AST_Toplevel,
  2031. ], noop);
  2032. // XXX: I'm not exactly sure if we need it for all of these nodes,
  2033. // or if we should add even more.
  2034. DEFMAP([
  2035. AST_Array,
  2036. AST_BlockStatement,
  2037. AST_Catch,
  2038. AST_Class,
  2039. AST_Constant,
  2040. AST_Debugger,
  2041. AST_Definitions,
  2042. AST_Directive,
  2043. AST_Finally,
  2044. AST_Jump,
  2045. AST_Lambda,
  2046. AST_New,
  2047. AST_Object,
  2048. AST_StatementWithBody,
  2049. AST_Symbol,
  2050. AST_Switch,
  2051. AST_SwitchBranch,
  2052. AST_TemplateString,
  2053. AST_TemplateSegment,
  2054. AST_Try,
  2055. ], function(output) {
  2056. output.add_mapping(this.start);
  2057. });
  2058. DEFMAP([
  2059. AST_ObjectGetter,
  2060. AST_ObjectSetter,
  2061. ], function(output) {
  2062. output.add_mapping(this.start, this.key.name);
  2063. });
  2064. DEFMAP([ AST_ObjectProperty ], function(output) {
  2065. output.add_mapping(this.start, this.key);
  2066. });
  2067. })();
  2068. export {
  2069. OutputStream,
  2070. };