summaryrefslogtreecommitdiff
path: root/node_modules/@babel/generator/lib/printer.js
diff options
context:
space:
mode:
authorlittleblack111 <littleblack11111@gmail.com>2024-10-13 10:37:19 +0800
committerlittleblack111 <littleblack11111@gmail.com>2024-10-13 10:37:19 +0800
commite29b244aeab08ea36fb385c7beb2ad0560217039 (patch)
treec66b744dc29573f1acc871862937a23f1b80e6bf /node_modules/@babel/generator/lib/printer.js
parent62c2881629e6413a8fec6d3dd092e4568e2a81dc (diff)
bump: @babel/cli 7.25.6 → 7.25.7, @babel/core 7.25.2 → 7.25.8, @babel/preset-env 7.25.4 → 7.25.8, postcss 8.4.41 → 8.4.47, swup 4.7.0 → 4.8.1
Diffstat (limited to 'node_modules/@babel/generator/lib/printer.js')
-rw-r--r--node_modules/@babel/generator/lib/printer.js146
1 files changed, 64 insertions, 82 deletions
diff --git a/node_modules/@babel/generator/lib/printer.js b/node_modules/@babel/generator/lib/printer.js
index caf822ad..0315a264 100644
--- a/node_modules/@babel/generator/lib/printer.js
+++ b/node_modules/@babel/generator/lib/printer.js
@@ -9,6 +9,7 @@ var n = require("./node/index.js");
var _t = require("@babel/types");
var generatorFunctions = require("./generators/index.js");
const {
+ isExpression,
isFunction,
isStatement,
isClassBody,
@@ -19,6 +20,9 @@ const SCIENTIFIC_NOTATION = /e/i;
const ZERO_DECIMAL_INTEGER = /\.0+$/;
const HAS_NEWLINE = /[\n\r\u2028\u2029]/;
const HAS_NEWLINE_OR_BlOCK_COMMENT_END = /[\n\r\u2028\u2029]|\*\//;
+function commentIsNewline(c) {
+ return c.type === "CommentLine" || HAS_NEWLINE.test(c.value);
+}
const {
needsParens
} = n;
@@ -30,8 +34,8 @@ class Printer {
this._indent = 0;
this._indentRepeat = 0;
this._insideAux = false;
- this._parenPushNewlineState = null;
this._noLineTerminator = false;
+ this._noLineTerminatorAfterNode = null;
this._printAuxAfterOnNextUserNode = false;
this._printedComments = new Set();
this._endsWithInteger = false;
@@ -45,12 +49,24 @@ class Printer {
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;
+ enterForStatementInit() {
+ if (this.inForStatementInit) return () => {};
+ this.inForStatementInit = true;
+ return () => {
+ this.inForStatementInit = false;
+ };
+ }
+ enterDelimited() {
+ const oldInForStatementInit = this.inForStatementInit;
+ const oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
+ if (oldInForStatementInit === false && oldNoLineTerminatorAfterNode === null) {
+ return () => {};
+ }
+ this.inForStatementInit = false;
+ this._noLineTerminatorAfterNode = null;
return () => {
- this.inForStatementInit = old;
+ this.inForStatementInit = oldInForStatementInit;
+ this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
};
}
generate(ast) {
@@ -201,7 +217,6 @@ class Printer {
this._queue(10);
}
_append(str, maybeNewline) {
- this._maybeAddParen(str);
this._maybeIndent(str.charCodeAt(0));
this._buf.append(str, maybeNewline);
this._endsWithWord = false;
@@ -209,7 +224,6 @@ class Printer {
this._endsWithDiv = false;
}
_appendChar(char) {
- this._maybeAddParenChar(char);
this._maybeIndent(char);
this._buf.appendChar(char);
this._endsWithWord = false;
@@ -217,7 +231,6 @@ class Printer {
this._endsWithDiv = false;
}
_queue(char) {
- this._maybeAddParenChar(char);
this._maybeIndent(char);
this._buf.queue(char);
this._endsWithWord = false;
@@ -233,47 +246,6 @@ class Printer {
return true;
}
}
- _maybeAddParenChar(char) {
- const parenPushNewlineState = this._parenPushNewlineState;
- if (!parenPushNewlineState) return;
- if (char === 32) {
- return;
- }
- if (char !== 10) {
- this._parenPushNewlineState = null;
- return;
- }
- this.tokenChar(40);
- this.indent();
- parenPushNewlineState.printed = true;
- }
- _maybeAddParen(str) {
- const parenPushNewlineState = this._parenPushNewlineState;
- if (!parenPushNewlineState) return;
- const len = str.length;
- let i;
- for (i = 0; i < len && str.charCodeAt(i) === 32; i++) continue;
- if (i === len) {
- return;
- }
- const cha = str.charCodeAt(i);
- if (cha !== 10) {
- if (cha !== 47 || i + 1 === len) {
- this._parenPushNewlineState = null;
- return;
- }
- const chaPost = str.charCodeAt(i + 1);
- if (chaPost === 42) {
- return;
- } else if (chaPost !== 47) {
- this._parenPushNewlineState = null;
- return;
- }
- }
- this.tokenChar(40);
- this.indent();
- parenPushNewlineState.printed = true;
- }
catchUp(line) {
if (!this.format.retainLines) return;
const count = line - this._buf.getCurrentLine();
@@ -295,25 +267,12 @@ class Printer {
_getIndent() {
return this._indentRepeat * this._indent;
}
- printTerminatorless(node, isLabel) {
- if (isLabel) {
- this._noLineTerminator = true;
- this.print(node);
- } else {
- const terminatorState = {
- printed: false
- };
- this._parenPushNewlineState = terminatorState;
- this.print(node);
- if (terminatorState.printed) {
- this.dedent();
- this.newline();
- this.tokenChar(41);
- }
- }
+ printTerminatorless(node) {
+ this._noLineTerminator = true;
+ this.print(node);
}
- print(node, noLineTerminatorAfter, trailingCommentsLineOffset, forceParens) {
- var _node$extra, _node$leadingComments;
+ print(node, noLineTerminatorAfter, trailingCommentsLineOffset) {
+ var _node$extra, _node$leadingComments, _node$leadingComments2;
if (!node) return;
this._endsWithInnerRaw = false;
const nodeType = node.type;
@@ -332,7 +291,7 @@ class Printer {
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.tokenContext, this.inForStatementInit);
+ let shouldPrintParens = 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) {
@@ -349,11 +308,35 @@ class Printer {
shouldPrintParens = true;
}
}
- let exitInForStatementInit;
+ let indentParenthesized = false;
+ 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())) {
+ shouldPrintParens = true;
+ indentParenthesized = true;
+ }
+ let oldNoLineTerminatorAfterNode;
+ let oldInForStatementInitWasTrue;
+ if (!shouldPrintParens) {
+ noLineTerminatorAfter || (noLineTerminatorAfter = parent && this._noLineTerminatorAfterNode === parent && n.isLastChild(parent, node));
+ if (noLineTerminatorAfter) {
+ var _node$trailingComment;
+ if ((_node$trailingComment = node.trailingComments) != null && _node$trailingComment.some(commentIsNewline)) {
+ if (isExpression(node)) shouldPrintParens = true;
+ } else {
+ oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
+ this._noLineTerminatorAfterNode = node;
+ }
+ }
+ }
if (shouldPrintParens) {
this.tokenChar(40);
+ if (indentParenthesized) this.indent();
this._endsWithInnerRaw = false;
- exitInForStatementInit = this.enterForStatementInit(false);
+ if (this.inForStatementInit) {
+ oldInForStatementInitWasTrue = true;
+ this.inForStatementInit = false;
+ }
+ oldNoLineTerminatorAfterNode = this._noLineTerminatorAfterNode;
+ this._noLineTerminatorAfterNode = null;
}
this._lastCommentLine = 0;
this._printLeadingComments(node, parent);
@@ -361,9 +344,13 @@ class Printer {
this.exactSource(loc, printMethod.bind(this, node, parent));
if (shouldPrintParens) {
this._printTrailingComments(node, parent);
+ if (indentParenthesized) {
+ this.dedent();
+ this.newline();
+ }
this.tokenChar(41);
this._noLineTerminator = noLineTerminatorAfter;
- exitInForStatementInit();
+ if (oldInForStatementInitWasTrue) this.inForStatementInit = true;
} else if (noLineTerminatorAfter && !this._noLineTerminator) {
this._noLineTerminator = true;
this._printTrailingComments(node, parent);
@@ -373,6 +360,9 @@ class Printer {
this._currentNode = parent;
format.concise = oldConcise;
this._insideAux = oldInAux;
+ if (oldNoLineTerminatorAfterNode !== undefined) {
+ this._noLineTerminatorAfterNode = oldNoLineTerminatorAfterNode;
+ }
this._endsWithInnerRaw = false;
}
_maybeAddAuxComment(enteredPositionlessNode) {
@@ -434,8 +424,8 @@ class Printer {
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)) {
+ var _node$trailingComment2;
+ if (!((_node$trailingComment2 = node.trailingComments) != null && _node$trailingComment2.length)) {
this._lastCommentLine = 0;
}
if (i + 1 === len) {
@@ -562,14 +552,6 @@ class Printer {
}
let val;
if (isBlockComment) {
- const {
- _parenPushNewlineState
- } = this;
- if ((_parenPushNewlineState == null ? void 0 : _parenPushNewlineState.printed) === false && HAS_NEWLINE.test(comment.value)) {
- this.tokenChar(40);
- this.indent();
- _parenPushNewlineState.printed = true;
- }
val = `/*${comment.value}*/`;
if (this.format.indent.adjustMultilineComment) {
var _comment$loc;