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

Commit

Permalink
Merge pull request #23 from danez/fix-comments
Browse files Browse the repository at this point in the history
Fix leading comments added from previous node
  • Loading branch information
sebmck committed Jun 22, 2016
2 parents 55d47ab + 4bfecec commit 7859729
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 71 deletions.
18 changes: 15 additions & 3 deletions src/parser/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pp.processComment = function (node) {

let stack = this.state.commentStack;

let lastChild, trailingComments, i;
let lastChild, trailingComments, i, j;

if (this.state.trailingComments.length > 0) {
// If the first comment in trailingComments comes after the
Expand Down Expand Up @@ -93,8 +93,18 @@ pp.processComment = function (node) {
}
} else if (this.state.leadingComments.length > 0) {
if (last(this.state.leadingComments).end <= node.start) {
node.leadingComments = this.state.leadingComments;
this.state.leadingComments = [];
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) {
node.leadingComments = this.state.leadingComments;
this.state.leadingComments = [];
}
} else {
// https://github.com/eslint/espree/issues/2
//
Expand Down Expand Up @@ -131,6 +141,8 @@ pp.processComment = function (node) {
}
}

this.state.commentPreviousNode = node;

if (trailingComments) {
if (trailingComments.length && trailingComments[0].start >= node.start && last(trailingComments).end <= node.end) {
node.innerComments = trailingComments;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module.exports = function(context) {
return isConstant(node.expressions[node.expressions.length - 1]);
// no default
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"type": "File",
"start": 0,
"end": 287,
"end": 296,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"line": 13,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 287,
"end": 296,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"line": 13,
"column": 2
}
},
Expand All @@ -31,28 +31,28 @@
{
"type": "ExpressionStatement",
"start": 0,
"end": 287,
"end": 296,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"line": 13,
"column": 2
}
},
"expression": {
"type": "AssignmentExpression",
"start": 0,
"end": 286,
"end": 295,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 12,
"line": 13,
"column": 1
}
},
Expand Down Expand Up @@ -108,14 +108,14 @@
"right": {
"type": "FunctionExpression",
"start": 17,
"end": 286,
"end": 295,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 12,
"line": 13,
"column": 1
}
},
Expand Down Expand Up @@ -143,29 +143,29 @@
"body": {
"type": "BlockStatement",
"start": 35,
"end": 286,
"end": 295,
"loc": {
"start": {
"line": 1,
"column": 35
},
"end": {
"line": 12,
"line": 13,
"column": 1
}
},
"body": [
{
"type": "FunctionDeclaration",
"start": 42,
"end": 283,
"end": 292,
"loc": {
"start": {
"line": 3,
"column": 4
},
"end": {
"line": 10,
"line": 11,
"column": 5
}
},
Expand Down Expand Up @@ -208,14 +208,14 @@
"body": {
"type": "BlockStatement",
"start": 68,
"end": 283,
"end": 292,
"loc": {
"start": {
"line": 3,
"column": 30
},
"end": {
"line": 10,
"line": 11,
"column": 5
}
},
Expand Down Expand Up @@ -571,53 +571,34 @@
},
{
"type": "ReturnStatement",
"start": 264,
"end": 277,
"start": 273,
"end": 286,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 8
},
"end": {
"line": 9,
"line": 10,
"column": 21
}
},
"argument": {
"type": "BooleanLiteral",
"start": 271,
"end": 276,
"start": 280,
"end": 285,
"loc": {
"start": {
"line": 9,
"line": 10,
"column": 15
},
"end": {
"line": 9,
"line": 10,
"column": 20
}
},
"value": false,
"leadingComments": null
},
"leadingComments": [
{
"type": "CommentLine",
"value": " no default",
"start": 232,
"end": 245,
"loc": {
"start": {
"line": 7,
"column": 12
},
"end": {
"line": 7,
"column": 25
}
}
}
]
"value": false
}
}
],
"directives": []
Expand Down Expand Up @@ -650,4 +631,4 @@
}
}
]
}
}
25 changes: 3 additions & 22 deletions test/fixtures/jsx/basic/10/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,27 +117,8 @@
"column": 31
}
},
"name": "a",
"leadingComments": null
},
"leadingComments": [
{
"type": "CommentBlock",
"value": " this is a comment ",
"start": 4,
"end": 27,
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 27
}
}
}
]
"name": "a"
}
},
"children": [
{
Expand Down Expand Up @@ -220,4 +201,4 @@
}
}
]
}
}

0 comments on commit 7859729

Please sign in to comment.