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
Fix: Curly rule doesn't account for leading comment (fixes #7538) #7539
Conversation
@wwwillchen, thanks for your PR! By analyzing the history of the files in this pull request, we identified @vitorbal, @not-an-aardvark and @nzakas to be potential reviewers. |
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
@@ -290,7 +290,7 @@ module.exports = { | |||
} | |||
} else if (multiOrNest) { | |||
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) { | |||
expected = false; | |||
expected = !!(body.body[0].leadingComments && body.body[0].leadingComments.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change the behavior of isOneLiner
function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried making my modification in isOneLiner
but it caused unit tests to fail. I think the use of isOneLiner in L294 makes modifying the isOneLiner
function different than my current change.
@@ -290,7 +290,7 @@ module.exports = { | |||
} | |||
} else if (multiOrNest) { | |||
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) { | |||
expected = false; | |||
expected = !!(body.body[0].leadingComments && body.body[0].leadingComments.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use sourceCode.getComments(body.body[0]).leading
instead of body.body[0].leadingComments
?
We have a few other cases of incorrectly using the leadingComments
property in the codebase, but as we are actively moving towards calculating location of comments relative to nodes in ESLint instead of the parser, this will save us having to change this down the road. Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback. Please take another look.
@@ -290,7 +290,7 @@ module.exports = { | |||
} | |||
} else if (multiOrNest) { | |||
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) { | |||
expected = false; | |||
expected = !!(body.body[0].leadingComments && body.body[0].leadingComments.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried making my modification in isOneLiner
but it caused unit tests to fail. I think the use of isOneLiner in L294 makes modifying the isOneLiner
function different than my current change.
@@ -290,7 +290,7 @@ module.exports = { | |||
} | |||
} else if (multiOrNest) { | |||
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) { | |||
expected = false; | |||
expected = !!(body.body[0].leadingComments && body.body[0].leadingComments.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
expected = false; | ||
const leadingComments = sourceCode.getComments(body.body[0]).leading; | ||
|
||
expected = !!(leadingComments && leadingComments.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check should be able to just be expected = !!leadingComments.length
now, since sourceCode.getComments()
will return an empty array if no leading comments are found
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall LGTM, just one small nitpick.
expected = false; | ||
const leadingComments = sourceCode.getComments(body.body[0]).leading; | ||
|
||
expected = !!leadingComments.length; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use leadingComments.length > 0
instead? I prefer to be explicit about the expectation here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
What is the purpose of this pull request? (put an "X" next to item)
[ ] Documentation update
[X] Bug fix (template)
[ ] New rule (template)
[ ] Changes an existing rule (template)
[ ] Add autofixing to a rule
[ ] Add a CLI option
[ ] Add something to the core
[ ] Other, please explain:
Fixes #7538
What changes did you make? (Give an overview)
Fixes the curly rule under the "multi-or-nest" setting, includes tests, and updates docs with an example
Is there anything you'd like reviewers to focus on?
This only addresses leading comments and not trailing comments, but I think trailing comment is a bit different because the intent is more ambiguous. For example:
It's unclear if the comment is describing the line above or mis-indented and describing the line below. In my experience, intentional trailing comments are rare.