Ignore:
Timestamp:
12/12/24 17:06:06 (5 weeks ago)
Author:
stefan toskovski <stefantoska84@…>
Branches:
main
Parents:
d565449
Message:

Pred finalna verzija

File:
1 edited

Legend:

Unmodified
Added
Removed
  • imaps-frontend/node_modules/@babel/generator/lib/printer.js

    rd565449 r0c6b92a  
    88var n = require("./node/index.js");
    99var _t = require("@babel/types");
     10var _tokenMap = require("./token-map.js");
    1011var generatorFunctions = require("./generators/index.js");
    1112const {
     13  isExpression,
    1214  isFunction,
    1315  isStatement,
     
    2022const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
    2123const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
     24function commentIsNewline(c) {
     25  return c.type === "CommentLine" || HAS_NEWLINE.test(c.value);
     26}
    2227const {
    2328  needsParens
    2429} = n;
    2530class Printer {
    26   constructor(format, map) {
     31  constructor(format, map, tokens, originalCode) {
    2732    this.inForStatementInit = false;
    2833    this.tokenContext = 0;
     34    this._tokens = null;
     35    this._originalCode = null;
    2936    this._currentNode = null;
    3037    this._indent = 0;
    3138    this._indentRepeat = 0;
    3239    this._insideAux = false;
    33     this._parenPushNewlineState = null;
    3440    this._noLineTerminator = false;
     41    this._noLineTerminatorAfterNode = null;
    3542    this._printAuxAfterOnNextUserNode = false;
    3643    this._printedComments = new Set();
    3744    this._endsWithInteger = false;
    3845    this._endsWithWord = false;
     46    this._endsWithDiv = false;
    3947    this._lastCommentLine = 0;
    4048    this._endsWithInnerRaw = false;
    4149    this._indentInnerComments = true;
     50    this.tokenMap = null;
     51    this._boundGetRawIdentifier = this._getRawIdentifier.bind(this);
    4252    this.format = format;
     53    this._tokens = tokens;
     54    this._originalCode = originalCode;
    4355    this._indentRepeat = format.indent.style.length;
    4456    this._inputMap = map == null ? void 0 : map._inputMap;
    4557    this._buf = new _buffer.default(map, format.indent.style[0]);
    4658  }
    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;
    5162    return () => {
    52       this.inForStatementInit = old;
     63      this.inForStatementInit = false;
    5364    };
    5465  }
     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  }
    5579  generate(ast) {
     80    if (this.format.preserveFormat) {
     81      this.tokenMap = new _tokenMap.TokenMap(ast, this._tokens, this._originalCode);
     82    }
    5683    this.print(ast);
    5784    this._maybeAddAuxComment();
     
    5986  }
    6087  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    }
    6294    this._indent++;
    6395  }
    6496  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    }
    66103    this._indent--;
    67104  }
     
    70107    if (force) {
    71108      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);
    75123    this._noLineTerminator = false;
    76124  }
     
    87135  }
    88136  space(force = false) {
    89     if (this.format.compact) return;
     137    const {
     138      format
     139    } = this;
     140    if (format.compact || format.preserveFormat) return;
    90141    if (force) {
    91142      this._space();
     
    99150  word(str, noLineTerminatorAfter = false) {
    100151    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) {
    103154      this._space();
    104155    }
     
    119170    this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46;
    120171  }
    121   token(str, maybeNewline = false) {
     172  token(str, maybeNewline = false, occurrenceCount = 0) {
    122173    this.tokenContext = 0;
    123     this._maybePrintInnerComments();
     174    this._maybePrintInnerComments(str, occurrenceCount);
    124175    const lastChar = this.getLastChar();
    125176    const strFirst = str.charCodeAt(0);
     
    128179    }
    129180    this._maybeAddAuxComment();
    130     this._append(str, maybeNewline);
     181    this._append(str, maybeNewline, occurrenceCount);
    131182    this._noLineTerminator = false;
    132183  }
    133184  tokenChar(char) {
    134185    this.tokenContext = 0;
    135     this._maybePrintInnerComments();
     186    this._maybePrintInnerComments(String.fromCharCode(char));
    136187    const lastChar = this.getLastChar();
    137188    if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) {
     
    184235  }
    185236  sourceWithOffset(prop, loc, columnOffset) {
    186     if (!loc) return;
     237    if (!loc || this.format.preserveFormat) return;
    187238    this._catchUp(prop, loc);
    188239    this._buf.sourceWithOffset(prop, loc, columnOffset);
     
    200251    this._queue(10);
    201252  }
    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    }
    204258    this._maybeIndent(str.charCodeAt(0));
    205259    this._buf.append(str, maybeNewline);
    206260    this._endsWithWord = false;
    207261    this._endsWithInteger = false;
     262    this._endsWithDiv = false;
    208263  }
    209264  _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    }
    211269    this._maybeIndent(char);
    212270    this._buf.appendChar(char);
    213271    this._endsWithWord = false;
    214272    this._endsWithInteger = false;
     273    this._endsWithDiv = false;
    215274  }
    216275  _queue(char) {
    217     this._maybeAddParenChar(char);
    218276    this._maybeIndent(char);
    219277    this._buf.queue(char);
     
    230288      return true;
    231289    }
    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;
    273290  }
    274291  catchUp(line) {
     
    280297  }
    281298  _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);
    290327    }
    291328  }
     
    293330    return this._indentRepeat * this._indent;
    294331  }
    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;
    314338    if (!node) return;
    315339    this._endsWithInnerRaw = false;
     
    324348      throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`);
    325349    }
    326     const oldNode = this._currentNode;
     350    const parent = this._currentNode;
    327351    this._currentNode = node;
    328352    const oldInAux = this._insideAux;
     
    330354    this._maybeAddAuxComment(this._insideAux && !oldInAux);
    331355    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);
    333357    if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") {
    334358      const parentType = parent == null ? void 0 : parent.type;
     
    347371      }
    348372    }
    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    }
    350392    if (shouldPrintParens) {
    351393      this.tokenChar(40);
     394      if (indentParenthesized) this.indent();
    352395      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;
    354402    }
    355403    this._lastCommentLine = 0;
     
    359407    if (shouldPrintParens) {
    360408      this._printTrailingComments(node, parent);
     409      if (indentParenthesized) {
     410        this.dedent();
     411        this.newline();
     412      }
    361413      this.tokenChar(41);
    362414      this._noLineTerminator = noLineTerminatorAfter;
    363       exitInForStatementInit();
     415      if (oldInForStatementInitWasTrue) this.inForStatementInit = true;
    364416    } else if (noLineTerminatorAfter && !this._noLineTerminator) {
    365417      this._noLineTerminator = true;
     
    368420      this._printTrailingComments(node, parent, trailingCommentsLineOffset);
    369421    }
    370     this._currentNode = oldNode;
     422    this._currentNode = parent;
    371423    format.concise = oldConcise;
    372424    this._insideAux = oldInAux;
     425    if (oldNoLineTerminatorAfterNode !== undefined) {
     426      this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
     427    }
    373428    this._endsWithInnerRaw = false;
    374429  }
     
    405460    }
    406461  }
    407   printJoin(nodes, parent, opts = {}) {
     462  printJoin(nodes, opts = {}) {
    408463    if (!(nodes != null && nodes.length)) return;
    409464    let {
     
    428483      if (!node) continue;
    429484      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);
    431486      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      }
    433490      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)) {
    436493          this._lastCommentLine = 0;
    437494        }
     
    448505    if (indent) this.dedent();
    449506  }
    450   printAndIndentOnComments(node, parent) {
     507  printAndIndentOnComments(node) {
    451508    const indent = node.leadingComments && node.leadingComments.length > 0;
    452509    if (indent) this.indent();
    453     this.print(node, parent);
     510    this.print(node);
    454511    if (indent) this.dedent();
    455512  }
     
    459516      this.space();
    460517    }
    461     this.print(node, parent);
     518    this.print(node);
    462519  }
    463520  _printTrailingComments(node, parent, lineOffset) {
     
    478535    this._printComments(0, comments, node, parent);
    479536  }
    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    }
    482542    this._endsWithInnerRaw = true;
    483543    this._indentInnerComments = true;
    484544  }
    485   printInnerComments() {
     545  printInnerComments(nextToken) {
    486546    const node = this._currentNode;
    487547    const comments = node.innerComments;
     
    491551    const printedCommentsCount = this._printedComments.size;
    492552    if (indent) this.indent();
    493     this._printComments(1, comments, node);
     553    this._printComments(1, comments, node, undefined, undefined, nextToken);
    494554    if (hasSpace && printedCommentsCount !== this._printedComments.size) {
    495555      this.space();
     
    500560    this._indentInnerComments = false;
    501561  }
    502   printSequence(nodes, parent, opts = {}) {
     562  printSequence(nodes, opts = {}) {
    503563    var _opts$indent;
    504564    opts.statement = true;
    505565    (_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 = {}) {
    509569    if (opts.separator == null) {
    510570      opts.separator = commaSeparator;
    511571    }
    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], ",");
    513579  }
    514580  _printNewline(newLine, opts) {
     
    535601    }
    536602  }
    537   _shouldPrintComment(comment) {
     603  _shouldPrintComment(comment, nextToken) {
    538604    if (comment.ignore) return 0;
    539605    if (this._printedComments.has(comment)) return 0;
    540606    if (this._noLineTerminator && HAS_NEWLINE_OR_BlOCK_COMMENT_END.test(comment.value)) {
    541607      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      }
    542614    }
    543615    this._printedComments.add(comment);
     
    555627    }
    556628    const lastCharCode = this.getLastChar();
    557     if (lastCharCode !== 91 && lastCharCode !== 123) {
     629    if (lastCharCode !== 91 && lastCharCode !== 123 && lastCharCode !== 40) {
    558630      this.space();
    559631    }
    560632    let val;
    561633    if (isBlockComment) {
    562       const {
    563         _parenPushNewlineState
    564       } = 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       }
    570634      val = `/*${comment.value}*/`;
    571635      if (this.format.indent.adjustMultilineComment) {
     
    591655      val = `/*${comment.value}*/`;
    592656    }
    593     if (this.endsWith(47)) this._space();
     657    if (this._endsWithDiv) this._space();
    594658    this.source("start", comment.loc);
    595659    this._append(val, isBlockComment);
     
    601665    }
    602666  }
    603   _printComments(type, comments, node, parent, lineOffset = 0) {
     667  _printComments(type, comments, node, parent, lineOffset = 0, nextToken) {
    604668    const nodeLoc = node.loc;
    605669    const len = comments.length;
     
    612676    for (let i = 0; i < len; i++) {
    613677      const comment = comments[i];
    614       const shouldPrint = this._shouldPrintComment(comment);
     678      const shouldPrint = this._shouldPrintComment(comment, nextToken);
    615679      if (shouldPrint === 2) {
    616680        hasLoc = false;
     
    685749}
    686750var _default = exports.default = Printer;
    687 function commaSeparator() {
    688   this.tokenChar(44);
    689   this.space();
     751function commaSeparator(occurrenceCount, last) {
     752  this.token(",", false, occurrenceCount);
     753  if (!last) this.space();
    690754}
    691755
Note: See TracChangeset for help on using the changeset viewer.