1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/devtools/pretty-fast/pretty-fast.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,844 @@ 1.4 +/* 1.5 + * Copyright 2013 Mozilla Foundation and contributors 1.6 + * Licensed under the New BSD license. See LICENSE.md or: 1.7 + * http://opensource.org/licenses/BSD-2-Clause 1.8 + */ 1.9 +(function (root, factory) { 1.10 + if (typeof define === 'function' && define.amd) { 1.11 + define(factory); 1.12 + } else if (typeof exports === 'object') { 1.13 + module.exports = factory(); 1.14 + } else { 1.15 + root.prettyFast = factory(); 1.16 + } 1.17 +}(this, function () { 1.18 + "use strict"; 1.19 + 1.20 + var acorn = this.acorn || require("acorn/acorn"); 1.21 + var sourceMap = this.sourceMap || require("source-map"); 1.22 + var SourceNode = sourceMap.SourceNode; 1.23 + 1.24 + // If any of these tokens are seen before a "[" token, we know that "[" token 1.25 + // is the start of an array literal, rather than a property access. 1.26 + // 1.27 + // The only exception is "}", which would need to be disambiguated by 1.28 + // parsing. The majority of the time, an open bracket following a closing 1.29 + // curly is going to be an array literal, so we brush the complication under 1.30 + // the rug, and handle the ambiguity by always assuming that it will be an 1.31 + // array literal. 1.32 + var PRE_ARRAY_LITERAL_TOKENS = { 1.33 + "typeof": true, 1.34 + "void": true, 1.35 + "delete": true, 1.36 + "case": true, 1.37 + "do": true, 1.38 + "=": true, 1.39 + "in": true, 1.40 + "{": true, 1.41 + "*": true, 1.42 + "/": true, 1.43 + "%": true, 1.44 + "else": true, 1.45 + ";": true, 1.46 + "++": true, 1.47 + "--": true, 1.48 + "+": true, 1.49 + "-": true, 1.50 + "~": true, 1.51 + "!": true, 1.52 + ":": true, 1.53 + "?": true, 1.54 + ">>": true, 1.55 + ">>>": true, 1.56 + "<<": true, 1.57 + "||": true, 1.58 + "&&": true, 1.59 + "<": true, 1.60 + ">": true, 1.61 + "<=": true, 1.62 + ">=": true, 1.63 + "instanceof": true, 1.64 + "&": true, 1.65 + "^": true, 1.66 + "|": true, 1.67 + "==": true, 1.68 + "!=": true, 1.69 + "===": true, 1.70 + "!==": true, 1.71 + ",": true, 1.72 + 1.73 + "}": true 1.74 + }; 1.75 + 1.76 + /** 1.77 + * Determines if we think that the given token starts an array literal. 1.78 + * 1.79 + * @param Object token 1.80 + * The token we want to determine if it is an array literal. 1.81 + * @param Object lastToken 1.82 + * The last token we added to the pretty printed results. 1.83 + * 1.84 + * @returns Boolean 1.85 + * True if we believe it is an array literal, false otherwise. 1.86 + */ 1.87 + function isArrayLiteral(token, lastToken) { 1.88 + if (token.type.type != "[") { 1.89 + return false; 1.90 + } 1.91 + if (!lastToken) { 1.92 + return true; 1.93 + } 1.94 + if (lastToken.type.isAssign) { 1.95 + return true; 1.96 + } 1.97 + return !!PRE_ARRAY_LITERAL_TOKENS[lastToken.type.keyword || lastToken.type.type]; 1.98 + } 1.99 + 1.100 + // If any of these tokens are followed by a token on a new line, we know that 1.101 + // ASI cannot happen. 1.102 + var PREVENT_ASI_AFTER_TOKENS = { 1.103 + // Binary operators 1.104 + "*": true, 1.105 + "/": true, 1.106 + "%": true, 1.107 + "+": true, 1.108 + "-": true, 1.109 + "<<": true, 1.110 + ">>": true, 1.111 + ">>>": true, 1.112 + "<": true, 1.113 + ">": true, 1.114 + "<=": true, 1.115 + ">=": true, 1.116 + "instanceof": true, 1.117 + "in": true, 1.118 + "==": true, 1.119 + "!=": true, 1.120 + "===": true, 1.121 + "!==": true, 1.122 + "&": true, 1.123 + "^": true, 1.124 + "|": true, 1.125 + "&&": true, 1.126 + "||": true, 1.127 + ",": true, 1.128 + ".": true, 1.129 + "=": true, 1.130 + "*=": true, 1.131 + "/=": true, 1.132 + "%=": true, 1.133 + "+=": true, 1.134 + "-=": true, 1.135 + "<<=": true, 1.136 + ">>=": true, 1.137 + ">>>=": true, 1.138 + "&=": true, 1.139 + "^=": true, 1.140 + "|=": true, 1.141 + // Unary operators 1.142 + "delete": true, 1.143 + "void": true, 1.144 + "typeof": true, 1.145 + "~": true, 1.146 + "!": true, 1.147 + "new": true, 1.148 + // Function calls and grouped expressions 1.149 + "(": true 1.150 + }; 1.151 + 1.152 + // If any of these tokens are on a line after the token before it, we know 1.153 + // that ASI cannot happen. 1.154 + var PREVENT_ASI_BEFORE_TOKENS = { 1.155 + // Binary operators 1.156 + "*": true, 1.157 + "/": true, 1.158 + "%": true, 1.159 + "<<": true, 1.160 + ">>": true, 1.161 + ">>>": true, 1.162 + "<": true, 1.163 + ">": true, 1.164 + "<=": true, 1.165 + ">=": true, 1.166 + "instanceof": true, 1.167 + "in": true, 1.168 + "==": true, 1.169 + "!=": true, 1.170 + "===": true, 1.171 + "!==": true, 1.172 + "&": true, 1.173 + "^": true, 1.174 + "|": true, 1.175 + "&&": true, 1.176 + "||": true, 1.177 + ",": true, 1.178 + ".": true, 1.179 + "=": true, 1.180 + "*=": true, 1.181 + "/=": true, 1.182 + "%=": true, 1.183 + "+=": true, 1.184 + "-=": true, 1.185 + "<<=": true, 1.186 + ">>=": true, 1.187 + ">>>=": true, 1.188 + "&=": true, 1.189 + "^=": true, 1.190 + "|=": true, 1.191 + // Function calls 1.192 + "(": true 1.193 + }; 1.194 + 1.195 + /** 1.196 + * Determines if Automatic Semicolon Insertion (ASI) occurs between these 1.197 + * tokens. 1.198 + * 1.199 + * @param Object token 1.200 + * The current token. 1.201 + * @param Object lastToken 1.202 + * The last token we added to the pretty printed results. 1.203 + * 1.204 + * @returns Boolean 1.205 + * True if we believe ASI occurs. 1.206 + */ 1.207 + function isASI(token, lastToken) { 1.208 + if (!lastToken) { 1.209 + return false; 1.210 + } 1.211 + if (token.startLoc.line === lastToken.startLoc.line) { 1.212 + return false; 1.213 + } 1.214 + if (PREVENT_ASI_AFTER_TOKENS[lastToken.type.type || lastToken.type.keyword]) { 1.215 + return false; 1.216 + } 1.217 + if (PREVENT_ASI_BEFORE_TOKENS[token.type.type || token.type.keyword]) { 1.218 + return false; 1.219 + } 1.220 + return true; 1.221 + } 1.222 + 1.223 + /** 1.224 + * Determine if we have encountered a getter or setter. 1.225 + * 1.226 + * @param Object token 1.227 + * The current token. If this is a getter or setter, it would be the 1.228 + * property name. 1.229 + * @param Object lastToken 1.230 + * The last token we added to the pretty printed results. If this is a 1.231 + * getter or setter, it would be the `get` or `set` keyword 1.232 + * respectively. 1.233 + * @param Array stack 1.234 + * The stack of open parens/curlies/brackets/etc. 1.235 + * 1.236 + * @returns Boolean 1.237 + * True if this is a getter or setter. 1.238 + */ 1.239 + function isGetterOrSetter(token, lastToken, stack) { 1.240 + return stack[stack.length - 1] == "{" 1.241 + && lastToken 1.242 + && lastToken.type.type == "name" 1.243 + && (lastToken.value == "get" || lastToken.value == "set") 1.244 + && token.type.type == "name"; 1.245 + } 1.246 + 1.247 + /** 1.248 + * Determine if we should add a newline after the given token. 1.249 + * 1.250 + * @param Object token 1.251 + * The token we are looking at. 1.252 + * @param Array stack 1.253 + * The stack of open parens/curlies/brackets/etc. 1.254 + * 1.255 + * @returns Boolean 1.256 + * True if we should add a newline. 1.257 + */ 1.258 + function isLineDelimiter(token, stack) { 1.259 + if (token.isArrayLiteral) { 1.260 + return true; 1.261 + } 1.262 + var ttt = token.type.type; 1.263 + var top = stack[stack.length - 1]; 1.264 + return ttt == ";" && top != "(" 1.265 + || ttt == "{" 1.266 + || ttt == "," && top != "(" 1.267 + || ttt == ":" && (top == "case" || top == "default"); 1.268 + } 1.269 + 1.270 + /** 1.271 + * Append the necessary whitespace to the result after we have added the given 1.272 + * token. 1.273 + * 1.274 + * @param Object token 1.275 + * The token that was just added to the result. 1.276 + * @param Function write 1.277 + * The function to write to the pretty printed results. 1.278 + * @param Array stack 1.279 + * The stack of open parens/curlies/brackets/etc. 1.280 + * 1.281 + * @returns Boolean 1.282 + * Returns true if we added a newline to result, false in all other 1.283 + * cases. 1.284 + */ 1.285 + function appendNewline(token, write, stack) { 1.286 + if (isLineDelimiter(token, stack)) { 1.287 + write("\n", token.startLoc.line, token.startLoc.column); 1.288 + return true; 1.289 + } 1.290 + return false; 1.291 + } 1.292 + 1.293 + /** 1.294 + * Determines if we need to add a space between the last token we added and 1.295 + * the token we are about to add. 1.296 + * 1.297 + * @param Object token 1.298 + * The token we are about to add to the pretty printed code. 1.299 + * @param Object lastToken 1.300 + * The last token added to the pretty printed code. 1.301 + */ 1.302 + function needsSpaceAfter(token, lastToken) { 1.303 + if (lastToken) { 1.304 + if (lastToken.type.isLoop) { 1.305 + return true; 1.306 + } 1.307 + if (lastToken.type.isAssign) { 1.308 + return true; 1.309 + } 1.310 + if (lastToken.type.binop != null) { 1.311 + return true; 1.312 + } 1.313 + 1.314 + var ltt = lastToken.type.type; 1.315 + if (ltt == "?") { 1.316 + return true; 1.317 + } 1.318 + if (ltt == ":") { 1.319 + return true; 1.320 + } 1.321 + if (ltt == ",") { 1.322 + return true; 1.323 + } 1.324 + if (ltt == ";") { 1.325 + return true; 1.326 + } 1.327 + 1.328 + var ltk = lastToken.type.keyword; 1.329 + if (ltk != null) { 1.330 + if (ltk == "break" || ltk == "continue") { 1.331 + return token.type.type != ";"; 1.332 + } 1.333 + if (ltk != "debugger" 1.334 + && ltk != "null" 1.335 + && ltk != "true" 1.336 + && ltk != "false" 1.337 + && ltk != "this" 1.338 + && ltk != "default") { 1.339 + return true; 1.340 + } 1.341 + } 1.342 + 1.343 + if (ltt == ")" && (token.type.type != ")" 1.344 + && token.type.type != "]" 1.345 + && token.type.type != ";" 1.346 + && token.type.type != ",")) { 1.347 + return true; 1.348 + } 1.349 + } 1.350 + 1.351 + if (token.type.isAssign) { 1.352 + return true; 1.353 + } 1.354 + if (token.type.binop != null) { 1.355 + return true; 1.356 + } 1.357 + if (token.type.type == "?") { 1.358 + return true; 1.359 + } 1.360 + 1.361 + return false; 1.362 + } 1.363 + 1.364 + /** 1.365 + * Add the required whitespace before this token, whether that is a single 1.366 + * space, newline, and/or the indent on fresh lines. 1.367 + * 1.368 + * @param Object token 1.369 + * The token we are about to add to the pretty printed code. 1.370 + * @param Object lastToken 1.371 + * The last token we added to the pretty printed code. 1.372 + * @param Boolean addedNewline 1.373 + * Whether we added a newline after adding the last token to the pretty 1.374 + * printed code. 1.375 + * @param Function write 1.376 + * The function to write pretty printed code to the result SourceNode. 1.377 + * @param Object options 1.378 + * The options object. 1.379 + * @param Number indentLevel 1.380 + * The number of indents deep we are. 1.381 + * @param Array stack 1.382 + * The stack of open curlies, brackets, etc. 1.383 + */ 1.384 + function prependWhiteSpace(token, lastToken, addedNewline, write, options, 1.385 + indentLevel, stack) { 1.386 + var ttk = token.type.keyword; 1.387 + var ttt = token.type.type; 1.388 + var newlineAdded = addedNewline; 1.389 + var ltt = lastToken ? lastToken.type.type : null; 1.390 + 1.391 + // Handle whitespace and newlines after "}" here instead of in 1.392 + // `isLineDelimiter` because it is only a line delimiter some of the 1.393 + // time. For example, we don't want to put "else if" on a new line after 1.394 + // the first if's block. 1.395 + if (lastToken && ltt == "}") { 1.396 + if (ttk == "while" && stack[stack.length - 1] == "do") { 1.397 + write(" ", 1.398 + lastToken.startLoc.line, 1.399 + lastToken.startLoc.column); 1.400 + } else if (ttk == "else" || 1.401 + ttk == "catch" || 1.402 + ttk == "finally") { 1.403 + write(" ", 1.404 + lastToken.startLoc.line, 1.405 + lastToken.startLoc.column); 1.406 + } else if (ttt != "(" && 1.407 + ttt != ";" && 1.408 + ttt != "," && 1.409 + ttt != ")" && 1.410 + ttt != ".") { 1.411 + write("\n", 1.412 + lastToken.startLoc.line, 1.413 + lastToken.startLoc.column); 1.414 + newlineAdded = true; 1.415 + } 1.416 + } 1.417 + 1.418 + if (isGetterOrSetter(token, lastToken, stack)) { 1.419 + write(" ", 1.420 + lastToken.startLoc.line, 1.421 + lastToken.startLoc.column); 1.422 + } 1.423 + 1.424 + if (ttt == ":" && stack[stack.length - 1] == "?") { 1.425 + write(" ", 1.426 + lastToken.startLoc.line, 1.427 + lastToken.startLoc.column); 1.428 + } 1.429 + 1.430 + if (lastToken && ltt != "}" && ttk == "else") { 1.431 + write(" ", 1.432 + lastToken.startLoc.line, 1.433 + lastToken.startLoc.column); 1.434 + } 1.435 + 1.436 + function ensureNewline() { 1.437 + if (!newlineAdded) { 1.438 + write("\n", 1.439 + lastToken.startLoc.line, 1.440 + lastToken.startLoc.column); 1.441 + newlineAdded = true; 1.442 + } 1.443 + } 1.444 + 1.445 + if (isASI(token, lastToken)) { 1.446 + ensureNewline(); 1.447 + } 1.448 + 1.449 + if (decrementsIndent(ttt, stack)) { 1.450 + ensureNewline(); 1.451 + } 1.452 + 1.453 + if (newlineAdded) { 1.454 + if (ttk == "case" || ttk == "default") { 1.455 + write(repeat(options.indent, indentLevel - 1), 1.456 + token.startLoc.line, 1.457 + token.startLoc.column); 1.458 + } else { 1.459 + write(repeat(options.indent, indentLevel), 1.460 + token.startLoc.line, 1.461 + token.startLoc.column); 1.462 + } 1.463 + } else if (needsSpaceAfter(token, lastToken)) { 1.464 + write(" ", 1.465 + lastToken.startLoc.line, 1.466 + lastToken.startLoc.column); 1.467 + } 1.468 + } 1.469 + 1.470 + /** 1.471 + * Repeat the `str` string `n` times. 1.472 + * 1.473 + * @param String str 1.474 + * The string to be repeated. 1.475 + * @param Number n 1.476 + * The number of times to repeat the string. 1.477 + * 1.478 + * @returns String 1.479 + * The repeated string. 1.480 + */ 1.481 + function repeat(str, n) { 1.482 + var result = ""; 1.483 + while (n > 0) { 1.484 + if (n & 1) { 1.485 + result += str; 1.486 + } 1.487 + n >>= 1; 1.488 + str += str; 1.489 + } 1.490 + return result; 1.491 + } 1.492 + 1.493 + /** 1.494 + * Make sure that we output the escaped character combination inside string literals 1.495 + * instead of various problematic characters. 1.496 + */ 1.497 + var sanitize = (function () { 1.498 + var escapeCharacters = { 1.499 + // Backslash 1.500 + "\\": "\\\\", 1.501 + // Newlines 1.502 + "\n": "\\n", 1.503 + // Carriage return 1.504 + "\r": "\\r", 1.505 + // Tab 1.506 + "\t": "\\t", 1.507 + // Vertical tab 1.508 + "\v": "\\v", 1.509 + // Form feed 1.510 + "\f": "\\f", 1.511 + // Null character 1.512 + "\0": "\\0", 1.513 + // Single quotes 1.514 + "'": "\\'" 1.515 + }; 1.516 + 1.517 + var regExpString = "(" 1.518 + + Object.keys(escapeCharacters) 1.519 + .map(function (c) { return escapeCharacters[c]; }) 1.520 + .join("|") 1.521 + + ")"; 1.522 + var escapeCharactersRegExp = new RegExp(regExpString, "g"); 1.523 + 1.524 + return function(str) { 1.525 + return str.replace(escapeCharactersRegExp, function (_, c) { 1.526 + return escapeCharacters[c]; 1.527 + }); 1.528 + } 1.529 + }()); 1.530 + /** 1.531 + * Add the given token to the pretty printed results. 1.532 + * 1.533 + * @param Object token 1.534 + * The token to add. 1.535 + * @param Function write 1.536 + * The function to write pretty printed code to the result SourceNode. 1.537 + * @param Object options 1.538 + * The options object. 1.539 + */ 1.540 + function addToken(token, write, options) { 1.541 + if (token.type.type == "string") { 1.542 + write("'" + sanitize(token.value) + "'", 1.543 + token.startLoc.line, 1.544 + token.startLoc.column); 1.545 + } else { 1.546 + write(String(token.value != null ? token.value : token.type.type), 1.547 + token.startLoc.line, 1.548 + token.startLoc.column); 1.549 + } 1.550 + } 1.551 + 1.552 + /** 1.553 + * Returns true if the given token type belongs on the stack. 1.554 + */ 1.555 + function belongsOnStack(token) { 1.556 + var ttt = token.type.type; 1.557 + var ttk = token.type.keyword; 1.558 + return ttt == "{" 1.559 + || ttt == "(" 1.560 + || ttt == "[" 1.561 + || ttt == "?" 1.562 + || ttk == "do" 1.563 + || ttk == "case" 1.564 + || ttk == "default"; 1.565 + } 1.566 + 1.567 + /** 1.568 + * Returns true if the given token should cause us to pop the stack. 1.569 + */ 1.570 + function shouldStackPop(token, stack) { 1.571 + var ttt = token.type.type; 1.572 + var ttk = token.type.keyword; 1.573 + var top = stack[stack.length - 1]; 1.574 + return ttt == "]" 1.575 + || ttt == ")" 1.576 + || ttt == "}" 1.577 + || (ttt == ":" && (top == "case" || top == "default" || top == "?")) 1.578 + || (ttk == "while" && top == "do"); 1.579 + } 1.580 + 1.581 + /** 1.582 + * Returns true if the given token type should cause us to decrement the 1.583 + * indent level. 1.584 + */ 1.585 + function decrementsIndent(tokenType, stack) { 1.586 + return tokenType == "}" 1.587 + || (tokenType == "]" && stack[stack.length - 1] == "[\n") 1.588 + } 1.589 + 1.590 + /** 1.591 + * Returns true if the given token should cause us to increment the indent 1.592 + * level. 1.593 + */ 1.594 + function incrementsIndent(token) { 1.595 + return token.type.type == "{" || token.isArrayLiteral; 1.596 + } 1.597 + 1.598 + /** 1.599 + * Add a comment to the pretty printed code. 1.600 + * 1.601 + * @param Function write 1.602 + * The function to write pretty printed code to the result SourceNode. 1.603 + * @param Number indentLevel 1.604 + * The number of indents deep we are. 1.605 + * @param Object options 1.606 + * The options object. 1.607 + * @param Boolean block 1.608 + * True if the comment is a multiline block style comment. 1.609 + * @param String text 1.610 + * The text of the comment. 1.611 + * @param Number line 1.612 + * The line number to comment appeared on. 1.613 + * @param Number column 1.614 + * The column number the comment appeared on. 1.615 + */ 1.616 + function addComment(write, indentLevel, options, block, text, line, column) { 1.617 + var indentString = repeat(options.indent, indentLevel); 1.618 + 1.619 + write(indentString, line, column); 1.620 + if (block) { 1.621 + write("/*"); 1.622 + write(text 1.623 + .split(new RegExp("/\n" + indentString + "/", "g")) 1.624 + .join("\n" + indentString)); 1.625 + write("*/"); 1.626 + } else { 1.627 + write("//"); 1.628 + write(text); 1.629 + } 1.630 + write("\n"); 1.631 + } 1.632 + 1.633 + /** 1.634 + * The main function. 1.635 + * 1.636 + * @param String input 1.637 + * The ugly JS code we want to pretty print. 1.638 + * @param Object options 1.639 + * The options object. Provides configurability of the pretty 1.640 + * printing. Properties: 1.641 + * - url: The URL string of the ugly JS code. 1.642 + * - indent: The string to indent code by. 1.643 + * 1.644 + * @returns Object 1.645 + * An object with the following properties: 1.646 + * - code: The pretty printed code string. 1.647 + * - map: A SourceMapGenerator instance. 1.648 + */ 1.649 + return function prettyFast(input, options) { 1.650 + // The level of indents deep we are. 1.651 + var indentLevel = 0; 1.652 + 1.653 + // We will accumulate the pretty printed code in this SourceNode. 1.654 + var result = new SourceNode(); 1.655 + 1.656 + /** 1.657 + * Write a pretty printed string to the result SourceNode. 1.658 + * 1.659 + * We buffer our writes so that we only create one mapping for each line in 1.660 + * the source map. This enhances performance by avoiding extraneous mapping 1.661 + * serialization, and flattening the tree that 1.662 + * `SourceNode#toStringWithSourceMap` will have to recursively walk. When 1.663 + * timing how long it takes to pretty print jQuery, this optimization 1.664 + * brought the time down from ~390 ms to ~190ms! 1.665 + * 1.666 + * @param String str 1.667 + * The string to be added to the result. 1.668 + * @param Number line 1.669 + * The line number the string came from in the ugly source. 1.670 + * @param Number column 1.671 + * The column number the string came from in the ugly source. 1.672 + */ 1.673 + var write = (function () { 1.674 + var buffer = []; 1.675 + var bufferLine = -1; 1.676 + var bufferColumn = -1; 1.677 + return function write(str, line, column) { 1.678 + if (line != null && bufferLine === -1) { 1.679 + bufferLine = line; 1.680 + } 1.681 + if (column != null && bufferColumn === -1) { 1.682 + bufferColumn = column; 1.683 + } 1.684 + buffer.push(str); 1.685 + 1.686 + if (str == "\n") { 1.687 + var lineStr = ""; 1.688 + for (var i = 0, len = buffer.length; i < len; i++) { 1.689 + lineStr += buffer[i]; 1.690 + } 1.691 + result.add(new SourceNode(bufferLine, bufferColumn, options.url, lineStr)); 1.692 + buffer.splice(0, buffer.length); 1.693 + bufferLine = -1; 1.694 + bufferColumn = -1; 1.695 + } 1.696 + } 1.697 + }()); 1.698 + 1.699 + // Whether or not we added a newline on after we added the last token. 1.700 + var addedNewline = false; 1.701 + 1.702 + // The current token we will be adding to the pretty printed code. 1.703 + var token; 1.704 + 1.705 + // Shorthand for token.type.type, so we don't have to repeatedly access 1.706 + // properties. 1.707 + var ttt; 1.708 + 1.709 + // Shorthand for token.type.keyword, so we don't have to repeatedly access 1.710 + // properties. 1.711 + var ttk; 1.712 + 1.713 + // The last token we added to the pretty printed code. 1.714 + var lastToken; 1.715 + 1.716 + // Stack of token types/keywords that can affect whether we want to add a 1.717 + // newline or a space. We can make that decision based on what token type is 1.718 + // on the top of the stack. For example, a comma in a parameter list should 1.719 + // be followed by a space, while a comma in an object literal should be 1.720 + // followed by a newline. 1.721 + // 1.722 + // Strings that go on the stack: 1.723 + // 1.724 + // - "{" 1.725 + // - "(" 1.726 + // - "[" 1.727 + // - "[\n" 1.728 + // - "do" 1.729 + // - "?" 1.730 + // - "case" 1.731 + // - "default" 1.732 + // 1.733 + // The difference between "[" and "[\n" is that "[\n" is used when we are 1.734 + // treating "[" and "]" tokens as line delimiters and should increment and 1.735 + // decrement the indent level when we find them. 1.736 + var stack = []; 1.737 + 1.738 + // Acorn's tokenizer will always yield comments *before* the token they 1.739 + // follow (unless the very first thing in the source is a comment), so we 1.740 + // have to queue the comments in order to pretty print them in the correct 1.741 + // location. For example, the source file: 1.742 + // 1.743 + // foo 1.744 + // // a 1.745 + // // b 1.746 + // bar 1.747 + // 1.748 + // When tokenized by acorn, gives us the following token stream: 1.749 + // 1.750 + // [ '// a', '// b', foo, bar ] 1.751 + var commentQueue = []; 1.752 + 1.753 + var getToken = acorn.tokenize(input, { 1.754 + locations: true, 1.755 + sourceFile: options.url, 1.756 + onComment: function (block, text, start, end, startLoc, endLoc) { 1.757 + if (lastToken) { 1.758 + commentQueue.push({ 1.759 + block: block, 1.760 + text: text, 1.761 + line: startLoc.line, 1.762 + column: startLoc.column 1.763 + }); 1.764 + } else { 1.765 + addComment(write, indentLevel, options, block, text, startLoc.line, 1.766 + startLoc.column); 1.767 + addedNewline = true; 1.768 + } 1.769 + } 1.770 + }); 1.771 + 1.772 + while (true) { 1.773 + token = getToken(); 1.774 + 1.775 + ttk = token.type.keyword; 1.776 + ttt = token.type.type; 1.777 + 1.778 + if (ttt == "eof") { 1.779 + if (!addedNewline) { 1.780 + write("\n"); 1.781 + } 1.782 + break; 1.783 + } 1.784 + 1.785 + token.isArrayLiteral = isArrayLiteral(token, lastToken); 1.786 + 1.787 + if (belongsOnStack(token)) { 1.788 + if (token.isArrayLiteral) { 1.789 + stack.push("[\n"); 1.790 + } else { 1.791 + stack.push(ttt || ttk); 1.792 + } 1.793 + } 1.794 + 1.795 + if (decrementsIndent(ttt, stack)) { 1.796 + indentLevel--; 1.797 + } 1.798 + 1.799 + prependWhiteSpace(token, lastToken, addedNewline, write, options, 1.800 + indentLevel, stack); 1.801 + addToken(token, write, options); 1.802 + addedNewline = appendNewline(token, write, stack); 1.803 + 1.804 + if (shouldStackPop(token, stack)) { 1.805 + stack.pop(); 1.806 + } 1.807 + 1.808 + if (incrementsIndent(token)) { 1.809 + indentLevel++; 1.810 + } 1.811 + 1.812 + // Acorn's tokenizer re-uses tokens, so we have to copy the last token on 1.813 + // every iteration. We follow acorn's lead here, and reuse the lastToken 1.814 + // object the same way that acorn reuses the token object. This allows us 1.815 + // to avoid allocations and minimize GC pauses. 1.816 + if (!lastToken) { 1.817 + lastToken = { startLoc: {}, endLoc: {} }; 1.818 + } 1.819 + lastToken.start = token.start; 1.820 + lastToken.end = token.end; 1.821 + lastToken.startLoc.line = token.startLoc.line; 1.822 + lastToken.startLoc.column = token.startLoc.column; 1.823 + lastToken.endLoc.line = token.endLoc.line; 1.824 + lastToken.endLoc.column = token.endLoc.column; 1.825 + lastToken.type = token.type; 1.826 + lastToken.value = token.value; 1.827 + lastToken.isArrayLiteral = token.isArrayLiteral; 1.828 + 1.829 + // Apply all the comments that have been queued up. 1.830 + if (commentQueue.length) { 1.831 + if (!addedNewline) { 1.832 + write("\n"); 1.833 + } 1.834 + for (var i = 0, n = commentQueue.length; i < n; i++) { 1.835 + var comment = commentQueue[i]; 1.836 + addComment(write, indentLevel, options, comment.block, comment.text, 1.837 + comment.line, comment.column); 1.838 + } 1.839 + addedNewline = true; 1.840 + commentQueue.splice(0, commentQueue.length); 1.841 + } 1.842 + } 1.843 + 1.844 + return result.toStringWithSourceMap({ file: options.url }); 1.845 + }; 1.846 + 1.847 +}.bind(this)));