michael@0: /*jslint onevar: false, plusplus: false */ michael@0: /*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */ michael@0: /* michael@0: michael@0: JS Beautifier michael@0: --------------- michael@0: michael@0: michael@0: Written by Einar Lielmanis, michael@0: http://jsbeautifier.org/ michael@0: michael@0: Originally converted to javascript by Vital, michael@0: "End braces on own line" added by Chris J. Shull, michael@0: michael@0: You are free to use this in any way you want, in case you find this useful or working for you. michael@0: michael@0: Usage: michael@0: js_beautify(js_source_text); michael@0: js_beautify(js_source_text, options); michael@0: michael@0: The options are: michael@0: indent_size (default 4) - indentation size, michael@0: indent_char (default space) - character to indent with, michael@0: preserve_newlines (default true) - whether existing line breaks should be preserved, michael@0: max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk, michael@0: michael@0: jslint_happy (default false) - if true, then jslint-stricter mode is enforced. michael@0: michael@0: jslint_happy !jslint_happy michael@0: --------------------------------- michael@0: function () function() michael@0: michael@0: brace_style (default "collapse") - "collapse" | "expand" | "end-expand" | "expand-strict" michael@0: 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. michael@0: michael@0: expand-strict: put brace on own line even in such cases: michael@0: michael@0: var a = michael@0: { michael@0: a: 5, michael@0: b: 6 michael@0: } michael@0: This mode may break your scripts - e.g "return { a: 1 }" will be broken into two lines, so beware. michael@0: michael@0: space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)", michael@0: michael@0: unescape_strings (default false) - should printable characters in strings encoded in \xNN notation be unescaped, "example" vs "\x65\x78\x61\x6d\x70\x6c\x65" michael@0: michael@0: e.g michael@0: michael@0: js_beautify(js_source_text, { michael@0: 'indent_size': 1, michael@0: 'indent_char': '\t' michael@0: }); michael@0: michael@0: michael@0: */ michael@0: michael@0: this.EXPORTED_SYMBOLS = ["js_beautify"]; michael@0: michael@0: this.js_beautify = function js_beautify(js_source_text, options) { michael@0: michael@0: var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string; michael@0: var whitespace, wordchar, punct, parser_pos, line_starters, digits; michael@0: var prefix, token_type, do_block_just_closed; michael@0: var wanted_newline, just_added_newline, n_newlines; michael@0: var preindent_string = ''; michael@0: michael@0: michael@0: // Some interpreters have unexpected results with foo = baz || bar; michael@0: options = options ? options : {}; michael@0: michael@0: var opt_brace_style; michael@0: michael@0: // compatibility michael@0: if (options.space_after_anon_function !== undefined && options.jslint_happy === undefined) { michael@0: options.jslint_happy = options.space_after_anon_function; michael@0: } michael@0: if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option michael@0: opt_brace_style = options.braces_on_own_line ? "expand" : "collapse"; michael@0: } michael@0: opt_brace_style = options.brace_style ? options.brace_style : (opt_brace_style ? opt_brace_style : "collapse"); michael@0: michael@0: michael@0: var opt_indent_size = options.indent_size ? options.indent_size : 4; michael@0: var opt_indent_char = options.indent_char ? options.indent_char : ' '; michael@0: var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines; michael@0: var opt_max_preserve_newlines = typeof options.max_preserve_newlines === 'undefined' ? false : options.max_preserve_newlines; michael@0: var opt_jslint_happy = options.jslint_happy === 'undefined' ? false : options.jslint_happy; michael@0: var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? false : options.keep_array_indentation; michael@0: var opt_space_before_conditional = typeof options.space_before_conditional === 'undefined' ? true : options.space_before_conditional; michael@0: var opt_indent_case = typeof options.indent_case === 'undefined' ? false : options.indent_case; michael@0: var opt_unescape_strings = typeof options.unescape_strings === 'undefined' ? false : options.unescape_strings; michael@0: michael@0: just_added_newline = false; michael@0: michael@0: // cache the source's length. michael@0: var input_length = js_source_text.length; michael@0: michael@0: function trim_output(eat_newlines) { michael@0: eat_newlines = typeof eat_newlines === 'undefined' ? false : eat_newlines; michael@0: while (output.length && (output[output.length - 1] === ' ' michael@0: || output[output.length - 1] === indent_string michael@0: || output[output.length - 1] === preindent_string michael@0: || (eat_newlines && (output[output.length - 1] === '\n' || output[output.length - 1] === '\r')))) { michael@0: output.pop(); michael@0: } michael@0: } michael@0: michael@0: function trim(s) { michael@0: return s.replace(/^\s\s*|\s\s*$/, ''); michael@0: } michael@0: michael@0: // we could use just string.split, but michael@0: // IE doesn't like returning empty strings michael@0: function split_newlines(s) { michael@0: return s.split(/\x0d\x0a|\x0a/); michael@0: } michael@0: michael@0: function force_newline() { michael@0: var old_keep_array_indentation = opt_keep_array_indentation; michael@0: opt_keep_array_indentation = false; michael@0: print_newline(); michael@0: opt_keep_array_indentation = old_keep_array_indentation; michael@0: } michael@0: michael@0: function print_newline(ignore_repeated) { michael@0: michael@0: flags.eat_next_space = false; michael@0: if (opt_keep_array_indentation && is_array(flags.mode)) { michael@0: return; michael@0: } michael@0: michael@0: ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated; michael@0: michael@0: flags.if_line = false; michael@0: trim_output(); michael@0: michael@0: if (!output.length) { michael@0: return; // no newline on start of file michael@0: } michael@0: michael@0: if (output[output.length - 1] !== "\n" || !ignore_repeated) { michael@0: just_added_newline = true; michael@0: output.push("\n"); michael@0: } michael@0: if (preindent_string) { michael@0: output.push(preindent_string); michael@0: } michael@0: for (var i = 0; i < flags.indentation_level; i += 1) { michael@0: output.push(indent_string); michael@0: } michael@0: if (flags.var_line && flags.var_line_reindented) { michael@0: output.push(indent_string); // skip space-stuffing, if indenting with a tab michael@0: } michael@0: if (flags.case_body) { michael@0: output.push(indent_string); michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: function print_single_space() { michael@0: michael@0: if (last_type === 'TK_COMMENT') { michael@0: return print_newline(); michael@0: } michael@0: if (flags.eat_next_space) { michael@0: flags.eat_next_space = false; michael@0: return; michael@0: } michael@0: var last_output = ' '; michael@0: if (output.length) { michael@0: last_output = output[output.length - 1]; michael@0: } michael@0: if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space michael@0: output.push(' '); michael@0: } michael@0: } michael@0: michael@0: michael@0: function print_token() { michael@0: just_added_newline = false; michael@0: flags.eat_next_space = false; michael@0: output.push(token_text); michael@0: } michael@0: michael@0: function indent() { michael@0: flags.indentation_level += 1; michael@0: } michael@0: michael@0: michael@0: function remove_indent() { michael@0: if (output.length && output[output.length - 1] === indent_string) { michael@0: output.pop(); michael@0: } michael@0: } michael@0: michael@0: function set_mode(mode) { michael@0: if (flags) { michael@0: flag_store.push(flags); michael@0: } michael@0: flags = { michael@0: previous_mode: flags ? flags.mode : 'BLOCK', michael@0: mode: mode, michael@0: var_line: false, michael@0: var_line_tainted: false, michael@0: var_line_reindented: false, michael@0: in_html_comment: false, michael@0: if_line: false, michael@0: in_case_statement: false, // switch(..){ INSIDE HERE } michael@0: in_case: false, // we're on the exact line with "case 0:" michael@0: case_body: false, // the indented case-action block michael@0: eat_next_space: false, michael@0: indentation_baseline: -1, michael@0: indentation_level: (flags ? flags.indentation_level + (flags.case_body ? 1 : 0) + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : 0), michael@0: ternary_depth: 0 michael@0: }; michael@0: } michael@0: michael@0: function is_array(mode) { michael@0: return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]'; michael@0: } michael@0: michael@0: function is_expression(mode) { michael@0: return in_array(mode, ['[EXPRESSION]', '(EXPRESSION)', '(FOR-EXPRESSION)', '(COND-EXPRESSION)']); michael@0: } michael@0: michael@0: function restore_mode() { michael@0: do_block_just_closed = flags.mode === 'DO_BLOCK'; michael@0: if (flag_store.length > 0) { michael@0: var mode = flags.mode; michael@0: flags = flag_store.pop(); michael@0: flags.previous_mode = mode; michael@0: } michael@0: } michael@0: michael@0: function all_lines_start_with(lines, c) { michael@0: for (var i = 0; i < lines.length; i++) { michael@0: var line = trim(lines[i]); michael@0: if (line.charAt(0) !== c) { michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function is_special_word(word) { michael@0: return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']); michael@0: } michael@0: michael@0: function in_array(what, arr) { michael@0: for (var i = 0; i < arr.length; i += 1) { michael@0: if (arr[i] === what) { michael@0: return true; michael@0: } michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: function look_up(exclude) { michael@0: var local_pos = parser_pos; michael@0: var c = input.charAt(local_pos); michael@0: while (in_array(c, whitespace) && c !== exclude) { michael@0: local_pos++; michael@0: if (local_pos >= input_length) { michael@0: return 0; michael@0: } michael@0: c = input.charAt(local_pos); michael@0: } michael@0: return c; michael@0: } michael@0: michael@0: function get_next_token() { michael@0: var i; michael@0: var resulting_string; michael@0: michael@0: n_newlines = 0; michael@0: michael@0: if (parser_pos >= input_length) { michael@0: return ['', 'TK_EOF']; michael@0: } michael@0: michael@0: wanted_newline = false; michael@0: michael@0: var c = input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: michael@0: michael@0: var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode); michael@0: michael@0: if (keep_whitespace) { michael@0: michael@0: // michael@0: // slight mess to allow nice preservation of array indentation and reindent that correctly michael@0: // first time when we get to the arrays: michael@0: // var a = [ michael@0: // ....'something' michael@0: // we make note of whitespace_count = 4 into flags.indentation_baseline michael@0: // so we know that 4 whitespaces in original source match indent_level of reindented source michael@0: // michael@0: // and afterwards, when we get to michael@0: // 'something, michael@0: // .......'something else' michael@0: // we know that this should be indented to indent_level + (7 - indentation_baseline) spaces michael@0: // michael@0: var whitespace_count = 0; michael@0: michael@0: while (in_array(c, whitespace)) { michael@0: michael@0: if (c === "\n") { michael@0: trim_output(); michael@0: output.push("\n"); michael@0: just_added_newline = true; michael@0: whitespace_count = 0; michael@0: } else { michael@0: if (c === '\t') { michael@0: whitespace_count += 4; michael@0: } else if (c === '\r') { michael@0: // nothing michael@0: } else { michael@0: whitespace_count += 1; michael@0: } michael@0: } michael@0: michael@0: if (parser_pos >= input_length) { michael@0: return ['', 'TK_EOF']; michael@0: } michael@0: michael@0: c = input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: michael@0: } michael@0: if (flags.indentation_baseline === -1) { michael@0: flags.indentation_baseline = whitespace_count; michael@0: } michael@0: michael@0: if (just_added_newline) { michael@0: for (i = 0; i < flags.indentation_level + 1; i += 1) { michael@0: output.push(indent_string); michael@0: } michael@0: if (flags.indentation_baseline !== -1) { michael@0: for (i = 0; i < whitespace_count - flags.indentation_baseline; i++) { michael@0: output.push(' '); michael@0: } michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: while (in_array(c, whitespace)) { michael@0: michael@0: if (c === "\n") { michael@0: n_newlines += ((opt_max_preserve_newlines) ? (n_newlines <= opt_max_preserve_newlines) ? 1 : 0 : 1); michael@0: } michael@0: michael@0: michael@0: if (parser_pos >= input_length) { michael@0: return ['', 'TK_EOF']; michael@0: } michael@0: michael@0: c = input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: michael@0: } michael@0: michael@0: if (opt_preserve_newlines) { michael@0: if (n_newlines > 1) { michael@0: for (i = 0; i < n_newlines; i += 1) { michael@0: print_newline(i === 0); michael@0: just_added_newline = true; michael@0: } michael@0: } michael@0: } michael@0: wanted_newline = n_newlines > 0; michael@0: } michael@0: michael@0: michael@0: if (in_array(c, wordchar)) { michael@0: if (parser_pos < input_length) { michael@0: while (in_array(input.charAt(parser_pos), wordchar)) { michael@0: c += input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: if (parser_pos === input_length) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // small and surprisingly unugly hack for 1E-10 representation michael@0: if (parser_pos !== input_length && c.match(/^[0-9]+[Ee]$/) && (input.charAt(parser_pos) === '-' || input.charAt(parser_pos) === '+')) { michael@0: michael@0: var sign = input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: michael@0: var t = get_next_token(); michael@0: c += sign + t[0]; michael@0: return [c, 'TK_WORD']; michael@0: } michael@0: michael@0: if (c === 'in') { // hack for 'in' operator michael@0: return [c, 'TK_OPERATOR']; michael@0: } michael@0: if (wanted_newline && last_type !== 'TK_OPERATOR' michael@0: && last_type !== 'TK_EQUALS' michael@0: && !flags.if_line && (opt_preserve_newlines || last_text !== 'var')) { michael@0: print_newline(); michael@0: } michael@0: return [c, 'TK_WORD']; michael@0: } michael@0: michael@0: if (c === '(' || c === '[') { michael@0: return [c, 'TK_START_EXPR']; michael@0: } michael@0: michael@0: if (c === ')' || c === ']') { michael@0: return [c, 'TK_END_EXPR']; michael@0: } michael@0: michael@0: if (c === '{') { michael@0: return [c, 'TK_START_BLOCK']; michael@0: } michael@0: michael@0: if (c === '}') { michael@0: return [c, 'TK_END_BLOCK']; michael@0: } michael@0: michael@0: if (c === ';') { michael@0: return [c, 'TK_SEMICOLON']; michael@0: } michael@0: michael@0: if (c === '/') { michael@0: var comment = ''; michael@0: // peek for comment /* ... */ michael@0: var inline_comment = true; michael@0: if (input.charAt(parser_pos) === '*') { michael@0: parser_pos += 1; michael@0: if (parser_pos < input_length) { michael@0: while (parser_pos < input_length && michael@0: ! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/')) { michael@0: c = input.charAt(parser_pos); michael@0: comment += c; michael@0: if (c === "\n" || c === "\r") { michael@0: inline_comment = false; michael@0: } michael@0: parser_pos += 1; michael@0: if (parser_pos >= input_length) { michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: parser_pos += 2; michael@0: if (inline_comment && n_newlines === 0) { michael@0: return ['/*' + comment + '*/', 'TK_INLINE_COMMENT']; michael@0: } else { michael@0: return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT']; michael@0: } michael@0: } michael@0: // peek for comment // ... michael@0: if (input.charAt(parser_pos) === '/') { michael@0: comment = c; michael@0: while (input.charAt(parser_pos) !== '\r' && input.charAt(parser_pos) !== '\n') { michael@0: comment += input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: if (parser_pos >= input_length) { michael@0: break; michael@0: } michael@0: } michael@0: if (wanted_newline) { michael@0: print_newline(); michael@0: } michael@0: return [comment, 'TK_COMMENT']; michael@0: } michael@0: michael@0: } michael@0: michael@0: if (c === "'" || // string michael@0: c === '"' || // string michael@0: (c === '/' && michael@0: ((last_type === 'TK_WORD' && is_special_word(last_text)) || michael@0: (last_text === ')' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) || michael@0: (last_type === 'TK_COMMA' || last_type === 'TK_COMMENT' || 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 michael@0: var sep = c; michael@0: var esc = false; michael@0: var esc1 = 0; michael@0: var esc2 = 0; michael@0: resulting_string = c; michael@0: michael@0: if (parser_pos < input_length) { michael@0: if (sep === '/') { michael@0: // michael@0: // handle regexp separately... michael@0: // michael@0: var in_char_class = false; michael@0: while (esc || in_char_class || input.charAt(parser_pos) !== sep) { michael@0: resulting_string += input.charAt(parser_pos); michael@0: if (!esc) { michael@0: esc = input.charAt(parser_pos) === '\\'; michael@0: if (input.charAt(parser_pos) === '[') { michael@0: in_char_class = true; michael@0: } else if (input.charAt(parser_pos) === ']') { michael@0: in_char_class = false; michael@0: } michael@0: } else { michael@0: esc = false; michael@0: } michael@0: parser_pos += 1; michael@0: if (parser_pos >= input_length) { michael@0: // incomplete string/rexp when end-of-file reached. michael@0: // bail out with what had been received so far. michael@0: return [resulting_string, 'TK_STRING']; michael@0: } michael@0: } michael@0: michael@0: } else { michael@0: // michael@0: // and handle string also separately michael@0: // michael@0: while (esc || input.charAt(parser_pos) !== sep) { michael@0: resulting_string += input.charAt(parser_pos); michael@0: if (esc1 && esc1 >= esc2) { michael@0: esc1 = parseInt(resulting_string.substr(-esc2), 16); michael@0: if (esc1 && esc1 >= 0x20 && esc1 <= 0x7e) { michael@0: esc1 = String.fromCharCode(esc1); michael@0: resulting_string = resulting_string.substr(0, resulting_string.length - esc2 - 2) + (((esc1 === sep) || (esc1 === '\\')) ? '\\' : '') + esc1; michael@0: } michael@0: esc1 = 0; michael@0: } michael@0: if (esc1) { michael@0: esc1++; michael@0: } else if (!esc) { michael@0: esc = input.charAt(parser_pos) === '\\'; michael@0: } else { michael@0: esc = false; michael@0: if (opt_unescape_strings) { michael@0: if (input.charAt(parser_pos) === 'x') { michael@0: esc1++; michael@0: esc2 = 2; michael@0: } else if (input.charAt(parser_pos) === 'u') { michael@0: esc1++; michael@0: esc2 = 4; michael@0: } michael@0: } michael@0: } michael@0: parser_pos += 1; michael@0: if (parser_pos >= input_length) { michael@0: // incomplete string/rexp when end-of-file reached. michael@0: // bail out with what had been received so far. michael@0: return [resulting_string, 'TK_STRING']; michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: michael@0: } michael@0: michael@0: parser_pos += 1; michael@0: michael@0: resulting_string += sep; michael@0: michael@0: if (sep === '/') { michael@0: // regexps may have modifiers /regexp/MOD , so fetch those, too michael@0: while (parser_pos < input_length && in_array(input.charAt(parser_pos), wordchar)) { michael@0: resulting_string += input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: } michael@0: } michael@0: return [resulting_string, 'TK_STRING']; michael@0: } michael@0: michael@0: if (c === '#') { michael@0: michael@0: michael@0: if (output.length === 0 && input.charAt(parser_pos) === '!') { michael@0: // shebang michael@0: resulting_string = c; michael@0: while (parser_pos < input_length && c !== '\n') { michael@0: c = input.charAt(parser_pos); michael@0: resulting_string += c; michael@0: parser_pos += 1; michael@0: } michael@0: output.push(trim(resulting_string) + '\n'); michael@0: print_newline(); michael@0: return get_next_token(); michael@0: } michael@0: michael@0: michael@0: michael@0: // Spidermonkey-specific sharp variables for circular references michael@0: // https://developer.mozilla.org/En/Sharp_variables_in_JavaScript michael@0: // http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935 michael@0: var sharp = '#'; michael@0: if (parser_pos < input_length && in_array(input.charAt(parser_pos), digits)) { michael@0: do { michael@0: c = input.charAt(parser_pos); michael@0: sharp += c; michael@0: parser_pos += 1; michael@0: } while (parser_pos < input_length && c !== '#' && c !== '='); michael@0: if (c === '#') { michael@0: // michael@0: } else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') { michael@0: sharp += '[]'; michael@0: parser_pos += 2; michael@0: } else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') { michael@0: sharp += '{}'; michael@0: parser_pos += 2; michael@0: } michael@0: return [sharp, 'TK_WORD']; michael@0: } michael@0: } michael@0: michael@0: if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '') { michael@0: flags.in_html_comment = false; michael@0: parser_pos += 2; michael@0: if (wanted_newline) { michael@0: print_newline(); michael@0: } michael@0: return ['-->', 'TK_COMMENT']; michael@0: } michael@0: michael@0: if (in_array(c, punct)) { michael@0: while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) { michael@0: c += input.charAt(parser_pos); michael@0: parser_pos += 1; michael@0: if (parser_pos >= input_length) { michael@0: break; michael@0: } michael@0: } michael@0: michael@0: if (c === ',') { michael@0: return [c, 'TK_COMMA']; michael@0: } else if (c === '=') { michael@0: return [c, 'TK_EQUALS']; michael@0: } else { michael@0: return [c, 'TK_OPERATOR']; michael@0: } michael@0: } michael@0: michael@0: return [c, 'TK_UNKNOWN']; michael@0: } michael@0: michael@0: //---------------------------------- michael@0: indent_string = ''; michael@0: while (opt_indent_size > 0) { michael@0: indent_string += opt_indent_char; michael@0: opt_indent_size -= 1; michael@0: } michael@0: michael@0: while (js_source_text && (js_source_text.charAt(0) === ' ' || js_source_text.charAt(0) === '\t')) { michael@0: preindent_string += js_source_text.charAt(0); michael@0: js_source_text = js_source_text.substring(1); michael@0: } michael@0: input = js_source_text; michael@0: michael@0: last_word = ''; // last 'TK_WORD' passed michael@0: last_type = 'TK_START_EXPR'; // last token type michael@0: last_text = ''; // last token text michael@0: last_last_text = ''; // pre-last token text michael@0: output = []; michael@0: michael@0: do_block_just_closed = false; michael@0: michael@0: whitespace = "\n\r\t ".split(''); michael@0: wordchar = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$'.split(''); michael@0: digits = '0123456789'.split(''); michael@0: michael@0: punct = '+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::'; michael@0: punct += ' <%= <% %> '; // try to be a good boy and try not to break the markup language identifiers michael@0: punct = punct.split(' '); michael@0: michael@0: // words which should always start on new line. michael@0: line_starters = 'continue,try,throw,return,var,if,switch,case,default,for,while,break,function'.split(','); michael@0: michael@0: // states showing if we are currently in expression (i.e. "if" case) - 'EXPRESSION', or in usual block (like, procedure), 'BLOCK'. michael@0: // some formatting depends on that. michael@0: flag_store = []; michael@0: set_mode('BLOCK'); michael@0: michael@0: parser_pos = 0; michael@0: while (true) { michael@0: var t = get_next_token(); michael@0: token_text = t[0]; michael@0: token_type = t[1]; michael@0: if (token_type === 'TK_EOF') { michael@0: break; michael@0: } michael@0: michael@0: switch (token_type) { michael@0: michael@0: case 'TK_START_EXPR': michael@0: michael@0: if (token_text === '[') { michael@0: michael@0: if (last_type === 'TK_WORD' || last_text === ')') { michael@0: // this is array index specifier, break immediately michael@0: // a[x], fn()[x] michael@0: if (in_array(last_text, line_starters)) { michael@0: print_single_space(); michael@0: } michael@0: set_mode('(EXPRESSION)'); michael@0: print_token(); michael@0: break; michael@0: } michael@0: michael@0: if (flags.mode === '[EXPRESSION]' || flags.mode === '[INDENTED-EXPRESSION]') { michael@0: if (last_last_text === ']' && last_text === ',') { michael@0: // ], [ goes to new line michael@0: if (flags.mode === '[EXPRESSION]') { michael@0: flags.mode = '[INDENTED-EXPRESSION]'; michael@0: if (!opt_keep_array_indentation) { michael@0: indent(); michael@0: } michael@0: } michael@0: set_mode('[EXPRESSION]'); michael@0: if (!opt_keep_array_indentation) { michael@0: print_newline(); michael@0: } michael@0: } else if (last_text === '[') { michael@0: if (flags.mode === '[EXPRESSION]') { michael@0: flags.mode = '[INDENTED-EXPRESSION]'; michael@0: if (!opt_keep_array_indentation) { michael@0: indent(); michael@0: } michael@0: } michael@0: set_mode('[EXPRESSION]'); michael@0: michael@0: if (!opt_keep_array_indentation) { michael@0: print_newline(); michael@0: } michael@0: } else { michael@0: set_mode('[EXPRESSION]'); michael@0: } michael@0: } else { michael@0: set_mode('[EXPRESSION]'); michael@0: } michael@0: michael@0: michael@0: michael@0: } else { michael@0: if (last_word === 'for') { michael@0: set_mode('(FOR-EXPRESSION)'); michael@0: } else if (in_array(last_word, ['if', 'while'])) { michael@0: set_mode('(COND-EXPRESSION)'); michael@0: } else { michael@0: set_mode('(EXPRESSION)'); michael@0: } michael@0: } michael@0: michael@0: if (last_text === ';' || last_type === 'TK_START_BLOCK') { michael@0: print_newline(); michael@0: } else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_text === '.') { michael@0: if (wanted_newline) { michael@0: print_newline(); michael@0: } michael@0: // do nothing on (( and )( and ][ and ]( and .( michael@0: } else if (last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { michael@0: print_single_space(); michael@0: } else if (last_word === 'function' || last_word === 'typeof') { michael@0: // function() vs function () michael@0: if (opt_jslint_happy) { michael@0: print_single_space(); michael@0: } michael@0: } else if (in_array(last_text, line_starters) || last_text === 'catch') { michael@0: if (opt_space_before_conditional) { michael@0: print_single_space(); michael@0: } michael@0: } michael@0: print_token(); michael@0: michael@0: break; michael@0: michael@0: case 'TK_END_EXPR': michael@0: if (token_text === ']') { michael@0: if (opt_keep_array_indentation) { michael@0: if (last_text === '}') { michael@0: // trim_output(); michael@0: // print_newline(true); michael@0: remove_indent(); michael@0: print_token(); michael@0: restore_mode(); michael@0: break; michael@0: } michael@0: } else { michael@0: if (flags.mode === '[INDENTED-EXPRESSION]') { michael@0: if (last_text === ']') { michael@0: restore_mode(); michael@0: print_newline(); michael@0: print_token(); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: restore_mode(); michael@0: print_token(); michael@0: break; michael@0: michael@0: case 'TK_START_BLOCK': michael@0: michael@0: if (last_word === 'do') { michael@0: set_mode('DO_BLOCK'); michael@0: } else { michael@0: set_mode('BLOCK'); michael@0: } michael@0: if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") { michael@0: var empty_braces = false; michael@0: if (opt_brace_style === "expand-strict") { michael@0: empty_braces = (look_up() === '}'); michael@0: if (!empty_braces) { michael@0: print_newline(true); michael@0: } michael@0: } else { michael@0: if (last_type !== 'TK_OPERATOR') { michael@0: if (last_text === '=' || (is_special_word(last_text) && last_text !== 'else')) { michael@0: print_single_space(); michael@0: } else { michael@0: print_newline(true); michael@0: } michael@0: } michael@0: } michael@0: print_token(); michael@0: if (!empty_braces) { michael@0: indent(); michael@0: } michael@0: } else { michael@0: if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') { michael@0: if (last_type === 'TK_START_BLOCK') { michael@0: print_newline(); michael@0: } else { michael@0: print_single_space(); michael@0: } michael@0: } else { michael@0: // if TK_OPERATOR or TK_START_EXPR michael@0: if (is_array(flags.previous_mode) && last_text === ',') { michael@0: if (last_last_text === '}') { michael@0: // }, { in array context michael@0: print_single_space(); michael@0: } else { michael@0: print_newline(); // [a, b, c, { michael@0: } michael@0: } michael@0: } michael@0: indent(); michael@0: print_token(); michael@0: } michael@0: michael@0: break; michael@0: michael@0: case 'TK_END_BLOCK': michael@0: restore_mode(); michael@0: if (opt_brace_style === "expand" || opt_brace_style === "expand-strict") { michael@0: if (last_text !== '{') { michael@0: print_newline(); michael@0: } michael@0: print_token(); michael@0: } else { michael@0: if (last_type === 'TK_START_BLOCK') { michael@0: // nothing michael@0: if (just_added_newline) { michael@0: remove_indent(); michael@0: } else { michael@0: // {} michael@0: trim_output(); michael@0: } michael@0: } else { michael@0: if (is_array(flags.mode) && opt_keep_array_indentation) { michael@0: // we REALLY need a newline here, but newliner would skip that michael@0: opt_keep_array_indentation = false; michael@0: print_newline(); michael@0: opt_keep_array_indentation = true; michael@0: michael@0: } else { michael@0: print_newline(); michael@0: } michael@0: } michael@0: print_token(); michael@0: } michael@0: break; michael@0: michael@0: case 'TK_WORD': michael@0: michael@0: // no, it's not you. even I have problems understanding how this works michael@0: // and what does what. michael@0: if (do_block_just_closed) { michael@0: // do {} ## while () michael@0: print_single_space(); michael@0: print_token(); michael@0: print_single_space(); michael@0: do_block_just_closed = false; michael@0: break; michael@0: } michael@0: michael@0: prefix = 'NONE'; michael@0: michael@0: if (token_text === 'function') { michael@0: if (flags.var_line && last_type !== 'TK_EQUALS' ) { michael@0: flags.var_line_reindented = true; michael@0: } michael@0: if ((just_added_newline || last_text === ';') && last_text !== '{' michael@0: && last_type !== 'TK_BLOCK_COMMENT' && last_type !== 'TK_COMMENT') { michael@0: // make sure there is a nice clean space of at least one blank line michael@0: // before a new function definition michael@0: n_newlines = just_added_newline ? n_newlines : 0; michael@0: if (!opt_preserve_newlines) { michael@0: n_newlines = 1; michael@0: } michael@0: michael@0: for (var i = 0; i < 2 - n_newlines; i++) { michael@0: print_newline(false); michael@0: } michael@0: } michael@0: if (last_type === 'TK_WORD') { michael@0: if (last_text === 'get' || last_text === 'set' || last_text === 'new' || last_text === 'return') { michael@0: print_single_space(); michael@0: } else { michael@0: print_newline(); michael@0: } michael@0: } else if (last_type === 'TK_OPERATOR' || last_text === '=') { michael@0: // foo = function michael@0: print_single_space(); michael@0: } else if (is_expression(flags.mode)) { michael@0: //รครค print nothing michael@0: } else { michael@0: print_newline(); michael@0: } michael@0: michael@0: print_token(); michael@0: last_word = token_text; michael@0: break; michael@0: } michael@0: michael@0: if (token_text === 'case' || (token_text === 'default' && flags.in_case_statement)) { michael@0: if (last_text === ':' || flags.case_body) { michael@0: // switch cases following one another michael@0: remove_indent(); michael@0: } else { michael@0: // case statement starts in the same line where switch michael@0: if (!opt_indent_case) { michael@0: flags.indentation_level--; michael@0: } michael@0: print_newline(); michael@0: if (!opt_indent_case) { michael@0: flags.indentation_level++; michael@0: } michael@0: } michael@0: print_token(); michael@0: flags.in_case = true; michael@0: flags.in_case_statement = true; michael@0: flags.case_body = false; michael@0: break; michael@0: } michael@0: michael@0: if (last_type === 'TK_END_BLOCK') { michael@0: michael@0: if (!in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { michael@0: prefix = 'NEWLINE'; michael@0: } else { michael@0: if (opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") { michael@0: prefix = 'NEWLINE'; michael@0: } else { michael@0: prefix = 'SPACE'; michael@0: print_single_space(); michael@0: } michael@0: } michael@0: } else if (last_type === 'TK_SEMICOLON' && (flags.mode === 'BLOCK' || flags.mode === 'DO_BLOCK')) { michael@0: prefix = 'NEWLINE'; michael@0: } else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) { michael@0: prefix = 'SPACE'; michael@0: } else if (last_type === 'TK_STRING') { michael@0: prefix = 'NEWLINE'; michael@0: } else if (last_type === 'TK_WORD') { michael@0: if (last_text === 'else') { michael@0: // eat newlines between ...else *** some_op... michael@0: // won't preserve extra newlines in this place (if any), but don't care that much michael@0: trim_output(true); michael@0: } michael@0: prefix = 'SPACE'; michael@0: } else if (last_type === 'TK_START_BLOCK') { michael@0: prefix = 'NEWLINE'; michael@0: } else if (last_type === 'TK_END_EXPR') { michael@0: print_single_space(); michael@0: prefix = 'NEWLINE'; michael@0: } michael@0: michael@0: if (in_array(token_text, line_starters) && last_text !== ')') { michael@0: if (last_text === 'else') { michael@0: prefix = 'SPACE'; michael@0: } else { michael@0: prefix = 'NEWLINE'; michael@0: } michael@0: michael@0: } michael@0: michael@0: if (flags.if_line && last_type === 'TK_END_EXPR') { michael@0: flags.if_line = false; michael@0: } michael@0: if (in_array(token_text.toLowerCase(), ['else', 'catch', 'finally'])) { michael@0: if (last_type !== 'TK_END_BLOCK' || opt_brace_style === "expand" || opt_brace_style === "end-expand" || opt_brace_style === "expand-strict") { michael@0: print_newline(); michael@0: } else { michael@0: trim_output(true); michael@0: print_single_space(); michael@0: } michael@0: } else if (prefix === 'NEWLINE') { michael@0: if (is_special_word(last_text)) { michael@0: // no newline between 'return nnn' michael@0: print_single_space(); michael@0: } else if (last_type !== 'TK_END_EXPR') { michael@0: if ((last_type !== 'TK_START_EXPR' || token_text !== 'var') && last_text !== ':') { michael@0: // no need to force newline on 'var': for (var x = 0...) michael@0: if (token_text === 'if' && last_word === 'else' && last_text !== '{') { michael@0: // no newline for } else if { michael@0: print_single_space(); michael@0: } else { michael@0: flags.var_line = false; michael@0: flags.var_line_reindented = false; michael@0: print_newline(); michael@0: } michael@0: } michael@0: } else if (in_array(token_text, line_starters) && last_text !== ')') { michael@0: flags.var_line = false; michael@0: flags.var_line_reindented = false; michael@0: print_newline(); michael@0: } michael@0: } else if (is_array(flags.mode) && last_text === ',' && last_last_text === '}') { michael@0: print_newline(); // }, in lists get a newline treatment michael@0: } else if (prefix === 'SPACE') { michael@0: print_single_space(); michael@0: } michael@0: print_token(); michael@0: last_word = token_text; michael@0: michael@0: if (token_text === 'var') { michael@0: flags.var_line = true; michael@0: flags.var_line_reindented = false; michael@0: flags.var_line_tainted = false; michael@0: } michael@0: michael@0: if (token_text === 'if') { michael@0: flags.if_line = true; michael@0: } michael@0: if (token_text === 'else') { michael@0: flags.if_line = false; michael@0: } michael@0: michael@0: break; michael@0: michael@0: case 'TK_SEMICOLON': michael@0: michael@0: print_token(); michael@0: flags.var_line = false; michael@0: flags.var_line_reindented = false; michael@0: if (flags.mode === 'OBJECT') { michael@0: // OBJECT mode is weird and doesn't get reset too well. michael@0: flags.mode = 'BLOCK'; michael@0: } michael@0: break; michael@0: michael@0: case 'TK_STRING': michael@0: michael@0: if (last_type === 'TK_END_EXPR' && in_array(flags.previous_mode, ['(COND-EXPRESSION)', '(FOR-EXPRESSION)'])) { michael@0: print_single_space(); michael@0: } else if (last_type === 'TK_COMMENT' || last_type === 'TK_STRING' || last_type === 'TK_START_BLOCK' || last_type === 'TK_END_BLOCK' || last_type === 'TK_SEMICOLON') { michael@0: print_newline(); michael@0: } else if (last_type === 'TK_WORD') { michael@0: print_single_space(); michael@0: } michael@0: print_token(); michael@0: break; michael@0: michael@0: case 'TK_EQUALS': michael@0: if (flags.var_line) { michael@0: // just got an '=' in a var-line, different formatting/line-breaking, etc will now be done michael@0: flags.var_line_tainted = true; michael@0: } michael@0: print_single_space(); michael@0: print_token(); michael@0: print_single_space(); michael@0: break; michael@0: michael@0: case 'TK_COMMA': michael@0: if (flags.var_line) { michael@0: if (is_expression(flags.mode) || last_type === 'TK_END_BLOCK' ) { michael@0: // do not break on comma, for(var a = 1, b = 2) michael@0: flags.var_line_tainted = false; michael@0: } michael@0: if (flags.var_line_tainted) { michael@0: print_token(); michael@0: flags.var_line_reindented = true; michael@0: flags.var_line_tainted = false; michael@0: print_newline(); michael@0: break; michael@0: } else { michael@0: flags.var_line_tainted = false; michael@0: } michael@0: michael@0: print_token(); michael@0: print_single_space(); michael@0: break; michael@0: } michael@0: michael@0: if (last_type === 'TK_COMMENT') { michael@0: print_newline(); michael@0: } michael@0: michael@0: if (last_type === 'TK_END_BLOCK' && flags.mode !== "(EXPRESSION)") { michael@0: print_token(); michael@0: if (flags.mode === 'OBJECT' && last_text === '}') { michael@0: print_newline(); michael@0: } else { michael@0: print_single_space(); michael@0: } michael@0: } else { michael@0: if (flags.mode === 'OBJECT') { michael@0: print_token(); michael@0: print_newline(); michael@0: } else { michael@0: // EXPR or DO_BLOCK michael@0: print_token(); michael@0: print_single_space(); michael@0: } michael@0: } michael@0: break; michael@0: michael@0: michael@0: case 'TK_OPERATOR': michael@0: michael@0: var space_before = true; michael@0: var space_after = true; michael@0: michael@0: if (is_special_word(last_text)) { michael@0: // "return" had a special handling in TK_WORD. Now we need to return the favor michael@0: print_single_space(); michael@0: print_token(); michael@0: break; michael@0: } michael@0: michael@0: // hack for actionscript's import .*; michael@0: if (token_text === '*' && last_type === 'TK_UNKNOWN' && !last_last_text.match(/^\d+$/)) { michael@0: print_token(); michael@0: break; michael@0: } michael@0: michael@0: if (token_text === ':' && flags.in_case) { michael@0: if (opt_indent_case) { michael@0: flags.case_body = true; michael@0: } michael@0: print_token(); // colon really asks for separate treatment michael@0: print_newline(); michael@0: flags.in_case = false; michael@0: break; michael@0: } michael@0: michael@0: if (token_text === '::') { michael@0: // no spaces around exotic namespacing syntax operator michael@0: print_token(); michael@0: break; michael@0: } michael@0: michael@0: 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)))) { michael@0: // unary operators (and binary +/- pretending to be unary) special cases michael@0: michael@0: space_before = false; michael@0: space_after = false; michael@0: michael@0: if (last_text === ';' && is_expression(flags.mode)) { michael@0: // for (;; ++i) michael@0: // ^^^ michael@0: space_before = true; michael@0: } michael@0: if (last_type === 'TK_WORD' && in_array(last_text, line_starters)) { michael@0: space_before = true; michael@0: } michael@0: michael@0: if (flags.mode === 'BLOCK' && (last_text === '{' || last_text === ';')) { michael@0: // { foo; --i } michael@0: // foo(); --bar; michael@0: print_newline(); michael@0: } michael@0: } else if (token_text === '.') { michael@0: // decimal digits or object.property michael@0: space_before = false; michael@0: michael@0: } else if (token_text === ':') { michael@0: if (flags.ternary_depth === 0) { michael@0: if (flags.mode === 'BLOCK') { michael@0: flags.mode = 'OBJECT'; michael@0: } michael@0: space_before = false; michael@0: } else { michael@0: flags.ternary_depth -= 1; michael@0: } michael@0: } else if (token_text === '?') { michael@0: flags.ternary_depth += 1; michael@0: } michael@0: if (space_before) { michael@0: print_single_space(); michael@0: } michael@0: michael@0: print_token(); michael@0: michael@0: if (space_after) { michael@0: print_single_space(); michael@0: } michael@0: michael@0: break; michael@0: michael@0: case 'TK_BLOCK_COMMENT': michael@0: michael@0: var lines = split_newlines(token_text); michael@0: var j; // iterator for this case michael@0: michael@0: if (all_lines_start_with(lines.slice(1), '*')) { michael@0: // javadoc: reformat and reindent michael@0: print_newline(); michael@0: output.push(lines[0]); michael@0: for (j = 1; j < lines.length; j++) { michael@0: print_newline(); michael@0: output.push(' '); michael@0: output.push(trim(lines[j])); michael@0: } michael@0: michael@0: } else { michael@0: michael@0: // simple block comment: leave intact michael@0: if (lines.length > 1) { michael@0: // multiline comment block starts with a new line michael@0: print_newline(); michael@0: } else { michael@0: // single-line /* comment */ stays where it is michael@0: if (last_type === 'TK_END_BLOCK') { michael@0: print_newline(); michael@0: } else { michael@0: print_single_space(); michael@0: } michael@0: michael@0: } michael@0: michael@0: for (j = 0; j < lines.length; j++) { michael@0: output.push(lines[j]); michael@0: output.push("\n"); michael@0: } michael@0: michael@0: } michael@0: if (look_up('\n') !== '\n') { michael@0: print_newline(); michael@0: } michael@0: break; michael@0: michael@0: case 'TK_INLINE_COMMENT': michael@0: print_single_space(); michael@0: print_token(); michael@0: if (is_expression(flags.mode)) { michael@0: print_single_space(); michael@0: } else { michael@0: force_newline(); michael@0: } michael@0: break; michael@0: michael@0: case 'TK_COMMENT': michael@0: michael@0: if (last_text === ',' && !wanted_newline) { michael@0: trim_output(true); michael@0: } michael@0: if (last_type !== 'TK_COMMENT') { michael@0: if (wanted_newline) { michael@0: print_newline(); michael@0: } else { michael@0: print_single_space(); michael@0: } michael@0: } michael@0: print_token(); michael@0: print_newline(); michael@0: break; michael@0: michael@0: case 'TK_UNKNOWN': michael@0: if (is_special_word(last_text)) { michael@0: print_single_space(); michael@0: } michael@0: print_token(); michael@0: break; michael@0: } michael@0: michael@0: last_last_text = last_text; michael@0: last_type = token_type; michael@0: last_text = token_text; michael@0: } michael@0: michael@0: var sweet_code = preindent_string + output.join('').replace(/[\r\n ]+$/, ''); michael@0: return sweet_code; michael@0: michael@0: }