Skip to content

Commit

Permalink
no-lonely-if: Fix an edge case (#2168)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jul 10, 2023
1 parent 13d454b commit ca837a8
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
2 changes: 1 addition & 1 deletion rules/no-lonely-if.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function fix(innerIfStatement, sourceCode) {
const lastToken = sourceCode.getLastToken(inner.consequent);
if (isNotSemicolonToken(lastToken)) {
const nextToken = sourceCode.getTokenAfter(outer);
if (needsSemicolon(lastToken, sourceCode, nextToken.value)) {
if (nextToken && needsSemicolon(lastToken, sourceCode, nextToken.value)) {
yield fixer.insertTextBefore(nextToken, ';');
}
}
Expand Down
5 changes: 5 additions & 0 deletions test/no-lonely-if.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ test.snapshot({
`,
// No `BlockStatement`
'if (a) if (b) foo();',
outdent`
if (a) {
if (b) foo()
}
`,
// `EmptyStatement`
'if (a) if (b);',
// Nested
Expand Down
36 changes: 28 additions & 8 deletions test/snapshots/no-lonely-if.mjs.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ Generated by [AVA](https://avajs.dev).
`

## Invalid #5
1 | if (a) {
2 | if (b) foo()
3 | }

> Output
`␊
1 | if (a && b) foo()␊
`

> Error 1/1
`␊
1 | if (a) {␊
> 2 | if (b) foo()␊
| ^^^^^^^^^^^^ Unexpected \`if\` as the only statement in a \`if\` block without \`else\`.␊
3 | }␊
`

## Invalid #6
1 | if (a) if (b);

> Output
Expand All @@ -104,7 +124,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^ Unexpected \`if\` as the only statement in a \`if\` block without \`else\`.␊
`

## Invalid #6
## Invalid #7
1 | if (a) {
2 | if (b) {
3 | // Should not report
Expand Down Expand Up @@ -140,7 +160,7 @@ Generated by [AVA](https://avajs.dev).
8 | }␊
`

## Invalid #7
## Invalid #8
1 | function * foo() {
2 | if (a || b)
3 | if (a ?? b)
Expand Down Expand Up @@ -325,7 +345,7 @@ Generated by [AVA](https://avajs.dev).
11 | }␊
`

## Invalid #8
## Invalid #9
1 | async function foo() {
2 | if (a)
3 | if (await a)
Expand Down Expand Up @@ -380,7 +400,7 @@ Generated by [AVA](https://avajs.dev).
6 | }␊
`

## Invalid #9
## Invalid #10
1 | if (((a || b))) if (((c || d)));

> Output
Expand All @@ -396,7 +416,7 @@ Generated by [AVA](https://avajs.dev).
| ^^^^^^^^^^^^^^^^ Unexpected \`if\` as the only statement in a \`if\` block without \`else\`.␊
`

## Invalid #10
## Invalid #11
1 | if // 1
2 | (
3 | // 2
Expand Down Expand Up @@ -472,7 +492,7 @@ Generated by [AVA](https://avajs.dev).
19 | }␊
`

## Invalid #11
## Invalid #12
1 | if (a) {
2 | if (b) foo()
3 | }
Expand All @@ -495,7 +515,7 @@ Generated by [AVA](https://avajs.dev).
4 | [].forEach(bar)␊
`

## Invalid #12
## Invalid #13
1 | if (a)
2 | if (b) foo()
3 | ;[].forEach(bar)
Expand All @@ -517,7 +537,7 @@ Generated by [AVA](https://avajs.dev).
| ^^ Unexpected \`if\` as the only statement in a \`if\` block without \`else\`.␊
`

## Invalid #13
## Invalid #14
1 | if (a) {
2 | if (b) foo()
3 | }
Expand Down
Binary file modified test/snapshots/no-lonely-if.mjs.snap
Binary file not shown.

0 comments on commit ca837a8

Please sign in to comment.