Skip to content

Commit

Permalink
Fix: Curly rule doesn't account for leading comment (fixes #7538) (#7539
Browse files Browse the repository at this point in the history
)

* Fix: Curly rule doesn't account for leading comment (fixes #7538)

* PR feedback

* simplify check

* check .length > 0
  • Loading branch information
wwwillchen authored and nzakas committed Nov 9, 2016
1 parent 5003b1c commit 0d60db7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions docs/rules/curly.md
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
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.length > 0;
} else if (!isOneLiner(body)) {
expected = true;
}
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/curly.js
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

0 comments on commit 0d60db7

Please sign in to comment.