Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor trailing comment adjustment #10380

Merged
merged 7 commits into from Sep 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
144 changes: 65 additions & 79 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also SequenceExpression? (I didn't check)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think SequenceExpression will allow trailing comma.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I skimmed through https://github.com/babel/babylon/blob/master/ast/spec.md and looks like ImportDeclaration and ArrayPattern also allow trailing commas. Should I add those?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeas please! And also ExportNamedDeclaration and ObjectPattern

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) {
Expand Down
@@ -0,0 +1,6 @@
const [
/* One */
x
/* Two */,
/* Three */
] /* Four */ = [];