Skip to content

Commit

Permalink
Fix: Leading comments for anonymous classes (fixes #155, fixes #158)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jul 10, 2015
1 parent 28eb9a2 commit d535900
Show file tree
Hide file tree
Showing 6 changed files with 427 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lib/comment-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,21 @@ module.exports = {
}

if (lastChild) {
if (lastChild.leadingComments && lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= node.range[0]) {
node.leadingComments = lastChild.leadingComments;
delete lastChild.leadingComments;
if (lastChild.leadingComments) {
if (lastChild.leadingComments[lastChild.leadingComments.length - 1].range[1] <= node.range[0]) {
node.leadingComments = lastChild.leadingComments;
delete lastChild.leadingComments;
} else {
// A leading comment for an anonymous class had been stolen by its first MethodDefinition,
// so this takes back the leading comment.
// See Also: https://github.com/eslint/espree/issues/158
for (i = lastChild.leadingComments.length - 2; i >= 0; --i) {
if (lastChild.leadingComments[i].range[1] <= node.range[0]) {
node.leadingComments = lastChild.leadingComments.splice(0, i + 1);
break;
}
}
}
}
} else if (extra.leadingComments.length > 0) {

Expand Down
152 changes: 152 additions & 0 deletions tests/fixtures/attach-comments/comment-within-condition.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
module.exports = {
"type": "Program",
"body": [
{
"type": "IfStatement",
"test": {
"type": "Identifier",
"name": "a",
"range": [
25,
26
],
"loc": {
"start": {
"line": 2,
"column": 15
},
"end": {
"line": 2,
"column": 16
}
},
"leadingComments": [
{
"type": "Block",
"value": " bar ",
"range": [
14,
23
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 13
}
}
}
]
},
"consequent": {
"type": "BlockStatement",
"body": [],
"range": [
28,
30
],
"loc": {
"start": {
"line": 2,
"column": 18
},
"end": {
"line": 2,
"column": 20
}
}
},
"alternate": null,
"range": [
10,
30
],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 20
}
},
"leadingComments": [
{
"type": "Block",
"value": " foo ",
"range": [
0,
9
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
}
}
]
}
],
"sourceType": "script",
"range": [
10,
30
],
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 20
}
},
"comments": [
{
"type": "Block",
"value": " foo ",
"range": [
0,
9
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 9
}
}
},
{
"type": "Block",
"value": " bar ",
"range": [
14,
23
],
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 13
}
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* foo */
if (/* bar */ a) {}

0 comments on commit d535900

Please sign in to comment.