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
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/rules/curly.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,11 @@ while (true) {
for (var i = 0; foo; i++) {
doSomething();
}

if (foo) {
// some comment
bar();
}
```

Examples of **correct** code for the `"multi-or-nest"` option:
Expand Down
4 changes: 3 additions & 1 deletion lib/rules/curly.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,9 @@ module.exports = {
}
} else if (multiOrNest) {
if (hasBlock && body.body.length === 1 && isOneLiner(body.body[0])) {
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

} else if (!isOneLiner(body)) {
expected = true;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/curly.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ ruleTester.run("curly", rule, {
code: "if (foo) \n quz = true;",
options: ["multi-or-nest"]
},
{
code: "if (foo) { \n // line of comment \n quz = true; \n }",
options: ["multi-or-nest"]
},
{
code: "// line of comment \n if (foo) \n quz = true; \n",
options: ["multi-or-nest"]
},
{
code: "while (true) \n doSomething();",
options: ["multi-or-nest"]
Expand Down