Skip to content
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

Merged
merged 4 commits into from Nov 9, 2016

Conversation

wwwillchen
Copy link
Contributor

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:

if (foo)
  baz();
  // line of comment
bar();

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.

@jsf-clabot
Copy link

jsf-clabot commented Nov 5, 2016

CLA assistant check
All committers have signed the CLA.

@mention-bot
Copy link

@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.

@eslintbot
Copy link

LGTM

Copy link
Member

@not-an-aardvark not-an-aardvark left a 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);
Copy link
Member

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?

Copy link
Contributor Author

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);
Copy link
Member

@kaicataldo kaicataldo Nov 5, 2016

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!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@eslintbot
Copy link

LGTM

Copy link
Contributor Author

@wwwillchen wwwillchen left a 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);
Copy link
Contributor Author

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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@wwwillchen wwwillchen added bug ESLint is working incorrectly rule Relates to ESLint's core rules accepted There is consensus among the team that this change meets the criteria for inclusion labels Nov 7, 2016
expected = false;
const leadingComments = sourceCode.getComments(body.body[0]).leading;

expected = !!(leadingComments && leadingComments.length);
Copy link
Member

@kaicataldo kaicataldo Nov 7, 2016

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@eslintbot
Copy link

LGTM

Copy link
Member

@nzakas nzakas left a 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;
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@eslintbot
Copy link

LGTM

Copy link
Member

@nzakas nzakas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@nzakas nzakas merged commit 0d60db7 into eslint:master Nov 9, 2016
@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion bug ESLint is working incorrectly rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

curly and comments
8 participants