beautify.js 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
  2. /*
  3. The MIT License (MIT)
  4. Copyright (c) 2007-2013 Einar Lielmanis and contributors.
  5. Permission is hereby granted, free of charge, to any person
  6. obtaining a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. SOFTWARE.
  22. JS Beautifier
  23. ---------------
  24. Written by Einar Lielmanis, <[email protected]>
  25. http://jsbeautifier.org/
  26. Originally converted to javascript by Vital, <[email protected]>
  27. "End braces on own line" added by Chris J. Shull, <[email protected]>
  28. Parsing improvements for brace-less statements by Liam Newman <[email protected]>
  29. Usage:
  30. js_beautify(js_source_text);
  31. js_beautify(js_source_text, options);
  32. The options are:
  33. indent_size (default 4) - indentation size,
  34. indent_char (default space) - character to indent with,
  35. preserve_newlines (default true) - whether existing line breaks should be preserved,
  36. max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk,
  37. jslint_happy (default false) - if true, then jslint-stricter mode is enforced.
  38. jslint_happy !jslint_happy
  39. ---------------------------------
  40. function () function()
  41. brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "expand-strict"
  42. put braces on the same line as control statements (default), or put braces on own line (Allman / ANSI style), or just put end braces on own line.
  43. expand-strict: put brace on own line even in such cases:
  44. var a =
  45. {
  46. a: 5,
  47. b: 6
  48. }
  49. This mode may break your scripts - e.g "return { a: 1 }" will be broken into two lines, so beware.
  50. space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)",
  51. unescape_strings (default false) - should printable characters in strings encoded in \xNN notation be unescaped, "example" vs "\x65\x78\x61\x6d\x70\x6c\x65"
  52. wrap_line_length (default unlimited) - lines should wrap at next opportunity after this number of characters.
  53. NOTE: This is not a hard limit. Lines will continue until a point where a newline would
  54. be preserved if it were present.
  55. e.g
  56. js_beautify(js_source_text, {
  57. 'indent_size': 1,
  58. 'indent_char': '\t'
  59. });
  60. */
  61. (function() {
  62. function js_beautify(js_source_text, options) {
  63. "use strict";
  64. var beautifier = new Beautifier(js_source_text, options);
  65. return beautifier.beautify();
  66. }
  67. function Beautifier(js_source_text, options) {
  68. "use strict";
  69. var input, output, token_text, token_type, last_type, last_last_text, indent_string;
  70. var flags, previous_flags, flag_store;
  71. var whitespace, wordchar, punct, parser_pos, line_starters, digits;
  72. var prefix;
  73. var wanted_newline, n_newlines, output_wrapped, output_space_before_token, whitespace_before_token;
  74. var input_length;
  75. var handlers, MODE, opt;
  76. var preindent_string = '';
  77. whitespace = "\n\r\t ".split('');
  78. wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split('');
  79. digits = '0123456789'.split('');
  80. punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::';
  81. punct += ' <%= <% %> <?= <? ?>'; // try to be a good boy and try not to break the markup language identifiers
  82. punct = punct.split(' ');
  83. // words which should always start on new line.
  84. line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(',');
  85. MODE = {
  86. BlockStatement: 'BlockStatement', // 'BLOCK'
  87. Statement: 'Statement', // 'STATEMENT'
  88. ObjectLiteral: 'ObjectLiteral', // 'OBJECT',
  89. ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]',
  90. ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)',
  91. Conditional: 'Conditional', //'(COND-EXPRESSION)',
  92. Expression: 'Expression' //'(EXPRESSION)'
  93. };
  94. handlers = {
  95. 'TK_START_EXPR': handle_start_expr,
  96. 'TK_END_EXPR': handle_end_expr,
  97. 'TK_START_BLOCK': handle_start_block,
  98. 'TK_END_BLOCK': handle_end_block,
  99. 'TK_WORD': handle_word,
  100. 'TK_SEMICOLON': handle_semicolon,
  101. 'TK_STRING': handle_string,
  102. 'TK_EQUALS': handle_equals,
  103. 'TK_OPERATOR': handle_operator,
  104. 'TK_COMMA': handle_comma,
  105. 'TK_BLOCK_COMMENT': handle_block_comment,
  106. 'TK_INLINE_COMMENT': handle_inline_comment,
  107. 'TK_COMMENT': handle_comment,
  108. 'TK_DOT': handle_dot,
  109. 'TK_UNKNOWN': handle_unknown
  110. };
  111. function create_flags(flags_base, mode) {
  112. return {
  113. mode: mode,
  114. last_text: flags_base ? flags_base.last_text : '', // last token text
  115. last_word: flags_base ? flags_base.last_word : '', // last 'TK_WORD' passed
  116. var_line: false,
  117. var_line_tainted: false,
  118. var_line_reindented: false,
  119. in_html_comment: false,
  120. multiline_array: false,
  121. if_block: false,
  122. do_block: false,
  123. do_while: false,
  124. in_case_statement: false, // switch(..){ INSIDE HERE }
  125. in_case: false, // we're on the exact line with "case 0:"
  126. case_body: false, // the indented case-action block
  127. indentation_level: (flags_base ? flags_base.indentation_level + ((flags_base.var_line && flags_base.var_line_reindented) ? 1 : 0) : 0),
  128. ternary_depth: 0
  129. };
  130. }
  131. // Some interpreters have unexpected results with foo = baz || bar;
  132. options = options ? options : {};
  133. opt = {};
  134. // compatibility
  135. if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) {
  136. options.jslint_happy = options.space_after_anon_function;
  137. }
  138. if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option
  139. opt.brace_style = options.braces_on_own_line ? "expand" : "collapse";
  140. }
  141. opt.brace_style = options.brace_style ? options.brace_style : (opt.brace_style ? opt.brace_style : "collapse");
  142. opt.indent_size = options.indent_size ? parseInt(options.indent_size, 10) : 4;
  143. opt.indent_char = options.indent_char ? options.indent_char : ' ';
  144. opt.preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines;
  145. opt.break_chained_methods = (options.break_chained_methods === undefined) ? false : options.break_chained_methods;
  146. opt.max_preserve_newlines = (options.max_preserve_newlines === undefined) ? 0 : parseInt(options.max_preserve_newlines, 10);
  147. opt.jslint_happy = (options.jslint_happy === undefined) ? false : options.jslint_happy;
  148. opt.keep_array_indentation = (options.keep_array_indentation === undefined) ? false : options.keep_array_indentation;
  149. opt.space_before_conditional= (options.space_before_conditional === undefined) ? true : options.space_before_conditional;
  150. opt.unescape_strings = (options.unescape_strings === undefined) ? false : options.unescape_strings;
  151. opt.wrap_line_length = (options.wrap_line_length === undefined) ? 0 : parseInt(options.wrap_line_length, 10);
  152. //----------------------------------
  153. indent_string = '';
  154. while (opt.indent_size > 0) {
  155. indent_string += opt.indent_char;
  156. opt.indent_size -= 1;
  157. }
  158. while (js_source_text && (js_source_text.charAt(0) === ' ' || js_source_text.charAt(0) === '\t')) {
  159. preindent_string += js_source_text.charAt(0);
  160. js_source_text = js_source_text.substring(1);
  161. }
  162. input = js_source_text;
  163. // cache the source's length.
  164. input_length = js_source_text.length;
  165. last_type = 'TK_START_BLOCK'; // last token type
  166. last_last_text = ''; // pre-last token text
  167. output = [];
  168. output_wrapped = false;
  169. output_space_before_token = false;
  170. whitespace_before_token = [];
  171. // Stack of parsing/formatting states, including MODE.
  172. // We tokenize, parse, and output in an almost purely a forward-only stream of token input
  173. // and formatted output. This makes the beautifier less accurate than full parsers
  174. // but also far more tolerant of syntax errors.
  175. //
  176. // For example, the default mode is MODE.BlockStatement. If we see a '{' we push a new frame of type
  177. // MODE.BlockStatement on the the stack, even though it could be object literal. If we later
  178. // encounter a ":", we'll switch to to MODE.ObjectLiteral. If we then see a ";",
  179. // most full parsers would die, but the beautifier gracefully falls back to
  180. // MODE.BlockStatement and continues on.
  181. flag_store = [];
  182. set_mode(MODE.BlockStatement);
  183. parser_pos = 0;
  184. this.beautify = function () {
  185. /*jshint onevar:true */
  186. var t, i, keep_whitespace, sweet_code;
  187. while (true) {
  188. t = get_next_token();
  189. token_text = t[0];
  190. token_type = t[1];
  191. if (token_type === 'TK_EOF') {
  192. break;
  193. }
  194. keep_whitespace = opt.keep_array_indentation && is_array(flags.mode);
  195. if (keep_whitespace) {
  196. for (i = 0; i < n_newlines; i += 1) {
  197. print_newline(true);
  198. }
  199. } else {
  200. wanted_newline = n_newlines > 0;
  201. if (opt.max_preserve_newlines && n_newlines > opt.max_preserve_newlines) {
  202. n_newlines = opt.max_preserve_newlines;
  203. }
  204. if (opt.preserve_newlines) {
  205. if (n_newlines > 1) {
  206. print_newline();
  207. for (i = 1; i < n_newlines; i += 1) {
  208. print_newline(true);
  209. }
  210. }
  211. }
  212. }
  213. handlers[token_type]();
  214. // The cleanest handling of inline comments is to treat them as though they aren't there.
  215. // Just continue formatting and the behavior should be logical.
  216. // Also ignore unknown tokens. Again, this should result in better behavior.
  217. if (token_type !== 'TK_INLINE_COMMENT' && token_type !== 'TK_COMMENT' &&
  218. token_type !== 'TK_UNKNOWN') {
  219. last_last_text = flags.last_text;
  220. last_type = token_type;
  221. flags.last_text = token_text;
  222. }
  223. }
  224. sweet_code = preindent_string + output.join('').replace(/[\r\n ]+$/, '');
  225. return sweet_code;
  226. };
  227. function trim_output(eat_newlines) {
  228. eat_newlines = (eat_newlines === undefined) ? false : eat_newlines;
  229. while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string || output[output.length - 1] === preindent_string || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) {
  230. output.pop();
  231. }
  232. }
  233. function trim(s) {
  234. return s.replace(/^\s\s*|\s\s*$/, '');
  235. }
  236. // we could use just string.split, but
  237. // IE doesn't like returning empty strings
  238. function split_newlines(s) {
  239. //return s.split(/\x0d\x0a|\x0a/);
  240. s = s.replace(/\x0d/g, '');
  241. var out = [],
  242. idx = s.indexOf("\n");
  243. while (idx !== -1) {
  244. out.push(s.substring(0, idx));
  245. s = s.substring(idx + 1);
  246. idx = s.indexOf("\n");
  247. }
  248. if (s.length) {
  249. out.push(s);
  250. }
  251. return out;
  252. }
  253. function just_added_newline() {
  254. return output.length && output[output.length - 1] === "\n";
  255. }
  256. function _last_index_of(arr, find) {
  257. var i = arr.length - 1;
  258. if (i < 0) {
  259. i += arr.length;
  260. }
  261. if (i > arr.length - 1) {
  262. i = arr.length - 1;
  263. }
  264. for (i++; i-- > 0;) {
  265. if (i in arr && arr[i] === find) {
  266. return i;
  267. }
  268. }
  269. return -1;
  270. }
  271. function allow_wrap_or_preserved_newline(force_linewrap) {
  272. force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
  273. if (opt.wrap_line_length && !force_linewrap) {
  274. var current_line = '';
  275. var proposed_line_length = 0;
  276. var start_line = _last_index_of(output, '\n') + 1;
  277. // never wrap the first token of a line.
  278. if (start_line < output.length) {
  279. current_line = output.slice(start_line).join('');
  280. proposed_line_length = current_line.length + token_text.length +
  281. (output_space_before_token ? 1 : 0);
  282. if (proposed_line_length >= opt.wrap_line_length) {
  283. force_linewrap = true;
  284. }
  285. }
  286. }
  287. if (((opt.preserve_newlines && wanted_newline) || force_linewrap) && !just_added_newline()) {
  288. print_newline(false, true);
  289. output_wrapped = true;
  290. wanted_newline = false;
  291. }
  292. }
  293. function print_newline(force_newline, preserve_statement_flags) {
  294. output_wrapped = false;
  295. output_space_before_token = false;
  296. if (!preserve_statement_flags) {
  297. if (flags.last_text !== ';') {
  298. while (flags.mode === MODE.Statement && !flags.if_block) {
  299. restore_mode();
  300. }
  301. }
  302. }
  303. if (flags.mode === MODE.ArrayLiteral) {
  304. flags.multiline_array = true;
  305. }
  306. if (!output.length) {
  307. return; // no newline on start of file
  308. }
  309. if (force_newline || !just_added_newline()) {
  310. output.push("\n");
  311. }
  312. }
  313. function print_token_line_indentation() {
  314. if (just_added_newline()) {
  315. if (opt.keep_array_indentation && is_array(flags.mode) && whitespace_before_token.length) {
  316. output.push(whitespace_before_token.join('') + '');
  317. } else {
  318. if (preindent_string) {
  319. output.push(preindent_string);
  320. }
  321. print_indent_string(flags.indentation_level);
  322. print_indent_string(flags.var_line && flags.var_line_reindented);
  323. print_indent_string(output_wrapped);
  324. }
  325. }
  326. }
  327. function print_indent_string(level) {
  328. if (level === undefined) {
  329. level = 1;
  330. } else if (typeof level !== 'number') {
  331. level = level ? 1 : 0;
  332. }
  333. // Never indent your first output indent at the start of the file
  334. if (flags.last_text !== '') {
  335. for (var i = 0; i < level; i += 1) {
  336. output.push(indent_string);
  337. }
  338. }
  339. }
  340. function print_token_space_before() {
  341. if (output_space_before_token && output.length) {
  342. var last_output = output[output.length - 1];
  343. if (!just_added_newline() && last_output !== ' ' && last_output !== indent_string) { // prevent occassional duplicate space
  344. output.push(' ');
  345. }
  346. }
  347. }
  348. function print_token(printable_token) {
  349. printable_token = printable_token || token_text;
  350. print_token_line_indentation();
  351. output_wrapped = false;
  352. print_token_space_before();
  353. output_space_before_token = false;
  354. output.push(printable_token);
  355. }
  356. function indent() {
  357. flags.indentation_level += 1;
  358. }
  359. function set_mode(mode) {
  360. if (flags) {
  361. flag_store.push(flags);
  362. previous_flags = flags;
  363. } else {
  364. previous_flags = create_flags(null, mode);
  365. }
  366. flags = create_flags(previous_flags, mode);
  367. }
  368. function is_array(mode) {
  369. return mode === MODE.ArrayLiteral;
  370. }
  371. function is_expression(mode) {
  372. return in_array(mode, [MODE.ArrayLiteral, MODE.Expression, MODE.ForInitializer, MODE.Conditional]);
  373. }
  374. function restore_mode() {
  375. if (flag_store.length > 0) {
  376. previous_flags = flags;
  377. flags = flag_store.pop();
  378. }
  379. }
  380. function start_of_statement() {
  381. if (
  382. (flags.last_text === 'do' ||
  383. (flags.last_text === 'else' && token_text !== 'if') ||
  384. (last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)))) {
  385. allow_wrap_or_preserved_newline();
  386. set_mode(MODE.Statement);
  387. indent();
  388. output_wrapped = false;
  389. return true;
  390. }
  391. return false;
  392. }
  393. function all_lines_start_with(lines, c) {
  394. for (var i = 0; i < lines.length; i++) {
  395. var line = trim(lines[i]);
  396. if (line.charAt(0) !== c) {
  397. return false;
  398. }
  399. }
  400. return true;
  401. }
  402. function is_special_word(word) {
  403. return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']);
  404. }
  405. function in_array(what, arr) {
  406. for (var i = 0; i < arr.length; i += 1) {
  407. if (arr[i] === what) {
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. function unescape_string(s) {
  414. var esc = false,
  415. out = '',
  416. pos = 0,
  417. s_hex = '',
  418. escaped = 0,
  419. c;
  420. while (esc || pos < s.length) {
  421. c = s.charAt(pos);
  422. pos++;
  423. if (esc) {
  424. esc = false;
  425. if (c === 'x') {
  426. // simple hex-escape \x24
  427. s_hex = s.substr(pos, 2);
  428. pos += 2;
  429. } else if (c === 'u') {
  430. // unicode-escape, \u2134
  431. s_hex = s.substr(pos, 4);
  432. pos += 4;
  433. } else {
  434. // some common escape, e.g \n
  435. out += '\\' + c;
  436. continue;
  437. }
  438. if (!s_hex.match(/^[0123456789abcdefABCDEF]+$/)) {
  439. // some weird escaping, bail out,
  440. // leaving whole string intact
  441. return s;
  442. }
  443. escaped = parseInt(s_hex, 16);
  444. if (escaped >= 0x00 && escaped < 0x20) {
  445. // leave 0x00...0x1f escaped
  446. if (c === 'x') {
  447. out += '\\x' + s_hex;
  448. } else {
  449. out += '\\u' + s_hex;
  450. }
  451. continue;
  452. } else if (escaped === 0x22 || escaped === 0x27 || escaped === 0x5c) {
  453. // single-quote, apostrophe, backslash - escape these
  454. out += '\\' + String.fromCharCode(escaped);
  455. } else if (c === 'x' && escaped > 0x7e && escaped <= 0xff) {
  456. // we bail out on \x7f..\xff,
  457. // leaving whole string escaped,
  458. // as it's probably completely binary
  459. return s;
  460. } else {
  461. out += String.fromCharCode(escaped);
  462. }
  463. } else if (c === '\\') {
  464. esc = true;
  465. } else {
  466. out += c;
  467. }
  468. }
  469. return out;
  470. }
  471. function is_next(find) {
  472. var local_pos = parser_pos;
  473. var c = input.charAt(local_pos);
  474. while (in_array(c, whitespace) && c !== find) {
  475. local_pos++;
  476. if (local_pos >= input_length) {
  477. return false;
  478. }
  479. c = input.charAt(local_pos);
  480. }
  481. return c === find;
  482. }
  483. function get_next_token() {
  484. var i, resulting_string;
  485. n_newlines = 0;
  486. if (parser_pos >= input_length) {
  487. return ['', 'TK_EOF'];
  488. }
  489. wanted_newline = false;
  490. whitespace_before_token = [];
  491. var c = input.charAt(parser_pos);
  492. parser_pos += 1;
  493. while (in_array(c, whitespace)) {
  494. if (c === '\n') {
  495. n_newlines += 1;
  496. whitespace_before_token = [];
  497. } else if (n_newlines) {
  498. if (c === indent_string) {
  499. whitespace_before_token.push(indent_string);
  500. } else if (c !== '\r') {
  501. whitespace_before_token.push(' ');
  502. }
  503. }
  504. if (parser_pos >= input_length) {
  505. return ['', 'TK_EOF'];
  506. }
  507. c = input.charAt(parser_pos);
  508. parser_pos += 1;
  509. }
  510. if (in_array(c, wordchar)) {
  511. if (parser_pos < input_length) {
  512. while (in_array(input.charAt(parser_pos), wordchar)) {
  513. c += input.charAt(parser_pos);
  514. parser_pos += 1;
  515. if (parser_pos === input_length) {
  516. break;
  517. }
  518. }
  519. }
  520. // small and surprisingly unugly hack for 1E-10 representation
  521. if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) {
  522. var sign = input.charAt(parser_pos);
  523. parser_pos += 1;
  524. var t = get_next_token();
  525. c += sign + t[0];
  526. return [c, 'TK_WORD'];
  527. }
  528. if (c === 'in') { // hack for 'in' operator
  529. return [c, 'TK_OPERATOR'];
  530. }
  531. return [c, 'TK_WORD'];
  532. }
  533. if (c === '(' || c === '[') {
  534. return [c, 'TK_START_EXPR'];
  535. }
  536. if (c === ')' || c === ']') {
  537. return [c, 'TK_END_EXPR'];
  538. }
  539. if (c === '{') {
  540. return [c, 'TK_START_BLOCK'];
  541. }
  542. if (c === '}') {
  543. return [c, 'TK_END_BLOCK'];
  544. }
  545. if (c === ';') {
  546. return [c, 'TK_SEMICOLON'];
  547. }
  548. if (c === '/') {
  549. var comment = '';
  550. // peek for comment /* ... */
  551. var inline_comment = true;
  552. if (input.charAt(parser_pos) === '*') {
  553. parser_pos += 1;
  554. if (parser_pos < input_length) {
  555. while (parser_pos < input_length && !(input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/')) {
  556. c = input.charAt(parser_pos);
  557. comment += c;
  558. if (c === "\n" || c === "\r") {
  559. inline_comment = false;
  560. }
  561. parser_pos += 1;
  562. if (parser_pos >= input_length) {
  563. break;
  564. }
  565. }
  566. }
  567. parser_pos += 2;
  568. if (inline_comment && n_newlines === 0) {
  569. return ['/*' + comment + '*/', 'TK_INLINE_COMMENT'];
  570. } else {
  571. return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
  572. }
  573. }
  574. // peek for comment // ...
  575. if (input.charAt(parser_pos) === '/') {
  576. comment = c;
  577. while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') {
  578. comment += input.charAt(parser_pos);
  579. parser_pos += 1;
  580. if (parser_pos >= input_length) {
  581. break;
  582. }
  583. }
  584. return [comment, 'TK_COMMENT'];
  585. }
  586. }
  587. if (c === "'" || c === '"' || // string
  588. (c === '/' &&
  589. ((last_type === 'TK_WORD' && is_special_word (flags.last_text)) ||
  590. (last_type === 'TK_END_EXPR' && in_array(previous_flags.mode, [MODE.Conditional, MODE.ForInitializer])) ||
  591. (in_array(last_type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK',
  592. 'TK_END_BLOCK', 'TK_OPERATOR', 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA'
  593. ]))))) { // regexp
  594. var sep = c,
  595. esc = false,
  596. has_char_escapes = false;
  597. resulting_string = c;
  598. if (parser_pos < input_length) {
  599. if (sep === '/') {
  600. //
  601. // handle regexp separately...
  602. //
  603. var in_char_class = false;
  604. while (esc || in_char_class || input.charAt(parser_pos) !== sep) {
  605. resulting_string += input.charAt(parser_pos);
  606. if (!esc) {
  607. esc = input.charAt(parser_pos) === '\\';
  608. if (input.charAt(parser_pos) === '[') {
  609. in_char_class = true;
  610. } else if (input.charAt(parser_pos) === ']') {
  611. in_char_class = false;
  612. }
  613. } else {
  614. esc = false;
  615. }
  616. parser_pos += 1;
  617. if (parser_pos >= input_length) {
  618. // incomplete string/rexp when end-of-file reached.
  619. // bail out with what had been received so far.
  620. return [resulting_string, 'TK_STRING'];
  621. }
  622. }
  623. } else {
  624. //
  625. // and handle string also separately
  626. //
  627. while (esc || input.charAt(parser_pos) !== sep) {
  628. resulting_string += input.charAt(parser_pos);
  629. if (esc) {
  630. if (input.charAt(parser_pos) === 'x' || input.charAt(parser_pos) === 'u') {
  631. has_char_escapes = true;
  632. }
  633. esc = false;
  634. } else {
  635. esc = input.charAt(parser_pos) === '\\';
  636. }
  637. parser_pos += 1;
  638. if (parser_pos >= input_length) {
  639. // incomplete string/rexp when end-of-file reached.
  640. // bail out with what had been received so far.
  641. return [resulting_string, 'TK_STRING'];
  642. }
  643. }
  644. }
  645. }
  646. parser_pos += 1;
  647. resulting_string += sep;
  648. if (has_char_escapes && opt.unescape_strings) {
  649. resulting_string = unescape_string(resulting_string);
  650. }
  651. if (sep === '/') {
  652. // regexps may have modifiers /regexp/MOD , so fetch those, too
  653. while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) {
  654. resulting_string += input.charAt(parser_pos);
  655. parser_pos += 1;
  656. }
  657. }
  658. return [resulting_string, 'TK_STRING'];
  659. }
  660. if (c === '#') {
  661. if (output.length === 0 && input.charAt(parser_pos) === '!') {
  662. // shebang
  663. resulting_string = c;
  664. while (parser_pos < input_length && c !== '\n') {
  665. c = input.charAt(parser_pos);
  666. resulting_string += c;
  667. parser_pos += 1;
  668. }
  669. return [trim(resulting_string) + '\n', 'TK_UNKNOWN'];
  670. }
  671. // Spidermonkey-specific sharp variables for circular references
  672. // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript
  673. // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935
  674. var sharp = '#';
  675. if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) {
  676. do {
  677. c = input.charAt(parser_pos);
  678. sharp += c;
  679. parser_pos += 1;
  680. } while (parser_pos < input_length && c !== '#' && c !== '=');
  681. if (c === '#') {
  682. //
  683. } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') {
  684. sharp += '[]';
  685. parser_pos += 2;
  686. } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') {
  687. sharp += '{}';
  688. parser_pos += 2;
  689. }
  690. return [sharp, 'TK_WORD'];
  691. }
  692. }
  693. if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') {
  694. parser_pos += 3;
  695. c = '<!--';
  696. while (input.charAt(parser_pos) !== '\n' && parser_pos < input_length) {
  697. c += input.charAt(parser_pos);
  698. parser_pos++;
  699. }
  700. flags.in_html_comment = true;
  701. return [c, 'TK_COMMENT'];
  702. }
  703. if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') {
  704. flags.in_html_comment = false;
  705. parser_pos += 2;
  706. return ['-->', 'TK_COMMENT'];
  707. }
  708. if (c === '.') {
  709. return [c, 'TK_DOT'];
  710. }
  711. if (in_array(c, punct)) {
  712. while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) {
  713. c += input.charAt(parser_pos);
  714. parser_pos += 1;
  715. if (parser_pos >= input_length) {
  716. break;
  717. }
  718. }
  719. if (c === ',') {
  720. return [c, 'TK_COMMA'];
  721. } else if (c === '=') {
  722. return [c, 'TK_EQUALS'];
  723. } else {
  724. return [c, 'TK_OPERATOR'];
  725. }
  726. }
  727. return [c, 'TK_UNKNOWN'];
  728. }
  729. function handle_start_expr() {
  730. if (start_of_statement()) {
  731. // The conditional starts the statement if appropriate.
  732. }
  733. if (token_text === '[') {
  734. if (last_type === 'TK_WORD' || flags.last_text === ')') {
  735. // this is array index specifier, break immediately
  736. // a[x], fn()[x]
  737. if (in_array (flags.last_text, line_starters)) {
  738. output_space_before_token = true;
  739. }
  740. set_mode(MODE.Expression);
  741. print_token();
  742. return;
  743. }
  744. if (is_array(flags.mode)) {
  745. if ( (flags.last_text === '[') ||
  746. (last_last_text === ']' && flags.last_text === ',')) {
  747. // ], [ goes to new line
  748. if (!opt.keep_array_indentation) {
  749. print_newline();
  750. }
  751. }
  752. }
  753. } else {
  754. if (flags.last_text === 'for') {
  755. set_mode(MODE.ForInitializer);
  756. } else if (in_array (flags.last_text, ['if', 'while'])) {
  757. set_mode(MODE.Conditional);
  758. } else {
  759. set_mode(MODE.Expression);
  760. }
  761. }
  762. if (flags.last_text === ';' || last_type === 'TK_START_BLOCK') {
  763. print_newline();
  764. } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || flags.last_text === '.') {
  765. if (wanted_newline) {
  766. print_newline();
  767. }
  768. // do nothing on (( and )( and ][ and ]( and .(
  769. } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') {
  770. output_space_before_token = true;
  771. } else if (flags.last_word === 'function' || flags.last_word === 'typeof') {
  772. // function() vs function ()
  773. if (opt.jslint_happy) {
  774. output_space_before_token = true;
  775. }
  776. } else if (in_array (flags.last_text, line_starters) || flags.last_text === 'catch') {
  777. if (opt.space_before_conditional) {
  778. output_space_before_token = true;
  779. }
  780. }
  781. // Support of this kind of newline preservation.
  782. // a = (b &&
  783. // (c || d));
  784. if (token_text === '(') {
  785. if (last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') {
  786. if (flags.mode !== MODE.ObjectLiteral) {
  787. allow_wrap_or_preserved_newline();
  788. }
  789. }
  790. }
  791. print_token();
  792. if (token_text === '[') {
  793. set_mode(MODE.ArrayLiteral);
  794. indent();
  795. }
  796. }
  797. function handle_end_expr() {
  798. // statements inside expressions are not valid syntax, but...
  799. // statements must all be closed when their container closes
  800. while (flags.mode === MODE.Statement) {
  801. restore_mode();
  802. }
  803. if (token_text === ']' && is_array(flags.mode) && flags.multiline_array && !opt.keep_array_indentation) {
  804. print_newline();
  805. }
  806. restore_mode();
  807. print_token();
  808. // do {} while () // no statement required after
  809. if (flags.do_while && previous_flags.mode === MODE.Conditional) {
  810. previous_flags.mode = MODE.Expression;
  811. flags.do_block = false;
  812. flags.do_while = false;
  813. }
  814. }
  815. function handle_start_block() {
  816. set_mode(MODE.BlockStatement);
  817. var empty_braces = is_next('}');
  818. if (opt.brace_style === "expand-strict") {
  819. if (!empty_braces) {
  820. print_newline();
  821. }
  822. } else if (opt.brace_style === "expand") {
  823. if (last_type !== 'TK_OPERATOR') {
  824. if (last_type === 'TK_EQUALS' ||
  825. (is_special_word (flags.last_text) && flags.last_text !== 'else')) {
  826. output_space_before_token = true;
  827. } else {
  828. print_newline();
  829. }
  830. }
  831. } else { // collapse
  832. if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') {
  833. if (last_type === 'TK_START_BLOCK') {
  834. print_newline();
  835. } else {
  836. output_space_before_token = true;
  837. }
  838. } else {
  839. // if TK_OPERATOR or TK_START_EXPR
  840. if (is_array(previous_flags.mode) && flags.last_text === ',') {
  841. if (last_last_text === '}') {
  842. // }, { in array context
  843. output_space_before_token = true;
  844. } else {
  845. print_newline(); // [a, b, c, {
  846. }
  847. }
  848. }
  849. }
  850. print_token();
  851. indent();
  852. }
  853. function handle_end_block() {
  854. // statements must all be closed when their container closes
  855. while (flags.mode === MODE.Statement) {
  856. restore_mode();
  857. }
  858. restore_mode();
  859. if (opt.brace_style === "expand" || opt.brace_style === "expand-strict") {
  860. if (last_type !== 'TK_START_BLOCK') {
  861. print_newline();
  862. }
  863. } else {
  864. // skip {}
  865. if (last_type !== 'TK_START_BLOCK') {
  866. if (is_array(flags.mode) && opt.keep_array_indentation) {
  867. // we REALLY need a newline here, but newliner would skip that
  868. opt.keep_array_indentation = false;
  869. print_newline();
  870. opt.keep_array_indentation = true;
  871. } else {
  872. print_newline();
  873. }
  874. }
  875. }
  876. print_token();
  877. }
  878. function handle_word() {
  879. if (start_of_statement()) {
  880. // The conditional starts the statement if appropriate.
  881. } else if (wanted_newline && !is_expression(flags.mode) &&
  882. (last_type !== 'TK_OPERATOR' || (flags.last_text === '--' || flags.last_text === '++')) &&
  883. last_type !== 'TK_EQUALS' &&
  884. (opt.preserve_newlines || flags.last_text !== 'var')) {
  885. print_newline();
  886. }
  887. if (flags.do_block && !flags.do_while) {
  888. if (token_text === 'while') {
  889. // do {} ## while ()
  890. output_space_before_token = true;
  891. print_token();
  892. output_space_before_token = true;
  893. flags.do_while = true;
  894. return;
  895. } else {
  896. // do {} should always have while as the next word.
  897. // if we don't see the expected while, recover
  898. print_newline();
  899. flags.do_block = false;
  900. }
  901. }
  902. // if may be followed by else, or not
  903. // Bare/inline ifs are tricky
  904. // Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e();
  905. if (flags.if_block) {
  906. if (token_text !== 'else') {
  907. while (flags.mode === MODE.Statement) {
  908. restore_mode();
  909. }
  910. flags.if_block = false;
  911. }
  912. }
  913. if (token_text === 'function') {
  914. if (flags.var_line && last_type !== 'TK_EQUALS') {
  915. flags.var_line_reindented = true;
  916. }
  917. if ((just_added_newline() || flags.last_text === ';') && flags.last_text !== '{' && last_type !== 'TK_BLOCK_COMMENT' && last_type !== 'TK_COMMENT') {
  918. // make sure there is a nice clean space of at least one blank line
  919. // before a new function definition
  920. n_newlines = just_added_newline() ? n_newlines : 0;
  921. if (!opt.preserve_newlines) {
  922. n_newlines = 1;
  923. }
  924. for (var i = 0; i < 2 - n_newlines; i++) {
  925. print_newline(true);
  926. }
  927. }
  928. if (last_type === 'TK_WORD') {
  929. if (flags.last_text === 'get' || flags.last_text === 'set' || flags.last_text === 'new' || flags.last_text === 'return') {
  930. output_space_before_token = true;
  931. } else {
  932. print_newline();
  933. }
  934. } else if (last_type === 'TK_OPERATOR' || flags.last_text === '=') {
  935. // foo = function
  936. output_space_before_token = true;
  937. } else if (is_expression(flags.mode)) {
  938. // (function
  939. } else {
  940. print_newline();
  941. }
  942. print_token();
  943. flags.last_word = token_text;
  944. return;
  945. }
  946. if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) {
  947. print_newline();
  948. if (flags.case_body || opt.jslint_happy) {
  949. // switch cases following one another
  950. flags.indentation_level--;
  951. flags.case_body = false;
  952. }
  953. print_token();
  954. flags.in_case = true;
  955. flags.in_case_statement = true;
  956. return;
  957. }
  958. prefix = 'NONE';
  959. if (last_type === 'TK_END_BLOCK') {
  960. if (!in_array(token_text, ['else', 'catch', 'finally'])) {
  961. prefix = 'NEWLINE';
  962. } else {
  963. if (opt.brace_style === "expand" || opt.brace_style === "end-expand" || opt.brace_style === "expand-strict") {
  964. prefix = 'NEWLINE';
  965. } else {
  966. prefix = 'SPACE';
  967. output_space_before_token = true;
  968. }
  969. }
  970. } else if (last_type === 'TK_SEMICOLON' && flags.mode === MODE.BlockStatement) {
  971. // TODO: Should this be for STATEMENT as well?
  972. prefix = 'NEWLINE';
  973. } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) {
  974. prefix = 'SPACE';
  975. } else if (last_type === 'TK_STRING') {
  976. prefix = 'NEWLINE';
  977. } else if (last_type === 'TK_WORD') {
  978. prefix = 'SPACE';
  979. } else if (last_type === 'TK_START_BLOCK') {
  980. prefix = 'NEWLINE';
  981. } else if (last_type === 'TK_END_EXPR') {
  982. output_space_before_token = true;
  983. prefix = 'NEWLINE';
  984. }
  985. if (in_array(token_text, line_starters) && flags.last_text !== ')') {
  986. if (flags.last_text === 'else') {
  987. prefix = 'SPACE';
  988. } else {
  989. prefix = 'NEWLINE';
  990. }
  991. }
  992. if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') {
  993. if (flags.mode !== MODE.ObjectLiteral) {
  994. allow_wrap_or_preserved_newline();
  995. }
  996. }
  997. if (in_array(token_text, ['else', 'catch', 'finally'])) {
  998. if (last_type !== 'TK_END_BLOCK' || opt.brace_style === "expand" || opt.brace_style === "end-expand" || opt.brace_style === "expand-strict") {
  999. print_newline();
  1000. } else {
  1001. trim_output(true);
  1002. // If we trimmed and there's something other than a close block before us
  1003. // put a newline back in. Handles '} // comment' scenario.
  1004. if (output[output.length - 1] !== '}') {
  1005. print_newline();
  1006. }
  1007. output_space_before_token = true;
  1008. }
  1009. } else if (prefix === 'NEWLINE') {
  1010. if (is_special_word (flags.last_text)) {
  1011. // no newline between 'return nnn'
  1012. output_space_before_token = true;
  1013. } else if (last_type !== 'TK_END_EXPR') {
  1014. if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && flags.last_text !== ':') {
  1015. // no need to force newline on 'var': for (var x = 0...)
  1016. if (token_text === 'if' && flags.last_word === 'else' && flags.last_text !== '{') {
  1017. // no newline for } else if {
  1018. output_space_before_token = true;
  1019. } else {
  1020. flags.var_line = false;
  1021. flags.var_line_reindented = false;
  1022. print_newline();
  1023. }
  1024. }
  1025. } else if (in_array(token_text, line_starters) && flags.last_text !== ')') {
  1026. flags.var_line = false;
  1027. flags.var_line_reindented = false;
  1028. print_newline();
  1029. }
  1030. } else if (is_array(flags.mode) && flags.last_text === ',' && last_last_text === '}') {
  1031. print_newline(); // }, in lists get a newline treatment
  1032. } else if (prefix === 'SPACE') {
  1033. output_space_before_token = true;
  1034. }
  1035. print_token();
  1036. flags.last_word = token_text;
  1037. if (token_text === 'var') {
  1038. flags.var_line = true;
  1039. flags.var_line_reindented = false;
  1040. flags.var_line_tainted = false;
  1041. }
  1042. if (token_text === 'do') {
  1043. flags.do_block = true;
  1044. }
  1045. if (token_text === 'if') {
  1046. flags.if_block = true;
  1047. }
  1048. }
  1049. function handle_semicolon() {
  1050. while (flags.mode === MODE.Statement && !flags.if_block) {
  1051. restore_mode();
  1052. }
  1053. print_token();
  1054. flags.var_line = false;
  1055. flags.var_line_reindented = false;
  1056. if (flags.mode === MODE.ObjectLiteral) {
  1057. // if we're in OBJECT mode and see a semicolon, its invalid syntax
  1058. // recover back to treating this as a BLOCK
  1059. flags.mode = MODE.BlockStatement;
  1060. }
  1061. }
  1062. function handle_string() {
  1063. if (start_of_statement()) {
  1064. // The conditional starts the statement if appropriate.
  1065. // One difference - strings want at least a space before
  1066. output_space_before_token = true;
  1067. } else if (last_type === 'TK_WORD') {
  1068. output_space_before_token = true;
  1069. } else if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') {
  1070. if (flags.mode !== MODE.ObjectLiteral) {
  1071. allow_wrap_or_preserved_newline();
  1072. }
  1073. } else {
  1074. print_newline();
  1075. }
  1076. print_token();
  1077. }
  1078. function handle_equals() {
  1079. if (flags.var_line) {
  1080. // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done
  1081. flags.var_line_tainted = true;
  1082. }
  1083. output_space_before_token = true;
  1084. print_token();
  1085. output_space_before_token = true;
  1086. }
  1087. function handle_comma() {
  1088. if (flags.var_line) {
  1089. if (is_expression(flags.mode) || last_type === 'TK_END_BLOCK') {
  1090. // do not break on comma, for(var a = 1, b = 2)
  1091. flags.var_line_tainted = false;
  1092. }
  1093. if (flags.var_line) {
  1094. flags.var_line_reindented = true;
  1095. }
  1096. print_token();
  1097. if (flags.var_line_tainted) {
  1098. flags.var_line_tainted = false;
  1099. print_newline();
  1100. } else {
  1101. output_space_before_token = true;
  1102. }
  1103. return;
  1104. }
  1105. if (last_type === 'TK_END_BLOCK' && flags.mode !== MODE.Expression) {
  1106. print_token();
  1107. if (flags.mode === MODE.ObjectLiteral && flags.last_text === '}') {
  1108. print_newline();
  1109. } else {
  1110. output_space_before_token = true;
  1111. }
  1112. } else {
  1113. if (flags.mode === MODE.ObjectLiteral) {
  1114. print_token();
  1115. print_newline();
  1116. } else {
  1117. // EXPR or DO_BLOCK
  1118. print_token();
  1119. output_space_before_token = true;
  1120. }
  1121. }
  1122. }
  1123. function handle_operator() {
  1124. var space_before = true;
  1125. var space_after = true;
  1126. if (is_special_word (flags.last_text)) {
  1127. // "return" had a special handling in TK_WORD. Now we need to return the favor
  1128. output_space_before_token = true;
  1129. print_token();
  1130. return;
  1131. }
  1132. // hack for actionscript's import .*;
  1133. if (token_text === '*' && last_type === 'TK_DOT' && !last_last_text.match(/^\d+$/)) {
  1134. print_token();
  1135. return;
  1136. }
  1137. if (token_text === ':' && flags.in_case) {
  1138. flags.case_body = true;
  1139. indent();
  1140. print_token();
  1141. print_newline();
  1142. flags.in_case = false;
  1143. return;
  1144. }
  1145. if (token_text === '::') {
  1146. // no spaces around exotic namespacing syntax operator
  1147. print_token();
  1148. return;
  1149. }
  1150. // http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1
  1151. // if there is a newline between -- or ++ and anything else we should preserve it.
  1152. if (wanted_newline && (token_text === '--' || token_text === '++')) {
  1153. print_newline();
  1154. }
  1155. if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || in_array (flags.last_text, line_starters) || flags.last_text === ','))) {
  1156. // unary operators (and binary +/- pretending to be unary) special cases
  1157. space_before = false;
  1158. space_after = false;
  1159. if (flags.last_text === ';' && is_expression(flags.mode)) {
  1160. // for (;; ++i)
  1161. // ^^^
  1162. space_before = true;
  1163. }
  1164. if (last_type === 'TK_WORD' && in_array (flags.last_text, line_starters)) {
  1165. space_before = true;
  1166. }
  1167. if ((flags.mode === MODE.BlockStatement || flags.mode === MODE.Statement) && (flags.last_text === '{' || flags.last_text === ';')) {
  1168. // { foo; --i }
  1169. // foo(); --bar;
  1170. print_newline();
  1171. }
  1172. } else if (token_text === ':') {
  1173. if (flags.ternary_depth === 0) {
  1174. if (flags.mode === MODE.BlockStatement) {
  1175. flags.mode = MODE.ObjectLiteral;
  1176. }
  1177. space_before = false;
  1178. } else {
  1179. flags.ternary_depth -= 1;
  1180. }
  1181. } else if (token_text === '?') {
  1182. flags.ternary_depth += 1;
  1183. }
  1184. output_space_before_token = output_space_before_token || space_before;
  1185. print_token();
  1186. output_space_before_token = space_after;
  1187. }
  1188. function handle_block_comment() {
  1189. var lines = split_newlines(token_text);
  1190. var j; // iterator for this case
  1191. if (all_lines_start_with(lines.slice(1), '*')) {
  1192. // javadoc: reformat and reindent
  1193. print_newline(false, true);
  1194. print_token(lines[0]);
  1195. for (j = 1; j < lines.length; j++) {
  1196. print_newline(false, true);
  1197. print_token(' ' + trim(lines[j]));
  1198. }
  1199. } else {
  1200. // simple block comment: leave intact
  1201. if (lines.length > 1) {
  1202. // multiline comment block starts with a new line
  1203. print_newline(false, true);
  1204. } else {
  1205. // single-line /* comment */ stays where it is
  1206. if (last_type === 'TK_END_BLOCK') {
  1207. print_newline(false, true);
  1208. } else {
  1209. output_space_before_token = true;
  1210. }
  1211. }
  1212. print_token(lines[0]);
  1213. output.push("\n");
  1214. for (j = 1; j < lines.length; j++) {
  1215. output.push(lines[j]);
  1216. output.push("\n");
  1217. }
  1218. }
  1219. if (!is_next('\n')) {
  1220. print_newline(false, true);
  1221. }
  1222. }
  1223. function handle_inline_comment() {
  1224. output_space_before_token = true;
  1225. print_token();
  1226. output_space_before_token = true;
  1227. }
  1228. function handle_comment() {
  1229. if (wanted_newline) {
  1230. print_newline(false, true);
  1231. }
  1232. if (flags.last_text === ',' && !wanted_newline) {
  1233. trim_output(true);
  1234. }
  1235. output_space_before_token = true;
  1236. print_token();
  1237. print_newline(false, true);
  1238. }
  1239. function handle_dot() {
  1240. if (is_special_word (flags.last_text)) {
  1241. output_space_before_token = true;
  1242. } else {
  1243. // allow preserved newlines before dots in general
  1244. // force newlines on dots after close paren when break_chained - for bar().baz()
  1245. allow_wrap_or_preserved_newline (flags.last_text === ')' && opt.break_chained_methods);
  1246. }
  1247. print_token();
  1248. }
  1249. function handle_unknown() {
  1250. print_token();
  1251. if (token_text[token_text.length - 1] === '\n') {
  1252. print_newline();
  1253. }
  1254. }
  1255. }
  1256. if (typeof define === "function") {
  1257. // Add support for require.js
  1258. define(function(require, exports, module) {
  1259. exports.js_beautify = js_beautify;
  1260. });
  1261. } else if (typeof exports !== "undefined") {
  1262. // Add support for CommonJS. Just put this file somewhere on your require.paths
  1263. // and you will be able to `var js_beautify = require("beautify").js_beautify`.
  1264. exports.js_beautify = js_beautify;
  1265. } else if (typeof window !== "undefined") {
  1266. // If we're running a web page and don't have either of the above, add our one global
  1267. window.js_beautify = js_beautify;
  1268. }
  1269. }());