a zip code crypto-currency system good for red ONLY

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