Refactor trailing comma comment adjustment (#10380)
* Refactor trailing comment adjustment Following up from https://github.com/babel/babel/pull/10369 - Unify the logic for adjusting trailing comments into a separate function - Use it for all three cases, which fixes a bug for ObjectExpressions and CallExpressions - Update tests to check for the fixed bug * Fix tests - Only modify trailingComments if necessary - Update snapshots of a couple of affected tests * Drop the underscore in adjustCommentsAfterTrailingComma_ * Handle ArrayPattern * Handle ObjectPattern * Handle import and export declarations These have to be handled a bit differently, because the node is visited after the and before the declaration. So we intercept when we are going from the last specifier to the source node. * Remove unnecessary check
This commit is contained in:
committed by
Huáng Jùnliàng
parent
e5afa57cca
commit
c0e3fa0081
@@ -38,6 +38,45 @@ export default class CommentsParser extends BaseParser {
|
||||
this.state.leadingComments.push(comment);
|
||||
}
|
||||
|
||||
adjustCommentsAfterTrailingComma(node: Node, elements: Node[]) {
|
||||
if (elements.length === 0) {
|
||||
return;
|
||||
}
|
||||
if (this.state.leadingComments.length === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
const lastElement = last(elements);
|
||||
|
||||
for (let j = 0; j < this.state.leadingComments.length; j++) {
|
||||
if (
|
||||
this.state.leadingComments[j].end < this.state.commentPreviousNode.end
|
||||
) {
|
||||
this.state.leadingComments.splice(j, 1);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
const newTrailingComments = [];
|
||||
while (this.state.leadingComments.length) {
|
||||
const leadingComment = this.state.leadingComments.shift();
|
||||
if (leadingComment.end < node.end) {
|
||||
newTrailingComments.push(leadingComment);
|
||||
} else {
|
||||
if (node.trailingComments === undefined) {
|
||||
node.trailingComments = [];
|
||||
}
|
||||
node.trailingComments.push(leadingComment);
|
||||
}
|
||||
}
|
||||
|
||||
if (newTrailingComments.length > 0) {
|
||||
lastElement.trailingComments = newTrailingComments;
|
||||
} else if (lastElement.trailingComments !== undefined) {
|
||||
lastElement.trailingComments = [];
|
||||
}
|
||||
}
|
||||
|
||||
processComment(node: Node): void {
|
||||
if (node.type === "Program" && node.body.length > 0) return;
|
||||
|
||||
@@ -84,86 +123,33 @@ export default class CommentsParser extends BaseParser {
|
||||
|
||||
if (!lastChild && firstChild) lastChild = firstChild;
|
||||
|
||||
// Attach comments that follow a trailing comma on the last
|
||||
// property in an object literal or a trailing comma in function arguments
|
||||
// or a trailing comma in array expressions as trailing comments
|
||||
if (firstChild && this.state.leadingComments.length > 0) {
|
||||
const lastComment = last(this.state.leadingComments);
|
||||
|
||||
if (firstChild.type === "ObjectProperty") {
|
||||
if (lastComment.start >= node.start && lastComment.end <= node.end) {
|
||||
if (this.state.commentPreviousNode) {
|
||||
for (j = 0; j < this.state.leadingComments.length; j++) {
|
||||
if (
|
||||
this.state.leadingComments[j].end <
|
||||
this.state.commentPreviousNode.end
|
||||
) {
|
||||
this.state.leadingComments.splice(j, 1);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.state.leadingComments.length > 0) {
|
||||
firstChild.trailingComments = this.state.leadingComments;
|
||||
this.state.leadingComments = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (
|
||||
node.type === "CallExpression" &&
|
||||
node.arguments &&
|
||||
node.arguments.length
|
||||
) {
|
||||
const lastArg = last(node.arguments);
|
||||
|
||||
if (
|
||||
lastArg &&
|
||||
lastComment.start >= lastArg.start &&
|
||||
lastComment.end <= node.end
|
||||
) {
|
||||
if (this.state.commentPreviousNode) {
|
||||
for (j = 0; j < this.state.leadingComments.length; j++) {
|
||||
if (
|
||||
this.state.leadingComments[j].end <
|
||||
this.state.commentPreviousNode.end
|
||||
) {
|
||||
this.state.leadingComments.splice(j, 1);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
if (this.state.leadingComments.length > 0) {
|
||||
lastArg.trailingComments = this.state.leadingComments;
|
||||
this.state.leadingComments = [];
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (node.type === "ArrayExpression" && node.elements.length > 0) {
|
||||
if (this.state.commentPreviousNode) {
|
||||
for (j = 0; j < this.state.leadingComments.length; j++) {
|
||||
if (
|
||||
this.state.leadingComments[j].end <
|
||||
this.state.commentPreviousNode.end
|
||||
) {
|
||||
this.state.leadingComments.splice(j, 1);
|
||||
j--;
|
||||
}
|
||||
}
|
||||
|
||||
const lastElement = last(node.elements);
|
||||
lastElement.trailingComments = [];
|
||||
while (this.state.leadingComments.length) {
|
||||
const leadingComment = this.state.leadingComments.shift();
|
||||
if (leadingComment.end < node.end) {
|
||||
lastElement.trailingComments.push(leadingComment);
|
||||
} else {
|
||||
if (node.trailingComments === undefined) {
|
||||
node.trailingComments = [];
|
||||
}
|
||||
node.trailingComments.push(leadingComment);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Adjust comments that follow a trailing comma on the last element in a
|
||||
// comma separated list of nodes to be the trailing comments on the last
|
||||
// element
|
||||
if (firstChild) {
|
||||
switch (node.type) {
|
||||
case "ObjectExpression":
|
||||
case "ObjectPattern":
|
||||
this.adjustCommentsAfterTrailingComma(node, node.properties);
|
||||
break;
|
||||
case "CallExpression":
|
||||
this.adjustCommentsAfterTrailingComma(node, node.arguments);
|
||||
break;
|
||||
case "ArrayExpression":
|
||||
case "ArrayPattern":
|
||||
this.adjustCommentsAfterTrailingComma(node, node.elements);
|
||||
break;
|
||||
}
|
||||
} else if (
|
||||
this.state.commentPreviousNode &&
|
||||
((this.state.commentPreviousNode.type === "ImportSpecifier" &&
|
||||
node.type !== "ImportSpecifier") ||
|
||||
(this.state.commentPreviousNode.type === "ExportSpecifier" &&
|
||||
node.type !== "ExportSpecifier"))
|
||||
) {
|
||||
this.adjustCommentsAfterTrailingComma(node, [
|
||||
this.state.commentPreviousNode,
|
||||
]);
|
||||
}
|
||||
|
||||
if (lastChild) {
|
||||
|
||||
Reference in New Issue
Block a user