Skip to content

Commit

Permalink
Fix comment attachment for call expressions (babel#575)
Browse files Browse the repository at this point in the history
  • Loading branch information
aardito2 authored and danez committed Jun 18, 2017
1 parent ec6d688 commit e79aadd
Show file tree
Hide file tree
Showing 5 changed files with 580 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/parser/comments.js
Expand Up @@ -84,23 +84,34 @@ pp.processComment = function (node) {
// 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) {
if (firstChild && 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 (firstChild.type === "ObjectProperty") {
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 (this.state.leadingComments.length > 0) {
firstChild.trailingComments = this.state.leadingComments;
this.state.leadingComments = [];
}
}
}
} else if (node.type === "CallExpression" && node.arguments && node.arguments.length) {
const lastArg = last(node.arguments);

if (lastArg && lastComment.start >= lastArg.start && lastComment.end <= node.end) {
if (this.state.commentPreviousNode) {
if (this.state.leadingComments.length > 0) {
lastArg.trailingComments = this.state.leadingComments;
this.state.leadingComments = [];
}
}
}
}
Expand Down
@@ -0,0 +1,5 @@
switch (x) {
case 1:
console.log('1')
// comment
}
@@ -0,0 +1,245 @@
{
"type": "File",
"start": 0,
"end": 60,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 60,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "SwitchStatement",
"start": 0,
"end": 60,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 5,
"column": 1
}
},
"discriminant": {
"type": "Identifier",
"start": 8,
"end": 9,
"loc": {
"start": {
"line": 1,
"column": 8
},
"end": {
"line": 1,
"column": 9
},
"identifierName": "x"
},
"name": "x"
},
"cases": [
{
"type": "SwitchCase",
"start": 15,
"end": 43,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 3,
"column": 20
}
},
"consequent": [
{
"type": "ExpressionStatement",
"start": 27,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 20
}
},
"expression": {
"type": "CallExpression",
"start": 27,
"end": 43,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 20
}
},
"callee": {
"type": "MemberExpression",
"start": 27,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 15
}
},
"object": {
"type": "Identifier",
"start": 27,
"end": 34,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 3,
"column": 11
},
"identifierName": "console"
},
"name": "console"
},
"property": {
"type": "Identifier",
"start": 35,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 12
},
"end": {
"line": 3,
"column": 15
},
"identifierName": "log"
},
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "StringLiteral",
"start": 39,
"end": 42,
"loc": {
"start": {
"line": 3,
"column": 16
},
"end": {
"line": 3,
"column": 19
}
},
"extra": {
"rawValue": "1",
"raw": "'1'"
},
"value": "1"
}
],
"trailingComments": null
},
"trailingComments": null
}
],
"test": {
"type": "NumericLiteral",
"start": 20,
"end": 21,
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 8
}
},
"extra": {
"rawValue": 1,
"raw": "1"
},
"value": 1
},
"trailingComments": [
{
"type": "CommentLine",
"value": " comment",
"start": 48,
"end": 58,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 14
}
}
}
]
}
]
}
],
"directives": []
},
"comments": [
{
"type": "CommentLine",
"value": " comment",
"start": 48,
"end": 58,
"loc": {
"start": {
"line": 4,
"column": 4
},
"end": {
"line": 4,
"column": 14
}
}
}
]
}
@@ -0,0 +1,7 @@
switch(x) {
case 1:
console.log('1')
// comment
default:
break;
}

0 comments on commit e79aadd

Please sign in to comment.