Skip to content

Commit

Permalink
Fix: curly false positive with no-semicolon style (#7509)
Browse files Browse the repository at this point in the history
With the `multi-line` option, `curly` should not report an error in cases like this:

```js
if (foo) bar()

;[1, 2, 3].map(foo)
```

However, it had a false positive because the semicolon before `[` is considered to be part of the same statement as `bar()`, so `curly` identified `bar()` as a multiline statement. This fixes `curly` to ignore trailing semicolons when determining whether a statement is multiline.

Originally reported here: standard/standard#664
  • Loading branch information
not-an-aardvark authored and nzakas committed Nov 3, 2016
1 parent af1fde1 commit ea0970d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/rules/curly.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,11 @@ module.exports = {
* @private
*/
function isCollapsedOneLiner(node) {
const before = sourceCode.getTokenBefore(node),
last = sourceCode.getLastToken(node);
const before = sourceCode.getTokenBefore(node);
const last = sourceCode.getLastToken(node);
const lastExcludingSemicolon = last.type === "Punctuator" && last.value === ";" ? sourceCode.getTokenBefore(last) : last;

return before.loc.start.line === last.loc.end.line;
return before.loc.start.line === lastExcludingSemicolon.loc.end.line;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/rules/curly.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ ruleTester.run("curly", rule, {
{
code: "if (true) { foo(); faa(); } else { bar(); }",
options: ["multi", "consistent"]
},
{

// https://github.com/feross/standard/issues/664
code: "if (true) foo()\n;[1, 2, 3].bar()",
options: ["multi-line"]
}
],
invalid: [
Expand Down Expand Up @@ -800,6 +806,12 @@ ruleTester.run("curly", rule, {
options: ["multi"],
parserOptions: { ecmaVersion: 6 },
errors: [{ message: "Unnecessary { after 'if' condition.", type: "IfStatement" }]
},
{
code: "if (true)\nfoo()\n;[1, 2, 3].bar()",
output: "if (true)\n{foo()\n;}[1, 2, 3].bar()",
options: ["multi-line"],
errors: [{ message: "Expected { after 'if' condition.", type: "IfStatement" }]
}
]
});

0 comments on commit ea0970d

Please sign in to comment.