node_modules/@babel/generator/lib/printer.js in immosquare-cleaner-0.1.47 vs node_modules/@babel/generator/lib/printer.js in immosquare-cleaner-0.1.48

- old
+ new

@@ -22,29 +22,39 @@ const { needsParens } = n; class Printer { constructor(format, map) { - this.inForStatementInitCounter = 0; - this._printStack = []; + this.inForStatementInit = false; + this.tokenContext = 0; + this._currentNode = null; this._indent = 0; this._indentRepeat = 0; this._insideAux = false; this._parenPushNewlineState = null; this._noLineTerminator = false; this._printAuxAfterOnNextUserNode = false; this._printedComments = new Set(); this._endsWithInteger = false; this._endsWithWord = false; + this._endsWithDiv = false; this._lastCommentLine = 0; this._endsWithInnerRaw = false; this._indentInnerComments = true; this.format = format; this._indentRepeat = format.indent.style.length; this._inputMap = map == null ? void 0 : map._inputMap; this._buf = new _buffer.default(map, format.indent.style[0]); } + enterForStatementInit(val) { + const old = this.inForStatementInit; + if (old === val) return () => {}; + this.inForStatementInit = val; + return () => { + this.inForStatementInit = old; + }; + } generate(ast) { this.print(ast); this._maybeAddAuxComment(); return this._buf.get(); } @@ -86,12 +96,13 @@ this._space(); } } } word(str, noLineTerminatorAfter = false) { + this.tokenContext = 0; this._maybePrintInnerComments(); - if (this._endsWithWord || str.charCodeAt(0) === 47 && this.endsWith(47)) { + if (this._endsWithWord || this._endsWithDiv && str.charCodeAt(0) === 47) { this._space(); } this._maybeAddAuxComment(); this._append(str, false); this._endsWithWord = true; @@ -107,10 +118,11 @@ } this.word(str); this._endsWithInteger = Number.isInteger(number) && !isNonDecimalLiteral(str) && !SCIENTIFIC_NOTATION.test(str) && !ZERO_DECIMAL_INTEGER.test(str) && str.charCodeAt(str.length - 1) !== 46; } token(str, maybeNewline = false) { + this.tokenContext = 0; this._maybePrintInnerComments(); const lastChar = this.getLastChar(); const strFirst = str.charCodeAt(0); if (lastChar === 33 && (str === "--" || strFirst === 61) || strFirst === 43 && lastChar === 43 || strFirst === 45 && lastChar === 45 || strFirst === 46 && this._endsWithInteger) { this._space(); @@ -118,10 +130,11 @@ this._maybeAddAuxComment(); this._append(str, maybeNewline); this._noLineTerminator = false; } tokenChar(char) { + this.tokenContext = 0; this._maybePrintInnerComments(); const lastChar = this.getLastChar(); if (char === 43 && lastChar === 43 || char === 45 && lastChar === 45 || char === 46 && this._endsWithInteger) { this._space(); } @@ -173,18 +186,10 @@ sourceWithOffset(prop, loc, columnOffset) { if (!loc) return; this._catchUp(prop, loc); this._buf.sourceWithOffset(prop, loc, columnOffset); } - withSource(prop, loc, cb) { - if (!loc) { - cb(); - return; - } - this._catchUp(prop, loc); - this._buf.withSource(prop, loc, cb); - } sourceIdentifierName(identifierName, pos) { if (!this._buf._canMarkIdName) return; const sourcePosition = this._buf._sourcePosition; sourcePosition.identifierNamePos = pos; sourcePosition.identifierName = identifierName; @@ -199,17 +204,19 @@ this._maybeAddParen(str); this._maybeIndent(str.charCodeAt(0)); this._buf.append(str, maybeNewline); this._endsWithWord = false; this._endsWithInteger = false; + this._endsWithDiv = false; } _appendChar(char) { this._maybeAddParenChar(char); this._maybeIndent(char); this._buf.appendChar(char); this._endsWithWord = false; this._endsWithInteger = false; + this._endsWithDiv = false; } _queue(char) { this._maybeAddParenChar(char); this._maybeIndent(char); this._buf.queue(char); @@ -289,25 +296,25 @@ return this._indentRepeat * this._indent; } printTerminatorless(node, parent, isLabel) { if (isLabel) { this._noLineTerminator = true; - this.print(node, parent); + this.print(node); } else { const terminatorState = { printed: false }; this._parenPushNewlineState = terminatorState; - this.print(node, parent); + this.print(node); if (terminatorState.printed) { this.dedent(); this.newline(); this.tokenChar(41); } } } - print(node, parent, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) { + print(node, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) { var _node$extra, _node$leadingComments; if (!node) return; this._endsWithInnerRaw = false; const nodeType = node.type; const format = this.format; @@ -317,16 +324,17 @@ } const printMethod = this[nodeType]; if (printMethod === undefined) { throw new ReferenceError(`unknown node of type ${JSON.stringify(nodeType)} with constructor ${JSON.stringify(node.constructor.name)}`); } - this._printStack.push(node); + const parent = this._currentNode; + this._currentNode = node; const oldInAux = this._insideAux; this._insideAux = node.loc == null; this._maybeAddAuxComment(this._insideAux && !oldInAux); const parenthesized = (_node$extra = node.extra) == null ? void 0 : _node$extra.parenthesized; - let shouldPrintParens = forceParens || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this._printStack); + let shouldPrintParens = forceParens || parenthesized && format.retainFunctionParens && nodeType === "FunctionExpression" || needsParens(node, parent, this.tokenContext, this.inForStatementInit); if (!shouldPrintParens && parenthesized && (_node$leadingComments = node.leadingComments) != null && _node$leadingComments.length && node.leadingComments[0].type === "CommentBlock") { const parentType = parent == null ? void 0 : parent.type; switch (parentType) { case "ExpressionStatement": case "VariableDeclarator": @@ -339,29 +347,32 @@ if (parent.callee !== node) break; default: shouldPrintParens = true; } } + let exitInForStatementInit; if (shouldPrintParens) { this.tokenChar(40); this._endsWithInnerRaw = false; + exitInForStatementInit = this.enterForStatementInit(false); } this._lastCommentLine = 0; this._printLeadingComments(node, parent); const loc = nodeType === "Program" || nodeType === "File" ? null : node.loc; this.exactSource(loc, printMethod.bind(this, node, parent)); if (shouldPrintParens) { this._printTrailingComments(node, parent); this.tokenChar(41); this._noLineTerminator = noLineTerminatorAfter; + exitInForStatementInit(); } else if (noLineTerminatorAfter && !this._noLineTerminator) { this._noLineTerminator = true; this._printTrailingComments(node, parent); } else { this._printTrailingComments(node, parent, trailingCommentsLineOffset); } - this._printStack.pop(); + this._currentNode = parent; format.concise = oldConcise; this._insideAux = oldInAux; this._endsWithInnerRaw = false; } _maybeAddAuxComment(enteredPositionlessNode) { @@ -417,11 +428,11 @@ const len = nodes.length; for (let i = 0; i < len; i++) { const node = nodes[i]; if (!node) continue; if (opts.statement) this._printNewline(i === 0, newlineOpts); - this.print(node, parent, undefined, opts.trailingCommentsLineOffset || 0); + this.print(node, undefined, opts.trailingCommentsLineOffset || 0); opts.iterator == null || opts.iterator(node, i); if (i < len - 1) separator == null || separator(); if (opts.statement) { var _node$trailingComment; if (!((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.length)) { @@ -437,22 +448,22 @@ } } } if (indent) this.dedent(); } - printAndIndentOnComments(node, parent) { + printAndIndentOnComments(node) { const indent = node.leadingComments && node.leadingComments.length > 0; if (indent) this.indent(); - this.print(node, parent); + this.print(node); if (indent) this.dedent(); } printBlock(parent) { const node = parent.body; if (node.type !== "EmptyStatement") { this.space(); } - this.print(node, parent); + this.print(node); } _printTrailingComments(node, parent, lineOffset) { const { innerComments, trailingComments @@ -473,11 +484,11 @@ if (this._endsWithInnerRaw) this.printInnerComments(); this._endsWithInnerRaw = true; this._indentInnerComments = true; } printInnerComments() { - const node = this._printStack[this._printStack.length - 1]; + const node = this._currentNode; const comments = node.innerComments; if (!(comments != null && comments.length)) return; const hasSpace = this.endsWith(32); const indent = this._indentInnerComments; const printedCommentsCount = this._printedComments.size; @@ -580,10 +591,10 @@ } else if (!noLineTerminator) { val = `//${comment.value}`; } else { val = `/*${comment.value}*/`; } - if (this.endsWith(47)) this._space(); + if (this._endsWithDiv) this._space(); this.source("start", comment.loc); this._append(val, isBlockComment); if (!isBlockComment && !noLineTerminator) { this.newline(1, true); }