Leading comments incorrectly include comments inside previous token
See original GitHub issueI recently contributed a rule that ended up having an issue regarding leading comments. The issue for the rule and the fix is here, but in looking at it, I started wondering if maybe this should be a fix in sourceCode.getComments
rather than having to be guarded against in each individual rule.
I’m currently using ESLint v2.3.0, and the problem seems to be that comments inside a previous object literal or block get added to the following token’s leading comments array when there are no other expressions between the comment and the token. Example:
function foo() {
var bar = {
// comment
};
return bar;
}
Running sourceCode.getComments
on the return
expression’s node will include the comment inside the previous object literal, despite it actually being part of the previous token.
I’m assuming this is a bug - it definitely wasn’t what I was expecting to happen - can someone more knowledgable confirm?
In looking at things, it looks like maybe the change needs to happen in Espree…Happy to make a PR if this is accepted.
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (11 by maintainers)
Top GitHub Comments
@ilyavolodin Thanks! I’ll try to get a PR together in the next day or two
PR opened for the Espree change. Will get to adding the tests on ESLint after that lands 👍
https://github.com/eslint/espree/pull/257