@babel/generator
Advanced tools
Comparing version 7.20.1 to 7.20.2
@@ -109,3 +109,3 @@ "use strict"; | ||
this.tsPrintClassMemberModifiers(node); | ||
this.word("accessor"); | ||
this.word("accessor", true); | ||
this.space(); | ||
@@ -112,0 +112,0 @@ if (node.computed) { |
@@ -51,9 +51,7 @@ "use strict"; | ||
function DoExpression(node) { | ||
this.ensureNoLineTerminator(() => { | ||
if (node.async) { | ||
this.word("async"); | ||
this.space(); | ||
} | ||
this.word("do"); | ||
}); | ||
if (node.async) { | ||
this.word("async", true); | ||
this.space(); | ||
} | ||
this.word("do"); | ||
this.space(); | ||
@@ -200,7 +198,5 @@ this.print(node.body, node); | ||
function YieldExpression(node) { | ||
this.word("yield"); | ||
this.word("yield", true); | ||
if (node.delegate) { | ||
this.ensureNoLineTerminator(() => { | ||
this.tokenChar(42); | ||
}); | ||
this.tokenChar(42); | ||
if (node.argument) { | ||
@@ -289,7 +285,5 @@ this.space(); | ||
function ModuleExpression(node) { | ||
this.word("module"); | ||
this.word("module", true); | ||
this.space(); | ||
this.ensureNoLineTerminator(() => { | ||
this.tokenChar(123); | ||
}); | ||
this.tokenChar(123); | ||
this.indent(); | ||
@@ -296,0 +290,0 @@ const { |
@@ -23,2 +23,3 @@ "use strict"; | ||
this.tokenChar(41); | ||
this._noLineTerminator = true; | ||
this.print(node.returnType, node, node.type === "ArrowFunctionExpression"); | ||
@@ -55,8 +56,4 @@ } | ||
} | ||
const { | ||
_noLineTerminator | ||
} = this; | ||
if (node.async) { | ||
this._noLineTerminator = true; | ||
this.word("async"); | ||
this.word("async", true); | ||
this.space(); | ||
@@ -68,3 +65,2 @@ } | ||
this.tokenChar(42); | ||
this._noLineTerminator = _noLineTerminator; | ||
} | ||
@@ -74,3 +70,2 @@ } | ||
this.tokenChar(91); | ||
this._noLineTerminator = _noLineTerminator; | ||
this.print(key, node); | ||
@@ -80,3 +75,2 @@ this.tokenChar(93); | ||
this.print(key, node); | ||
this._noLineTerminator = _noLineTerminator; | ||
} | ||
@@ -89,3 +83,3 @@ if ( | ||
} | ||
function _predicate(node) { | ||
function _predicate(node, noLineTerminatorAfter) { | ||
if (node.predicate) { | ||
@@ -96,3 +90,3 @@ if (!node.returnType) { | ||
this.space(); | ||
this.print(node.predicate, node); | ||
this.print(node.predicate, node, noLineTerminatorAfter); | ||
} | ||
@@ -126,8 +120,4 @@ } | ||
function ArrowFunctionExpression(node) { | ||
const { | ||
_noLineTerminator | ||
} = this; | ||
if (node.async) { | ||
this._noLineTerminator = true; | ||
this.word("async"); | ||
this.word("async", true); | ||
this.space(); | ||
@@ -138,15 +128,11 @@ } | ||
if (!this.format.retainLines && node.params.length === 1 && isIdentifier(firstParam = node.params[0]) && !hasTypesOrComments(node, firstParam)) { | ||
this.print(firstParam, node); | ||
this._noLineTerminator = _noLineTerminator; | ||
this.print(firstParam, node, true); | ||
} else { | ||
this._noLineTerminator = _noLineTerminator; | ||
this._params(node); | ||
} | ||
this._predicate(node); | ||
this.ensureNoLineTerminator(() => { | ||
this.space(); | ||
this.printInnerComments(); | ||
this.token("=>"); | ||
}); | ||
this._predicate(node, true); | ||
this.space(); | ||
this.printInnerComments(); | ||
this.token("=>"); | ||
this.space(); | ||
this.print(node.body, node); | ||
@@ -153,0 +139,0 @@ } |
@@ -67,2 +67,3 @@ "use strict"; | ||
function _printAssertions(node) { | ||
this.word("assert"); | ||
this.space(); | ||
@@ -88,7 +89,4 @@ this.tokenChar(123); | ||
if ((_node$assertions = node.assertions) != null && _node$assertions.length) { | ||
this.ensureNoLineTerminator(() => { | ||
this.print(node.source, node); | ||
this.space(); | ||
this.word("assert"); | ||
}); | ||
this.print(node.source, node, true); | ||
this.space(); | ||
this._printAssertions(node); | ||
@@ -148,7 +146,4 @@ } else { | ||
if ((_node$assertions2 = node.assertions) != null && _node$assertions2.length) { | ||
this.ensureNoLineTerminator(() => { | ||
this.print(node.source, node); | ||
this.space(); | ||
this.word("assert"); | ||
}); | ||
this.print(node.source, node, true); | ||
this.space(); | ||
this._printAssertions(node); | ||
@@ -222,6 +217,3 @@ } else { | ||
this.print(node.source, node, true); | ||
this.ensureNoLineTerminator(() => { | ||
this.space(); | ||
this.word("assert"); | ||
}); | ||
this.space(); | ||
this._printAssertions(node); | ||
@@ -228,0 +220,0 @@ } else { |
@@ -241,9 +241,3 @@ "use strict"; | ||
} = node; | ||
this.word(kind); | ||
const { | ||
_noLineTerminator | ||
} = this; | ||
if (kind === "using") { | ||
this._noLineTerminator = true; | ||
} | ||
this.word(kind, kind === "using"); | ||
this.space(); | ||
@@ -259,10 +253,2 @@ let hasInits = false; | ||
let iterator; | ||
if (kind === "using") { | ||
iterator = (_, i) => { | ||
if (i === 0) { | ||
this._noLineTerminator = _noLineTerminator; | ||
} | ||
}; | ||
} | ||
this.printList(node.declarations, node, { | ||
@@ -273,3 +259,2 @@ separator: hasInits ? function () { | ||
} : undefined, | ||
iterator, | ||
indent: node.declarations.length > 1 ? true : false | ||
@@ -276,0 +261,0 @@ }); |
@@ -21,2 +21,3 @@ "use strict"; | ||
const PURE_ANNOTATION_RE = /^\s*[@#]__PURE__\s*$/; | ||
const HAS_NEWLINE = /[\n\r\u2028\u2029]/; | ||
const { | ||
@@ -70,2 +71,3 @@ needsParens | ||
} | ||
this._noLineTerminator = false; | ||
} | ||
@@ -92,3 +94,3 @@ | ||
word(str) { | ||
word(str, noLineTerminatorAfter = false) { | ||
this._maybePrintInnerComments(); | ||
@@ -102,2 +104,3 @@ | ||
this._endsWithWord = true; | ||
this._noLineTerminator = noLineTerminatorAfter; | ||
} | ||
@@ -123,2 +126,3 @@ | ||
this._append(str, maybeNewline); | ||
this._noLineTerminator = false; | ||
} | ||
@@ -136,2 +140,3 @@ tokenChar(char) { | ||
this._appendChar(char); | ||
this._noLineTerminator = false; | ||
} | ||
@@ -299,15 +304,6 @@ | ||
} | ||
ensureNoLineTerminator(fn) { | ||
const { | ||
_noLineTerminator | ||
} = this; | ||
this._noLineTerminator = true; | ||
fn(); | ||
this._noLineTerminator = _noLineTerminator; | ||
} | ||
printTerminatorless(node, parent, isLabel) { | ||
if (isLabel) { | ||
this.ensureNoLineTerminator(() => { | ||
this.print(node, parent); | ||
}); | ||
this._noLineTerminator = true; | ||
this.print(node, parent); | ||
} else { | ||
@@ -326,3 +322,3 @@ const terminatorState = { | ||
} | ||
print(node, parent, noLineTerminator, | ||
print(node, parent, noLineTerminatorAfter, | ||
trailingCommentsLineOffset, forceParens) { | ||
@@ -359,10 +355,12 @@ if (!node) return; | ||
this.exactSource(loc, printMethod.bind(this, node, parent)); | ||
if (noLineTerminator && !this._noLineTerminator) { | ||
if (shouldPrintParens) { | ||
this._printTrailingComments(node, parent); | ||
this.tokenChar(41); | ||
this._noLineTerminator = noLineTerminatorAfter; | ||
} else if (noLineTerminatorAfter && !this._noLineTerminator) { | ||
this._noLineTerminator = true; | ||
this._printTrailingComments(node, parent); | ||
this._noLineTerminator = false; | ||
} else { | ||
this._printTrailingComments(node, parent, trailingCommentsLineOffset); | ||
} | ||
if (shouldPrintParens) this.tokenChar(41); | ||
@@ -515,6 +513,10 @@ this._printStack.pop(); | ||
} | ||
_printComment(comment, skipNewLines) { | ||
if (comment.ignore) return; | ||
if (this._printedComments.has(comment)) return; | ||
if (!this.format.shouldPrintComment(comment.value)) return; | ||
if (comment.ignore) return false; | ||
if (this._printedComments.has(comment)) return false; | ||
if (this._noLineTerminator && HAS_NEWLINE.test(comment.value)) { | ||
return true; | ||
} | ||
if (!this.format.shouldPrintComment(comment.value)) return false; | ||
this._printedComments.add(comment); | ||
@@ -562,74 +564,79 @@ const isBlockComment = comment.type === "CommentBlock"; | ||
} | ||
return false; | ||
} | ||
_printComments(type, comments, node, parent, lineOffset = 0) { | ||
{ | ||
const nodeLoc = node.loc; | ||
const len = comments.length; | ||
let hasLoc = !!nodeLoc; | ||
const nodeStartLine = hasLoc ? nodeLoc.start.line : 0; | ||
const nodeEndLine = hasLoc ? nodeLoc.end.line : 0; | ||
let lastLine = 0; | ||
let leadingCommentNewline = 0; | ||
for (let i = 0; i < len; i++) { | ||
const comment = comments[i]; | ||
if (hasLoc && comment.loc && !this._printedComments.has(comment)) { | ||
const commentStartLine = comment.loc.start.line; | ||
const commentEndLine = comment.loc.end.line; | ||
if (type === 0) { | ||
let offset = 0; | ||
if (i === 0) { | ||
if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine != commentEndLine)) { | ||
offset = leadingCommentNewline = 1; | ||
} | ||
} else { | ||
offset = commentStartLine - lastLine; | ||
const nodeLoc = node.loc; | ||
const len = comments.length; | ||
let hasLoc = !!nodeLoc; | ||
const nodeStartLine = hasLoc ? nodeLoc.start.line : 0; | ||
const nodeEndLine = hasLoc ? nodeLoc.end.line : 0; | ||
let lastLine = 0; | ||
let leadingCommentNewline = 0; | ||
const { | ||
_noLineTerminator | ||
} = this; | ||
for (let i = 0; i < len; i++) { | ||
const comment = comments[i]; | ||
if (hasLoc && comment.loc && !this._printedComments.has(comment)) { | ||
const commentStartLine = comment.loc.start.line; | ||
const commentEndLine = comment.loc.end.line; | ||
if (type === 0) { | ||
let offset = 0; | ||
if (i === 0) { | ||
if (this._buf.hasContent() && (comment.type === "CommentLine" || commentStartLine != commentEndLine)) { | ||
offset = leadingCommentNewline = 1; | ||
} | ||
lastLine = commentEndLine; | ||
this.newline(offset); | ||
this._printComment(comment, 1); | ||
if (i + 1 === len) { | ||
this.newline(Math.max(nodeStartLine - lastLine, leadingCommentNewline)); | ||
lastLine = nodeStartLine; | ||
} | ||
} else if (type === 1) { | ||
const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine); | ||
lastLine = commentEndLine; | ||
this.newline(offset); | ||
this._printComment(comment, 1); | ||
if (i + 1 === len) { | ||
this.newline(Math.min(1, nodeEndLine - lastLine)); | ||
lastLine = nodeEndLine; | ||
} | ||
} else { | ||
const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine); | ||
lastLine = commentEndLine; | ||
this.newline(offset); | ||
this._printComment(comment, 1); | ||
offset = commentStartLine - lastLine; | ||
} | ||
lastLine = commentEndLine; | ||
if (!_noLineTerminator) this.newline(offset); | ||
this._printComment(comment, 1); | ||
if (!_noLineTerminator && i + 1 === len) { | ||
this.newline(Math.max(nodeStartLine - lastLine, leadingCommentNewline)); | ||
lastLine = nodeStartLine; | ||
} | ||
} else if (type === 1) { | ||
const offset = commentStartLine - (i === 0 ? nodeStartLine : lastLine); | ||
lastLine = commentEndLine; | ||
if (!_noLineTerminator) this.newline(offset); | ||
if (this._printComment(comment, 1)) break; | ||
if (!_noLineTerminator && i + 1 === len) { | ||
this.newline(Math.min(1, nodeEndLine - lastLine)); | ||
lastLine = nodeEndLine; | ||
} | ||
} else { | ||
hasLoc = false; | ||
if (len === 1) { | ||
const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line : !comment.value.includes("\n"); | ||
const shouldSkipNewline = singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent); | ||
if (type === 0) { | ||
this._printComment(comment, shouldSkipNewline && node.type !== "ObjectExpression" || singleLine && isFunction(parent, { | ||
body: node | ||
}) ? 1 : 0); | ||
} else if (shouldSkipNewline && type === 2) { | ||
this._printComment(comment, 1); | ||
} else { | ||
this._printComment(comment, 0); | ||
const offset = commentStartLine - (i === 0 ? nodeEndLine - lineOffset : lastLine); | ||
lastLine = commentEndLine; | ||
if (!_noLineTerminator) this.newline(offset); | ||
this._printComment(comment, 1); | ||
} | ||
} else { | ||
hasLoc = false; | ||
if (len === 1) { | ||
const singleLine = comment.loc ? comment.loc.start.line === comment.loc.end.line : !comment.value.includes("\n"); | ||
const shouldSkipNewline = singleLine && !isStatement(node) && !isClassBody(parent) && !isTSInterfaceBody(parent); | ||
if (type === 0) { | ||
this._printComment(comment, shouldSkipNewline && node.type !== "ObjectExpression" || singleLine && isFunction(parent, { | ||
body: node | ||
}) ? 1 : 0); | ||
} else if (shouldSkipNewline && type === 2) { | ||
if (this._printComment(comment, 1)) { | ||
break; | ||
} | ||
} else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody" && node.type !== "TSInterfaceBody") { | ||
this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0); | ||
} else { | ||
this._printComment(comment, 0); | ||
} | ||
} else if (type === 1 && !(node.type === "ObjectExpression" && node.properties.length > 1) && node.type !== "ClassBody" && node.type !== "TSInterfaceBody") { | ||
const skippedDueToNewlie = this._printComment(comment, i === 0 ? 2 : i === len - 1 ? 3 : 0); | ||
if (skippedDueToNewlie) break; | ||
} else { | ||
this._printComment(comment, 0); | ||
} | ||
} | ||
if (type === 2 && hasLoc && lastLine) { | ||
this._lastCommentLine = lastLine; | ||
} | ||
} | ||
if (type === 2 && hasLoc && lastLine) { | ||
this._lastCommentLine = lastLine; | ||
} | ||
} | ||
@@ -636,0 +643,0 @@ } |
{ | ||
"name": "@babel/generator", | ||
"version": "7.20.1", | ||
"version": "7.20.2", | ||
"description": "Turns an AST into code.", | ||
@@ -22,3 +22,3 @@ "author": "The Babel Team (https://babel.dev/team)", | ||
"dependencies": { | ||
"@babel/types": "^7.20.0", | ||
"@babel/types": "^7.20.2", | ||
"@jridgewell/gen-mapping": "^0.3.2", | ||
@@ -29,3 +29,3 @@ "jsesc": "^2.5.1" | ||
"@babel/helper-fixtures": "^7.19.4", | ||
"@babel/parser": "^7.20.1", | ||
"@babel/parser": "^7.20.2", | ||
"@jridgewell/trace-mapping": "^0.3.8", | ||
@@ -32,0 +32,0 @@ "@types/jsesc": "^2.5.0", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
452034
4608
Updated@babel/types@^7.20.2