|
@@ -0,0 +1,1462 @@
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ The MIT License (MIT)
|
|
|
|
+
|
|
|
|
+ Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
|
|
|
|
+
|
|
|
|
+ Permission is hereby granted, free of charge, to any person
|
|
|
|
+ obtaining a copy of this software and associated documentation files
|
|
|
|
+ (the "Software"), to deal in the Software without restriction,
|
|
|
|
+ including without limitation the rights to use, copy, modify, merge,
|
|
|
|
+ publish, distribute, sublicense, and/or sell copies of the Software,
|
|
|
|
+ and to permit persons to whom the Software is furnished to do so,
|
|
|
|
+ subject to the following conditions:
|
|
|
|
+
|
|
|
|
+ The above copyright notice and this permission notice shall be
|
|
|
|
+ included in all copies or substantial portions of the Software.
|
|
|
|
+
|
|
|
|
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
|
|
|
+ BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
|
|
+ ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
|
|
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
+ SOFTWARE.
|
|
|
|
+*/
|
|
|
|
+
|
|
|
|
+'use strict';
|
|
|
|
+
|
|
|
|
+var Output = require('../core/output').Output;
|
|
|
|
+var Token = require('../core/token').Token;
|
|
|
|
+var acorn = require('./acorn');
|
|
|
|
+var Options = require('./options').Options;
|
|
|
|
+var Tokenizer = require('./tokenizer').Tokenizer;
|
|
|
|
+var line_starters = require('./tokenizer').line_starters;
|
|
|
|
+var positionable_operators = require('./tokenizer').positionable_operators;
|
|
|
|
+var TOKEN = require('./tokenizer').TOKEN;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function in_array(what, arr) {
|
|
|
|
+ return arr.indexOf(what) !== -1;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function ltrim(s) {
|
|
|
|
+ return s.replace(/^\s+/g, '');
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function generateMapFromStrings(list) {
|
|
|
|
+ var result = {};
|
|
|
|
+ for (var x = 0; x < list.length; x++) {
|
|
|
|
+
|
|
|
|
+ result[list[x].replace(/-/g, '_')] = list[x];
|
|
|
|
+ }
|
|
|
|
+ return result;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function reserved_word(token, word) {
|
|
|
|
+ return token && token.type === TOKEN.RESERVED && token.text === word;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function reserved_array(token, words) {
|
|
|
|
+ return token && token.type === TOKEN.RESERVED && in_array(token.text, words);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+var special_words = ['case', 'return', 'do', 'if', 'throw', 'else', 'await', 'break', 'continue', 'async'];
|
|
|
|
+
|
|
|
|
+var validPositionValues = ['before-newline', 'after-newline', 'preserve-newline'];
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+var OPERATOR_POSITION = generateMapFromStrings(validPositionValues);
|
|
|
|
+
|
|
|
|
+var OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION.before_newline, OPERATOR_POSITION.preserve_newline];
|
|
|
|
+
|
|
|
|
+var MODE = {
|
|
|
|
+ BlockStatement: 'BlockStatement',
|
|
|
|
+ Statement: 'Statement',
|
|
|
|
+ ObjectLiteral: 'ObjectLiteral',
|
|
|
|
+ ArrayLiteral: 'ArrayLiteral',
|
|
|
|
+ ForInitializer: 'ForInitializer',
|
|
|
|
+ Conditional: 'Conditional',
|
|
|
|
+ Expression: 'Expression'
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+function remove_redundant_indentation(output, frame) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (frame.multiline_frame ||
|
|
|
|
+ frame.mode === MODE.ForInitializer ||
|
|
|
|
+ frame.mode === MODE.Conditional) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ output.remove_indent(frame.start_line_index);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function split_linebreaks(s) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ s = s.replace(acorn.allLineBreaks, '\n');
|
|
|
|
+ var out = [],
|
|
|
|
+ idx = s.indexOf("\n");
|
|
|
|
+ while (idx !== -1) {
|
|
|
|
+ out.push(s.substring(0, idx));
|
|
|
|
+ s = s.substring(idx + 1);
|
|
|
|
+ idx = s.indexOf("\n");
|
|
|
|
+ }
|
|
|
|
+ if (s.length) {
|
|
|
|
+ out.push(s);
|
|
|
|
+ }
|
|
|
|
+ return out;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function is_array(mode) {
|
|
|
|
+ return mode === MODE.ArrayLiteral;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function is_expression(mode) {
|
|
|
|
+ return in_array(mode, [MODE.Expression, MODE.ForInitializer, MODE.Conditional]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function all_lines_start_with(lines, c) {
|
|
|
|
+ for (var i = 0; i < lines.length; i++) {
|
|
|
|
+ var line = lines[i].trim();
|
|
|
|
+ if (line.charAt(0) !== c) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+function each_line_matches_indent(lines, indent) {
|
|
|
|
+ var i = 0,
|
|
|
|
+ len = lines.length,
|
|
|
|
+ line;
|
|
|
|
+ for (; i < len; i++) {
|
|
|
|
+ line = lines[i];
|
|
|
|
+
|
|
|
|
+ if (line && line.indexOf(indent) !== 0) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+function Beautifier(source_text, options) {
|
|
|
|
+ options = options || {};
|
|
|
|
+ this._source_text = source_text || '';
|
|
|
|
+
|
|
|
|
+ this._output = null;
|
|
|
|
+ this._tokens = null;
|
|
|
|
+ this._last_last_text = null;
|
|
|
|
+ this._flags = null;
|
|
|
|
+ this._previous_flags = null;
|
|
|
|
+
|
|
|
|
+ this._flag_store = null;
|
|
|
|
+ this._options = new Options(options);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.create_flags = function(flags_base, mode) {
|
|
|
|
+ var next_indent_level = 0;
|
|
|
|
+ if (flags_base) {
|
|
|
|
+ next_indent_level = flags_base.indentation_level;
|
|
|
|
+ if (!this._output.just_added_newline() &&
|
|
|
|
+ flags_base.line_indent_level > next_indent_level) {
|
|
|
|
+ next_indent_level = flags_base.line_indent_level;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var next_flags = {
|
|
|
|
+ mode: mode,
|
|
|
|
+ parent: flags_base,
|
|
|
|
+ last_token: flags_base ? flags_base.last_token : new Token(TOKEN.START_BLOCK, ''),
|
|
|
|
+ last_word: flags_base ? flags_base.last_word : '',
|
|
|
|
+ declaration_statement: false,
|
|
|
|
+ declaration_assignment: false,
|
|
|
|
+ multiline_frame: false,
|
|
|
|
+ inline_frame: false,
|
|
|
|
+ if_block: false,
|
|
|
|
+ else_block: false,
|
|
|
|
+ do_block: false,
|
|
|
|
+ do_while: false,
|
|
|
|
+ import_block: false,
|
|
|
|
+ in_case_statement: false,
|
|
|
|
+ in_case: false,
|
|
|
|
+ case_body: false,
|
|
|
|
+ case_block: false,
|
|
|
|
+ indentation_level: next_indent_level,
|
|
|
|
+ alignment: 0,
|
|
|
|
+ line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level,
|
|
|
|
+ start_line_index: this._output.get_line_number(),
|
|
|
|
+ ternary_depth: 0
|
|
|
|
+ };
|
|
|
|
+ return next_flags;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype._reset = function(source_text) {
|
|
|
|
+ var baseIndentString = source_text.match(/^[\t ]*/)[0];
|
|
|
|
+
|
|
|
|
+ this._last_last_text = '';
|
|
|
|
+ this._output = new Output(this._options, baseIndentString);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this._output.raw = this._options.test_output_raw;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this._flag_store = [];
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ var tokenizer = new Tokenizer(source_text, this._options);
|
|
|
|
+ this._tokens = tokenizer.tokenize();
|
|
|
|
+ return source_text;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.beautify = function() {
|
|
|
|
+
|
|
|
|
+ if (this._options.disabled) {
|
|
|
|
+ return this._source_text;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var sweet_code;
|
|
|
|
+ var source_text = this._reset(this._source_text);
|
|
|
|
+
|
|
|
|
+ var eol = this._options.eol;
|
|
|
|
+ if (this._options.eol === 'auto') {
|
|
|
|
+ eol = '\n';
|
|
|
|
+ if (source_text && acorn.lineBreak.test(source_text || '')) {
|
|
|
|
+ eol = source_text.match(acorn.lineBreak)[0];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var current_token = this._tokens.next();
|
|
|
|
+ while (current_token) {
|
|
|
|
+ this.handle_token(current_token);
|
|
|
|
+
|
|
|
|
+ this._last_last_text = this._flags.last_token.text;
|
|
|
|
+ this._flags.last_token = current_token;
|
|
|
|
+
|
|
|
|
+ current_token = this._tokens.next();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ sweet_code = this._output.get_code(eol);
|
|
|
|
+
|
|
|
|
+ return sweet_code;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_token = function(current_token, preserve_statement_flags) {
|
|
|
|
+ if (current_token.type === TOKEN.START_EXPR) {
|
|
|
|
+ this.handle_start_expr(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.END_EXPR) {
|
|
|
|
+ this.handle_end_expr(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.START_BLOCK) {
|
|
|
|
+ this.handle_start_block(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.END_BLOCK) {
|
|
|
|
+ this.handle_end_block(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.WORD) {
|
|
|
|
+ this.handle_word(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.RESERVED) {
|
|
|
|
+ this.handle_word(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.SEMICOLON) {
|
|
|
|
+ this.handle_semicolon(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.STRING) {
|
|
|
|
+ this.handle_string(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.EQUALS) {
|
|
|
|
+ this.handle_equals(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.OPERATOR) {
|
|
|
|
+ this.handle_operator(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.COMMA) {
|
|
|
|
+ this.handle_comma(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.BLOCK_COMMENT) {
|
|
|
|
+ this.handle_block_comment(current_token, preserve_statement_flags);
|
|
|
|
+ } else if (current_token.type === TOKEN.COMMENT) {
|
|
|
|
+ this.handle_comment(current_token, preserve_statement_flags);
|
|
|
|
+ } else if (current_token.type === TOKEN.DOT) {
|
|
|
|
+ this.handle_dot(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.EOF) {
|
|
|
|
+ this.handle_eof(current_token);
|
|
|
|
+ } else if (current_token.type === TOKEN.UNKNOWN) {
|
|
|
|
+ this.handle_unknown(current_token, preserve_statement_flags);
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_unknown(current_token, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_whitespace_and_comments = function(current_token, preserve_statement_flags) {
|
|
|
|
+ var newlines = current_token.newlines;
|
|
|
|
+ var keep_whitespace = this._options.keep_array_indentation && is_array(this._flags.mode);
|
|
|
|
+
|
|
|
|
+ if (current_token.comments_before) {
|
|
|
|
+ var comment_token = current_token.comments_before.next();
|
|
|
|
+ while (comment_token) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.handle_whitespace_and_comments(comment_token, preserve_statement_flags);
|
|
|
|
+ this.handle_token(comment_token, preserve_statement_flags);
|
|
|
|
+ comment_token = current_token.comments_before.next();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (keep_whitespace) {
|
|
|
|
+ for (var i = 0; i < newlines; i += 1) {
|
|
|
|
+ this.print_newline(i > 0, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (this._options.max_preserve_newlines && newlines > this._options.max_preserve_newlines) {
|
|
|
|
+ newlines = this._options.max_preserve_newlines;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._options.preserve_newlines) {
|
|
|
|
+ if (newlines > 1) {
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+ for (var j = 1; j < newlines; j += 1) {
|
|
|
|
+ this.print_newline(true, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+var newline_restricted_tokens = ['async', 'break', 'continue', 'return', 'throw', 'yield'];
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.allow_wrap_or_preserved_newline = function(current_token, force_linewrap) {
|
|
|
|
+ force_linewrap = (force_linewrap === undefined) ? false : force_linewrap;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._output.just_added_newline()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var shouldPreserveOrForce = (this._options.preserve_newlines && current_token.newlines) || force_linewrap;
|
|
|
|
+ var operatorLogicApplies = in_array(this._flags.last_token.text, positionable_operators) ||
|
|
|
|
+ in_array(current_token.text, positionable_operators);
|
|
|
|
+
|
|
|
|
+ if (operatorLogicApplies) {
|
|
|
|
+ var shouldPrintOperatorNewline = (
|
|
|
|
+ in_array(this._flags.last_token.text, positionable_operators) &&
|
|
|
|
+ in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)
|
|
|
|
+ ) ||
|
|
|
|
+ in_array(current_token.text, positionable_operators);
|
|
|
|
+ shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (shouldPreserveOrForce) {
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ } else if (this._options.wrap_line_length) {
|
|
|
|
+ if (reserved_array(this._flags.last_token, newline_restricted_tokens)) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ this._output.set_wrap_point();
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.print_newline = function(force_newline, preserve_statement_flags) {
|
|
|
|
+ if (!preserve_statement_flags) {
|
|
|
|
+ if (this._flags.last_token.text !== ';' && this._flags.last_token.text !== ',' && this._flags.last_token.text !== '=' && (this._flags.last_token.type !== TOKEN.OPERATOR || this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) {
|
|
|
|
+ var next_token = this._tokens.peek();
|
|
|
|
+ while (this._flags.mode === MODE.Statement &&
|
|
|
|
+ !(this._flags.if_block && reserved_word(next_token, 'else')) &&
|
|
|
|
+ !this._flags.do_block) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._output.add_new_line(force_newline)) {
|
|
|
|
+ this._flags.multiline_frame = true;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.print_token_line_indentation = function(current_token) {
|
|
|
|
+ if (this._output.just_added_newline()) {
|
|
|
|
+ if (this._options.keep_array_indentation &&
|
|
|
|
+ current_token.newlines &&
|
|
|
|
+ (current_token.text === '[' || is_array(this._flags.mode))) {
|
|
|
|
+ this._output.current_line.set_indent(-1);
|
|
|
|
+ this._output.current_line.push(current_token.whitespace_before);
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ } else if (this._output.set_indent(this._flags.indentation_level, this._flags.alignment)) {
|
|
|
|
+ this._flags.line_indent_level = this._flags.indentation_level;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.print_token = function(current_token) {
|
|
|
|
+ if (this._output.raw) {
|
|
|
|
+ this._output.add_raw_token(current_token);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._options.comma_first && current_token.previous && current_token.previous.type === TOKEN.COMMA &&
|
|
|
|
+ this._output.just_added_newline()) {
|
|
|
|
+ if (this._output.previous_line.last() === ',') {
|
|
|
|
+ var popped = this._output.previous_line.pop();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._output.previous_line.is_empty()) {
|
|
|
|
+ this._output.previous_line.push(popped);
|
|
|
|
+ this._output.trim(true);
|
|
|
|
+ this._output.current_line.pop();
|
|
|
|
+ this._output.trim();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_token_line_indentation(current_token);
|
|
|
|
+ this._output.add_token(',');
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.print_token_line_indentation(current_token);
|
|
|
|
+ this._output.non_breaking_space = true;
|
|
|
|
+ this._output.add_token(current_token.text);
|
|
|
|
+ if (this._output.previous_token_wrapped) {
|
|
|
|
+ this._flags.multiline_frame = true;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.indent = function() {
|
|
|
|
+ this._flags.indentation_level += 1;
|
|
|
|
+ this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.deindent = function() {
|
|
|
|
+ if (this._flags.indentation_level > 0 &&
|
|
|
|
+ ((!this._flags.parent) || this._flags.indentation_level > this._flags.parent.indentation_level)) {
|
|
|
|
+ this._flags.indentation_level -= 1;
|
|
|
|
+ this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.set_mode = function(mode) {
|
|
|
|
+ if (this._flags) {
|
|
|
|
+ this._flag_store.push(this._flags);
|
|
|
|
+ this._previous_flags = this._flags;
|
|
|
|
+ } else {
|
|
|
|
+ this._previous_flags = this.create_flags(null, mode);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._flags = this.create_flags(this._previous_flags, mode);
|
|
|
|
+ this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.restore_mode = function() {
|
|
|
|
+ if (this._flag_store.length > 0) {
|
|
|
|
+ this._previous_flags = this._flags;
|
|
|
|
+ this._flags = this._flag_store.pop();
|
|
|
|
+ if (this._previous_flags.mode === MODE.Statement) {
|
|
|
|
+ remove_redundant_indentation(this._output, this._previous_flags);
|
|
|
|
+ }
|
|
|
|
+ this._output.set_indent(this._flags.indentation_level, this._flags.alignment);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.start_of_object_property = function() {
|
|
|
|
+ return this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement && (
|
|
|
|
+ (this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || (reserved_array(this._flags.last_token, ['get', 'set'])));
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.start_of_statement = function(current_token) {
|
|
|
|
+ var start = false;
|
|
|
|
+ start = start || reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD;
|
|
|
|
+ start = start || reserved_word(this._flags.last_token, 'do');
|
|
|
|
+ start = start || (!(this._flags.parent.mode === MODE.ObjectLiteral && this._flags.mode === MODE.Statement)) && reserved_array(this._flags.last_token, newline_restricted_tokens) && !current_token.newlines;
|
|
|
|
+ start = start || reserved_word(this._flags.last_token, 'else') &&
|
|
|
|
+ !(reserved_word(current_token, 'if') && !current_token.comments_before);
|
|
|
|
+ start = start || (this._flags.last_token.type === TOKEN.END_EXPR && (this._previous_flags.mode === MODE.ForInitializer || this._previous_flags.mode === MODE.Conditional));
|
|
|
|
+ start = start || (this._flags.last_token.type === TOKEN.WORD && this._flags.mode === MODE.BlockStatement &&
|
|
|
|
+ !this._flags.in_case &&
|
|
|
|
+ !(current_token.text === '--' || current_token.text === '++') &&
|
|
|
|
+ this._last_last_text !== 'function' &&
|
|
|
|
+ current_token.type !== TOKEN.WORD && current_token.type !== TOKEN.RESERVED);
|
|
|
|
+ start = start || (this._flags.mode === MODE.ObjectLiteral && (
|
|
|
|
+ (this._flags.last_token.text === ':' && this._flags.ternary_depth === 0) || reserved_array(this._flags.last_token, ['get', 'set'])));
|
|
|
|
+
|
|
|
|
+ if (start) {
|
|
|
|
+ this.set_mode(MODE.Statement);
|
|
|
|
+ this.indent();
|
|
|
|
+
|
|
|
|
+ this.handle_whitespace_and_comments(current_token, true);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!this.start_of_object_property()) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token,
|
|
|
|
+ reserved_array(current_token, ['do', 'for', 'if', 'while']));
|
|
|
|
+ }
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_start_expr = function(current_token) {
|
|
|
|
+
|
|
|
|
+ if (!this.start_of_statement(current_token)) {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var next_mode = MODE.Expression;
|
|
|
|
+ if (current_token.text === '[') {
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.WORD || this._flags.last_token.text === ')') {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (reserved_array(this._flags.last_token, line_starters)) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this.set_mode(next_mode);
|
|
|
|
+ this.indent();
|
|
|
|
+ if (this._options.space_in_paren) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ next_mode = MODE.ArrayLiteral;
|
|
|
|
+ if (is_array(this._flags.mode)) {
|
|
|
|
+ if (this._flags.last_token.text === '[' ||
|
|
|
|
+ (this._flags.last_token.text === ',' && (this._last_last_text === ']' || this._last_last_text === '}'))) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!this._options.keep_array_indentation) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!in_array(this._flags.last_token.type, [TOKEN.START_EXPR, TOKEN.END_EXPR, TOKEN.WORD, TOKEN.OPERATOR, TOKEN.DOT])) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.RESERVED) {
|
|
|
|
+ if (this._flags.last_token.text === 'for') {
|
|
|
|
+ this._output.space_before_token = this._options.space_before_conditional;
|
|
|
|
+ next_mode = MODE.ForInitializer;
|
|
|
|
+ } else if (in_array(this._flags.last_token.text, ['if', 'while', 'switch'])) {
|
|
|
|
+ this._output.space_before_token = this._options.space_before_conditional;
|
|
|
|
+ next_mode = MODE.Conditional;
|
|
|
|
+ } else if (in_array(this._flags.last_word, ['await', 'async'])) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.last_token.text === 'import' && current_token.whitespace_before === '') {
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ } else if (in_array(this._flags.last_token.text, line_starters) || this._flags.last_token.text === 'catch') {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!this.start_of_object_property()) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.WORD) {
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ var peek_back_two = this._tokens.peek(-3);
|
|
|
|
+ if (this._options.space_after_named_function && peek_back_two) {
|
|
|
|
+
|
|
|
|
+ var peek_back_three = this._tokens.peek(-4);
|
|
|
|
+ if (reserved_array(peek_back_two, ['async', 'function']) ||
|
|
|
|
+ (peek_back_two.text === '*' && reserved_array(peek_back_three, ['async', 'function']))) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.mode === MODE.ObjectLiteral) {
|
|
|
|
+ if ((peek_back_two.text === '{' || peek_back_two.text === ',') ||
|
|
|
|
+ (peek_back_two.text === '*' && (peek_back_three.text === '{' || peek_back_three.text === ','))) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if ((this._flags.last_token.type === TOKEN.RESERVED && (this._flags.last_word === 'function' || this._flags.last_word === 'typeof')) ||
|
|
|
|
+ (this._flags.last_token.text === '*' &&
|
|
|
|
+ (in_array(this._last_last_text, ['function', 'yield']) ||
|
|
|
|
+ (this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
|
|
|
|
+ this._output.space_before_token = this._options.space_after_anon_function;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.text === ';' || this._flags.last_token.type === TOKEN.START_BLOCK) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.END_EXPR || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.END_BLOCK || this._flags.last_token.text === '.' || this._flags.last_token.type === TOKEN.COMMA) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token, current_token.newlines);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this.set_mode(next_mode);
|
|
|
|
+ if (this._options.space_in_paren) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.indent();
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_end_expr = function(current_token) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ while (this._flags.mode === MODE.Statement) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+
|
|
|
|
+ if (this._flags.multiline_frame) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token,
|
|
|
|
+ current_token.text === ']' && is_array(this._flags.mode) && !this._options.keep_array_indentation);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._options.space_in_paren) {
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.START_EXPR && !this._options.space_in_empty_paren) {
|
|
|
|
+
|
|
|
|
+ this._output.trim();
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ } else {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.deindent();
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this.restore_mode();
|
|
|
|
+
|
|
|
|
+ remove_redundant_indentation(this._output, this._previous_flags);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._flags.do_while && this._previous_flags.mode === MODE.Conditional) {
|
|
|
|
+ this._previous_flags.mode = MODE.Expression;
|
|
|
|
+ this._flags.do_block = false;
|
|
|
|
+ this._flags.do_while = false;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_start_block = function(current_token) {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ var next_token = this._tokens.peek();
|
|
|
|
+ var second_token = this._tokens.peek(1);
|
|
|
|
+ if (this._flags.last_word === 'switch' && this._flags.last_token.type === TOKEN.END_EXPR) {
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ this._flags.in_case_statement = true;
|
|
|
|
+ } else if (this._flags.case_body) {
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ } else if (second_token && (
|
|
|
|
+ (in_array(second_token.text, [':', ',']) && in_array(next_token.type, [TOKEN.STRING, TOKEN.WORD, TOKEN.RESERVED])) ||
|
|
|
|
+ (in_array(next_token.text, ['get', 'set', '...']) && in_array(second_token.type, [TOKEN.WORD, TOKEN.RESERVED]))
|
|
|
|
+ )) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!in_array(this._last_last_text, ['class', 'interface'])) {
|
|
|
|
+ this.set_mode(MODE.ObjectLiteral);
|
|
|
|
+ } else {
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.OPERATOR && this._flags.last_token.text === '=>') {
|
|
|
|
+
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ } else if (in_array(this._flags.last_token.type, [TOKEN.EQUALS, TOKEN.START_EXPR, TOKEN.COMMA, TOKEN.OPERATOR]) ||
|
|
|
|
+ reserved_array(this._flags.last_token, ['return', 'throw', 'import', 'default'])
|
|
|
|
+ ) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.set_mode(MODE.ObjectLiteral);
|
|
|
|
+ } else {
|
|
|
|
+ this.set_mode(MODE.BlockStatement);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var empty_braces = !next_token.comments_before && next_token.text === '}';
|
|
|
|
+ var empty_anonymous_function = empty_braces && this._flags.last_word === 'function' &&
|
|
|
|
+ this._flags.last_token.type === TOKEN.END_EXPR;
|
|
|
|
+
|
|
|
|
+ if (this._options.brace_preserve_inline)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ var index = 0;
|
|
|
|
+ var check_token = null;
|
|
|
|
+ this._flags.inline_frame = true;
|
|
|
|
+ do {
|
|
|
|
+ index += 1;
|
|
|
|
+ check_token = this._tokens.peek(index - 1);
|
|
|
|
+ if (check_token.newlines) {
|
|
|
|
+ this._flags.inline_frame = false;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ } while (check_token.type !== TOKEN.EOF &&
|
|
|
|
+ !(check_token.type === TOKEN.END_BLOCK && check_token.opened === current_token));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if ((this._options.brace_style === "expand" ||
|
|
|
|
+ (this._options.brace_style === "none" && current_token.newlines)) &&
|
|
|
|
+ !this._flags.inline_frame) {
|
|
|
|
+ if (this._flags.last_token.type !== TOKEN.OPERATOR &&
|
|
|
|
+ (empty_anonymous_function ||
|
|
|
|
+ this._flags.last_token.type === TOKEN.EQUALS ||
|
|
|
|
+ (reserved_array(this._flags.last_token, special_words) && this._flags.last_token.text !== 'else'))) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ if (is_array(this._previous_flags.mode) && (this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.COMMA)) {
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.COMMA || this._options.space_in_paren) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.COMMA || (this._flags.last_token.type === TOKEN.START_EXPR && this._flags.inline_frame)) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ this._previous_flags.multiline_frame = this._previous_flags.multiline_frame || this._flags.multiline_frame;
|
|
|
|
+ this._flags.multiline_frame = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (this._flags.last_token.type !== TOKEN.OPERATOR && this._flags.last_token.type !== TOKEN.START_EXPR) {
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.START_BLOCK && !this._flags.inline_frame) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ } else {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this.indent();
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!empty_braces && !(this._options.brace_preserve_inline && this._flags.inline_frame)) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_end_block = function(current_token) {
|
|
|
|
+
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+
|
|
|
|
+ while (this._flags.mode === MODE.Statement) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var empty_braces = this._flags.last_token.type === TOKEN.START_BLOCK;
|
|
|
|
+
|
|
|
|
+ if (this._flags.inline_frame && !empty_braces) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._options.brace_style === "expand") {
|
|
|
|
+ if (!empty_braces) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ if (!empty_braces) {
|
|
|
|
+ if (is_array(this._flags.mode) && this._options.keep_array_indentation) {
|
|
|
|
+
|
|
|
|
+ this._options.keep_array_indentation = false;
|
|
|
|
+ this.print_newline();
|
|
|
|
+ this._options.keep_array_indentation = true;
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_word = function(current_token) {
|
|
|
|
+ if (current_token.type === TOKEN.RESERVED) {
|
|
|
|
+ if (in_array(current_token.text, ['set', 'get']) && this._flags.mode !== MODE.ObjectLiteral) {
|
|
|
|
+ current_token.type = TOKEN.WORD;
|
|
|
|
+ } else if (current_token.text === 'import' && this._tokens.peek().text === '(') {
|
|
|
|
+ current_token.type = TOKEN.WORD;
|
|
|
|
+ } else if (in_array(current_token.text, ['as', 'from']) && !this._flags.import_block) {
|
|
|
|
+ current_token.type = TOKEN.WORD;
|
|
|
|
+ } else if (this._flags.mode === MODE.ObjectLiteral) {
|
|
|
|
+ var next_token = this._tokens.peek();
|
|
|
|
+ if (next_token.text === ':') {
|
|
|
|
+ current_token.type = TOKEN.WORD;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+ if (reserved_array(this._flags.last_token, ['var', 'let', 'const']) && current_token.type === TOKEN.WORD) {
|
|
|
|
+ this._flags.declaration_statement = true;
|
|
|
|
+ }
|
|
|
|
+ } else if (current_token.newlines && !is_expression(this._flags.mode) &&
|
|
|
|
+ (this._flags.last_token.type !== TOKEN.OPERATOR || (this._flags.last_token.text === '--' || this._flags.last_token.text === '++')) &&
|
|
|
|
+ this._flags.last_token.type !== TOKEN.EQUALS &&
|
|
|
|
+ (this._options.preserve_newlines || !reserved_array(this._flags.last_token, ['var', 'let', 'const', 'set', 'get']))) {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ this.print_newline();
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.do_block && !this._flags.do_while) {
|
|
|
|
+ if (reserved_word(current_token, 'while')) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this._flags.do_while = true;
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_newline();
|
|
|
|
+ this._flags.do_block = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._flags.if_block) {
|
|
|
|
+ if (!this._flags.else_block && reserved_word(current_token, 'else')) {
|
|
|
|
+ this._flags.else_block = true;
|
|
|
|
+ } else {
|
|
|
|
+ while (this._flags.mode === MODE.Statement) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+ this._flags.if_block = false;
|
|
|
|
+ this._flags.else_block = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.in_case_statement && reserved_array(current_token, ['case', 'default'])) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ if (!this._flags.case_block && (this._flags.case_body || this._options.jslint_happy)) {
|
|
|
|
+
|
|
|
|
+ this.deindent();
|
|
|
|
+ }
|
|
|
|
+ this._flags.case_body = false;
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._flags.in_case = true;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
|
|
|
|
+ if (!this.start_of_object_property()) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reserved_word(current_token, 'function')) {
|
|
|
|
+ if (in_array(this._flags.last_token.text, ['}', ';']) ||
|
|
|
|
+ (this._output.just_added_newline() && !(in_array(this._flags.last_token.text, ['(', '[', '{', ':', '=', ',']) || this._flags.last_token.type === TOKEN.OPERATOR))) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!this._output.just_added_blankline() && !current_token.comments_before) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ this.print_newline(true);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD) {
|
|
|
|
+ if (reserved_array(this._flags.last_token, ['get', 'set', 'new', 'export']) ||
|
|
|
|
+ reserved_array(this._flags.last_token, newline_restricted_tokens)) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (reserved_word(this._flags.last_token, 'default') && this._last_last_text === 'export') {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.last_token.text === 'declare') {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.OPERATOR || this._flags.last_token.text === '=') {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (!this._flags.multiline_frame && (is_expression(this._flags.mode) || is_array(this._flags.mode))) {
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._flags.last_word = current_token.text;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var prefix = 'NONE';
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.END_BLOCK) {
|
|
|
|
+
|
|
|
|
+ if (this._previous_flags.inline_frame) {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else if (!reserved_array(current_token, ['else', 'catch', 'finally', 'from'])) {
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ } else {
|
|
|
|
+ if (this._options.brace_style === "expand" ||
|
|
|
|
+ this._options.brace_style === "end-expand" ||
|
|
|
|
+ (this._options.brace_style === "none" && current_token.newlines)) {
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ } else {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.SEMICOLON && this._flags.mode === MODE.BlockStatement) {
|
|
|
|
+
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.SEMICOLON && is_expression(this._flags.mode)) {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.STRING) {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD ||
|
|
|
|
+ (this._flags.last_token.text === '*' &&
|
|
|
|
+ (in_array(this._last_last_text, ['function', 'yield']) ||
|
|
|
|
+ (this._flags.mode === MODE.ObjectLiteral && in_array(this._last_last_text, ['{', ',']))))) {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.START_BLOCK) {
|
|
|
|
+ if (this._flags.inline_frame) {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else {
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.END_EXPR) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
|
|
|
|
+ if (this._flags.inline_frame || this._flags.last_token.text === 'else' || this._flags.last_token.text === 'export') {
|
|
|
|
+ prefix = 'SPACE';
|
|
|
|
+ } else {
|
|
|
|
+ prefix = 'NEWLINE';
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reserved_array(current_token, ['else', 'catch', 'finally'])) {
|
|
|
|
+ if ((!(this._flags.last_token.type === TOKEN.END_BLOCK && this._previous_flags.mode === MODE.BlockStatement) ||
|
|
|
|
+ this._options.brace_style === "expand" ||
|
|
|
|
+ this._options.brace_style === "end-expand" ||
|
|
|
|
+ (this._options.brace_style === "none" && current_token.newlines)) &&
|
|
|
|
+ !this._flags.inline_frame) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ } else {
|
|
|
|
+ this._output.trim(true);
|
|
|
|
+ var line = this._output.current_line;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (line.last() !== '}') {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ } else if (prefix === 'NEWLINE') {
|
|
|
|
+ if (reserved_array(this._flags.last_token, special_words)) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.last_token.text === 'declare' && reserved_array(current_token, ['var', 'let', 'const'])) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.last_token.type !== TOKEN.END_EXPR) {
|
|
|
|
+ if ((this._flags.last_token.type !== TOKEN.START_EXPR || !reserved_array(current_token, ['var', 'let', 'const'])) && this._flags.last_token.text !== ':') {
|
|
|
|
+
|
|
|
|
+ if (reserved_word(current_token, 'if') && reserved_word(current_token.previous, 'else')) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ } else if (reserved_array(current_token, line_starters) && this._flags.last_token.text !== ')') {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.multiline_frame && is_array(this._flags.mode) && this._flags.last_token.text === ',' && this._last_last_text === '}') {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ } else if (prefix === 'SPACE') {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ if (current_token.previous && (current_token.previous.type === TOKEN.WORD || current_token.previous.type === TOKEN.RESERVED)) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._flags.last_word = current_token.text;
|
|
|
|
+
|
|
|
|
+ if (current_token.type === TOKEN.RESERVED) {
|
|
|
|
+ if (current_token.text === 'do') {
|
|
|
|
+ this._flags.do_block = true;
|
|
|
|
+ } else if (current_token.text === 'if') {
|
|
|
|
+ this._flags.if_block = true;
|
|
|
|
+ } else if (current_token.text === 'import') {
|
|
|
|
+ this._flags.import_block = true;
|
|
|
|
+ } else if (this._flags.import_block && reserved_word(current_token, 'from')) {
|
|
|
|
+ this._flags.import_block = false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_semicolon = function(current_token) {
|
|
|
|
+ if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var next_token = this._tokens.peek();
|
|
|
|
+ while (this._flags.mode === MODE.Statement &&
|
|
|
|
+ !(this._flags.if_block && reserved_word(next_token, 'else')) &&
|
|
|
|
+ !this._flags.do_block) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._flags.import_block) {
|
|
|
|
+ this._flags.import_block = false;
|
|
|
|
+ }
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_string = function(current_token) {
|
|
|
|
+ if (current_token.text.startsWith("`") && current_token.newlines === 0 && current_token.whitespace_before === '' && (current_token.previous.text === ')' || this._flags.last_token.type === TOKEN.WORD)) {
|
|
|
|
+
|
|
|
|
+ } else if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.RESERVED || this._flags.last_token.type === TOKEN.WORD || this._flags.inline_frame) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR || this._flags.last_token.type === TOKEN.EQUALS || this._flags.last_token.type === TOKEN.OPERATOR) {
|
|
|
|
+ if (!this.start_of_object_property()) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+ } else if ((current_token.text.startsWith("`") && this._flags.last_token.type === TOKEN.END_EXPR && (current_token.previous.text === ']' || current_token.previous.text === ')') && current_token.newlines === 0)) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ } else {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_equals = function(current_token) {
|
|
|
|
+ if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.declaration_statement) {
|
|
|
|
+
|
|
|
|
+ this._flags.declaration_assignment = true;
|
|
|
|
+ }
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_comma = function(current_token) {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token, true);
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ if (this._flags.declaration_statement) {
|
|
|
|
+ if (is_expression(this._flags.parent.mode)) {
|
|
|
|
+
|
|
|
|
+ this._flags.declaration_assignment = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.declaration_assignment) {
|
|
|
|
+ this._flags.declaration_assignment = false;
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ } else if (this._options.comma_first) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+ } else if (this._flags.mode === MODE.ObjectLiteral ||
|
|
|
|
+ (this._flags.mode === MODE.Statement && this._flags.parent.mode === MODE.ObjectLiteral)) {
|
|
|
|
+ if (this._flags.mode === MODE.Statement) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (!this._flags.inline_frame) {
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ } else if (this._options.comma_first) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_operator = function(current_token) {
|
|
|
|
+ var isGeneratorAsterisk = current_token.text === '*' &&
|
|
|
|
+ (reserved_array(this._flags.last_token, ['function', 'yield']) ||
|
|
|
|
+ (in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.COMMA, TOKEN.END_BLOCK, TOKEN.SEMICOLON]))
|
|
|
|
+ );
|
|
|
|
+ var isUnary = in_array(current_token.text, ['-', '+']) && (
|
|
|
|
+ in_array(this._flags.last_token.type, [TOKEN.START_BLOCK, TOKEN.START_EXPR, TOKEN.EQUALS, TOKEN.OPERATOR]) ||
|
|
|
|
+ in_array(this._flags.last_token.text, line_starters) ||
|
|
|
|
+ this._flags.last_token.text === ','
|
|
|
|
+ );
|
|
|
|
+
|
|
|
|
+ if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ var preserve_statement_flags = !isGeneratorAsterisk;
|
|
|
|
+ this.handle_whitespace_and_comments(current_token, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reserved_array(this._flags.last_token, special_words)) {
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (current_token.text === '*' && this._flags.last_token.type === TOKEN.DOT) {
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (current_token.text === '::') {
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.OPERATOR && in_array(this._options.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (current_token.text === ':' && this._flags.in_case) {
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+
|
|
|
|
+ this._flags.in_case = false;
|
|
|
|
+ this._flags.case_body = true;
|
|
|
|
+ if (this._tokens.peek().type !== TOKEN.START_BLOCK) {
|
|
|
|
+ this.indent();
|
|
|
|
+ this.print_newline();
|
|
|
|
+ this._flags.case_block = false;
|
|
|
|
+ } else {
|
|
|
|
+ this._flags.case_block = true;
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ var space_before = true;
|
|
|
|
+ var space_after = true;
|
|
|
|
+ var in_ternary = false;
|
|
|
|
+ if (current_token.text === ':') {
|
|
|
|
+ if (this._flags.ternary_depth === 0) {
|
|
|
|
+
|
|
|
|
+ space_before = false;
|
|
|
|
+ } else {
|
|
|
|
+ this._flags.ternary_depth -= 1;
|
|
|
|
+ in_ternary = true;
|
|
|
|
+ }
|
|
|
|
+ } else if (current_token.text === '?') {
|
|
|
|
+ this._flags.ternary_depth += 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!isUnary && !isGeneratorAsterisk && this._options.preserve_newlines && in_array(current_token.text, positionable_operators)) {
|
|
|
|
+ var isColon = current_token.text === ':';
|
|
|
|
+ var isTernaryColon = (isColon && in_ternary);
|
|
|
|
+ var isOtherColon = (isColon && !in_ternary);
|
|
|
|
+
|
|
|
|
+ switch (this._options.operator_position) {
|
|
|
|
+ case OPERATOR_POSITION.before_newline:
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = !isOtherColon;
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+
|
|
|
|
+ if (!isColon || isTernaryColon) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ case OPERATOR_POSITION.after_newline:
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+
|
|
|
|
+ if (!isColon || isTernaryColon) {
|
|
|
|
+ if (this._tokens.peek().newlines) {
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ } else {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+ } else {
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ case OPERATOR_POSITION.preserve_newline:
|
|
|
|
+ if (!isOtherColon) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ space_before = !(this._output.just_added_newline() || isOtherColon);
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = space_before;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (isGeneratorAsterisk) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ space_before = false;
|
|
|
|
+ var next_token = this._tokens.peek();
|
|
|
|
+ space_after = next_token && in_array(next_token.type, [TOKEN.WORD, TOKEN.RESERVED]);
|
|
|
|
+ } else if (current_token.text === '...') {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ space_before = this._flags.last_token.type === TOKEN.START_BLOCK;
|
|
|
|
+ space_after = false;
|
|
|
|
+ } else if (in_array(current_token.text, ['--', '++', '!', '~']) || isUnary) {
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.COMMA || this._flags.last_token.type === TOKEN.START_EXPR) {
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ space_before = false;
|
|
|
|
+ space_after = false;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (current_token.newlines && (current_token.text === '--' || current_token.text === '++' || current_token.text === '~')) {
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.text === ';' && is_expression(this._flags.mode)) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ space_before = true;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (this._flags.last_token.type === TOKEN.RESERVED) {
|
|
|
|
+ space_before = true;
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.END_EXPR) {
|
|
|
|
+ space_before = !(this._flags.last_token.text === ']' && (current_token.text === '--' || current_token.text === '++'));
|
|
|
|
+ } else if (this._flags.last_token.type === TOKEN.OPERATOR) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ space_before = in_array(current_token.text, ['--', '-', '++', '+']) && in_array(this._flags.last_token.text, ['--', '-', '++', '+']);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (in_array(current_token.text, ['+', '-']) && in_array(this._flags.last_token.text, ['--', '++'])) {
|
|
|
|
+ space_after = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (((this._flags.mode === MODE.BlockStatement && !this._flags.inline_frame) || this._flags.mode === MODE.Statement) &&
|
|
|
|
+ (this._flags.last_token.text === '{' || this._flags.last_token.text === ';')) {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_newline();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = this._output.space_before_token || space_before;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = space_after;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_block_comment = function(current_token, preserve_statement_flags) {
|
|
|
|
+ if (this._output.raw) {
|
|
|
|
+ this._output.add_raw_token(current_token);
|
|
|
|
+ if (current_token.directives && current_token.directives.preserve === 'end') {
|
|
|
|
+
|
|
|
|
+ this._output.raw = this._options.test_output_raw;
|
|
|
|
+ }
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (current_token.directives) {
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ if (current_token.directives.preserve === 'start') {
|
|
|
|
+ this._output.raw = true;
|
|
|
|
+ }
|
|
|
|
+ this.print_newline(false, true);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (!acorn.newline.test(current_token.text) && !current_token.newlines) {
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ return;
|
|
|
|
+ } else {
|
|
|
|
+ this.print_block_commment(current_token, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.print_block_commment = function(current_token, preserve_statement_flags) {
|
|
|
|
+ var lines = split_linebreaks(current_token.text);
|
|
|
|
+ var j;
|
|
|
|
+ var javadoc = false;
|
|
|
|
+ var starless = false;
|
|
|
|
+ var lastIndent = current_token.whitespace_before;
|
|
|
|
+ var lastIndentLength = lastIndent.length;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_token_line_indentation(current_token);
|
|
|
|
+ this._output.add_token(lines[0]);
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (lines.length > 1) {
|
|
|
|
+ lines = lines.slice(1);
|
|
|
|
+ javadoc = all_lines_start_with(lines, '*');
|
|
|
|
+ starless = each_line_matches_indent(lines, lastIndent);
|
|
|
|
+
|
|
|
|
+ if (javadoc) {
|
|
|
|
+ this._flags.alignment = 1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (j = 0; j < lines.length; j++) {
|
|
|
|
+ if (javadoc) {
|
|
|
|
+
|
|
|
|
+ this.print_token_line_indentation(current_token);
|
|
|
|
+ this._output.add_token(ltrim(lines[j]));
|
|
|
|
+ } else if (starless && lines[j]) {
|
|
|
|
+
|
|
|
|
+ this.print_token_line_indentation(current_token);
|
|
|
|
+ this._output.add_token(lines[j].substring(lastIndentLength));
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+ this._output.current_line.set_indent(-1);
|
|
|
|
+ this._output.add_token(lines[j]);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._flags.alignment = 0;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_comment = function(current_token, preserve_statement_flags) {
|
|
|
|
+ if (current_token.newlines) {
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+ } else {
|
|
|
|
+ this._output.trim(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this._output.space_before_token = true;
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_dot = function(current_token) {
|
|
|
|
+ if (this.start_of_statement(current_token)) {
|
|
|
|
+
|
|
|
|
+ } else {
|
|
|
|
+ this.handle_whitespace_and_comments(current_token, true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (reserved_array(this._flags.last_token, special_words)) {
|
|
|
|
+ this._output.space_before_token = false;
|
|
|
|
+ } else {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ this.allow_wrap_or_preserved_newline(current_token,
|
|
|
|
+ this._flags.last_token.text === ')' && this._options.break_chained_methods);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ if (this._options.unindent_chained_methods && this._output.just_added_newline()) {
|
|
|
|
+ this.deindent();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_unknown = function(current_token, preserve_statement_flags) {
|
|
|
|
+ this.print_token(current_token);
|
|
|
|
+
|
|
|
|
+ if (current_token.text[current_token.text.length - 1] === '\n') {
|
|
|
|
+ this.print_newline(false, preserve_statement_flags);
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+Beautifier.prototype.handle_eof = function(current_token) {
|
|
|
|
+
|
|
|
|
+ while (this._flags.mode === MODE.Statement) {
|
|
|
|
+ this.restore_mode();
|
|
|
|
+ }
|
|
|
|
+ this.handle_whitespace_and_comments(current_token);
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+module.exports.Beautifier = Beautifier;
|