|
Added
Link Here
|
| 1 |
/*eslint-env amd, node*/ |
| 2 |
/* |
| 3 |
|
| 4 |
The MIT License (MIT) |
| 5 |
|
| 6 |
Copyright (c) 2007-2013 Einar Lielmanis and contributors. |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person |
| 9 |
obtaining a copy of this software and associated documentation files |
| 10 |
(the "Software"), to deal in the Software without restriction, |
| 11 |
including without limitation the rights to use, copy, modify, merge, |
| 12 |
publish, distribute, sublicense, and/or sell copies of the Software, |
| 13 |
and to permit persons to whom the Software is furnished to do so, |
| 14 |
subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be |
| 17 |
included in all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 23 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 24 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 25 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 26 |
SOFTWARE. |
| 27 |
|
| 28 |
JS Beautifier |
| 29 |
--------------- |
| 30 |
|
| 31 |
|
| 32 |
Written by Einar Lielmanis, <einar@jsbeautifier.org> |
| 33 |
http://jsbeautifier.org/ |
| 34 |
|
| 35 |
Originally converted to javascript by Vital, <vital76@gmail.com> |
| 36 |
"End braces on own line" added by Chris J. Shull, <chrisjshull@gmail.com> |
| 37 |
Parsing improvements for brace-less statements by Liam Newman <bitwiseman@gmail.com> |
| 38 |
|
| 39 |
|
| 40 |
Usage: |
| 41 |
js_beautify(js_source_text); |
| 42 |
js_beautify(js_source_text, options); |
| 43 |
|
| 44 |
The options are: |
| 45 |
indent_size (default 4) - indentation size, |
| 46 |
indent_char (default space) - character to indent with, |
| 47 |
preserve_newlines (default true) - whether existing line breaks should be preserved, |
| 48 |
max_preserve_newlines (default unlimited) - maximum number of line breaks to be preserved in one chunk, |
| 49 |
|
| 50 |
jslint_happy (default false) - if true, then jslint-stricter mode is enforced. |
| 51 |
|
| 52 |
jslint_happy !jslint_happy |
| 53 |
--------------------------------- |
| 54 |
function () function() |
| 55 |
|
| 56 |
switch () { switch() { |
| 57 |
case 1: case 1: |
| 58 |
break; break; |
| 59 |
} } |
| 60 |
|
| 61 |
space_after_anon_function (default false) - should the space before an anonymous function's parens be added, "function()" vs "function ()", |
| 62 |
NOTE: This option is overriden by jslint_happy (i.e. if jslint_happy is true, space_after_anon_function is true by design) |
| 63 |
|
| 64 |
brace_style (default "collapse") - "collapse-preserve-inline" | "collapse" | "expand" | "end-expand" | "none" |
| 65 |
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, or attempt to keep them where they are. |
| 66 |
|
| 67 |
space_before_conditional (default true) - should the space before conditional statement be added, "if(true)" vs "if (true)", |
| 68 |
|
| 69 |
unescape_strings (default false) - should printable characters in strings encoded in \xNN notation be unescaped, "example" vs "\x65\x78\x61\x6d\x70\x6c\x65" |
| 70 |
|
| 71 |
wrap_line_length (default unlimited) - lines should wrap at next opportunity after this number of characters. |
| 72 |
NOTE: This is not a hard limit. Lines will continue until a point where a newline would |
| 73 |
be preserved if it were present. |
| 74 |
|
| 75 |
end_with_newline (default false) - end output with a newline |
| 76 |
|
| 77 |
|
| 78 |
e.g |
| 79 |
|
| 80 |
js_beautify(js_source_text, { |
| 81 |
'indent_size': 1, |
| 82 |
'indent_char': '\t' |
| 83 |
}); |
| 84 |
|
| 85 |
*/ |
| 86 |
define([ |
| 87 |
"acorn/dist/acorn" |
| 88 |
], function(acorn) { |
| 89 |
|
| 90 |
|
| 91 |
// Object.values polyfill found here: |
| 92 |
// http://tokenposts.blogspot.com.au/2012/04/javascript-objectkeys-browser.html |
| 93 |
if (!Object.values) { |
| 94 |
Object.values = function(o) { |
| 95 |
if (o !== Object(o)) { |
| 96 |
throw new TypeError('Object.values called on a non-object'); |
| 97 |
} |
| 98 |
var k = [], |
| 99 |
p; |
| 100 |
for (p in o) { |
| 101 |
if (Object.prototype.hasOwnProperty.call(o, p)) { |
| 102 |
k.push(o[p]); |
| 103 |
} |
| 104 |
} |
| 105 |
return k; |
| 106 |
}; |
| 107 |
} |
| 108 |
|
| 109 |
function js_beautify(js_source_text, options) { |
| 110 |
|
| 111 |
function in_array(what, arr) { |
| 112 |
for (var i = 0; i < arr.length; i += 1) { |
| 113 |
if (arr[i] === what) { |
| 114 |
return true; |
| 115 |
} |
| 116 |
} |
| 117 |
return false; |
| 118 |
} |
| 119 |
|
| 120 |
function trim(s) { |
| 121 |
return s.replace(/^\s+|\s+$/g, ''); |
| 122 |
} |
| 123 |
|
| 124 |
function ltrim(s) { |
| 125 |
return s.replace(/^\s+/g, ''); |
| 126 |
} |
| 127 |
|
| 128 |
// function rtrim(s) { |
| 129 |
// return s.replace(/\s+$/g, ''); |
| 130 |
// } |
| 131 |
|
| 132 |
function sanitizeOperatorPosition(opPosition) { |
| 133 |
opPosition = opPosition || OPERATOR_POSITION.before_newline; |
| 134 |
|
| 135 |
var validPositionValues = Object.values(OPERATOR_POSITION); |
| 136 |
|
| 137 |
if (!in_array(opPosition, validPositionValues)) { |
| 138 |
throw new Error("Invalid Option Value: The option 'operator_position' must be one of the following values\n" + |
| 139 |
validPositionValues + |
| 140 |
"\nYou passed in: '" + opPosition + "'"); |
| 141 |
} |
| 142 |
|
| 143 |
return opPosition; |
| 144 |
} |
| 145 |
|
| 146 |
var OPERATOR_POSITION = { |
| 147 |
before_newline: 'before-newline', |
| 148 |
after_newline: 'after-newline', |
| 149 |
preserve_newline: 'preserve-newline', |
| 150 |
}; |
| 151 |
|
| 152 |
var OPERATOR_POSITION_BEFORE_OR_PRESERVE = [OPERATOR_POSITION.before_newline, OPERATOR_POSITION.preserve_newline]; |
| 153 |
|
| 154 |
var MODE = { |
| 155 |
BlockStatement: 'BlockStatement', // 'BLOCK' |
| 156 |
Statement: 'Statement', // 'STATEMENT' |
| 157 |
ObjectLiteral: 'ObjectLiteral', // 'OBJECT', |
| 158 |
ArrayLiteral: 'ArrayLiteral', //'[EXPRESSION]', |
| 159 |
ForInitializer: 'ForInitializer', //'(FOR-EXPRESSION)', |
| 160 |
Conditional: 'Conditional', //'(COND-EXPRESSION)', |
| 161 |
Expression: 'Expression' //'(EXPRESSION)' |
| 162 |
}; |
| 163 |
|
| 164 |
function Beautifier(js_source_text, options) { |
| 165 |
"use strict"; |
| 166 |
var output; |
| 167 |
var tokens = [], |
| 168 |
token_pos; |
| 169 |
var Tokenizer; |
| 170 |
var current_token; |
| 171 |
var last_type, last_last_text, indent_string; |
| 172 |
var flags, previous_flags, flag_store; |
| 173 |
var prefix; |
| 174 |
|
| 175 |
var handlers, opt; |
| 176 |
var baseIndentString = ''; |
| 177 |
|
| 178 |
handlers = { |
| 179 |
'TK_START_EXPR': handle_start_expr, |
| 180 |
'TK_END_EXPR': handle_end_expr, |
| 181 |
'TK_START_BLOCK': handle_start_block, |
| 182 |
'TK_END_BLOCK': handle_end_block, |
| 183 |
'TK_WORD': handle_word, |
| 184 |
'TK_RESERVED': handle_word, |
| 185 |
'TK_SEMICOLON': handle_semicolon, |
| 186 |
'TK_STRING': handle_string, |
| 187 |
'TK_EQUALS': handle_equals, |
| 188 |
'TK_OPERATOR': handle_operator, |
| 189 |
'TK_COMMA': handle_comma, |
| 190 |
'TK_BLOCK_COMMENT': handle_block_comment, |
| 191 |
'TK_COMMENT': handle_comment, |
| 192 |
'TK_DOT': handle_dot, |
| 193 |
'TK_UNKNOWN': handle_unknown, |
| 194 |
'TK_EOF': handle_eof |
| 195 |
}; |
| 196 |
|
| 197 |
function create_flags(flags_base, mode) { |
| 198 |
var next_indent_level = 0; |
| 199 |
if (flags_base) { |
| 200 |
next_indent_level = flags_base.indentation_level; |
| 201 |
if (!output.just_added_newline() && |
| 202 |
flags_base.line_indent_level > next_indent_level) { |
| 203 |
next_indent_level = flags_base.line_indent_level; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
var next_flags = { |
| 208 |
mode: mode, |
| 209 |
parent: flags_base, |
| 210 |
last_text: flags_base ? flags_base.last_text : '', // last token text |
| 211 |
last_word: flags_base ? flags_base.last_word : '', // last 'TK_WORD' passed |
| 212 |
declaration_statement: false, |
| 213 |
declaration_assignment: false, |
| 214 |
multiline_frame: false, |
| 215 |
inline_frame: false, |
| 216 |
if_block: false, |
| 217 |
else_block: false, |
| 218 |
do_block: false, |
| 219 |
do_while: false, |
| 220 |
import_block: false, |
| 221 |
in_case_statement: false, // switch(..){ INSIDE HERE } |
| 222 |
in_case: false, // we're on the exact line with "case 0:" |
| 223 |
case_body: false, // the indented case-action block |
| 224 |
indentation_level: next_indent_level, |
| 225 |
line_indent_level: flags_base ? flags_base.line_indent_level : next_indent_level, |
| 226 |
start_line_index: output.get_line_number(), |
| 227 |
ternary_depth: 0 |
| 228 |
}; |
| 229 |
return next_flags; |
| 230 |
} |
| 231 |
|
| 232 |
// Some interpreters have unexpected results with foo = baz || bar; |
| 233 |
options = options ? options : {}; |
| 234 |
opt = {}; |
| 235 |
|
| 236 |
// compatibility |
| 237 |
if (options.braces_on_own_line !== undefined) { //graceful handling of deprecated option |
| 238 |
opt.brace_style = options.braces_on_own_line ? "expand" : "collapse"; |
| 239 |
} |
| 240 |
opt.brace_style = options.brace_style ? options.brace_style : (opt.brace_style ? opt.brace_style : "collapse"); |
| 241 |
|
| 242 |
// graceful handling of deprecated option |
| 243 |
if (opt.brace_style === "expand-strict") { |
| 244 |
opt.brace_style = "expand"; |
| 245 |
} |
| 246 |
|
| 247 |
opt.indent_size = options.indent_size ? parseInt(options.indent_size, 10) : 4; |
| 248 |
opt.indent_char = options.indent_char ? options.indent_char : ' '; |
| 249 |
opt.eol = options.eol ? options.eol : 'auto'; |
| 250 |
opt.preserve_newlines = (options.preserve_newlines === undefined) ? true : options.preserve_newlines; |
| 251 |
opt.break_chained_methods = (options.break_chained_methods === undefined) ? false : options.break_chained_methods; |
| 252 |
opt.max_preserve_newlines = (options.max_preserve_newlines === undefined) ? 0 : parseInt(options.max_preserve_newlines, 10); |
| 253 |
opt.space_in_paren = (options.space_in_paren === undefined) ? false : options.space_in_paren; |
| 254 |
opt.space_in_empty_paren = (options.space_in_empty_paren === undefined) ? false : options.space_in_empty_paren; |
| 255 |
opt.jslint_happy = (options.jslint_happy === undefined) ? false : options.jslint_happy; |
| 256 |
opt.space_after_anon_function = (options.space_after_anon_function === undefined) ? false : options.space_after_anon_function; |
| 257 |
opt.keep_array_indentation = (options.keep_array_indentation === undefined) ? false : options.keep_array_indentation; |
| 258 |
opt.space_before_conditional = (options.space_before_conditional === undefined) ? true : options.space_before_conditional; |
| 259 |
opt.unescape_strings = (options.unescape_strings === undefined) ? false : options.unescape_strings; |
| 260 |
opt.wrap_line_length = (options.wrap_line_length === undefined) ? 0 : parseInt(options.wrap_line_length, 10); |
| 261 |
opt.e4x = (options.e4x === undefined) ? false : options.e4x; |
| 262 |
opt.end_with_newline = (options.end_with_newline === undefined) ? false : options.end_with_newline; |
| 263 |
opt.comma_first = (options.comma_first === undefined) ? false : options.comma_first; |
| 264 |
opt.operator_position = sanitizeOperatorPosition(options.operator_position); |
| 265 |
|
| 266 |
// For testing of beautify ignore:start directive |
| 267 |
opt.test_output_raw = (options.test_output_raw === undefined) ? false : options.test_output_raw; |
| 268 |
|
| 269 |
// force opt.space_after_anon_function to true if opt.jslint_happy |
| 270 |
if (opt.jslint_happy) { |
| 271 |
opt.space_after_anon_function = true; |
| 272 |
} |
| 273 |
|
| 274 |
if (options.indent_with_tabs) { |
| 275 |
opt.indent_char = '\t'; |
| 276 |
opt.indent_size = 1; |
| 277 |
} |
| 278 |
|
| 279 |
if (opt.eol === 'auto') { |
| 280 |
opt.eol = '\n'; |
| 281 |
if (js_source_text && acorn.lineBreak.test(js_source_text || '')) { |
| 282 |
opt.eol = js_source_text.match(acorn.lineBreak)[0]; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
opt.eol = opt.eol.replace(/\\r/, '\r').replace(/\\n/, '\n'); |
| 287 |
|
| 288 |
//---------------------------------- |
| 289 |
indent_string = ''; |
| 290 |
while (opt.indent_size > 0) { |
| 291 |
indent_string += opt.indent_char; |
| 292 |
opt.indent_size -= 1; |
| 293 |
} |
| 294 |
|
| 295 |
var preindent_index = 0; |
| 296 |
if (js_source_text && js_source_text.length) { |
| 297 |
while ((js_source_text.charAt(preindent_index) === ' ' || |
| 298 |
js_source_text.charAt(preindent_index) === '\t')) { |
| 299 |
baseIndentString += js_source_text.charAt(preindent_index); |
| 300 |
preindent_index += 1; |
| 301 |
} |
| 302 |
js_source_text = js_source_text.substring(preindent_index); |
| 303 |
} |
| 304 |
|
| 305 |
last_type = 'TK_START_BLOCK'; // last token type |
| 306 |
last_last_text = ''; // pre-last token text |
| 307 |
output = new Output(indent_string, baseIndentString); |
| 308 |
|
| 309 |
// If testing the ignore directive, start with output disable set to true |
| 310 |
output.raw = opt.test_output_raw; |
| 311 |
|
| 312 |
|
| 313 |
// Stack of parsing/formatting states, including MODE. |
| 314 |
// We tokenize, parse, and output in an almost purely a forward-only stream of token input |
| 315 |
// and formatted output. This makes the beautifier less accurate than full parsers |
| 316 |
// but also far more tolerant of syntax errors. |
| 317 |
// |
| 318 |
// For example, the default mode is MODE.BlockStatement. If we see a '{' we push a new frame of type |
| 319 |
// MODE.BlockStatement on the the stack, even though it could be object literal. If we later |
| 320 |
// encounter a ":", we'll switch to to MODE.ObjectLiteral. If we then see a ";", |
| 321 |
// most full parsers would die, but the beautifier gracefully falls back to |
| 322 |
// MODE.BlockStatement and continues on. |
| 323 |
flag_store = []; |
| 324 |
set_mode(MODE.BlockStatement); |
| 325 |
|
| 326 |
this.beautify = function() { |
| 327 |
|
| 328 |
/*jshint onevar:true */ |
| 329 |
var local_token, sweet_code; |
| 330 |
Tokenizer = new tokenizer(js_source_text, opt, indent_string); |
| 331 |
tokens = Tokenizer.tokenize(); |
| 332 |
token_pos = 0; |
| 333 |
|
| 334 |
function get_local_token() { |
| 335 |
local_token = get_token(); |
| 336 |
return local_token; |
| 337 |
} |
| 338 |
|
| 339 |
while (get_local_token()) { |
| 340 |
for (var i = 0; i < local_token.comments_before.length; i++) { |
| 341 |
// The cleanest handling of inline comments is to treat them as though they aren't there. |
| 342 |
// Just continue formatting and the behavior should be logical. |
| 343 |
// Also ignore unknown tokens. Again, this should result in better behavior. |
| 344 |
handle_token(local_token.comments_before[i]); |
| 345 |
} |
| 346 |
handle_token(local_token); |
| 347 |
|
| 348 |
last_last_text = flags.last_text; |
| 349 |
last_type = local_token.type; |
| 350 |
flags.last_text = local_token.text; |
| 351 |
|
| 352 |
token_pos += 1; |
| 353 |
} |
| 354 |
|
| 355 |
sweet_code = output.get_code(); |
| 356 |
if (opt.end_with_newline) { |
| 357 |
sweet_code += '\n'; |
| 358 |
} |
| 359 |
|
| 360 |
if (opt.eol !== '\n') { |
| 361 |
sweet_code = sweet_code.replace(/[\n]/g, opt.eol); |
| 362 |
} |
| 363 |
|
| 364 |
return sweet_code; |
| 365 |
}; |
| 366 |
|
| 367 |
function handle_token(local_token) { |
| 368 |
var newlines = local_token.newlines; |
| 369 |
var keep_whitespace = opt.keep_array_indentation && is_array(flags.mode); |
| 370 |
|
| 371 |
if (keep_whitespace) { |
| 372 |
for (var i = 0; i < newlines; i += 1) { |
| 373 |
print_newline(i > 0); |
| 374 |
} |
| 375 |
} else { |
| 376 |
if (opt.max_preserve_newlines && newlines > opt.max_preserve_newlines) { |
| 377 |
newlines = opt.max_preserve_newlines; |
| 378 |
} |
| 379 |
|
| 380 |
if (opt.preserve_newlines) { |
| 381 |
if (local_token.newlines > 1) { |
| 382 |
print_newline(); |
| 383 |
for (var j = 1; j < newlines; j += 1) { |
| 384 |
print_newline(true); |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
current_token = local_token; |
| 391 |
handlers[current_token.type](); |
| 392 |
} |
| 393 |
|
| 394 |
// we could use just string.split, but |
| 395 |
// IE doesn't like returning empty strings |
| 396 |
function split_linebreaks(s) { |
| 397 |
//return s.split(/\x0d\x0a|\x0a/); |
| 398 |
|
| 399 |
s = s.replace(acorn.allLineBreaks, '\n'); |
| 400 |
var out = [], |
| 401 |
idx = s.indexOf("\n"); |
| 402 |
while (idx !== -1) { |
| 403 |
out.push(s.substring(0, idx)); |
| 404 |
s = s.substring(idx + 1); |
| 405 |
idx = s.indexOf("\n"); |
| 406 |
} |
| 407 |
if (s.length) { |
| 408 |
out.push(s); |
| 409 |
} |
| 410 |
return out; |
| 411 |
} |
| 412 |
|
| 413 |
var newline_restricted_tokens = ['break', 'contiue', 'return', 'throw']; |
| 414 |
|
| 415 |
function allow_wrap_or_preserved_newline(force_linewrap) { |
| 416 |
force_linewrap = (force_linewrap === undefined) ? false : force_linewrap; |
| 417 |
|
| 418 |
// Never wrap the first token on a line |
| 419 |
if (output.just_added_newline()) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
var shouldPreserveOrForce = (opt.preserve_newlines && current_token.wanted_newline) || force_linewrap; |
| 424 |
var operatorLogicApplies = in_array(flags.last_text, Tokenizer.positionable_operators) || in_array(current_token.text, Tokenizer.positionable_operators); |
| 425 |
|
| 426 |
if (operatorLogicApplies) { |
| 427 |
var shouldPrintOperatorNewline = ( |
| 428 |
in_array(flags.last_text, Tokenizer.positionable_operators) && |
| 429 |
in_array(opt.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE) |
| 430 |
) || |
| 431 |
in_array(current_token.text, Tokenizer.positionable_operators); |
| 432 |
shouldPreserveOrForce = shouldPreserveOrForce && shouldPrintOperatorNewline; |
| 433 |
} |
| 434 |
|
| 435 |
if (shouldPreserveOrForce) { |
| 436 |
print_newline(false, true); |
| 437 |
} else if (opt.wrap_line_length) { |
| 438 |
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, newline_restricted_tokens)) { |
| 439 |
// These tokens should never have a newline inserted |
| 440 |
// between them and the following expression. |
| 441 |
return; |
| 442 |
} |
| 443 |
var proposed_line_length = output.current_line.get_character_count() + current_token.text.length + |
| 444 |
(output.space_before_token ? 1 : 0); |
| 445 |
if (proposed_line_length >= opt.wrap_line_length) { |
| 446 |
print_newline(false, true); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
function print_newline(force_newline, preserve_statement_flags) { |
| 452 |
if (!preserve_statement_flags) { |
| 453 |
if (flags.last_text !== ';' && flags.last_text !== ',' && flags.last_text !== '=' && last_type !== 'TK_OPERATOR') { |
| 454 |
while (flags.mode === MODE.Statement && !flags.if_block && !flags.do_block) { |
| 455 |
restore_mode(); |
| 456 |
} |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
if (output.add_new_line(force_newline)) { |
| 461 |
flags.multiline_frame = true; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
function print_token_line_indentation() { |
| 466 |
if (output.just_added_newline()) { |
| 467 |
if (opt.keep_array_indentation && is_array(flags.mode) && current_token.wanted_newline) { |
| 468 |
output.current_line.push(current_token.whitespace_before); |
| 469 |
output.space_before_token = false; |
| 470 |
} else if (output.set_indent(flags.indentation_level)) { |
| 471 |
flags.line_indent_level = flags.indentation_level; |
| 472 |
} |
| 473 |
} |
| 474 |
} |
| 475 |
|
| 476 |
function print_token(printable_token) { |
| 477 |
if (output.raw) { |
| 478 |
output.add_raw_token(current_token); |
| 479 |
return; |
| 480 |
} |
| 481 |
|
| 482 |
if (opt.comma_first && last_type === 'TK_COMMA' && |
| 483 |
output.just_added_newline()) { |
| 484 |
if (output.previous_line.last() === ',') { |
| 485 |
var popped = output.previous_line.pop(); |
| 486 |
// if the comma was already at the start of the line, |
| 487 |
// pull back onto that line and reprint the indentation |
| 488 |
if (output.previous_line.is_empty()) { |
| 489 |
output.previous_line.push(popped); |
| 490 |
output.trim(true); |
| 491 |
output.current_line.pop(); |
| 492 |
output.trim(); |
| 493 |
} |
| 494 |
|
| 495 |
// add the comma in front of the next token |
| 496 |
print_token_line_indentation(); |
| 497 |
output.add_token(','); |
| 498 |
output.space_before_token = true; |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
printable_token = printable_token || current_token.text; |
| 503 |
print_token_line_indentation(); |
| 504 |
output.add_token(printable_token); |
| 505 |
} |
| 506 |
|
| 507 |
function indent() { |
| 508 |
flags.indentation_level += 1; |
| 509 |
} |
| 510 |
|
| 511 |
function deindent() { |
| 512 |
if (flags.indentation_level > 0 && |
| 513 |
((!flags.parent) || flags.indentation_level > flags.parent.indentation_level)) { |
| 514 |
flags.indentation_level -= 1; |
| 515 |
|
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
function set_mode(mode) { |
| 520 |
if (flags) { |
| 521 |
flag_store.push(flags); |
| 522 |
previous_flags = flags; |
| 523 |
} else { |
| 524 |
previous_flags = create_flags(null, mode); |
| 525 |
} |
| 526 |
|
| 527 |
flags = create_flags(previous_flags, mode); |
| 528 |
} |
| 529 |
|
| 530 |
function is_array(mode) { |
| 531 |
return mode === MODE.ArrayLiteral; |
| 532 |
} |
| 533 |
|
| 534 |
function is_expression(mode) { |
| 535 |
return in_array(mode, [MODE.Expression, MODE.ForInitializer, MODE.Conditional]); |
| 536 |
} |
| 537 |
|
| 538 |
function restore_mode() { |
| 539 |
if (flag_store.length > 0) { |
| 540 |
previous_flags = flags; |
| 541 |
flags = flag_store.pop(); |
| 542 |
if (previous_flags.mode === MODE.Statement) { |
| 543 |
output.remove_redundant_indentation(previous_flags); |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
function start_of_object_property() { |
| 549 |
return flags.parent.mode === MODE.ObjectLiteral && flags.mode === MODE.Statement && ( |
| 550 |
(flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set']))); |
| 551 |
} |
| 552 |
|
| 553 |
function start_of_statement() { |
| 554 |
if ( |
| 555 |
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') || |
| 556 |
(last_type === 'TK_RESERVED' && flags.last_text === 'do') || |
| 557 |
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw']) && !current_token.wanted_newline) || |
| 558 |
(last_type === 'TK_RESERVED' && flags.last_text === 'else' && !(current_token.type === 'TK_RESERVED' && current_token.text === 'if')) || |
| 559 |
(last_type === 'TK_END_EXPR' && (previous_flags.mode === MODE.ForInitializer || previous_flags.mode === MODE.Conditional)) || |
| 560 |
(last_type === 'TK_WORD' && flags.mode === MODE.BlockStatement && |
| 561 |
!flags.in_case && |
| 562 |
!(current_token.text === '--' || current_token.text === '++') && |
| 563 |
last_last_text !== 'function' && |
| 564 |
current_token.type !== 'TK_WORD' && current_token.type !== 'TK_RESERVED') || |
| 565 |
(flags.mode === MODE.ObjectLiteral && ( |
| 566 |
(flags.last_text === ':' && flags.ternary_depth === 0) || (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set'])))) |
| 567 |
) { |
| 568 |
|
| 569 |
set_mode(MODE.Statement); |
| 570 |
indent(); |
| 571 |
|
| 572 |
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const']) && current_token.type === 'TK_WORD') { |
| 573 |
flags.declaration_statement = true; |
| 574 |
} |
| 575 |
|
| 576 |
// Issue #276: |
| 577 |
// If starting a new statement with [if, for, while, do], push to a new line. |
| 578 |
// if (a) if (b) if(c) d(); else e(); else f(); |
| 579 |
if (!start_of_object_property()) { |
| 580 |
allow_wrap_or_preserved_newline( |
| 581 |
current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['do', 'for', 'if', 'while'])); |
| 582 |
} |
| 583 |
|
| 584 |
return true; |
| 585 |
} |
| 586 |
return false; |
| 587 |
} |
| 588 |
|
| 589 |
function all_lines_start_with(lines, c) { |
| 590 |
for (var i = 0; i < lines.length; i++) { |
| 591 |
var line = trim(lines[i]); |
| 592 |
if (line.charAt(0) !== c) { |
| 593 |
return false; |
| 594 |
} |
| 595 |
} |
| 596 |
return true; |
| 597 |
} |
| 598 |
|
| 599 |
function each_line_matches_indent(lines, indent) { |
| 600 |
var i = 0, |
| 601 |
len = lines.length, |
| 602 |
line; |
| 603 |
for (; i < len; i++) { |
| 604 |
line = lines[i]; |
| 605 |
// allow empty lines to pass through |
| 606 |
if (line && line.indexOf(indent) !== 0) { |
| 607 |
return false; |
| 608 |
} |
| 609 |
} |
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
function is_special_word(word) { |
| 614 |
return in_array(word, ['case', 'return', 'do', 'if', 'throw', 'else']); |
| 615 |
} |
| 616 |
|
| 617 |
function get_token(offset) { |
| 618 |
var index = token_pos + (offset || 0); |
| 619 |
return (index < 0 || index >= tokens.length) ? null : tokens[index]; |
| 620 |
} |
| 621 |
|
| 622 |
function handle_start_expr() { |
| 623 |
if (start_of_statement()) { |
| 624 |
// The conditional starts the statement if appropriate. |
| 625 |
} |
| 626 |
|
| 627 |
var next_mode = MODE.Expression; |
| 628 |
if (current_token.text === '[') { |
| 629 |
|
| 630 |
if (last_type === 'TK_WORD' || flags.last_text === ')') { |
| 631 |
// this is array index specifier, break immediately |
| 632 |
// a[x], fn()[x] |
| 633 |
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, Tokenizer.line_starters)) { |
| 634 |
output.space_before_token = true; |
| 635 |
} |
| 636 |
set_mode(next_mode); |
| 637 |
print_token(); |
| 638 |
indent(); |
| 639 |
if (opt.space_in_paren) { |
| 640 |
output.space_before_token = true; |
| 641 |
} |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
next_mode = MODE.ArrayLiteral; |
| 646 |
if (is_array(flags.mode)) { |
| 647 |
if (flags.last_text === '[' || |
| 648 |
(flags.last_text === ',' && (last_last_text === ']' || last_last_text === '}'))) { |
| 649 |
// ], [ goes to new line |
| 650 |
// }, [ goes to new line |
| 651 |
if (!opt.keep_array_indentation) { |
| 652 |
print_newline(); |
| 653 |
} |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
} else { |
| 658 |
if (last_type === 'TK_RESERVED' && flags.last_text === 'for') { |
| 659 |
next_mode = MODE.ForInitializer; |
| 660 |
} else if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['if', 'while'])) { |
| 661 |
next_mode = MODE.Conditional; |
| 662 |
} else { |
| 663 |
// next_mode = MODE.Expression; |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
if (flags.last_text === ';' || last_type === 'TK_START_BLOCK') { |
| 668 |
print_newline(); |
| 669 |
} else if (last_type === 'TK_END_EXPR' || last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || flags.last_text === '.') { |
| 670 |
// TODO: Consider whether forcing this is required. Review failing tests when removed. |
| 671 |
allow_wrap_or_preserved_newline(current_token.wanted_newline); |
| 672 |
// do nothing on (( and )( and ][ and ]( and .( |
| 673 |
} else if (!(last_type === 'TK_RESERVED' && current_token.text === '(') && last_type !== 'TK_WORD' && last_type !== 'TK_OPERATOR') { |
| 674 |
output.space_before_token = true; |
| 675 |
} else if ((last_type === 'TK_RESERVED' && (flags.last_word === 'function' || flags.last_word === 'typeof')) || |
| 676 |
(flags.last_text === '*' && last_last_text === 'function')) { |
| 677 |
// function() vs function () |
| 678 |
if (opt.space_after_anon_function) { |
| 679 |
output.space_before_token = true; |
| 680 |
} |
| 681 |
} else if (last_type === 'TK_RESERVED' && (in_array(flags.last_text, Tokenizer.line_starters) || flags.last_text === 'catch')) { |
| 682 |
if (opt.space_before_conditional) { |
| 683 |
output.space_before_token = true; |
| 684 |
} |
| 685 |
} |
| 686 |
|
| 687 |
// Should be a space between await and an IIFE |
| 688 |
if (current_token.text === '(' && last_type === 'TK_RESERVED' && flags.last_word === 'await') { |
| 689 |
output.space_before_token = true; |
| 690 |
} |
| 691 |
|
| 692 |
// Support of this kind of newline preservation. |
| 693 |
// a = (b && |
| 694 |
// (c || d)); |
| 695 |
if (current_token.text === '(') { |
| 696 |
if (last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') { |
| 697 |
if (!start_of_object_property()) { |
| 698 |
allow_wrap_or_preserved_newline(); |
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
// Support preserving wrapped arrow function expressions |
| 704 |
// a.b('c', |
| 705 |
// () => d.e |
| 706 |
// ) |
| 707 |
if (current_token.text === '(' && last_type !== 'TK_WORD' && last_type !== 'TK_RESERVED') { |
| 708 |
allow_wrap_or_preserved_newline(); |
| 709 |
} |
| 710 |
|
| 711 |
set_mode(next_mode); |
| 712 |
print_token(); |
| 713 |
if (opt.space_in_paren) { |
| 714 |
output.space_before_token = true; |
| 715 |
} |
| 716 |
|
| 717 |
// In all cases, if we newline while inside an expression it should be indented. |
| 718 |
indent(); |
| 719 |
} |
| 720 |
|
| 721 |
function handle_end_expr() { |
| 722 |
// statements inside expressions are not valid syntax, but... |
| 723 |
// statements must all be closed when their container closes |
| 724 |
while (flags.mode === MODE.Statement) { |
| 725 |
restore_mode(); |
| 726 |
} |
| 727 |
|
| 728 |
if (flags.multiline_frame) { |
| 729 |
allow_wrap_or_preserved_newline(current_token.text === ']' && is_array(flags.mode) && !opt.keep_array_indentation); |
| 730 |
} |
| 731 |
|
| 732 |
if (opt.space_in_paren) { |
| 733 |
if (last_type === 'TK_START_EXPR' && !opt.space_in_empty_paren) { |
| 734 |
// () [] no inner space in empty parens like these, ever, ref #320 |
| 735 |
output.trim(); |
| 736 |
output.space_before_token = false; |
| 737 |
} else { |
| 738 |
output.space_before_token = true; |
| 739 |
} |
| 740 |
} |
| 741 |
if (current_token.text === ']' && opt.keep_array_indentation) { |
| 742 |
print_token(); |
| 743 |
restore_mode(); |
| 744 |
} else { |
| 745 |
restore_mode(); |
| 746 |
print_token(); |
| 747 |
} |
| 748 |
output.remove_redundant_indentation(previous_flags); |
| 749 |
|
| 750 |
// do {} while () // no statement required after |
| 751 |
if (flags.do_while && previous_flags.mode === MODE.Conditional) { |
| 752 |
previous_flags.mode = MODE.Expression; |
| 753 |
flags.do_block = false; |
| 754 |
flags.do_while = false; |
| 755 |
|
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
function handle_start_block() { |
| 760 |
// Check if this is should be treated as a ObjectLiteral |
| 761 |
var next_token = get_token(1); |
| 762 |
var second_token = get_token(2); |
| 763 |
if (second_token && ( |
| 764 |
(in_array(second_token.text, [':', ',']) && in_array(next_token.type, ['TK_STRING', 'TK_WORD', 'TK_RESERVED'])) || |
| 765 |
(in_array(next_token.text, ['get', 'set']) && in_array(second_token.type, ['TK_WORD', 'TK_RESERVED'])) |
| 766 |
)) { |
| 767 |
// We don't support TypeScript,but we didn't break it for a very long time. |
| 768 |
// We'll try to keep not breaking it. |
| 769 |
if (!in_array(last_last_text, ['class', 'interface'])) { |
| 770 |
set_mode(MODE.ObjectLiteral); |
| 771 |
} else { |
| 772 |
set_mode(MODE.BlockStatement); |
| 773 |
} |
| 774 |
} else if (last_type === 'TK_OPERATOR' && flags.last_text === '=>') { |
| 775 |
// arrow function: (param1, paramN) => { statements } |
| 776 |
set_mode(MODE.BlockStatement); |
| 777 |
} else if (in_array(last_type, ['TK_EQUALS', 'TK_START_EXPR', 'TK_COMMA', 'TK_OPERATOR']) || |
| 778 |
(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['return', 'throw', 'import'])) |
| 779 |
) { |
| 780 |
// Detecting shorthand function syntax is difficult by scanning forward, |
| 781 |
// so check the surrounding context. |
| 782 |
// If the block is being returned, imported, passed as arg, |
| 783 |
// assigned with = or assigned in a nested object, treat as an ObjectLiteral. |
| 784 |
set_mode(MODE.ObjectLiteral); |
| 785 |
} else { |
| 786 |
set_mode(MODE.BlockStatement); |
| 787 |
} |
| 788 |
|
| 789 |
var empty_braces = !next_token.comments_before.length && next_token.text === '}'; |
| 790 |
var empty_anonymous_function = empty_braces && flags.last_word === 'function' && |
| 791 |
last_type === 'TK_END_EXPR'; |
| 792 |
|
| 793 |
|
| 794 |
if (opt.brace_style === "expand" || |
| 795 |
(opt.brace_style === "none" && current_token.wanted_newline)) { |
| 796 |
if (last_type !== 'TK_OPERATOR' && |
| 797 |
(empty_anonymous_function || |
| 798 |
last_type === 'TK_EQUALS' || |
| 799 |
(last_type === 'TK_RESERVED' && is_special_word(flags.last_text) && flags.last_text !== 'else'))) { |
| 800 |
output.space_before_token = true; |
| 801 |
} else { |
| 802 |
print_newline(false, true); |
| 803 |
} |
| 804 |
} else { // collapse |
| 805 |
if (opt.brace_style === 'collapse-preserve-inline') { |
| 806 |
// search forward for a newline wanted inside this block |
| 807 |
var index = 0; |
| 808 |
var check_token = null; |
| 809 |
flags.inline_frame = true; |
| 810 |
do { |
| 811 |
index += 1; |
| 812 |
check_token = get_token(index); |
| 813 |
if (check_token.wanted_newline) { |
| 814 |
flags.inline_frame = false; |
| 815 |
break; |
| 816 |
} |
| 817 |
} while (check_token.type !== 'TK_EOF' && |
| 818 |
!(check_token.type === 'TK_END_BLOCK' && check_token.opened === current_token)); |
| 819 |
} |
| 820 |
|
| 821 |
if (is_array(previous_flags.mode) && (last_type === 'TK_START_EXPR' || last_type === 'TK_COMMA')) { |
| 822 |
// if we're preserving inline, |
| 823 |
// allow newline between comma and next brace. |
| 824 |
if (last_type === 'TK_COMMA' || opt.space_in_paren) { |
| 825 |
output.space_before_token = true; |
| 826 |
} |
| 827 |
|
| 828 |
if (opt.brace_style === 'collapse-preserve-inline' && |
| 829 |
(last_type === 'TK_COMMA' || (last_type === 'TK_START_EXPR' && flags.inline_frame))) { |
| 830 |
allow_wrap_or_preserved_newline(); |
| 831 |
previous_flags.multiline_frame = previous_flags.multiline_frame || flags.multiline_frame; |
| 832 |
flags.multiline_frame = false; |
| 833 |
} |
| 834 |
} else if (last_type !== 'TK_OPERATOR' && last_type !== 'TK_START_EXPR') { |
| 835 |
if (last_type === 'TK_START_BLOCK') { |
| 836 |
print_newline(); |
| 837 |
} else { |
| 838 |
output.space_before_token = true; |
| 839 |
} |
| 840 |
} |
| 841 |
} |
| 842 |
print_token(); |
| 843 |
indent(); |
| 844 |
} |
| 845 |
|
| 846 |
function handle_end_block() { |
| 847 |
// statements must all be closed when their container closes |
| 848 |
while (flags.mode === MODE.Statement) { |
| 849 |
restore_mode(); |
| 850 |
} |
| 851 |
var empty_braces = last_type === 'TK_START_BLOCK'; |
| 852 |
|
| 853 |
if (opt.brace_style === "expand") { |
| 854 |
if (!empty_braces) { |
| 855 |
print_newline(); |
| 856 |
} |
| 857 |
} else { |
| 858 |
// skip {} |
| 859 |
if (!empty_braces) { |
| 860 |
if (flags.inline_frame) { |
| 861 |
output.space_before_token = true; |
| 862 |
} else if (is_array(flags.mode) && opt.keep_array_indentation) { |
| 863 |
// we REALLY need a newline here, but newliner would skip that |
| 864 |
opt.keep_array_indentation = false; |
| 865 |
print_newline(); |
| 866 |
opt.keep_array_indentation = true; |
| 867 |
|
| 868 |
} else { |
| 869 |
print_newline(); |
| 870 |
} |
| 871 |
} |
| 872 |
} |
| 873 |
restore_mode(); |
| 874 |
print_token(); |
| 875 |
} |
| 876 |
|
| 877 |
function handle_word() { |
| 878 |
if (current_token.type === 'TK_RESERVED') { |
| 879 |
if (in_array(current_token.text, ['set', 'get']) && flags.mode !== MODE.ObjectLiteral) { |
| 880 |
current_token.type = 'TK_WORD'; |
| 881 |
} else if (in_array(current_token.text, ['as', 'from']) && !flags.import_block) { |
| 882 |
current_token.type = 'TK_WORD'; |
| 883 |
} else if (flags.mode === MODE.ObjectLiteral) { |
| 884 |
var next_token = get_token(1); |
| 885 |
if (next_token.text === ':') { |
| 886 |
current_token.type = 'TK_WORD'; |
| 887 |
} |
| 888 |
} |
| 889 |
} |
| 890 |
|
| 891 |
if (start_of_statement()) { |
| 892 |
// The conditional starts the statement if appropriate. |
| 893 |
} else if (current_token.wanted_newline && !is_expression(flags.mode) && |
| 894 |
(last_type !== 'TK_OPERATOR' || (flags.last_text === '--' || flags.last_text === '++')) && |
| 895 |
last_type !== 'TK_EQUALS' && |
| 896 |
(opt.preserve_newlines || !(last_type === 'TK_RESERVED' && in_array(flags.last_text, ['var', 'let', 'const', 'set', 'get'])))) { |
| 897 |
|
| 898 |
print_newline(); |
| 899 |
} |
| 900 |
|
| 901 |
if (flags.do_block && !flags.do_while) { |
| 902 |
if (current_token.type === 'TK_RESERVED' && current_token.text === 'while') { |
| 903 |
// do {} ## while () |
| 904 |
output.space_before_token = true; |
| 905 |
print_token(); |
| 906 |
output.space_before_token = true; |
| 907 |
flags.do_while = true; |
| 908 |
return; |
| 909 |
} else { |
| 910 |
// do {} should always have while as the next word. |
| 911 |
// if we don't see the expected while, recover |
| 912 |
print_newline(); |
| 913 |
flags.do_block = false; |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
// if may be followed by else, or not |
| 918 |
// Bare/inline ifs are tricky |
| 919 |
// Need to unwind the modes correctly: if (a) if (b) c(); else d(); else e(); |
| 920 |
if (flags.if_block) { |
| 921 |
if (!flags.else_block && (current_token.type === 'TK_RESERVED' && current_token.text === 'else')) { |
| 922 |
flags.else_block = true; |
| 923 |
} else { |
| 924 |
while (flags.mode === MODE.Statement) { |
| 925 |
restore_mode(); |
| 926 |
} |
| 927 |
flags.if_block = false; |
| 928 |
flags.else_block = false; |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
if (current_token.type === 'TK_RESERVED' && (current_token.text === 'case' || (current_token.text === 'default' && flags.in_case_statement))) { |
| 933 |
print_newline(); |
| 934 |
if (flags.case_body || opt.jslint_happy) { |
| 935 |
// switch cases following one another |
| 936 |
deindent(); |
| 937 |
flags.case_body = false; |
| 938 |
} |
| 939 |
print_token(); |
| 940 |
flags.in_case = true; |
| 941 |
flags.in_case_statement = true; |
| 942 |
return; |
| 943 |
} |
| 944 |
|
| 945 |
if (current_token.type === 'TK_RESERVED' && current_token.text === 'function') { |
| 946 |
if (in_array(flags.last_text, ['}', ';']) || (output.just_added_newline() && !in_array(flags.last_text, ['[', '{', ':', '=', ',']))) { |
| 947 |
// make sure there is a nice clean space of at least one blank line |
| 948 |
// before a new function definition |
| 949 |
if (!output.just_added_blankline() && !current_token.comments_before.length) { |
| 950 |
print_newline(); |
| 951 |
print_newline(true); |
| 952 |
} |
| 953 |
} |
| 954 |
if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD') { |
| 955 |
if (last_type === 'TK_RESERVED' && in_array(flags.last_text, ['get', 'set', 'new', 'return', 'export', 'async'])) { |
| 956 |
output.space_before_token = true; |
| 957 |
} else if (last_type === 'TK_RESERVED' && flags.last_text === 'default' && last_last_text === 'export') { |
| 958 |
output.space_before_token = true; |
| 959 |
} else { |
| 960 |
print_newline(); |
| 961 |
} |
| 962 |
} else if (last_type === 'TK_OPERATOR' || flags.last_text === '=') { |
| 963 |
// foo = function |
| 964 |
output.space_before_token = true; |
| 965 |
} else if (!flags.multiline_frame && (is_expression(flags.mode) || is_array(flags.mode))) { |
| 966 |
// (function |
| 967 |
} else { |
| 968 |
print_newline(); |
| 969 |
} |
| 970 |
} |
| 971 |
|
| 972 |
if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') { |
| 973 |
if (!start_of_object_property()) { |
| 974 |
allow_wrap_or_preserved_newline(); |
| 975 |
} |
| 976 |
} |
| 977 |
|
| 978 |
if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['function', 'get', 'set'])) { |
| 979 |
print_token(); |
| 980 |
flags.last_word = current_token.text; |
| 981 |
return; |
| 982 |
} |
| 983 |
|
| 984 |
prefix = 'NONE'; |
| 985 |
|
| 986 |
if (last_type === 'TK_END_BLOCK') { |
| 987 |
|
| 988 |
if (!(current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['else', 'catch', 'finally', 'from']))) { |
| 989 |
prefix = 'NEWLINE'; |
| 990 |
} else { |
| 991 |
if (opt.brace_style === "expand" || |
| 992 |
opt.brace_style === "end-expand" || |
| 993 |
(opt.brace_style === "none" && current_token.wanted_newline)) { |
| 994 |
prefix = 'NEWLINE'; |
| 995 |
} else { |
| 996 |
prefix = 'SPACE'; |
| 997 |
output.space_before_token = true; |
| 998 |
} |
| 999 |
} |
| 1000 |
} else if (last_type === 'TK_SEMICOLON' && flags.mode === MODE.BlockStatement) { |
| 1001 |
// TODO: Should this be for STATEMENT as well? |
| 1002 |
prefix = 'NEWLINE'; |
| 1003 |
} else if (last_type === 'TK_SEMICOLON' && is_expression(flags.mode)) { |
| 1004 |
prefix = 'SPACE'; |
| 1005 |
} else if (last_type === 'TK_STRING') { |
| 1006 |
prefix = 'NEWLINE'; |
| 1007 |
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' || |
| 1008 |
(flags.last_text === '*' && last_last_text === 'function')) { |
| 1009 |
prefix = 'SPACE'; |
| 1010 |
} else if (last_type === 'TK_START_BLOCK') { |
| 1011 |
if (flags.inline_frame) { |
| 1012 |
prefix = 'SPACE'; |
| 1013 |
} else { |
| 1014 |
prefix = 'NEWLINE'; |
| 1015 |
} |
| 1016 |
} else if (last_type === 'TK_END_EXPR') { |
| 1017 |
output.space_before_token = true; |
| 1018 |
prefix = 'NEWLINE'; |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, Tokenizer.line_starters) && flags.last_text !== ')') { |
| 1022 |
if (flags.last_text === 'else' || flags.last_text === 'export') { |
| 1023 |
prefix = 'SPACE'; |
| 1024 |
} else { |
| 1025 |
prefix = 'NEWLINE'; |
| 1026 |
} |
| 1027 |
|
| 1028 |
} |
| 1029 |
|
| 1030 |
if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['else', 'catch', 'finally'])) { |
| 1031 |
if (!(last_type === 'TK_END_BLOCK' && previous_flags.mode === MODE.BlockStatement) || |
| 1032 |
opt.brace_style === "expand" || |
| 1033 |
opt.brace_style === "end-expand" || |
| 1034 |
(opt.brace_style === "none" && current_token.wanted_newline)) { |
| 1035 |
print_newline(); |
| 1036 |
} else { |
| 1037 |
output.trim(true); |
| 1038 |
var line = output.current_line; |
| 1039 |
// If we trimmed and there's something other than a close block before us |
| 1040 |
// put a newline back in. Handles '} // comment' scenario. |
| 1041 |
if (line.last() !== '}') { |
| 1042 |
print_newline(); |
| 1043 |
} |
| 1044 |
output.space_before_token = true; |
| 1045 |
} |
| 1046 |
} else if (prefix === 'NEWLINE') { |
| 1047 |
if (last_type === 'TK_RESERVED' && is_special_word(flags.last_text)) { |
| 1048 |
// no newline between 'return nnn' |
| 1049 |
output.space_before_token = true; |
| 1050 |
} else if (last_type !== 'TK_END_EXPR') { |
| 1051 |
if ((last_type !== 'TK_START_EXPR' || !(current_token.type === 'TK_RESERVED' && in_array(current_token.text, ['var', 'let', 'const']))) && flags.last_text !== ':') { |
| 1052 |
// no need to force newline on 'var': for (var x = 0...) |
| 1053 |
if (current_token.type === 'TK_RESERVED' && current_token.text === 'if' && flags.last_text === 'else') { |
| 1054 |
// no newline for } else if { |
| 1055 |
output.space_before_token = true; |
| 1056 |
} else { |
| 1057 |
print_newline(); |
| 1058 |
} |
| 1059 |
} |
| 1060 |
} else if (current_token.type === 'TK_RESERVED' && in_array(current_token.text, Tokenizer.line_starters) && flags.last_text !== ')') { |
| 1061 |
print_newline(); |
| 1062 |
} |
| 1063 |
} else if (flags.multiline_frame && is_array(flags.mode) && flags.last_text === ',' && last_last_text === '}') { |
| 1064 |
print_newline(); // }, in lists get a newline treatment |
| 1065 |
} else if (prefix === 'SPACE') { |
| 1066 |
output.space_before_token = true; |
| 1067 |
} |
| 1068 |
print_token(); |
| 1069 |
flags.last_word = current_token.text; |
| 1070 |
|
| 1071 |
if (current_token.type === 'TK_RESERVED') { |
| 1072 |
if (current_token.text === 'do') { |
| 1073 |
flags.do_block = true; |
| 1074 |
} else if (current_token.text === 'if') { |
| 1075 |
flags.if_block = true; |
| 1076 |
} else if (current_token.text === 'import') { |
| 1077 |
flags.import_block = true; |
| 1078 |
} else if (flags.import_block && current_token.type === 'TK_RESERVED' && current_token.text === 'from') { |
| 1079 |
flags.import_block = false; |
| 1080 |
} |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
function handle_semicolon() { |
| 1085 |
if (start_of_statement()) { |
| 1086 |
// The conditional starts the statement if appropriate. |
| 1087 |
// Semicolon can be the start (and end) of a statement |
| 1088 |
output.space_before_token = false; |
| 1089 |
} |
| 1090 |
while (flags.mode === MODE.Statement && !flags.if_block && !flags.do_block) { |
| 1091 |
restore_mode(); |
| 1092 |
} |
| 1093 |
|
| 1094 |
// hacky but effective for the moment |
| 1095 |
if (flags.import_block) { |
| 1096 |
flags.import_block = false; |
| 1097 |
} |
| 1098 |
print_token(); |
| 1099 |
} |
| 1100 |
|
| 1101 |
function handle_string() { |
| 1102 |
if (start_of_statement()) { |
| 1103 |
// The conditional starts the statement if appropriate. |
| 1104 |
// One difference - strings want at least a space before |
| 1105 |
output.space_before_token = true; |
| 1106 |
} else if (last_type === 'TK_RESERVED' || last_type === 'TK_WORD' || flags.inline_frame) { |
| 1107 |
output.space_before_token = true; |
| 1108 |
} else if (last_type === 'TK_COMMA' || last_type === 'TK_START_EXPR' || last_type === 'TK_EQUALS' || last_type === 'TK_OPERATOR') { |
| 1109 |
if (!start_of_object_property()) { |
| 1110 |
allow_wrap_or_preserved_newline(); |
| 1111 |
} |
| 1112 |
} else { |
| 1113 |
print_newline(); |
| 1114 |
} |
| 1115 |
print_token(); |
| 1116 |
} |
| 1117 |
|
| 1118 |
function handle_equals() { |
| 1119 |
if (start_of_statement()) { |
| 1120 |
// The conditional starts the statement if appropriate. |
| 1121 |
} |
| 1122 |
|
| 1123 |
if (flags.declaration_statement) { |
| 1124 |
// just got an '=' in a var-line, different formatting/line-breaking, etc will now be done |
| 1125 |
flags.declaration_assignment = true; |
| 1126 |
} |
| 1127 |
output.space_before_token = true; |
| 1128 |
print_token(); |
| 1129 |
output.space_before_token = true; |
| 1130 |
} |
| 1131 |
|
| 1132 |
function handle_comma() { |
| 1133 |
print_token(); |
| 1134 |
output.space_before_token = true; |
| 1135 |
if (flags.declaration_statement) { |
| 1136 |
if (is_expression(flags.parent.mode)) { |
| 1137 |
// do not break on comma, for(var a = 1, b = 2) |
| 1138 |
flags.declaration_assignment = false; |
| 1139 |
} |
| 1140 |
|
| 1141 |
if (flags.declaration_assignment) { |
| 1142 |
flags.declaration_assignment = false; |
| 1143 |
print_newline(false, true); |
| 1144 |
} else if (opt.comma_first) { |
| 1145 |
// for comma-first, we want to allow a newline before the comma |
| 1146 |
// to turn into a newline after the comma, which we will fixup later |
| 1147 |
allow_wrap_or_preserved_newline(); |
| 1148 |
} |
| 1149 |
} else if (flags.mode === MODE.ObjectLiteral || |
| 1150 |
(flags.mode === MODE.Statement && flags.parent.mode === MODE.ObjectLiteral)) { |
| 1151 |
if (flags.mode === MODE.Statement) { |
| 1152 |
restore_mode(); |
| 1153 |
} |
| 1154 |
|
| 1155 |
if (!flags.inline_frame) { |
| 1156 |
print_newline(); |
| 1157 |
} |
| 1158 |
} else if (opt.comma_first) { |
| 1159 |
// EXPR or DO_BLOCK |
| 1160 |
// for comma-first, we want to allow a newline before the comma |
| 1161 |
// to turn into a newline after the comma, which we will fixup later |
| 1162 |
allow_wrap_or_preserved_newline(); |
| 1163 |
} |
| 1164 |
} |
| 1165 |
|
| 1166 |
function handle_operator() { |
| 1167 |
if (start_of_statement()) { |
| 1168 |
// The conditional starts the statement if appropriate. |
| 1169 |
} |
| 1170 |
|
| 1171 |
if (last_type === 'TK_RESERVED' && is_special_word(flags.last_text)) { |
| 1172 |
// "return" had a special handling in TK_WORD. Now we need to return the favor |
| 1173 |
output.space_before_token = true; |
| 1174 |
print_token(); |
| 1175 |
return; |
| 1176 |
} |
| 1177 |
|
| 1178 |
// hack for actionscript's import .*; |
| 1179 |
if (current_token.text === '*' && last_type === 'TK_DOT') { |
| 1180 |
print_token(); |
| 1181 |
return; |
| 1182 |
} |
| 1183 |
|
| 1184 |
if (current_token.text === '::') { |
| 1185 |
// no spaces around exotic namespacing syntax operator |
| 1186 |
print_token(); |
| 1187 |
return; |
| 1188 |
} |
| 1189 |
|
| 1190 |
// Allow line wrapping between operators when operator_position is |
| 1191 |
// set to before or preserve |
| 1192 |
if (last_type === 'TK_OPERATOR' && in_array(opt.operator_position, OPERATOR_POSITION_BEFORE_OR_PRESERVE)) { |
| 1193 |
allow_wrap_or_preserved_newline(); |
| 1194 |
} |
| 1195 |
|
| 1196 |
if (current_token.text === ':' && flags.in_case) { |
| 1197 |
flags.case_body = true; |
| 1198 |
indent(); |
| 1199 |
print_token(); |
| 1200 |
print_newline(); |
| 1201 |
flags.in_case = false; |
| 1202 |
return; |
| 1203 |
} |
| 1204 |
|
| 1205 |
var space_before = true; |
| 1206 |
var space_after = true; |
| 1207 |
var in_ternary = false; |
| 1208 |
var isGeneratorAsterisk = current_token.text === '*' && last_type === 'TK_RESERVED' && flags.last_text === 'function'; |
| 1209 |
var isUnary = in_array(current_token.text, ['-', '+']) && ( |
| 1210 |
in_array(last_type, ['TK_START_BLOCK', 'TK_START_EXPR', 'TK_EQUALS', 'TK_OPERATOR']) || |
| 1211 |
in_array(flags.last_text, Tokenizer.line_starters) || |
| 1212 |
flags.last_text === ',' |
| 1213 |
); |
| 1214 |
|
| 1215 |
if (current_token.text === ':') { |
| 1216 |
if (flags.ternary_depth === 0) { |
| 1217 |
// Colon is invalid javascript outside of ternary and object, but do our best to guess what was meant. |
| 1218 |
space_before = false; |
| 1219 |
} else { |
| 1220 |
flags.ternary_depth -= 1; |
| 1221 |
in_ternary = true; |
| 1222 |
} |
| 1223 |
} else if (current_token.text === '?') { |
| 1224 |
flags.ternary_depth += 1; |
| 1225 |
} |
| 1226 |
|
| 1227 |
// let's handle the operator_position option prior to any conflicting logic |
| 1228 |
if (!isUnary && !isGeneratorAsterisk && opt.preserve_newlines && in_array(current_token.text, Tokenizer.positionable_operators)) { |
| 1229 |
var isColon = current_token.text === ':'; |
| 1230 |
var isTernaryColon = (isColon && in_ternary); |
| 1231 |
var isOtherColon = (isColon && !in_ternary); |
| 1232 |
|
| 1233 |
switch (opt.operator_position) { |
| 1234 |
case OPERATOR_POSITION.before_newline: |
| 1235 |
// if the current token is : and it's not a ternary statement then we set space_before to false |
| 1236 |
output.space_before_token = !isOtherColon; |
| 1237 |
|
| 1238 |
print_token(); |
| 1239 |
|
| 1240 |
if (!isColon || isTernaryColon) { |
| 1241 |
allow_wrap_or_preserved_newline(); |
| 1242 |
} |
| 1243 |
|
| 1244 |
output.space_before_token = true; |
| 1245 |
return; |
| 1246 |
|
| 1247 |
case OPERATOR_POSITION.after_newline: |
| 1248 |
// if the current token is anything but colon, or (via deduction) it's a colon and in a ternary statement, |
| 1249 |
// then print a newline. |
| 1250 |
|
| 1251 |
output.space_before_token = true; |
| 1252 |
|
| 1253 |
if (!isColon || isTernaryColon) { |
| 1254 |
if (get_token(1).wanted_newline) { |
| 1255 |
print_newline(false, true); |
| 1256 |
} else { |
| 1257 |
allow_wrap_or_preserved_newline(); |
| 1258 |
} |
| 1259 |
} else { |
| 1260 |
output.space_before_token = false; |
| 1261 |
} |
| 1262 |
|
| 1263 |
print_token(); |
| 1264 |
|
| 1265 |
output.space_before_token = true; |
| 1266 |
return; |
| 1267 |
|
| 1268 |
case OPERATOR_POSITION.preserve_newline: |
| 1269 |
if (!isOtherColon) { |
| 1270 |
allow_wrap_or_preserved_newline(); |
| 1271 |
} |
| 1272 |
|
| 1273 |
// if we just added a newline, or the current token is : and it's not a ternary statement, |
| 1274 |
// then we set space_before to false |
| 1275 |
space_before = !(output.just_added_newline() || isOtherColon); |
| 1276 |
|
| 1277 |
output.space_before_token = space_before; |
| 1278 |
print_token(); |
| 1279 |
output.space_before_token = true; |
| 1280 |
return; |
| 1281 |
} |
| 1282 |
} |
| 1283 |
|
| 1284 |
if (in_array(current_token.text, ['--', '++', '!', '~']) || isUnary) { |
| 1285 |
// unary operators (and binary +/- pretending to be unary) special cases |
| 1286 |
|
| 1287 |
space_before = false; |
| 1288 |
space_after = false; |
| 1289 |
|
| 1290 |
// http://www.ecma-international.org/ecma-262/5.1/#sec-7.9.1 |
| 1291 |
// if there is a newline between -- or ++ and anything else we should preserve it. |
| 1292 |
if (current_token.wanted_newline && (current_token.text === '--' || current_token.text === '++')) { |
| 1293 |
print_newline(false, true); |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (flags.last_text === ';' && is_expression(flags.mode)) { |
| 1297 |
// for (;; ++i) |
| 1298 |
// ^^^ |
| 1299 |
space_before = true; |
| 1300 |
} |
| 1301 |
|
| 1302 |
if (last_type === 'TK_RESERVED') { |
| 1303 |
space_before = true; |
| 1304 |
} else if (last_type === 'TK_END_EXPR') { |
| 1305 |
space_before = !(flags.last_text === ']' && (current_token.text === '--' || current_token.text === '++')); |
| 1306 |
} else if (last_type === 'TK_OPERATOR') { |
| 1307 |
// a++ + ++b; |
| 1308 |
// a - -b |
| 1309 |
space_before = in_array(current_token.text, ['--', '-', '++', '+']) && in_array(flags.last_text, ['--', '-', '++', '+']); |
| 1310 |
// + and - are not unary when preceeded by -- or ++ operator |
| 1311 |
// a-- + b |
| 1312 |
// a * +b |
| 1313 |
// a - -b |
| 1314 |
if (in_array(current_token.text, ['+', '-']) && in_array(flags.last_text, ['--', '++'])) { |
| 1315 |
space_after = true; |
| 1316 |
} |
| 1317 |
} |
| 1318 |
|
| 1319 |
|
| 1320 |
if (((flags.mode === MODE.BlockStatement && !flags.inline_frame) || flags.mode === MODE.Statement) && |
| 1321 |
(flags.last_text === '{' || flags.last_text === ';')) { |
| 1322 |
// { foo; --i } |
| 1323 |
// foo(); --bar; |
| 1324 |
print_newline(); |
| 1325 |
} |
| 1326 |
} else if (isGeneratorAsterisk) { |
| 1327 |
space_before = false; |
| 1328 |
space_after = false; |
| 1329 |
} |
| 1330 |
output.space_before_token = output.space_before_token || space_before; |
| 1331 |
print_token(); |
| 1332 |
output.space_before_token = space_after; |
| 1333 |
} |
| 1334 |
|
| 1335 |
function handle_block_comment() { |
| 1336 |
if (output.raw) { |
| 1337 |
output.add_raw_token(current_token); |
| 1338 |
if (current_token.directives && current_token.directives.preserve === 'end') { |
| 1339 |
// If we're testing the raw output behavior, do not allow a directive to turn it off. |
| 1340 |
output.raw = opt.test_output_raw; |
| 1341 |
} |
| 1342 |
return; |
| 1343 |
} |
| 1344 |
|
| 1345 |
if (current_token.directives) { |
| 1346 |
print_newline(false, true); |
| 1347 |
print_token(); |
| 1348 |
if (current_token.directives.preserve === 'start') { |
| 1349 |
output.raw = true; |
| 1350 |
} |
| 1351 |
print_newline(false, true); |
| 1352 |
return; |
| 1353 |
} |
| 1354 |
|
| 1355 |
// inline block |
| 1356 |
if (!acorn.isNewLine(current_token.text.charCodeAt(0)) && !current_token.wanted_newline) { |
| 1357 |
output.space_before_token = true; |
| 1358 |
print_token(); |
| 1359 |
output.space_before_token = true; |
| 1360 |
return; |
| 1361 |
} |
| 1362 |
|
| 1363 |
var lines = split_linebreaks(current_token.text); |
| 1364 |
var j; // iterator for this case |
| 1365 |
var javadoc = false; |
| 1366 |
var starless = false; |
| 1367 |
var lastIndent = current_token.whitespace_before; |
| 1368 |
var lastIndentLength = lastIndent.length; |
| 1369 |
|
| 1370 |
// block comment starts with a new line |
| 1371 |
print_newline(false, true); |
| 1372 |
if (lines.length > 1) { |
| 1373 |
javadoc = all_lines_start_with(lines.slice(1), '*'); |
| 1374 |
starless = each_line_matches_indent(lines.slice(1), lastIndent); |
| 1375 |
} |
| 1376 |
|
| 1377 |
// first line always indented |
| 1378 |
print_token(lines[0]); |
| 1379 |
for (j = 1; j < lines.length; j++) { |
| 1380 |
print_newline(false, true); |
| 1381 |
if (javadoc) { |
| 1382 |
// javadoc: reformat and re-indent |
| 1383 |
print_token(' ' + ltrim(lines[j])); |
| 1384 |
} else if (starless && lines[j].length > lastIndentLength) { |
| 1385 |
// starless: re-indent non-empty content, avoiding trim |
| 1386 |
print_token(lines[j].substring(lastIndentLength)); |
| 1387 |
} else { |
| 1388 |
// normal comments output raw |
| 1389 |
output.add_token(lines[j]); |
| 1390 |
} |
| 1391 |
} |
| 1392 |
|
| 1393 |
// for comments of more than one line, make sure there's a new line after |
| 1394 |
print_newline(false, true); |
| 1395 |
} |
| 1396 |
|
| 1397 |
function handle_comment() { |
| 1398 |
if (current_token.wanted_newline) { |
| 1399 |
print_newline(false, true); |
| 1400 |
} else { |
| 1401 |
output.trim(true); |
| 1402 |
} |
| 1403 |
|
| 1404 |
output.space_before_token = true; |
| 1405 |
print_token(); |
| 1406 |
print_newline(false, true); |
| 1407 |
} |
| 1408 |
|
| 1409 |
function handle_dot() { |
| 1410 |
if (start_of_statement()) { |
| 1411 |
// The conditional starts the statement if appropriate. |
| 1412 |
} |
| 1413 |
|
| 1414 |
if (last_type === 'TK_RESERVED' && is_special_word(flags.last_text)) { |
| 1415 |
output.space_before_token = true; |
| 1416 |
} else { |
| 1417 |
// allow preserved newlines before dots in general |
| 1418 |
// force newlines on dots after close paren when break_chained - for bar().baz() |
| 1419 |
allow_wrap_or_preserved_newline(flags.last_text === ')' && opt.break_chained_methods); |
| 1420 |
} |
| 1421 |
|
| 1422 |
print_token(); |
| 1423 |
} |
| 1424 |
|
| 1425 |
function handle_unknown() { |
| 1426 |
print_token(); |
| 1427 |
|
| 1428 |
if (current_token.text[current_token.text.length - 1] === '\n') { |
| 1429 |
print_newline(); |
| 1430 |
} |
| 1431 |
} |
| 1432 |
|
| 1433 |
function handle_eof() { |
| 1434 |
// Unwind any open statements |
| 1435 |
while (flags.mode === MODE.Statement) { |
| 1436 |
restore_mode(); |
| 1437 |
} |
| 1438 |
} |
| 1439 |
} |
| 1440 |
|
| 1441 |
|
| 1442 |
function OutputLine(parent) { |
| 1443 |
var _character_count = 0; |
| 1444 |
// use indent_count as a marker for lines that have preserved indentation |
| 1445 |
var _indent_count = -1; |
| 1446 |
|
| 1447 |
var _items = []; |
| 1448 |
var _empty = true; |
| 1449 |
|
| 1450 |
this.set_indent = function(level) { |
| 1451 |
_character_count = parent.baseIndentLength + level * parent.indent_length; |
| 1452 |
_indent_count = level; |
| 1453 |
}; |
| 1454 |
|
| 1455 |
this.get_character_count = function() { |
| 1456 |
return _character_count; |
| 1457 |
}; |
| 1458 |
|
| 1459 |
this.is_empty = function() { |
| 1460 |
return _empty; |
| 1461 |
}; |
| 1462 |
|
| 1463 |
this.last = function() { |
| 1464 |
if (!this._empty) { |
| 1465 |
return _items[_items.length - 1]; |
| 1466 |
} else { |
| 1467 |
return null; |
| 1468 |
} |
| 1469 |
}; |
| 1470 |
|
| 1471 |
this.push = function(input) { |
| 1472 |
_items.push(input); |
| 1473 |
_character_count += input.length; |
| 1474 |
_empty = false; |
| 1475 |
}; |
| 1476 |
|
| 1477 |
this.pop = function() { |
| 1478 |
var item = null; |
| 1479 |
if (!_empty) { |
| 1480 |
item = _items.pop(); |
| 1481 |
_character_count -= item.length; |
| 1482 |
_empty = _items.length === 0; |
| 1483 |
} |
| 1484 |
return item; |
| 1485 |
}; |
| 1486 |
|
| 1487 |
this.remove_indent = function() { |
| 1488 |
if (_indent_count > 0) { |
| 1489 |
_indent_count -= 1; |
| 1490 |
_character_count -= parent.indent_length; |
| 1491 |
} |
| 1492 |
}; |
| 1493 |
|
| 1494 |
this.trim = function() { |
| 1495 |
while (this.last() === ' ') { |
| 1496 |
_items.pop(); |
| 1497 |
_character_count -= 1; |
| 1498 |
} |
| 1499 |
_empty = _items.length === 0; |
| 1500 |
}; |
| 1501 |
|
| 1502 |
this.toString = function() { |
| 1503 |
var result = ''; |
| 1504 |
if (!this._empty) { |
| 1505 |
if (_indent_count >= 0) { |
| 1506 |
result = parent.indent_cache[_indent_count]; |
| 1507 |
} |
| 1508 |
result += _items.join(''); |
| 1509 |
} |
| 1510 |
return result; |
| 1511 |
}; |
| 1512 |
} |
| 1513 |
|
| 1514 |
function Output(indent_string, baseIndentString) { |
| 1515 |
baseIndentString = baseIndentString || ''; |
| 1516 |
this.indent_cache = [baseIndentString]; |
| 1517 |
this.baseIndentLength = baseIndentString.length; |
| 1518 |
this.indent_length = indent_string.length; |
| 1519 |
this.raw = false; |
| 1520 |
|
| 1521 |
var lines = []; |
| 1522 |
this.baseIndentString = baseIndentString; |
| 1523 |
this.indent_string = indent_string; |
| 1524 |
this.previous_line = null; |
| 1525 |
this.current_line = null; |
| 1526 |
this.space_before_token = false; |
| 1527 |
|
| 1528 |
this.add_outputline = function() { |
| 1529 |
this.previous_line = this.current_line; |
| 1530 |
this.current_line = new OutputLine(this); |
| 1531 |
lines.push(this.current_line); |
| 1532 |
}; |
| 1533 |
|
| 1534 |
// initialize |
| 1535 |
this.add_outputline(); |
| 1536 |
|
| 1537 |
|
| 1538 |
this.get_line_number = function() { |
| 1539 |
return lines.length; |
| 1540 |
}; |
| 1541 |
|
| 1542 |
// Using object instead of string to allow for later expansion of info about each line |
| 1543 |
this.add_new_line = function(force_newline) { |
| 1544 |
if (this.get_line_number() === 1 && this.just_added_newline()) { |
| 1545 |
return false; // no newline on start of file |
| 1546 |
} |
| 1547 |
|
| 1548 |
if (force_newline || !this.just_added_newline()) { |
| 1549 |
if (!this.raw) { |
| 1550 |
this.add_outputline(); |
| 1551 |
} |
| 1552 |
return true; |
| 1553 |
} |
| 1554 |
|
| 1555 |
return false; |
| 1556 |
}; |
| 1557 |
|
| 1558 |
this.get_code = function() { |
| 1559 |
var sweet_code = lines.join('\n').replace(/[\r\n\t ]+$/, ''); |
| 1560 |
return sweet_code; |
| 1561 |
}; |
| 1562 |
|
| 1563 |
this.set_indent = function(level) { |
| 1564 |
// Never indent your first output indent at the start of the file |
| 1565 |
if (lines.length > 1) { |
| 1566 |
while (level >= this.indent_cache.length) { |
| 1567 |
this.indent_cache.push(this.indent_cache[this.indent_cache.length - 1] + this.indent_string); |
| 1568 |
} |
| 1569 |
|
| 1570 |
this.current_line.set_indent(level); |
| 1571 |
return true; |
| 1572 |
} |
| 1573 |
this.current_line.set_indent(0); |
| 1574 |
return false; |
| 1575 |
}; |
| 1576 |
|
| 1577 |
this.add_raw_token = function(token) { |
| 1578 |
for (var x = 0; x < token.newlines; x++) { |
| 1579 |
this.add_outputline(); |
| 1580 |
} |
| 1581 |
this.current_line.push(token.whitespace_before); |
| 1582 |
this.current_line.push(token.text); |
| 1583 |
this.space_before_token = false; |
| 1584 |
}; |
| 1585 |
|
| 1586 |
this.add_token = function(printable_token) { |
| 1587 |
this.add_space_before_token(); |
| 1588 |
this.current_line.push(printable_token); |
| 1589 |
}; |
| 1590 |
|
| 1591 |
this.add_space_before_token = function() { |
| 1592 |
if (this.space_before_token && !this.just_added_newline()) { |
| 1593 |
this.current_line.push(' '); |
| 1594 |
} |
| 1595 |
this.space_before_token = false; |
| 1596 |
}; |
| 1597 |
|
| 1598 |
this.remove_redundant_indentation = function(frame) { |
| 1599 |
// This implementation is effective but has some issues: |
| 1600 |
// - can cause line wrap to happen too soon due to indent removal |
| 1601 |
// after wrap points are calculated |
| 1602 |
// These issues are minor compared to ugly indentation. |
| 1603 |
|
| 1604 |
if (frame.multiline_frame || |
| 1605 |
frame.mode === MODE.ForInitializer || |
| 1606 |
frame.mode === MODE.Conditional) { |
| 1607 |
return; |
| 1608 |
} |
| 1609 |
|
| 1610 |
// remove one indent from each line inside this section |
| 1611 |
var index = frame.start_line_index; |
| 1612 |
|
| 1613 |
var output_length = lines.length; |
| 1614 |
while (index < output_length) { |
| 1615 |
lines[index].remove_indent(); |
| 1616 |
index++; |
| 1617 |
} |
| 1618 |
}; |
| 1619 |
|
| 1620 |
this.trim = function(eat_newlines) { |
| 1621 |
eat_newlines = (eat_newlines === undefined) ? false : eat_newlines; |
| 1622 |
|
| 1623 |
this.current_line.trim(indent_string, baseIndentString); |
| 1624 |
|
| 1625 |
while (eat_newlines && lines.length > 1 && |
| 1626 |
this.current_line.is_empty()) { |
| 1627 |
lines.pop(); |
| 1628 |
this.current_line = lines[lines.length - 1]; |
| 1629 |
this.current_line.trim(); |
| 1630 |
} |
| 1631 |
|
| 1632 |
this.previous_line = lines.length > 1 ? lines[lines.length - 2] : null; |
| 1633 |
}; |
| 1634 |
|
| 1635 |
this.just_added_newline = function() { |
| 1636 |
return this.current_line.is_empty(); |
| 1637 |
}; |
| 1638 |
|
| 1639 |
this.just_added_blankline = function() { |
| 1640 |
if (this.just_added_newline()) { |
| 1641 |
if (lines.length === 1) { |
| 1642 |
return true; // start of the file and newline = blank |
| 1643 |
} |
| 1644 |
|
| 1645 |
var line = lines[lines.length - 2]; |
| 1646 |
return line.is_empty(); |
| 1647 |
} |
| 1648 |
return false; |
| 1649 |
}; |
| 1650 |
} |
| 1651 |
|
| 1652 |
|
| 1653 |
var Token = function(type, text, newlines, whitespace_before, parent) { |
| 1654 |
this.type = type; |
| 1655 |
this.text = text; |
| 1656 |
this.comments_before = []; |
| 1657 |
this.newlines = newlines || 0; |
| 1658 |
this.wanted_newline = newlines > 0; |
| 1659 |
this.whitespace_before = whitespace_before || ''; |
| 1660 |
this.parent = parent || null; |
| 1661 |
this.opened = null; |
| 1662 |
this.directives = null; |
| 1663 |
}; |
| 1664 |
|
| 1665 |
function tokenizer(input, opts) { |
| 1666 |
|
| 1667 |
var whitespace = "\n\r\t ".split(''); |
| 1668 |
var digit = /[0-9]/; |
| 1669 |
var digit_bin = /[01]/; |
| 1670 |
var digit_oct = /[01234567]/; |
| 1671 |
var digit_hex = /[0123456789abcdefABCDEF]/; |
| 1672 |
|
| 1673 |
this.positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' '); |
| 1674 |
var punct = this.positionable_operators.concat( |
| 1675 |
// non-positionable operators - these do not follow operator position settings |
| 1676 |
'! %= &= *= **= ++ += , -- -= /= :: <<= = => >>= >>>= ^= |= ~'.split(' ')); |
| 1677 |
|
| 1678 |
// words which should always start on new line. |
| 1679 |
this.line_starters = 'continue,try,throw,return,var,let,const,if,switch,case,default,for,while,break,function,import,export'.split(','); |
| 1680 |
var reserved_words = this.line_starters.concat(['do', 'in', 'else', 'get', 'set', 'new', 'catch', 'finally', 'typeof', 'yield', 'async', 'await', 'from', 'as']); |
| 1681 |
|
| 1682 |
// /* ... */ comment ends with nearest */ or end of file |
| 1683 |
var block_comment_pattern = /([\s\S]*?)((?:\*\/)|$)/g; |
| 1684 |
|
| 1685 |
// comment ends just before nearest linefeed or end of file |
| 1686 |
var comment_pattern = /([^\n\r\u2028\u2029]*)/g; |
| 1687 |
|
| 1688 |
var directives_block_pattern = /\/\* beautify( \w+[:]\w+)+ \*\//g; |
| 1689 |
var directive_pattern = / (\w+)[:](\w+)/g; |
| 1690 |
var directives_end_ignore_pattern = /([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)/g; |
| 1691 |
|
| 1692 |
var template_pattern = /((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)/g; |
| 1693 |
|
| 1694 |
var n_newlines, whitespace_before_token, in_html_comment, tokens, parser_pos; |
| 1695 |
var input_length; |
| 1696 |
|
| 1697 |
this.tokenize = function() { |
| 1698 |
// cache the source's length. |
| 1699 |
input_length = input.length; |
| 1700 |
parser_pos = 0; |
| 1701 |
in_html_comment = false; |
| 1702 |
tokens = []; |
| 1703 |
|
| 1704 |
var next, last; |
| 1705 |
var token_values; |
| 1706 |
var open = null; |
| 1707 |
var open_stack = []; |
| 1708 |
var comments = []; |
| 1709 |
|
| 1710 |
while (!(last && last.type === 'TK_EOF')) { |
| 1711 |
token_values = tokenize_next(); |
| 1712 |
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token); |
| 1713 |
while (next.type === 'TK_COMMENT' || next.type === 'TK_BLOCK_COMMENT' || next.type === 'TK_UNKNOWN') { |
| 1714 |
if (next.type === 'TK_BLOCK_COMMENT') { |
| 1715 |
next.directives = token_values[2]; |
| 1716 |
} |
| 1717 |
comments.push(next); |
| 1718 |
token_values = tokenize_next(); |
| 1719 |
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token); |
| 1720 |
} |
| 1721 |
|
| 1722 |
if (comments.length) { |
| 1723 |
next.comments_before = comments; |
| 1724 |
comments = []; |
| 1725 |
} |
| 1726 |
|
| 1727 |
if (next.type === 'TK_START_BLOCK' || next.type === 'TK_START_EXPR') { |
| 1728 |
next.parent = last; |
| 1729 |
open_stack.push(open); |
| 1730 |
open = next; |
| 1731 |
} else if ((next.type === 'TK_END_BLOCK' || next.type === 'TK_END_EXPR') && |
| 1732 |
(open && ( |
| 1733 |
(next.text === ']' && open.text === '[') || |
| 1734 |
(next.text === ')' && open.text === '(') || |
| 1735 |
(next.text === '}' && open.text === '{')))) { |
| 1736 |
next.parent = open.parent; |
| 1737 |
next.opened = open; |
| 1738 |
|
| 1739 |
open = open_stack.pop(); |
| 1740 |
} |
| 1741 |
|
| 1742 |
tokens.push(next); |
| 1743 |
last = next; |
| 1744 |
} |
| 1745 |
|
| 1746 |
return tokens; |
| 1747 |
}; |
| 1748 |
|
| 1749 |
function get_directives(text) { |
| 1750 |
if (!text.match(directives_block_pattern)) { |
| 1751 |
return null; |
| 1752 |
} |
| 1753 |
|
| 1754 |
var directives = {}; |
| 1755 |
directive_pattern.lastIndex = 0; |
| 1756 |
var directive_match = directive_pattern.exec(text); |
| 1757 |
|
| 1758 |
while (directive_match) { |
| 1759 |
directives[directive_match[1]] = directive_match[2]; |
| 1760 |
directive_match = directive_pattern.exec(text); |
| 1761 |
} |
| 1762 |
|
| 1763 |
return directives; |
| 1764 |
} |
| 1765 |
|
| 1766 |
function tokenize_next() { |
| 1767 |
var resulting_string; |
| 1768 |
var whitespace_on_this_line = []; |
| 1769 |
|
| 1770 |
n_newlines = 0; |
| 1771 |
whitespace_before_token = ''; |
| 1772 |
|
| 1773 |
if (parser_pos >= input_length) { |
| 1774 |
return ['', 'TK_EOF']; |
| 1775 |
} |
| 1776 |
|
| 1777 |
var last_token; |
| 1778 |
if (tokens.length) { |
| 1779 |
last_token = tokens[tokens.length - 1]; |
| 1780 |
} else { |
| 1781 |
// For the sake of tokenizing we can pretend that there was on open brace to start |
| 1782 |
last_token = new Token('TK_START_BLOCK', '{'); |
| 1783 |
} |
| 1784 |
|
| 1785 |
|
| 1786 |
var c = input.charAt(parser_pos); |
| 1787 |
parser_pos += 1; |
| 1788 |
|
| 1789 |
while (in_array(c, whitespace)) { |
| 1790 |
|
| 1791 |
if (acorn.isNewLine(c.charCodeAt(0))) { |
| 1792 |
if (!(c === '\n' && input.charAt(parser_pos - 2) === '\r')) { |
| 1793 |
n_newlines += 1; |
| 1794 |
whitespace_on_this_line = []; |
| 1795 |
} |
| 1796 |
} else { |
| 1797 |
whitespace_on_this_line.push(c); |
| 1798 |
} |
| 1799 |
|
| 1800 |
if (parser_pos >= input_length) { |
| 1801 |
return ['', 'TK_EOF']; |
| 1802 |
} |
| 1803 |
|
| 1804 |
c = input.charAt(parser_pos); |
| 1805 |
parser_pos += 1; |
| 1806 |
} |
| 1807 |
|
| 1808 |
if (whitespace_on_this_line.length) { |
| 1809 |
whitespace_before_token = whitespace_on_this_line.join(''); |
| 1810 |
} |
| 1811 |
|
| 1812 |
if (digit.test(c) || (c === '.' && digit.test(input.charAt(parser_pos)))) { |
| 1813 |
var allow_decimal = true; |
| 1814 |
var allow_e = true; |
| 1815 |
var local_digit = digit; |
| 1816 |
|
| 1817 |
if (c === '0' && parser_pos < input_length && /[XxOoBb]/.test(input.charAt(parser_pos))) { |
| 1818 |
// switch to hex/oct/bin number, no decimal or e, just hex/oct/bin digits |
| 1819 |
allow_decimal = false; |
| 1820 |
allow_e = false; |
| 1821 |
if (/[Bb]/.test(input.charAt(parser_pos))) { |
| 1822 |
local_digit = digit_bin; |
| 1823 |
} else if (/[Oo]/.test(input.charAt(parser_pos))) { |
| 1824 |
local_digit = digit_oct; |
| 1825 |
} else { |
| 1826 |
local_digit = digit_hex; |
| 1827 |
} |
| 1828 |
c += input.charAt(parser_pos); |
| 1829 |
parser_pos += 1; |
| 1830 |
} else if (c === '.') { |
| 1831 |
// Already have a decimal for this literal, don't allow another |
| 1832 |
allow_decimal = false; |
| 1833 |
} else { |
| 1834 |
// we know this first loop will run. It keeps the logic simpler. |
| 1835 |
c = ''; |
| 1836 |
parser_pos -= 1; |
| 1837 |
} |
| 1838 |
|
| 1839 |
// Add the digits |
| 1840 |
while (parser_pos < input_length && local_digit.test(input.charAt(parser_pos))) { |
| 1841 |
c += input.charAt(parser_pos); |
| 1842 |
parser_pos += 1; |
| 1843 |
|
| 1844 |
if (allow_decimal && parser_pos < input_length && input.charAt(parser_pos) === '.') { |
| 1845 |
c += input.charAt(parser_pos); |
| 1846 |
parser_pos += 1; |
| 1847 |
allow_decimal = false; |
| 1848 |
} else if (allow_e && parser_pos < input_length && /[Ee]/.test(input.charAt(parser_pos))) { |
| 1849 |
c += input.charAt(parser_pos); |
| 1850 |
parser_pos += 1; |
| 1851 |
|
| 1852 |
if (parser_pos < input_length && /[+-]/.test(input.charAt(parser_pos))) { |
| 1853 |
c += input.charAt(parser_pos); |
| 1854 |
parser_pos += 1; |
| 1855 |
} |
| 1856 |
|
| 1857 |
allow_e = false; |
| 1858 |
allow_decimal = false; |
| 1859 |
} |
| 1860 |
} |
| 1861 |
|
| 1862 |
return [c, 'TK_WORD']; |
| 1863 |
} |
| 1864 |
|
| 1865 |
if (acorn.isIdentifierStart(input.charCodeAt(parser_pos - 1))) { |
| 1866 |
if (parser_pos < input_length) { |
| 1867 |
while (acorn.isIdentifierChar(input.charCodeAt(parser_pos))) { |
| 1868 |
c += input.charAt(parser_pos); |
| 1869 |
parser_pos += 1; |
| 1870 |
if (parser_pos === input_length) { |
| 1871 |
break; |
| 1872 |
} |
| 1873 |
} |
| 1874 |
} |
| 1875 |
|
| 1876 |
if (!(last_token.type === 'TK_DOT' || |
| 1877 |
(last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['set', 'get']))) && |
| 1878 |
in_array(c, reserved_words)) { |
| 1879 |
if (c === 'in') { // hack for 'in' operator |
| 1880 |
return [c, 'TK_OPERATOR']; |
| 1881 |
} |
| 1882 |
return [c, 'TK_RESERVED']; |
| 1883 |
} |
| 1884 |
|
| 1885 |
return [c, 'TK_WORD']; |
| 1886 |
} |
| 1887 |
|
| 1888 |
if (c === '(' || c === '[') { |
| 1889 |
return [c, 'TK_START_EXPR']; |
| 1890 |
} |
| 1891 |
|
| 1892 |
if (c === ')' || c === ']') { |
| 1893 |
return [c, 'TK_END_EXPR']; |
| 1894 |
} |
| 1895 |
|
| 1896 |
if (c === '{') { |
| 1897 |
return [c, 'TK_START_BLOCK']; |
| 1898 |
} |
| 1899 |
|
| 1900 |
if (c === '}') { |
| 1901 |
return [c, 'TK_END_BLOCK']; |
| 1902 |
} |
| 1903 |
|
| 1904 |
if (c === ';') { |
| 1905 |
return [c, 'TK_SEMICOLON']; |
| 1906 |
} |
| 1907 |
|
| 1908 |
if (c === '/') { |
| 1909 |
var comment = ''; |
| 1910 |
var comment_match; |
| 1911 |
// peek for comment /* ... */ |
| 1912 |
if (input.charAt(parser_pos) === '*') { |
| 1913 |
parser_pos += 1; |
| 1914 |
block_comment_pattern.lastIndex = parser_pos; |
| 1915 |
comment_match = block_comment_pattern.exec(input); |
| 1916 |
comment = '/*' + comment_match[0]; |
| 1917 |
parser_pos += comment_match[0].length; |
| 1918 |
var directives = get_directives(comment); |
| 1919 |
if (directives && directives.ignore === 'start') { |
| 1920 |
directives_end_ignore_pattern.lastIndex = parser_pos; |
| 1921 |
comment_match = directives_end_ignore_pattern.exec(input); |
| 1922 |
comment += comment_match[0]; |
| 1923 |
parser_pos += comment_match[0].length; |
| 1924 |
} |
| 1925 |
comment = comment.replace(acorn.allLineBreaks, '\n'); |
| 1926 |
return [comment, 'TK_BLOCK_COMMENT', directives]; |
| 1927 |
} |
| 1928 |
// peek for comment // ... |
| 1929 |
if (input.charAt(parser_pos) === '/') { |
| 1930 |
parser_pos += 1; |
| 1931 |
comment_pattern.lastIndex = parser_pos; |
| 1932 |
comment_match = comment_pattern.exec(input); |
| 1933 |
comment = '//' + comment_match[0]; |
| 1934 |
parser_pos += comment_match[0].length; |
| 1935 |
return [comment, 'TK_COMMENT']; |
| 1936 |
} |
| 1937 |
|
| 1938 |
} |
| 1939 |
|
| 1940 |
var startXmlRegExp = /^<([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/; |
| 1941 |
|
| 1942 |
if (c === '`' || c === "'" || c === '"' || // string |
| 1943 |
( |
| 1944 |
(c === '/') || // regexp |
| 1945 |
(opts.e4x && c === "<" && input.slice(parser_pos - 1).match(startXmlRegExp)) // xml |
| 1946 |
) && ( // regex and xml can only appear in specific locations during parsing |
| 1947 |
(last_token.type === 'TK_RESERVED' && in_array(last_token.text, ['return', 'case', 'throw', 'else', 'do', 'typeof', 'yield'])) || |
| 1948 |
(last_token.type === 'TK_END_EXPR' && last_token.text === ')' && |
| 1949 |
last_token.parent && last_token.parent.type === 'TK_RESERVED' && in_array(last_token.parent.text, ['if', 'while', 'for'])) || |
| 1950 |
(in_array(last_token.type, ['TK_COMMENT', 'TK_START_EXPR', 'TK_START_BLOCK', |
| 1951 |
'TK_END_BLOCK', 'TK_OPERATOR', 'TK_EQUALS', 'TK_EOF', 'TK_SEMICOLON', 'TK_COMMA' |
| 1952 |
])) |
| 1953 |
)) { |
| 1954 |
|
| 1955 |
var sep = c, |
| 1956 |
esc = false, |
| 1957 |
has_char_escapes = false; |
| 1958 |
|
| 1959 |
resulting_string = c; |
| 1960 |
|
| 1961 |
if (sep === '/') { |
| 1962 |
// |
| 1963 |
// handle regexp |
| 1964 |
// |
| 1965 |
var in_char_class = false; |
| 1966 |
while (parser_pos < input_length && |
| 1967 |
((esc || in_char_class || input.charAt(parser_pos) !== sep) && |
| 1968 |
!acorn.isNewLine(input.charCodeAt(parser_pos)))) { |
| 1969 |
resulting_string += input.charAt(parser_pos); |
| 1970 |
if (!esc) { |
| 1971 |
esc = input.charAt(parser_pos) === '\\'; |
| 1972 |
if (input.charAt(parser_pos) === '[') { |
| 1973 |
in_char_class = true; |
| 1974 |
} else if (input.charAt(parser_pos) === ']') { |
| 1975 |
in_char_class = false; |
| 1976 |
} |
| 1977 |
} else { |
| 1978 |
esc = false; |
| 1979 |
} |
| 1980 |
parser_pos += 1; |
| 1981 |
} |
| 1982 |
} else if (opts.e4x && sep === '<') { |
| 1983 |
// |
| 1984 |
// handle e4x xml literals |
| 1985 |
// |
| 1986 |
|
| 1987 |
var xmlRegExp = /<(\/?)([-a-zA-Z:0-9_.]+|{.+?}|!\[CDATA\[[\s\S]*?\]\])(\s+{.+?}|\s+[-a-zA-Z:0-9_.]+|\s+[-a-zA-Z:0-9_.]+\s*=\s*('[^']*'|"[^"]*"|{.+?}))*\s*(\/?)\s*>/g; |
| 1988 |
var xmlStr = input.slice(parser_pos - 1); |
| 1989 |
var match = xmlRegExp.exec(xmlStr); |
| 1990 |
if (match && match.index === 0) { |
| 1991 |
var rootTag = match[2]; |
| 1992 |
var depth = 0; |
| 1993 |
while (match) { |
| 1994 |
var isEndTag = !!match[1]; |
| 1995 |
var tagName = match[2]; |
| 1996 |
var isSingletonTag = (!!match[match.length - 1]) || (tagName.slice(0, 8) === "![CDATA["); |
| 1997 |
if (tagName === rootTag && !isSingletonTag) { |
| 1998 |
if (isEndTag) { |
| 1999 |
--depth; |
| 2000 |
} else { |
| 2001 |
++depth; |
| 2002 |
} |
| 2003 |
} |
| 2004 |
if (depth <= 0) { |
| 2005 |
break; |
| 2006 |
} |
| 2007 |
match = xmlRegExp.exec(xmlStr); |
| 2008 |
} |
| 2009 |
var xmlLength = match ? match.index + match[0].length : xmlStr.length; |
| 2010 |
xmlStr = xmlStr.slice(0, xmlLength); |
| 2011 |
parser_pos += xmlLength - 1; |
| 2012 |
xmlStr = xmlStr.replace(acorn.allLineBreaks, '\n'); |
| 2013 |
return [xmlStr, "TK_STRING"]; |
| 2014 |
} |
| 2015 |
} else { |
| 2016 |
// |
| 2017 |
// handle string |
| 2018 |
// |
| 2019 |
var parse_string = function(delimiter, allow_unescaped_newlines, start_sub) { |
| 2020 |
// Template strings can travers lines without escape characters. |
| 2021 |
// Other strings cannot |
| 2022 |
var current_char; |
| 2023 |
while (parser_pos < input_length) { |
| 2024 |
current_char = input.charAt(parser_pos); |
| 2025 |
if (!(esc || (current_char !== delimiter && |
| 2026 |
(allow_unescaped_newlines || !acorn.isNewLine(current_char.charCodeAt(0)))))) { |
| 2027 |
break; |
| 2028 |
} |
| 2029 |
|
| 2030 |
// Handle \r\n linebreaks after escapes or in template strings |
| 2031 |
if ((esc || allow_unescaped_newlines) && acorn.isNewLine(current_char.charCodeAt(0))) { |
| 2032 |
if (current_char === '\r' && input.charAt(parser_pos + 1) === '\n') { |
| 2033 |
parser_pos += 1; |
| 2034 |
current_char = input.charAt(parser_pos); |
| 2035 |
} |
| 2036 |
resulting_string += '\n'; |
| 2037 |
} else { |
| 2038 |
resulting_string += current_char; |
| 2039 |
} |
| 2040 |
if (esc) { |
| 2041 |
if (current_char === 'x' || current_char === 'u') { |
| 2042 |
has_char_escapes = true; |
| 2043 |
} |
| 2044 |
esc = false; |
| 2045 |
} else { |
| 2046 |
esc = current_char === '\\'; |
| 2047 |
} |
| 2048 |
|
| 2049 |
parser_pos += 1; |
| 2050 |
|
| 2051 |
if (start_sub && resulting_string.indexOf(start_sub, resulting_string.length - start_sub.length) !== -1) { |
| 2052 |
if (delimiter === '`') { |
| 2053 |
parse_string('}', allow_unescaped_newlines, '`'); |
| 2054 |
} else { |
| 2055 |
parse_string('`', allow_unescaped_newlines, '${'); |
| 2056 |
} |
| 2057 |
} |
| 2058 |
} |
| 2059 |
}; |
| 2060 |
|
| 2061 |
if (sep === '`') { |
| 2062 |
parse_string('`', true, '${'); |
| 2063 |
} else { |
| 2064 |
parse_string(sep); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
if (has_char_escapes && opts.unescape_strings) { |
| 2069 |
resulting_string = unescape_string(resulting_string); |
| 2070 |
} |
| 2071 |
|
| 2072 |
if (parser_pos < input_length && input.charAt(parser_pos) === sep) { |
| 2073 |
resulting_string += sep; |
| 2074 |
parser_pos += 1; |
| 2075 |
|
| 2076 |
if (sep === '/') { |
| 2077 |
// regexps may have modifiers /regexp/MOD , so fetch those, too |
| 2078 |
// Only [gim] are valid, but if the user puts in garbage, do what we can to take it. |
| 2079 |
while (parser_pos < input_length && acorn.isIdentifierStart(input.charCodeAt(parser_pos))) { |
| 2080 |
resulting_string += input.charAt(parser_pos); |
| 2081 |
parser_pos += 1; |
| 2082 |
} |
| 2083 |
} |
| 2084 |
} |
| 2085 |
return [resulting_string, 'TK_STRING']; |
| 2086 |
} |
| 2087 |
|
| 2088 |
if (c === '#') { |
| 2089 |
|
| 2090 |
if (tokens.length === 0 && input.charAt(parser_pos) === '!') { |
| 2091 |
// shebang |
| 2092 |
resulting_string = c; |
| 2093 |
while (parser_pos < input_length && c !== '\n') { |
| 2094 |
c = input.charAt(parser_pos); |
| 2095 |
resulting_string += c; |
| 2096 |
parser_pos += 1; |
| 2097 |
} |
| 2098 |
return [trim(resulting_string) + '\n', 'TK_UNKNOWN']; |
| 2099 |
} |
| 2100 |
|
| 2101 |
|
| 2102 |
|
| 2103 |
// Spidermonkey-specific sharp variables for circular references |
| 2104 |
// https://developer.mozilla.org/En/Sharp_variables_in_JavaScript |
| 2105 |
// http://mxr.mozilla.org/mozilla-central/source/js/src/jsscan.cpp around line 1935 |
| 2106 |
var sharp = '#'; |
| 2107 |
if (parser_pos < input_length && digit.test(input.charAt(parser_pos))) { |
| 2108 |
do { |
| 2109 |
c = input.charAt(parser_pos); |
| 2110 |
sharp += c; |
| 2111 |
parser_pos += 1; |
| 2112 |
} while (parser_pos < input_length && c !== '#' && c !== '='); |
| 2113 |
if (c === '#') { |
| 2114 |
// |
| 2115 |
} else if (input.charAt(parser_pos) === '[' && input.charAt(parser_pos + 1) === ']') { |
| 2116 |
sharp += '[]'; |
| 2117 |
parser_pos += 2; |
| 2118 |
} else if (input.charAt(parser_pos) === '{' && input.charAt(parser_pos + 1) === '}') { |
| 2119 |
sharp += '{}'; |
| 2120 |
parser_pos += 2; |
| 2121 |
} |
| 2122 |
return [sharp, 'TK_WORD']; |
| 2123 |
} |
| 2124 |
} |
| 2125 |
|
| 2126 |
if (c === '<' && (input.charAt(parser_pos) === '?' || input.charAt(parser_pos) === '%')) { |
| 2127 |
template_pattern.lastIndex = parser_pos - 1; |
| 2128 |
var template_match = template_pattern.exec(input); |
| 2129 |
if (template_match) { |
| 2130 |
c = template_match[0]; |
| 2131 |
parser_pos += c.length - 1; |
| 2132 |
c = c.replace(acorn.allLineBreaks, '\n'); |
| 2133 |
return [c, 'TK_STRING']; |
| 2134 |
} |
| 2135 |
} |
| 2136 |
|
| 2137 |
if (c === '<' && input.substring(parser_pos - 1, parser_pos + 3) === '<!--') { |
| 2138 |
parser_pos += 3; |
| 2139 |
c = '<!--'; |
| 2140 |
while (!acorn.isNewLine(input.charCodeAt(parser_pos)) && parser_pos < input_length) { |
| 2141 |
c += input.charAt(parser_pos); |
| 2142 |
parser_pos++; |
| 2143 |
} |
| 2144 |
in_html_comment = true; |
| 2145 |
return [c, 'TK_COMMENT']; |
| 2146 |
} |
| 2147 |
|
| 2148 |
if (c === '-' && in_html_comment && input.substring(parser_pos - 1, parser_pos + 2) === '-->') { |
| 2149 |
in_html_comment = false; |
| 2150 |
parser_pos += 2; |
| 2151 |
return ['-->', 'TK_COMMENT']; |
| 2152 |
} |
| 2153 |
|
| 2154 |
if (c === '.') { |
| 2155 |
return [c, 'TK_DOT']; |
| 2156 |
} |
| 2157 |
|
| 2158 |
if (in_array(c, punct)) { |
| 2159 |
while (parser_pos < input_length && in_array(c + input.charAt(parser_pos), punct)) { |
| 2160 |
c += input.charAt(parser_pos); |
| 2161 |
parser_pos += 1; |
| 2162 |
if (parser_pos >= input_length) { |
| 2163 |
break; |
| 2164 |
} |
| 2165 |
} |
| 2166 |
|
| 2167 |
if (c === ',') { |
| 2168 |
return [c, 'TK_COMMA']; |
| 2169 |
} else if (c === '=') { |
| 2170 |
return [c, 'TK_EQUALS']; |
| 2171 |
} else { |
| 2172 |
return [c, 'TK_OPERATOR']; |
| 2173 |
} |
| 2174 |
} |
| 2175 |
|
| 2176 |
return [c, 'TK_UNKNOWN']; |
| 2177 |
} |
| 2178 |
|
| 2179 |
|
| 2180 |
function unescape_string(s) { |
| 2181 |
var esc = false, |
| 2182 |
out = '', |
| 2183 |
pos = 0, |
| 2184 |
s_hex = '', |
| 2185 |
escaped = 0, |
| 2186 |
c; |
| 2187 |
|
| 2188 |
while (esc || pos < s.length) { |
| 2189 |
|
| 2190 |
c = s.charAt(pos); |
| 2191 |
pos++; |
| 2192 |
|
| 2193 |
if (esc) { |
| 2194 |
esc = false; |
| 2195 |
if (c === 'x') { |
| 2196 |
// simple hex-escape \x24 |
| 2197 |
s_hex = s.substr(pos, 2); |
| 2198 |
pos += 2; |
| 2199 |
} else if (c === 'u') { |
| 2200 |
// unicode-escape, \u2134 |
| 2201 |
s_hex = s.substr(pos, 4); |
| 2202 |
pos += 4; |
| 2203 |
} else { |
| 2204 |
// some common escape, e.g \n |
| 2205 |
out += '\\' + c; |
| 2206 |
continue; |
| 2207 |
} |
| 2208 |
if (!s_hex.match(/^[0123456789abcdefABCDEF]+$/)) { |
| 2209 |
// some weird escaping, bail out, |
| 2210 |
// leaving whole string intact |
| 2211 |
return s; |
| 2212 |
} |
| 2213 |
|
| 2214 |
escaped = parseInt(s_hex, 16); |
| 2215 |
|
| 2216 |
if (escaped >= 0x00 && escaped < 0x20) { |
| 2217 |
// leave 0x00...0x1f escaped |
| 2218 |
if (c === 'x') { |
| 2219 |
out += '\\x' + s_hex; |
| 2220 |
} else { |
| 2221 |
out += '\\u' + s_hex; |
| 2222 |
} |
| 2223 |
continue; |
| 2224 |
} else if (escaped === 0x22 || escaped === 0x27 || escaped === 0x5c) { |
| 2225 |
// single-quote, apostrophe, backslash - escape these |
| 2226 |
out += '\\' + String.fromCharCode(escaped); |
| 2227 |
} else if (c === 'x' && escaped > 0x7e && escaped <= 0xff) { |
| 2228 |
// we bail out on \x7f..\xff, |
| 2229 |
// leaving whole string escaped, |
| 2230 |
// as it's probably completely binary |
| 2231 |
return s; |
| 2232 |
} else { |
| 2233 |
out += String.fromCharCode(escaped); |
| 2234 |
} |
| 2235 |
} else if (c === '\\') { |
| 2236 |
esc = true; |
| 2237 |
} else { |
| 2238 |
out += c; |
| 2239 |
} |
| 2240 |
} |
| 2241 |
return out; |
| 2242 |
} |
| 2243 |
} |
| 2244 |
|
| 2245 |
var beautifier = new Beautifier(js_source_text, options); |
| 2246 |
return beautifier.beautify(); |
| 2247 |
|
| 2248 |
} |
| 2249 |
|
| 2250 |
return { |
| 2251 |
js_beautify: js_beautify |
| 2252 |
}; |
| 2253 |
}); |