Skip to content
This repository has been archived by the owner on May 19, 2018. It is now read-only.

Fixed disappearing comments following a trailing comma #478

Merged
merged 1 commit into from
May 22, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/parser/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn(a, { b }, /* comment */);
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
{
"type": "File",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"program": {
"type": "Program",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"sourceType": "script",
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 28,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 28
}
},
"expression": {
"type": "CallExpression",
"start": 0,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 27
}
},
"callee": {
"type": "Identifier",
"start": 0,
"end": 2,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 2
},
"identifierName": "fn"
},
"name": "fn"
},
"arguments": [
{
"type": "Identifier",
"start": 3,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 3
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "a"
},
"name": "a"
},
{
"type": "ObjectExpression",
"start": 6,
"end": 11,
"loc": {
"start": {
"line": 1,
"column": 6
},
"end": {
"line": 1,
"column": 11
}
},
"properties": [
{
"type": "ObjectProperty",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
}
},
"method": false,
"shorthand": true,
"computed": false,
"key": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "b"
},
"name": "b"
},
"value": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "b"
},
"name": "b"
},
"extra": {
"shorthand": true
}
}
],
"trailingComments": [
{
"type": "CommentBlock",
"value": " comment ",
"start": 13,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 26
}
}
}
]
}
]
}
}
],
"directives": []
},
"comments": [
{
"type": "CommentBlock",
"value": " comment ",
"start": 13,
"end": 26,
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 26
}
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn(a, b, /* comment */);
Loading