output.js 79 KB

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