818
Du
dustymarkdown / files
dustymarkdown / node_modules / dust / vendor / beautify.js
37.3 KB
1/*jslint onevar: false, plusplus: false */ 2/* 3 4 JS Beautifier 5--------------- 6 7 8 Written by Einar Lielmanis, <einar@jsbeautifier.org> 9 http://jsbeautifier.org/ 10 11 Originally converted to javascript by Vital, <vital76@gmail.com> 12 13 You are free to use this in any way you want, in case you find this useful or working for you. 14 15 Usage: 16 js_beautify(js_source_text); 17 js_beautify(js_source_text, options); 18 19 The options are: 20 indent_size (default 4) — indentation size, 21 indent_char (default space) — character to indent with, 22 preserve_newlines (default true) — whether existing line breaks should be preserved, 23 indent_level (default 0) — initial indentation level, you probably won't need this ever, 24 25 space_after_anon_function (default false) — if true, then space is added between "function ()" 26 (jslint is happy about this); if false, then the common "function()" output is used. 27 braces_on_own_line (default false) - ANSI / Allman brace style, each opening/closing brace gets its own line. 28 29 e.g 30 31 js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'}); 32 33 34*/ 35 36 37 38function js_beautify(js_source_text, options) { 39 40 var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string; 41 var whitespace, wordchar, punct, parser_pos, line_starters, digits; 42 var prefix, token_type, do_block_just_closed; 43 var wanted_newline, just_added_newline, n_newlines; 44 45 46 // Some interpreters have unexpected results with foo = baz || bar; 47 options = options ? options : {}; 48 var opt_braces_on_own_line = options.braces_on_own_line ? options.braces_on_own_line : false; 49 var opt_indent_size = options.indent_size ? options.indent_size : 4; 50 var opt_indent_char = options.indent_char ? options.indent_char : ' '; 51 var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines; 52 var opt_indent_level = options.indent_level ? options.indent_level : 0; // starting indentation 53 var opt_space_after_anon_function = options.space_after_anon_function === 'undefined' ? false : options.space_after_anon_function; 54 var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation; 55 56 just_added_newline = false; 57 58 // cache the source's length. 59 var input_length = js_source_text.length; 60 61 function trim_output() { 62 while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) { 63 output.pop(); 64 } 65 } 66 67 function is_array(mode) { 68 return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]'; 69 } 70 71 72 function print_newline(ignore_repeated) { 73 74 flags.eat_next_space = false; 75 if (opt_keep_array_indentation && is_array(flags.mode)) { 76 return; 77 } 78 79 ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated; 80 81 flags.if_line = false; 82 trim_output(); 83 84 if (!output.length) { 85 return; // no newline on start of file 86 } 87 88 if (output[output.length - 1] !== "\n" || !ignore_repeated) { 89 just_added_newline = true; 90 output.push("\n"); 91 } 92 for (var i = 0; i < flags.indentation_level; i += 1) { 93 output.push(indent_string); 94 } 95 if (flags.var_line && flags.var_line_reindented) { 96 if (opt_indent_char === ' ') { 97 output.push(' '); // var_line always pushes 4 spaces, so that the variables would be one under another 98 } else { 99 output.push(indent_string); // skip space-stuffing, if indenting with a tab 100 } 101 } 102 } 103 104 105 106 function print_single_space() { 107 if (flags.eat_next_space) { 108 flags.eat_next_space = false; 109 return; 110 } 111 var last_output = ' '; 112 if (output.length) { 113 last_output = output[output.length - 1]; 114 } 115 if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space 116 output.push(' '); 117 } 118 } 119 120 121 function print_token() { 122 just_added_newline = false; 123 flags.eat_next_space = false; 124 output.push(token_text); 125 } 126 127 function indent() { 128 flags.indentation_level += 1; 129 } 130 131 132 function remove_indent() { 133 if (output.length && output[output.length - 1] === indent_string) { 134 output.pop(); 135 } 136 } 137 138 function set_mode(mode) { 139 if (flags) { 140 flag_store.push(flags); 141 } 142 flags = { 143 previous_mode: flags ? flags.mode : 'BLOCK', 144 mode: mode, 145 var_line: false, 146 var_line_tainted: false, 147 var_line_reindented: false, 148 in_html_comment: false, 149 if_line: false, 150 in_case: false, 151 eat_next_space: false, 152 indentation_baseline: -1, 153 indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : opt_indent_level) 154 }; 155 } 156 157 function is_array(mode) { 158 return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]'; 159 } 160 161 function is_expression(mode) { 162 return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)'; 163 } 164 165 function restore_mode() { 166 do_block_just_closed = flags.mode === 'DO_BLOCK'; 167 if (flag_store.length > 0) { 168 flags = flag_store.pop(); 169 } 170 } 171 172 173 function in_array(what, arr) { 174 for (var i = 0; i < arr.length; i += 1) { 175 if (arr[i] === what) { 176 return true; 177 } 178 } 179 return false; 180 } 181 182 // Walk backwards from the colon to find a '?' (colon is part of a ternary op) 183 // or a '{' (colon is part of a class literal). Along the way, keep track of 184 // the blocks and expressions we pass so we only trigger on those chars in our 185 // own level, and keep track of the colons so we only trigger on the matching '?'. 186 187 188 function is_ternary_op() { 189 var level = 0, 190 colon_count = 0; 191 for (var i = output.length - 1; i >= 0; i--) { 192 switch (output[i]) { 193 case ':': 194 if (level === 0) { 195 colon_count++; 196 } 197 break; 198 case '?': 199 if (level === 0) { 200 if (colon_count === 0) { 201 return true; 202 } else { 203 colon_count--; 204 } 205 } 206 break; 207 case '{': 208 if (level === 0) { 209 return false; 210 } 211 level--; 212 break; 213 case '(': 214 case '[': 215 level--; 216 break; 217 case ')': 218 case ']': 219 case '}': 220 level++; 221 break; 222 } 223 } 224 } 225 226 function get_next_token() { 227 n_newlines = 0; 228 229 if (parser_pos >= input_length) { 230 return ['', 'TK_EOF']; 231 } 232 233 wanted_newline = false; 234 235 var c = input.charAt(parser_pos); 236 parser_pos += 1; 237 238 239 var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode); 240 241 if (keep_whitespace) { 242 243 // 244 // slight mess to allow nice preservation of array indentation and reindent that correctly 245 // first time when we get to the arrays: 246 // var a = [ 247 // ....'something' 248 // we make note of whitespace_count = 4 into flags.indentation_baseline 249 // so we know that 4 whitespaces in original source match indent_level of reindented source 250 // 251 // and afterwards, when we get to 252 // 'something, 253 // .......'something else' 254 // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces 255 // 256 var whitespace_count = 0; 257 258 while (in_array(c, whitespace)) { 259 260 if (c === "\n") { 261 trim_output(); 262 output.push("\n"); 263 just_added_newline = true; 264 whitespace_count = 0; 265 } else { 266 if (c === '\t') { 267 whitespace_count += 4; 268 } else { 269 whitespace_count += 1; 270 } 271 } 272 273 if (parser_pos >= input_length) { 274 return ['', 'TK_EOF']; 275 } 276 277 c = input.charAt(parser_pos); 278 parser_pos += 1; 279 280 } 281 if (flags.indentation_baseline === -1) { 282 flags.indentation_baseline = whitespace_count; 283 } 284 285 if (just_added_newline) { 286 var i; 287 for (i = 0; i < flags.indentation_level + 1; i += 1) { 288 output.push(indent_string); 289 } 290 if (flags.indentation_baseline !== -1) { 291 for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) { 292 output.push(' '); 293 } 294 } 295 } 296 297 } else { 298 while (in_array(c, whitespace)) { 299 300 if (c === "\n") { 301 n_newlines += 1; 302 } 303 304 305 if (parser_pos >= input_length) { 306 return ['', 'TK_EOF']; 307 } 308 309 c = input.charAt(parser_pos); 310 parser_pos += 1; 311 312 } 313 314 if (opt_preserve_newlines) { 315 if (n_newlines > 1) { 316 for (i = 0; i < n_newlines; i += 1) { 317 print_newline(i === 0); 318 just_added_newline = true; 319 } 320 } 321 } 322 wanted_newline = n_newlines > 0; 323 } 324 325 326 if (in_array(c, wordchar)) { 327 if (parser_pos < input_length) { 328 while (in_array(input.charAt(parser_pos), wordchar)) { 329 c += input.charAt(parser_pos); 330 parser_pos += 1; 331 if (parser_pos === input_length) { 332 break; 333 } 334 } 335 } 336 337 // small and surprisingly unugly hack for 1E-10 representation 338 if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) { 339 340 var sign = input.charAt(parser_pos); 341 parser_pos += 1; 342 343 var t = get_next_token(parser_pos); 344 c += sign + t[0]; 345 return [c, 'TK_WORD']; 346 } 347 348 if (c === 'in') { // hack for 'in' operator 349 return [c, 'TK_OPERATOR']; 350 } 351 if (wanted_newline && last_type !== 'TK_OPERATOR' && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) { 352 print_newline(); 353 } 354 return [c, 'TK_WORD']; 355 } 356 357 if (c === '(' || c === '[') { 358 return [c, 'TK_START_EXPR']; 359 } 360 361 if (c === ')' || c === ']') { 362 return [c, 'TK_END_EXPR']; 363 } 364 365 if (c === '{') { 366 return [c, 'TK_START_BLOCK']; 367 } 368 369 if (c === '}') { 370 return [c, 'TK_END_BLOCK']; 371 } 372 373 if (c === ';') { 374 return [c, 'TK_SEMICOLON']; 375 } 376 377 if (c === '/') { 378 var comment = ''; 379 // peek for comment /* ... */ 380 var inline_comment = true; 381 if (input.charAt(parser_pos) === '*') { 382 parser_pos += 1; 383 if (parser_pos < input_length) { 384 while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input_length) { 385 c = input.charAt(parser_pos); 386 comment += c; 387 if (c === '\x0d' || c === '\x0a') { 388 inline_comment = false; 389 } 390 parser_pos += 1; 391 if (parser_pos >= input_length) { 392 break; 393 } 394 } 395 } 396 parser_pos += 2; 397 if (inline_comment) { 398 return ['/*' + comment + '*/', 'TK_INLINE_COMMENT']; 399 } else { 400 return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT']; 401 } 402 } 403 // peek for comment // ... 404 if (input.charAt(parser_pos) === '/') { 405 comment = c; 406 while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") { 407 comment += input.charAt(parser_pos); 408 parser_pos += 1; 409 if (parser_pos >= input_length) { 410 break; 411 } 412 } 413 parser_pos += 1; 414 if (wanted_newline) { 415 print_newline(); 416 } 417 return [comment, 'TK_COMMENT']; 418 } 419 420 } 421 422 if (c === "'" || // string 423 c === '"' || // string 424 (c === '/' && ((last_type === 'TK_WORD' && in_array(last_text, ['return', 'do'])) || (last_type === 'TK_START_EXPR' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EQUALS' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp 425 var sep = c; 426 var esc = false; 427 var resulting_string = c; 428 429 if (parser_pos < input_length) { 430 if (sep === '/') { 431 // 432 // handle regexp separately... 433 // 434 var in_char_class = false; 435 while (esc || in_char_class || input.charAt(parser_pos) !== sep) { 436 resulting_string += input.charAt(parser_pos); 437 if (!esc) { 438 esc = input.charAt(parser_pos) === '\\'; 439 if (input.charAt(parser_pos) === '[') { 440 in_char_class = true; 441 } else if (input.charAt(parser_pos) === ']') { 442 in_char_class = false; 443 } 444 } else { 445 esc = false; 446 } 447 parser_pos += 1; 448 if (parser_pos >= input_length) { 449 // incomplete string/rexp when end-of-file reached. 450 // bail out with what had been received so far. 451 return [resulting_string, 'TK_STRING']; 452 } 453 } 454 455 } else { 456 // 457 // and handle string also separately 458 // 459 while (esc || input.charAt(parser_pos) !== sep) { 460 resulting_string += input.charAt(parser_pos); 461 if (!esc) { 462 esc = input.charAt(parser_pos) === '\\'; 463 } else { 464 esc = false; 465 } 466 parser_pos += 1; 467 if (parser_pos >= input_length) { 468 // incomplete string/rexp when end-of-file reached. 469 // bail out with what had been received so far. 470 return [resulting_string, 'TK_STRING']; 471 } 472 } 473 } 474 475 476 477 } 478 479 parser_pos += 1; 480 481 resulting_string += sep; 482 483 if (sep === '/') { 484 // regexps may have modifiers /regexp/MOD , so fetch those, too 485 while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) { 486 resulting_string += input.charAt(parser_pos); 487 parser_pos += 1; 488 } 489 } 490 return [resulting_string, 'TK_STRING']; 491 } 492 493 if (c === '#') { 494 // Spidermonkey-specific sharp variables for circular references 495 // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript 496 // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935 497 var sharp = '#'; 498 if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) { 499 do { 500 c = input.charAt(parser_pos); 501 sharp += c; 502 parser_pos += 1; 503 } while (parser_pos < input_length && c !== '#' && c !== '='); 504 if (c === '#') { 505 // 506 } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') { 507 sharp += '[]'; 508 parser_pos += 2; 509 } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') { 510 sharp += '{}'; 511 parser_pos += 2; 512 } 513 return [sharp, 'TK_WORD']; 514 } 515 } 516 517 if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') { 518 parser_pos += 3; 519 flags.in_html_comment = true; 520 return ['<!--', 'TK_COMMENT']; 521 } 522 523 if (c === '-' && flags.in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') { 524 flags.in_html_comment = false; 525 parser_pos += 2; 526 if (wanted_newline) { 527 print_newline(); 528 } 529 return ['-->', 'TK_COMMENT']; 530 } 531 532 if (in_array(c, punct)) { 533 while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) { 534 c += input.charAt(parser_pos); 535 parser_pos += 1; 536 if (parser_pos >= input_length) { 537 break; 538 } 539 } 540 541 if (c === '=') { 542 return [c, 'TK_EQUALS']; 543 } else { 544 return [c, 'TK_OPERATOR']; 545 } 546 } 547 548 return [c, 'TK_UNKNOWN']; 549 } 550 551 //---------------------------------- 552 indent_string = ''; 553 while (opt_indent_size > 0) { 554 indent_string += opt_indent_char; 555 opt_indent_size -= 1; 556 } 557 558 input = js_source_text; 559 560 last_word = ''; // last 'TK_WORD' passed 561 last_type = 'TK_START_EXPR'; // last token type 562 last_text = ''; // last token text 563 last_last_text = ''; // pre-last token text 564 output = []; 565 566 do_block_just_closed = false; 567 568 whitespace = "\n\r\t ".split(''); 569 wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split(''); 570 digits = '0123456789'.split(''); 571 572 punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'.split(' '); 573 574 // words which should always start on new line. 575 line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(','); 576 577 // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'. 578 // some formatting depends on that. 579 flag_store = []; 580 set_mode('BLOCK'); 581 582 parser_pos = 0; 583 while (true) { 584 var t = get_next_token(parser_pos); 585 token_text = t[0]; 586 token_type = t[1]; 587 if (token_type === 'TK_EOF') { 588 break; 589 } 590 591 switch (token_type) { 592 593 case 'TK_START_EXPR': 594 595 if (token_text === '[') { 596 597 if (last_type === 'TK_WORD' || last_text === ')') { 598 // this is array index specifier, break immediately 599 // a[x], fn()[x] 600 if (in_array(last_text, line_starters)) { 601 print_single_space(); 602 } 603 set_mode('(EXPRESSION)'); 604 print_token(); 605 break; 606 } 607 608 if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') { 609 if (last_last_text === ']' && last_text === ',') { 610 // ], [ goes to new line 611 if (flags.mode === '[EXPRESSION]') { 612 flags.mode = '[INDENTED-EXPRESSION]'; 613 if (!opt_keep_array_indentation) { 614 indent(); 615 } 616 } 617 set_mode('[EXPRESSION]'); 618 if (!opt_keep_array_indentation) { 619 print_newline(); 620 } 621 } else if (last_text === '[') { 622 if (flags.mode === '[EXPRESSION]') { 623 flags.mode = '[INDENTED-EXPRESSION]'; 624 if (!opt_keep_array_indentation) { 625 indent(); 626 } 627 } 628 set_mode('[EXPRESSION]'); 629 630 if (!opt_keep_array_indentation) { 631 print_newline(); 632 } 633 } else { 634 set_mode('[EXPRESSION]'); 635 } 636 } else { 637 set_mode('[EXPRESSION]'); 638 } 639 640 641 642 } else { 643 set_mode('(EXPRESSION)'); 644 } 645 646 if (last_text === ';' || last_type === 'TK_START_BLOCK') { 647 print_newline(); 648 } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') { 649 // do nothing on (( and )( and ][ and ]( and .( 650 } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { 651 print_single_space(); 652 } else if (last_word === 'function') { 653 // function() vs function () 654 if (opt_space_after_anon_function) { 655 print_single_space(); 656 } 657 } else if (in_array(last_text, line_starters) || last_text === 'catch') { 658 print_single_space(); 659 } 660 print_token(); 661 662 break; 663 664 case 'TK_END_EXPR': 665 if (token_text === ']') { 666 if (opt_keep_array_indentation) { 667 if (last_text === '}') { 668 // trim_output(); 669 // print_newline(true); 670 remove_indent(); 671 print_token(); 672 restore_mode(); 673 break; 674 } 675 } else { 676 if (flags.mode === '[INDENTED-EXPRESSION]') { 677 if (last_text === ']') { 678 restore_mode(); 679 print_newline(); 680 print_token(); 681 break; 682 } 683 } 684 } 685 } 686 restore_mode(); 687 print_token(); 688 break; 689 690 case 'TK_START_BLOCK': 691 692 if (last_word === 'do') { 693 set_mode('DO_BLOCK'); 694 } else { 695 set_mode('BLOCK'); 696 } 697 if (opt_braces_on_own_line) { 698 if (last_type !== 'TK_OPERATOR') { 699 if (last_text == 'return') { 700 print_single_space(); 701 } else { 702 print_newline(true); 703 } 704 } 705 print_token(); 706 indent(); 707 } else { 708 if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') { 709 if (last_type === 'TK_START_BLOCK') { 710 print_newline(); 711 } else { 712 print_single_space(); 713 } 714 } else { 715 // if TK_OPERATOR or TK_START_EXPR 716 if (is_array(flags.previous_mode) && last_text === ',') { 717 print_newline(); // [a, b, c, { 718 } 719 } 720 indent(); 721 print_token(); 722 } 723 724 break; 725 726 case 'TK_END_BLOCK': 727 restore_mode(); 728 if (opt_braces_on_own_line) { 729 print_newline(); 730 print_token(); 731 } else { 732 if (last_type === 'TK_START_BLOCK') { 733 // nothing 734 if (just_added_newline) { 735 remove_indent(); 736 } else { 737 // {} 738 trim_output(); 739 } 740 } else { 741 print_newline(); 742 } 743 print_token(); 744 } 745 break; 746 747 case 'TK_WORD': 748 749 // no, it's not you. even I have problems understanding how this works 750 // and what does what. 751 if (do_block_just_closed) { 752 // do {} ## while () 753 print_single_space(); 754 print_token(); 755 print_single_space(); 756 do_block_just_closed = false; 757 break; 758 } 759 760 if (token_text === 'function') { 761 if ((just_added_newline || last_text === ';') && last_text !== '{') { 762 // make sure there is a nice clean space of at least one blank line 763 // before a new function definition 764 n_newlines = just_added_newline ? n_newlines : 0; 765 766 for (var i = 0; i < 2 - n_newlines; i++) { 767 print_newline(false); 768 } 769 770 } 771 } 772 773 if (token_text === 'case' || token_text === 'default') { 774 if (last_text === ':') { 775 // switch cases following one another 776 remove_indent(); 777 } else { 778 // case statement starts in the same line where switch 779 flags.indentation_level--; 780 print_newline(); 781 flags.indentation_level++; 782 } 783 print_token(); 784 flags.in_case = true; 785 break; 786 } 787 788 prefix = 'NONE'; 789 790 if (last_type === 'TK_END_BLOCK') { 791 if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { 792 prefix = 'NEWLINE'; 793 } else { 794 if (opt_braces_on_own_line) { 795 prefix = 'NEWLINE'; 796 } else { 797 prefix = 'SPACE'; 798 print_single_space(); 799 } 800 } 801 } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) { 802 prefix = 'NEWLINE'; 803 } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) { 804 prefix = 'SPACE'; 805 } else if (last_type === 'TK_STRING') { 806 prefix = 'NEWLINE'; 807 } else if (last_type === 'TK_WORD') { 808 prefix = 'SPACE'; 809 } else if (last_type === 'TK_START_BLOCK') { 810 prefix = 'NEWLINE'; 811 } else if (last_type === 'TK_END_EXPR') { 812 print_single_space(); 813 prefix = 'NEWLINE'; 814 } 815 816 if (last_type !== 'TK_END_BLOCK' && in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { 817 print_newline(); 818 } else if (in_array(token_text, line_starters) || prefix === 'NEWLINE') { 819 if (last_text === 'else') { 820 // no need to force newline on else break 821 print_single_space(); 822 } else if ((last_type === 'TK_START_EXPR' || last_text === '=' || last_text === ',') && token_text === 'function') { 823 // no need to force newline on 'function': (function 824 // DONOTHING 825 } else if (last_text === 'return' || last_text === 'throw') { 826 // no newline between 'return nnn' 827 print_single_space(); 828 } else if (last_type !== 'TK_END_EXPR') { 829 if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') { 830 // no need to force newline on 'var': for (var x = 0...) 831 if (token_text === 'if' && last_word === 'else' && last_text !== '{') { 832 // no newline for } else if { 833 print_single_space(); 834 } else { 835 print_newline(); 836 } 837 } 838 } else { 839 if (in_array(token_text, line_starters) && last_text !== ')') { 840 print_newline(); 841 } 842 } 843 } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') { 844 print_newline(); // }, in lists get a newline treatment 845 } else if (prefix === 'SPACE') { 846 print_single_space(); 847 } 848 print_token(); 849 last_word = token_text; 850 851 if (token_text === 'var') { 852 flags.var_line = true; 853 flags.var_line_reindented = false; 854 flags.var_line_tainted = false; 855 } 856 857 if (token_text === 'if' || token_text === 'else') { 858 flags.if_line = true; 859 } 860 861 break; 862 863 case 'TK_SEMICOLON': 864 865 print_token(); 866 flags.var_line = false; 867 flags.var_line_reindented = false; 868 break; 869 870 case 'TK_STRING': 871 872 if (last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') { 873 print_newline(); 874 } else if (last_type === 'TK_WORD') { 875 print_single_space(); 876 } 877 print_token(); 878 break; 879 880 case 'TK_EQUALS': 881 if (flags.var_line) { 882 // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done 883 flags.var_line_tainted = true; 884 } 885 print_single_space(); 886 print_token(); 887 print_single_space(); 888 break; 889 890 case 'TK_OPERATOR': 891 892 var space_before = true; 893 var space_after = true; 894 895 if (flags.var_line && token_text === ',' && (is_expression(flags.mode))) { 896 // do not break on comma, for(var a = 1, b = 2) 897 flags.var_line_tainted = false; 898 } 899 900 if (flags.var_line) { 901 if (token_text === ',') { 902 if (flags.var_line_tainted) { 903 print_token(); 904 flags.var_line_reindented = true; 905 flags.var_line_tainted = false; 906 print_newline(); 907 break; 908 } else { 909 flags.var_line_tainted = false; 910 } 911 // } else if (token_text === ':') { 912 // hmm, when does this happen? tests don't catch this 913 // flags.var_line = false; 914 } 915 } 916 917 if (last_text === 'return' || last_text === 'throw') { 918 // "return" had a special handling in TK_WORD. Now we need to return the favor 919 print_single_space(); 920 print_token(); 921 break; 922 } 923 924 if (token_text === ':' && flags.in_case) { 925 print_token(); // colon really asks for separate treatment 926 print_newline(); 927 flags.in_case = false; 928 break; 929 } 930 931 if (token_text === '::') { 932 // no spaces around exotic namespacing syntax operator 933 print_token(); 934 break; 935 } 936 937 if (token_text === ',') { 938 if (flags.var_line) { 939 if (flags.var_line_tainted) { 940 print_token(); 941 print_newline(); 942 flags.var_line_tainted = false; 943 } else { 944 print_token(); 945 print_single_space(); 946 } 947 } else if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") { 948 print_token(); 949 if (flags.mode === 'OBJECT' && last_text === '}') { 950 print_newline(); 951 } else { 952 print_single_space(); 953 } 954 } else { 955 if (flags.mode === 'OBJECT') { 956 print_token(); 957 print_newline(); 958 } else { 959 // EXPR or DO_BLOCK 960 print_token(); 961 print_single_space(); 962 } 963 } 964 break; 965 // } else if (in_array(token_text, ['--', '++', '!']) || (in_array(token_text, ['-', '+']) && (in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS']) || in_array(last_text, line_starters) || in_array(last_text, ['==', '!=', '+=', '-=', '*=', '/=', '+', '-'])))) { 966 } else 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(last_text, line_starters)))) { 967 // unary operators (and binary +/- pretending to be unary) special cases 968 969 space_before = false; 970 space_after = false; 971 972 if (last_text === ';' && is_expression(flags.mode)) { 973 // for (;; ++i) 974 // ^^^ 975 space_before = true; 976 } 977 if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) { 978 space_before = true; 979 } 980 981 if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) { 982 // { foo; --i } 983 // foo(); --bar; 984 print_newline(); 985 } 986 } else if (token_text === '.') { 987 // decimal digits or object.property 988 space_before = false; 989 990 } else if (token_text === ':') { 991 if (!is_ternary_op()) { 992 flags.mode = 'OBJECT'; 993 space_before = false; 994 } 995 } 996 if (space_before) { 997 print_single_space(); 998 } 999 1000 print_token(); 1001 1002 if (space_after) { 1003 print_single_space(); 1004 } 1005 1006 if (token_text === '!') { 1007 // flags.eat_next_space = true; 1008 } 1009 1010 break; 1011 1012 case 'TK_BLOCK_COMMENT': 1013 1014 var lines = token_text.split(/\x0a|\x0d\x0a/); 1015 1016 if (/^\/\*\*/.test(token_text)) { 1017 // javadoc: reformat and reindent 1018 print_newline(); 1019 output.push(lines[0]); 1020 for (i = 1; i < lines.length; i++) { 1021 print_newline(); 1022 output.push(' '); 1023 output.push(lines[i].replace(/^\s\s*|\s\s*$/, '')); 1024 } 1025 1026 } else { 1027 // simple block comment: leave intact 1028 if (lines.length > 1) { 1029 // multiline comment block starts with a new line 1030 print_newline(); 1031 trim_output(); 1032 } else { 1033 // single-line /* comment */ stays where it is 1034 print_single_space(); 1035 } 1036 for (i = 0; i < lines.length; i++) { 1037 output.push(lines[i]); 1038 output.push('\n'); 1039 } 1040 1041 } 1042 print_newline(); 1043 break; 1044 1045 case 'TK_INLINE_COMMENT': 1046 1047 print_single_space(); 1048 print_token(); 1049 if (is_expression(flags.mode)) { 1050 print_single_space(); 1051 } else { 1052 print_newline(); 1053 } 1054 break; 1055 1056 case 'TK_COMMENT': 1057 1058 // print_newline(); 1059 if (wanted_newline) { 1060 print_newline(); 1061 } else { 1062 print_single_space(); 1063 } 1064 print_token(); 1065 print_newline(); 1066 break; 1067 1068 case 'TK_UNKNOWN': 1069 print_token(); 1070 break; 1071 } 1072 1073 last_last_text = last_text; 1074 last_type = token_type; 1075 last_text = token_text; 1076 } 1077 1078 return output.join('').replace(/[\n ]+$/, ''); 1079 1080} 1081 1082// Add support for CommonJS. Just put this file somewhere on your require.paths 1083// and you will be able to `var js_beautify = require("beautify").js_beautify`. 1084if (typeof exports !== "undefined") 1085 exports.js_beautify = js_beautify;