Skip to content

Commit

Permalink
Update: fix indentation of EmptyStatements (fixes #8882) (#8885)
Browse files Browse the repository at this point in the history
The `indent` rule contains some logic to ensure that semicolons at the start of a line are indented correctly when using semicolon-free style. For example, in the following code the semicolon on the second line is not indented, even though it's part of the `bar()` statement.

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

Due to a bug, this logic also applied to semicolons in `EmptyStatement` nodes, resulting in an incorrect indentation for those semicolons. This commit fixes that bug.
  • Loading branch information
not-an-aardvark authored and gyandeeps committed Jul 8, 2017
1 parent 88ed041 commit 975dacf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/indent.js
Expand Up @@ -825,7 +825,7 @@ module.exports = {
*/
const lastToken = sourceCode.getLastToken(node);

if (astUtils.isSemicolonToken(lastToken)) {
if (node.type !== "EmptyStatement" && astUtils.isSemicolonToken(lastToken)) {
offsets.matchIndentOf(lastParentToken, lastToken);
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/lib/rules/indent.js
Expand Up @@ -3343,6 +3343,12 @@ ruleTester.run("indent", rule, {
; [1, 2, 3].map(baz)
`
},
{
code: unIndent`
if (foo)
;
`
},
{
code: "x => {}"
},
Expand Down Expand Up @@ -8028,6 +8034,17 @@ ruleTester.run("indent", rule, {
`,
errors: expectedErrors([3, 0, 4, "Punctuator"])
},
{
code: unIndent`
if (foo)
;
`,
output: unIndent`
if (foo)
;
`,
errors: expectedErrors([2, 4, 0, "Punctuator"])
},
{
code: unIndent`
import {foo}
Expand Down

0 comments on commit 975dacf

Please sign in to comment.