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

Leave trailing comments after handling a possible trailing comma #10445

Merged
merged 2 commits into from Sep 23, 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
41 changes: 33 additions & 8 deletions packages/babel-parser/src/parser/comments.js
Expand Up @@ -38,7 +38,19 @@ export default class CommentsParser extends BaseParser {
this.state.leadingComments.push(comment);
}

adjustCommentsAfterTrailingComma(node: Node, elements: Node[]) {
adjustCommentsAfterTrailingComma(
node: Node,
elements: Node[],
// When the current node is followed by a token which hasn't a respective AST node, we
// need to take all the trailing comments to prevent them from being attached to an
// unrelated node. e.g. in
// var { x } /* cmt */ = { y }
// we don't want /* cmt */ to be attached to { y }.
// On the other hand, in
// fn(x) [new line] /* cmt */ [new line] y
// /* cmt */ is both a trailing comment of fn(x) and a leading comment of y
takeAllComments?: boolean,
) {
if (this.state.leadingComments.length === 0) {
return;
}
Expand All @@ -59,17 +71,24 @@ export default class CommentsParser extends BaseParser {
}

const newTrailingComments = [];
while (this.state.leadingComments.length) {
const leadingComment = this.state.leadingComments.shift();
for (let i = 0; i < this.state.leadingComments.length; i++) {
const leadingComment = this.state.leadingComments[i];
if (leadingComment.end < node.end) {
newTrailingComments.push(leadingComment);

// Perf: we don't need to splice if we are going to reset the array anyway
if (!takeAllComments) {
this.state.leadingComments.splice(i, 1);
i--;
}
} else {
if (node.trailingComments === undefined) {
node.trailingComments = [];
}
node.trailingComments.push(leadingComment);
}
}
if (takeAllComments) this.state.leadingComments = [];

if (newTrailingComments.length > 0) {
lastElement.trailingComments = newTrailingComments;
Expand Down Expand Up @@ -130,16 +149,20 @@ export default class CommentsParser extends BaseParser {
if (firstChild) {
switch (node.type) {
case "ObjectExpression":
case "ObjectPattern":
this.adjustCommentsAfterTrailingComma(node, node.properties);
break;
case "ObjectPattern":
this.adjustCommentsAfterTrailingComma(node, node.properties, true);
break;
case "CallExpression":
this.adjustCommentsAfterTrailingComma(node, node.arguments);
break;
case "ArrayExpression":
case "ArrayPattern":
this.adjustCommentsAfterTrailingComma(node, node.elements);
break;
case "ArrayPattern":
this.adjustCommentsAfterTrailingComma(node, node.elements, true);
break;
}
} else if (
this.state.commentPreviousNode &&
Expand All @@ -148,9 +171,11 @@ export default class CommentsParser extends BaseParser {
(this.state.commentPreviousNode.type === "ExportSpecifier" &&
node.type !== "ExportSpecifier"))
) {
this.adjustCommentsAfterTrailingComma(node, [
this.state.commentPreviousNode,
]);
this.adjustCommentsAfterTrailingComma(
node,
[this.state.commentPreviousNode],
true,
);
}

if (lastChild) {
Expand Down
Expand Up @@ -250,7 +250,25 @@
"label": null
}
],
"test": null
"test": null,
"leadingComments": [
{
"type": "CommentLine",
"value": " comment",
"start": 47,
"end": 57,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 14
}
}
}
]
}
]
}
Expand Down
Expand Up @@ -215,7 +215,25 @@
"identifierName": "B"
},
"name": "B"
}
},
"leadingComments": [
{
"type": "CommentLine",
"value": " Two",
"start": 27,
"end": 33,
"loc": {
"start": {
"line": 6,
"column": 0
},
"end": {
"line": 6,
"column": 6
}
}
}
]
}
],
"directives": []
Expand Down
@@ -0,0 +1,5 @@
const socket = socketClient(address)
/* istanbul ignore next */
socket.on('connect', function () {
debug('Connected to ' + address)
})