Legend:
- Unmodified
- Added
- Removed
-
imaps-frontend/node_modules/@babel/generator/lib/printer.js
rd565449 r0c6b92a 8 8 var n = require("./node/index.js"); 9 9 var _t = require("@babel/types"); 10 var _tokenMap = require("./token-map.js"); 10 11 var generatorFunctions = require("./generators/index.js"); 11 12 const { 13 isExpression, 12 14 isFunction, 13 15 isStatement, … … 20 22 const HAS_NEWLINE = /[\n\r\u2028\u2029]/; 21 23 const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//; 24 function commentIsNewline(c) { 25 return c.type === "CommentLine" || HAS_NEWLINE.test(c.value); 26 } 22 27 const { 23 28 needsParens 24 29 } = n; 25 30 class Printer { 26 constructor(format, map ) {31 constructor(format, map, tokens, originalCode) { 27 32 this.inForStatementInit = false; 28 33 this.tokenContext = 0; 34 this._tokens = null; 35 this._originalCode = null; 29 36 this._currentNode = null; 30 37 this._indent = 0; 31 38 this._indentRepeat = 0; 32 39 this._insideAux = false; 33 this._parenPushNewlineState = null;34 40 this._noLineTerminator = false; 41 this._noLineTerminatorAfterNode = null; 35 42 this._printAuxAfterOnNextUserNode = false; 36 43 this._printedComments = new Set(); 37 44 this._endsWithInteger = false; 38 45 this._endsWithWord = false; 46 this._endsWithDiv = false; 39 47 this._lastCommentLine = 0; 40 48 this._endsWithInnerRaw = false; 41 49 this._indentInnerComments = true; 50 this.tokenMap = null; 51 this._boundGetRawIdentifier = this._getRawIdentifier.bind(this); 42 52 this.format = format; 53 this._tokens = tokens; 54 this._originalCode = originalCode; 43 55 this._indentRepeat = format.indent.style.length; 44 56 this._inputMap = map == null ? void 0 : map._inputMap; 45 57 this._buf = new _buffer.default(map, format.indent.style[0]); 46 58 } 47 enterForStatementInit(val) { 48 const old = this.inForStatementInit; 49 if (old === val) return () => {}; 50 this.inForStatementInit = val; 59 enterForStatementInit() { 60 if (this.inForStatementInit) return () => {}; 61 this.inForStatementInit = true; 51 62 return () => { 52 this.inForStatementInit = old;63 this.inForStatementInit = false; 53 64 }; 54 65 } 66 enterDelimited() { 67 const oldInForStatementInit = this.inForStatementInit; 68 const oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode; 69 if (oldInForStatementInit === false && oldNoLineTerminatorAfterNode === null) { 70 return () => {}; 71 } 72 this.inForStatementInit = false; 73 this._noLineTerminatorAfterNode = null; 74 return () => { 75 this.inForStatementInit = oldInForStatementInit; 76 this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; 77 }; 78 } 55 79 generate(ast) { 80 if (this.format.preserveFormat) { 81 this.tokenMap = new _tokenMap.TokenMap(ast, this._tokens, this._originalCode); 82 } 56 83 this.print(ast); 57 84 this._maybeAddAuxComment(); … … 59 86 } 60 87 indent() { 61 if (this.format.compact || this.format.concise) return; 88 const { 89 format 90 } = this; 91 if (format.preserveFormat || format.compact || format.concise) { 92 return; 93 } 62 94 this._indent++; 63 95 } 64 96 dedent() { 65 if (this.format.compact || this.format.concise) return; 97 const { 98 format 99 } = this; 100 if (format.preserveFormat || format.compact || format.concise) { 101 return; 102 } 66 103 this._indent--; 67 104 } … … 70 107 if (force) { 71 108 this._appendChar(59); 72 } else { 73 this._queue(59); 74 } 109 this._noLineTerminator = false; 110 return; 111 } 112 if (this.tokenMap) { 113 const node = this._currentNode; 114 if (node.start != null && node.end != null) { 115 if (!this.tokenMap.endMatches(node, ";")) { 116 return; 117 } 118 const indexes = this.tokenMap.getIndexes(this._currentNode); 119 this._catchUpTo(this._tokens[indexes[indexes.length - 1]].loc.start); 120 } 121 } 122 this._queue(59); 75 123 this._noLineTerminator = false; 76 124 } … … 87 135 } 88 136 space(force = false) { 89 if (this.format.compact) return; 137 const { 138 format 139 } = this; 140 if (format.compact || format.preserveFormat) return; 90 141 if (force) { 91 142 this._space(); … … 99 150 word(str, noLineTerminatorAfter = false) { 100 151 this.tokenContext = 0; 101 this._maybePrintInnerComments( );102 if (this._endsWithWord || str.charCodeAt(0) === 47 && this.endsWith(47)) {152 this._maybePrintInnerComments(str); 153 if (this._endsWithWord || this._endsWithDiv && str.charCodeAt(0) === 47) { 103 154 this._space(); 104 155 } … … 119 170 this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46; 120 171 } 121 token(str, maybeNewline = false ) {172 token(str, maybeNewline = false, occurrenceCount = 0) { 122 173 this.tokenContext = 0; 123 this._maybePrintInnerComments( );174 this._maybePrintInnerComments(str, occurrenceCount); 124 175 const lastChar = this.getLastChar(); 125 176 const strFirst = str.charCodeAt(0); … … 128 179 } 129 180 this._maybeAddAuxComment(); 130 this._append(str, maybeNewline );181 this._append(str, maybeNewline, occurrenceCount); 131 182 this._noLineTerminator = false; 132 183 } 133 184 tokenChar(char) { 134 185 this.tokenContext = 0; 135 this._maybePrintInnerComments( );186 this._maybePrintInnerComments(String.fromCharCode(char)); 136 187 const lastChar = this.getLastChar(); 137 188 if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) { … … 184 235 } 185 236 sourceWithOffset(prop, loc, columnOffset) { 186 if (!loc ) return;237 if (!loc || this.format.preserveFormat) return; 187 238 this._catchUp(prop, loc); 188 239 this._buf.sourceWithOffset(prop, loc, columnOffset); … … 200 251 this._queue(10); 201 252 } 202 _append(str, maybeNewline) { 203 this._maybeAddParen(str); 253 _append(str, maybeNewline, occurrenceCount = 0) { 254 if (this.tokenMap) { 255 const token = this.tokenMap.findMatching(this._currentNode, str, occurrenceCount); 256 if (token) this._catchUpTo(token.loc.start); 257 } 204 258 this._maybeIndent(str.charCodeAt(0)); 205 259 this._buf.append(str, maybeNewline); 206 260 this._endsWithWord = false; 207 261 this._endsWithInteger = false; 262 this._endsWithDiv = false; 208 263 } 209 264 _appendChar(char) { 210 this._maybeAddParenChar(char); 265 if (this.tokenMap) { 266 const token = this.tokenMap.findMatching(this._currentNode, String.fromCharCode(char)); 267 if (token) this._catchUpTo(token.loc.start); 268 } 211 269 this._maybeIndent(char); 212 270 this._buf.appendChar(char); 213 271 this._endsWithWord = false; 214 272 this._endsWithInteger = false; 273 this._endsWithDiv = false; 215 274 } 216 275 _queue(char) { 217 this._maybeAddParenChar(char);218 276 this._maybeIndent(char); 219 277 this._buf.queue(char); … … 230 288 return true; 231 289 } 232 }233 _maybeAddParenChar(char) {234 const parenPushNewlineState = this._parenPushNewlineState;235 if (!parenPushNewlineState) return;236 if (char === 32) {237 return;238 }239 if (char !== 10) {240 this._parenPushNewlineState = null;241 return;242 }243 this.tokenChar(40);244 this.indent();245 parenPushNewlineState.printed = true;246 }247 _maybeAddParen(str) {248 const parenPushNewlineState = this._parenPushNewlineState;249 if (!parenPushNewlineState) return;250 const len = str.length;251 let i;252 for (i = 0; i < len && str.charCodeAt(i) === 32; i++) continue;253 if (i === len) {254 return;255 }256 const cha = str.charCodeAt(i);257 if (cha !== 10) {258 if (cha !== 47 || i + 1 === len) {259 this._parenPushNewlineState = null;260 return;261 }262 const chaPost = str.charCodeAt(i + 1);263 if (chaPost === 42) {264 return;265 } else if (chaPost !== 47) {266 this._parenPushNewlineState = null;267 return;268 }269 }270 this.tokenChar(40);271 this.indent();272 parenPushNewlineState.printed = true;273 290 } 274 291 catchUp(line) { … … 280 297 } 281 298 _catchUp(prop, loc) { 282 var _loc$prop; 283 if (!this.format.retainLines) return; 284 const line = loc == null || (_loc$prop = loc[prop]) == null ? void 0 : _loc$prop.line; 285 if (line != null) { 286 const count = line - this._buf.getCurrentLine(); 287 for (let i = 0; i < count; i++) { 288 this._newline(); 289 } 299 const { 300 format 301 } = this; 302 if (!format.preserveFormat) { 303 if (format.retainLines && loc != null && loc[prop]) { 304 this.catchUp(loc[prop].line); 305 } 306 return; 307 } 308 const pos = loc == null ? void 0 : loc[prop]; 309 if (pos != null) this._catchUpTo(pos); 310 } 311 _catchUpTo({ 312 line, 313 column, 314 index 315 }) { 316 const count = line - this._buf.getCurrentLine(); 317 if (count > 0 && this._noLineTerminator) { 318 return; 319 } 320 for (let i = 0; i < count; i++) { 321 this._newline(); 322 } 323 const spacesCount = count > 0 ? column : column - this._buf.getCurrentColumn(); 324 if (spacesCount > 0) { 325 const spaces = this._originalCode ? this._originalCode.slice(index - spacesCount, index).replace(/[^\t\x0B\f \xA0\u1680\u2000-\u200A\u202F\u205F\u3000\uFEFF]/gu, " ") : " ".repeat(spacesCount); 326 this._append(spaces, false); 290 327 } 291 328 } … … 293 330 return this._indentRepeat * this._indent; 294 331 } 295 printTerminatorless(node, parent, isLabel) { 296 if (isLabel) { 297 this._noLineTerminator = true; 298 this.print(node, parent); 299 } else { 300 const terminatorState = { 301 printed: false 302 }; 303 this._parenPushNewlineState = terminatorState; 304 this.print(node, parent); 305 if (terminatorState.printed) { 306 this.dedent(); 307 this.newline(); 308 this.tokenChar(41); 309 } 310 } 311 } 312 print(node, parent, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) { 313 var _node$extra, _node$leadingComments; 332 printTerminatorless(node) { 333 this._noLineTerminator = true; 334 this.print(node); 335 } 336 print(node, noLineTerminatorAfter, trailingCommentsLineOffset) { 337 var _node$extra, _node$leadingComments, _node$leadingComments2; 314 338 if (!node) return; 315 339 this._endsWithInnerRaw = false; … … 324 348 throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`); 325 349 } 326 const oldNode= this._currentNode;350 const parent = this._currentNode; 327 351 this._currentNode = node; 328 352 const oldInAux = this._insideAux; … … 330 354 this._maybeAddAuxComment(this._insideAux && !oldInAux); 331 355 const parenthesized = (_node$extra = node.extra) == null ? void 0 : _node$extra.parenthesized; 332 let shouldPrintParens = forceParens || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this.tokenContext, this.inForStatementInit);356 let shouldPrintParens = parenthesized && format.preserveFormat || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this.tokenContext, this.inForStatementInit, format.preserveFormat ? this._boundGetRawIdentifier : undefined); 333 357 if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") { 334 358 const parentType = parent == null ? void 0 : parent.type; … … 347 371 } 348 372 } 349 let exitInForStatementInit; 373 let indentParenthesized = false; 374 if (!shouldPrintParens && this._noLineTerminator && ((_node$leadingComments2 = node.leadingComments) != null && _node$leadingComments2.some(commentIsNewline) || this.format.retainLines && node.loc && node.loc.start.line > this._buf.getCurrentLine())) { 375 shouldPrintParens = true; 376 indentParenthesized = true; 377 } 378 let oldNoLineTerminatorAfterNode; 379 let oldInForStatementInitWasTrue; 380 if (!shouldPrintParens) { 381 noLineTerminatorAfter || (noLineTerminatorAfter = parent && this._noLineTerminatorAfterNode === parent && n.isLastChild(parent, node)); 382 if (noLineTerminatorAfter) { 383 var _node$trailingComment; 384 if ((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.some(commentIsNewline)) { 385 if (isExpression(node)) shouldPrintParens = true; 386 } else { 387 oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode; 388 this._noLineTerminatorAfterNode = node; 389 } 390 } 391 } 350 392 if (shouldPrintParens) { 351 393 this.tokenChar(40); 394 if (indentParenthesized) this.indent(); 352 395 this._endsWithInnerRaw = false; 353 exitInForStatementInit = this.enterForStatementInit(false); 396 if (this.inForStatementInit) { 397 oldInForStatementInitWasTrue = true; 398 this.inForStatementInit = false; 399 } 400 oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode; 401 this._noLineTerminatorAfterNode = null; 354 402 } 355 403 this._lastCommentLine = 0; … … 359 407 if (shouldPrintParens) { 360 408 this._printTrailingComments(node, parent); 409 if (indentParenthesized) { 410 this.dedent(); 411 this.newline(); 412 } 361 413 this.tokenChar(41); 362 414 this._noLineTerminator = noLineTerminatorAfter; 363 exitInForStatementInit();415 if (oldInForStatementInitWasTrue) this.inForStatementInit = true; 364 416 } else if (noLineTerminatorAfter && !this._noLineTerminator) { 365 417 this._noLineTerminator = true; … … 368 420 this._printTrailingComments(node, parent, trailingCommentsLineOffset); 369 421 } 370 this._currentNode = oldNode;422 this._currentNode = parent; 371 423 format.concise = oldConcise; 372 424 this._insideAux = oldInAux; 425 if (oldNoLineTerminatorAfterNode !== undefined) { 426 this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode; 427 } 373 428 this._endsWithInnerRaw = false; 374 429 } … … 405 460 } 406 461 } 407 printJoin(nodes, parent,opts = {}) {462 printJoin(nodes, opts = {}) { 408 463 if (!(nodes != null && nodes.length)) return; 409 464 let { … … 428 483 if (!node) continue; 429 484 if (opts.statement) this._printNewline(i === 0, newlineOpts); 430 this.print(node, parent,undefined, opts.trailingCommentsLineOffset || 0);485 this.print(node, undefined, opts.trailingCommentsLineOffset || 0); 431 486 opts.iterator == null || opts.iterator(node, i); 432 if (i < len - 1) separator == null || separator(); 487 if (separator != null) { 488 if (i < len - 1) separator(i, false);else if (opts.printTrailingSeparator) separator(i, true); 489 } 433 490 if (opts.statement) { 434 var _node$trailingComment ;435 if (!((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.length)) {491 var _node$trailingComment2; 492 if (!((_node$trailingComment2 = node.trailingComments) != null && _node$trailingComment2.length)) { 436 493 this._lastCommentLine = 0; 437 494 } … … 448 505 if (indent) this.dedent(); 449 506 } 450 printAndIndentOnComments(node , parent) {507 printAndIndentOnComments(node) { 451 508 const indent = node.leadingComments && node.leadingComments.length > 0; 452 509 if (indent) this.indent(); 453 this.print(node , parent);510 this.print(node); 454 511 if (indent) this.dedent(); 455 512 } … … 459 516 this.space(); 460 517 } 461 this.print(node , parent);518 this.print(node); 462 519 } 463 520 _printTrailingComments(node, parent, lineOffset) { … … 478 535 this._printComments(0, comments, node, parent); 479 536 } 480 _maybePrintInnerComments() { 481 if (this._endsWithInnerRaw) this.printInnerComments(); 537 _maybePrintInnerComments(nextTokenStr, nextTokenOccurrenceCount) { 538 if (this._endsWithInnerRaw) { 539 var _this$tokenMap; 540 this.printInnerComments((_this$tokenMap = this.tokenMap) == null ? void 0 : _this$tokenMap.findMatching(this._currentNode, nextTokenStr, nextTokenOccurrenceCount)); 541 } 482 542 this._endsWithInnerRaw = true; 483 543 this._indentInnerComments = true; 484 544 } 485 printInnerComments( ) {545 printInnerComments(nextToken) { 486 546 const node = this._currentNode; 487 547 const comments = node.innerComments; … … 491 551 const printedCommentsCount = this._printedComments.size; 492 552 if (indent) this.indent(); 493 this._printComments(1, comments, node );553 this._printComments(1, comments, node, undefined, undefined, nextToken); 494 554 if (hasSpace && printedCommentsCount !== this._printedComments.size) { 495 555 this.space(); … … 500 560 this._indentInnerComments = false; 501 561 } 502 printSequence(nodes, parent,opts = {}) {562 printSequence(nodes, opts = {}) { 503 563 var _opts$indent; 504 564 opts.statement = true; 505 565 (_opts$indent = opts.indent) != null ? _opts$indent : opts.indent = false; 506 this.printJoin(nodes, parent,opts);507 } 508 printList(items, parent,opts = {}) {566 this.printJoin(nodes, opts); 567 } 568 printList(items, opts = {}) { 509 569 if (opts.separator == null) { 510 570 opts.separator = commaSeparator; 511 571 } 512 this.printJoin(items, parent, opts); 572 this.printJoin(items, opts); 573 } 574 shouldPrintTrailingComma(listEnd) { 575 if (!this.tokenMap) return null; 576 const listEndIndex = this.tokenMap.findLastIndex(this._currentNode, token => this.tokenMap.matchesOriginal(token, listEnd)); 577 if (listEndIndex <= 0) return null; 578 return this.tokenMap.matchesOriginal(this._tokens[listEndIndex - 1], ","); 513 579 } 514 580 _printNewline(newLine, opts) { … … 535 601 } 536 602 } 537 _shouldPrintComment(comment ) {603 _shouldPrintComment(comment, nextToken) { 538 604 if (comment.ignore) return 0; 539 605 if (this._printedComments.has(comment)) return 0; 540 606 if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) { 541 607 return 2; 608 } 609 if (nextToken && this.tokenMap) { 610 const commentTok = this.tokenMap.find(this._currentNode, token => token.value === comment.value); 611 if (commentTok && commentTok.start > nextToken.start) { 612 return 2; 613 } 542 614 } 543 615 this._printedComments.add(comment); … … 555 627 } 556 628 const lastCharCode = this.getLastChar(); 557 if (lastCharCode !== 91 && lastCharCode !== 123 ) {629 if (lastCharCode !== 91 && lastCharCode !== 123 && lastCharCode !== 40) { 558 630 this.space(); 559 631 } 560 632 let val; 561 633 if (isBlockComment) { 562 const {563 _parenPushNewlineState564 } = this;565 if ((_parenPushNewlineState == null ? void 0 : _parenPushNewlineState.printed) === false && HAS_NEWLINE.test(comment.value)) {566 this.tokenChar(40);567 this.indent();568 _parenPushNewlineState.printed = true;569 }570 634 val = `/*${comment.value}*/`; 571 635 if (this.format.indent.adjustMultilineComment) { … … 591 655 val = `/*${comment.value}*/`; 592 656 } 593 if (this. endsWith(47)) this._space();657 if (this._endsWithDiv) this._space(); 594 658 this.source("start", comment.loc); 595 659 this._append(val, isBlockComment); … … 601 665 } 602 666 } 603 _printComments(type, comments, node, parent, lineOffset = 0 ) {667 _printComments(type, comments, node, parent, lineOffset = 0, nextToken) { 604 668 const nodeLoc = node.loc; 605 669 const len = comments.length; … … 612 676 for (let i = 0; i < len; i++) { 613 677 const comment = comments[i]; 614 const shouldPrint = this._shouldPrintComment(comment );678 const shouldPrint = this._shouldPrintComment(comment, nextToken); 615 679 if (shouldPrint === 2) { 616 680 hasLoc = false; … … 685 749 } 686 750 var _default = exports.default = Printer; 687 function commaSeparator( ) {688 this.token Char(44);689 this.space();751 function commaSeparator(occurrenceCount, last) { 752 this.token(",", false, occurrenceCount); 753 if (!last) this.space(); 690 754 } 691 755
Note:
See TracChangeset
for help on using the changeset viewer.