Fix leading comments added from previous node
This fixes an issue that comments were added as leading comments to expressions, which are not immediately folowing the comment. This was also reported and fixed in espree, and is basically a port to babylon. eslint/espree#256 this also fixes an issue in babel-eslint with certain rules babel/babel-eslint#289
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
//
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user