output.js 48 KB

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