-
-
Notifications
You must be signed in to change notification settings - Fork 206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect leading comments. #256
Comments
I'd like to work on this :) |
Ooh, go for it! |
Spent some time with this tonight and think I understand what's going on now. What I'm not sure of is how or where to put this fix. Seems to me like the best fix is to check something like if (node.leadingComments[x].range[1] < previousNode.range[1]) {
delete node.leadingComments[x];
} where Ideally the change outlined above would happen in We could perform this check and remove offending leadingComments somewhere after the comments are attached and we have access to all the nodes (somewhere after this line, I think?). Wanted to see what someone with a little more knowledge/experience thought about this before I try to make this change. One final thing - an alternative would be to see why acorn is attaching these comments to the node in the first place. Definitely does not seem like desired behavior. Sorry for the wall of text, but looking forward to seeing what you all think and figuring this out. Cheers! EDIT: It's always easier to show someone code than try to explain it, so here's an example of what I was thinking about (this would happen on this line): var i, j;
if (extra.comment || extra.attachComment) {
// Remove leading comments that are added when no expression exists between comment and the relevant node
for (i = 1; i < program.body.length; i++) {
if (program.body[i].leadingComments) {
for (j = 0; j < program.body[i].leadingComments.length; j++) {
if (program.body[i].leadingComments[j].range[1] < program.body[i - 1].range[1]) {
program.body[i].leadingComments.splice(j, 1);
}
}
if (program.body[i].leadingComments.length < 1) {
delete program.body[i].leadingComments;
}
}
}
program.comments = extra.comments;
} I tried this out locally and it seems to be working, though not sure how it affects performance. I can make a PR if this seems like a good way forward! |
Sorry, thought I was going to put this down when I made my last comment...but here I am, still looking at it :) This actually can work in var i, j;
if (node.type === astNodeTypes.Program) {
// Remove leading comments that are added when no expression exists between comment and the relevant node
for (i = 1; i < node.body.length; i++) {
if (node.body[i].leadingComments) {
for (j = 0; j < node.body[i].leadingComments.length; j++) {
if (node.body[i].leadingComments[j].range[1] < node.body[i - 1].range[1]) {
node.body[i].leadingComments.splice(j, 1);
}
}
if (node.body[i].leadingComments.length < 1) {
delete node.body[i].leadingComments;
}
}
}
if (node.body.length > 0) {
return;
}
} |
Comment attachment is a big pile of hacks anyway, so this looks fine to me. |
Fix: leading comments added from previous node (fixes #256)
Fix: Fix behavior of ignoring comments within previous nodes (refs #256)
From eslint/eslint#5512
In the following case:
// foo
comment has attached to the leading ofvar bar
.The text was updated successfully, but these errors were encountered: