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

Retain trailing comments in array expressions #10369

Merged
merged 5 commits into from Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
20 changes: 19 additions & 1 deletion packages/babel-parser/src/parser/comments.js
Expand Up @@ -86,7 +86,7 @@ export default class CommentsParser extends BaseParser {

// Attach comments that follow a trailing comma on the last
// property in an object literal or a trailing comma in function arguments
// as trailing comments
// or a trailing comma in array expressions as trailing comments
if (firstChild && this.state.leadingComments.length > 0) {
const lastComment = last(this.state.leadingComments);

Expand Down Expand Up @@ -137,6 +137,24 @@ export default class CommentsParser extends BaseParser {
}
}
}
} else if (node.type === "ArrayExpression" && node.elements.length > 0) {
const lastElement = last(node.elements);
banga marked this conversation as resolved.
Show resolved Hide resolved
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) {
lastElement.trailingComments = this.state.leadingComments;
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
this.state.leadingComments = [];
}
}
}
}

Expand Down
@@ -0,0 +1,13 @@
const trailing = [
banga marked this conversation as resolved.
Show resolved Hide resolved
"One", // One
// Two
"Two", // Three
// Four
]

const nonTrailing = [
"One", // One
// Two
"Two" // Three
// Four
]