Fixed disappearing comments following a trailing comma on the last property of an object literal or the last argument of a call expression (#478)

This commit is contained in:
aardito2
2017-05-22 17:02:02 -04:00
committed by Henry Zhu
parent 0f66dbd4b6
commit 557696da3a
7 changed files with 772 additions and 1 deletions

View File

@@ -45,7 +45,7 @@ export default class CommentsParser extends BaseParser {
const stack = this.state.commentStack;
let lastChild, trailingComments, i, j;
let firstChild, lastChild, trailingComments, i, j;
if (this.state.trailingComments.length > 0) {
// If the first comment in trailingComments comes after the
@@ -73,10 +73,41 @@ export default class CommentsParser extends BaseParser {
}
// Eating the stack.
if (stack.length > 0 && last(stack).start >= node.start) {
firstChild = stack.pop();
}
while (stack.length > 0 && last(stack).start >= node.start) {
lastChild = stack.pop();
}
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
// as trailing comments
if (firstChild &&
(firstChild.type === "ObjectProperty" ||
(node.type === "CallExpression")) &&
this.state.leadingComments.length > 0) {
const lastComment = last(this.state.leadingComments);
if (lastComment.start >= node.start) {
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 = [];
}
}
}
}
if (lastChild) {
if (lastChild.leadingComments) {
if (lastChild !== node && last(lastChild.leadingComments).end <= node.start) {