output.js 47 KB

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