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

no-lonely-if: Fix an edge case #2168

Merged
merged 3 commits into from Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion rules/no-lonely-if.js
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
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
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.