Skip to content

Commit

Permalink
Fixed disappearing comments following a trailing comma on the last pr…
Browse files Browse the repository at this point in the history
…operty of an object literal or the last argument of a call expression (babel#478) (babel#551)
  • Loading branch information
danez committed May 31, 2017
1 parent 990675d commit 8a8cc1a
Show file tree
Hide file tree
Showing 7 changed files with 772 additions and 1 deletion.
33 changes: 32 additions & 1 deletion src/parser/comments.js
Expand Up @@ -43,7 +43,7 @@ pp.processComment = function (node) {

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 @@ -71,10 +71,41 @@ pp.processComment = function (node) {
}

// 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
@@ -0,0 +1 @@
fn(a, { b }, /* comment */);
@@ -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
}
}
}
]
}
@@ -0,0 +1 @@
fn(a, b, /* comment */);

0 comments on commit 8a8cc1a

Please sign in to comment.