Skip to content

Commit ca837a8

Browse files
authored
no-lonely-if: Fix an edge case (#2168)
1 parent 13d454b commit ca837a8

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

rules/no-lonely-if.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ function fix(innerIfStatement, sourceCode) {
9797
const lastToken = sourceCode.getLastToken(inner.consequent);
9898
if (isNotSemicolonToken(lastToken)) {
9999
const nextToken = sourceCode.getTokenAfter(outer);
100-
if (needsSemicolon(lastToken, sourceCode, nextToken.value)) {
100+
if (nextToken && needsSemicolon(lastToken, sourceCode, nextToken.value)) {
101101
yield fixer.insertTextBefore(nextToken, ';');
102102
}
103103
}

test/no-lonely-if.mjs

+5
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ test.snapshot({
5151
`,
5252
// No `BlockStatement`
5353
'if (a) if (b) foo();',
54+
outdent`
55+
if (a) {
56+
if (b) foo()
57+
}
58+
`,
5459
// `EmptyStatement`
5560
'if (a) if (b);',
5661
// Nested

test/snapshots/no-lonely-if.mjs.md

+28-8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,26 @@ Generated by [AVA](https://avajs.dev).
8989
`
9090

9191
## Invalid #5
92+
1 | if (a) {
93+
2 | if (b) foo()
94+
3 | }
95+
96+
> Output
97+
98+
`␊
99+
1 | if (a && b) foo()␊
100+
`
101+
102+
> Error 1/1
103+
104+
`␊
105+
1 | if (a) {␊
106+
> 2 | if (b) foo()␊
107+
| ^^^^^^^^^^^^ Unexpected \`if\` as the only statement in a \`if\` block without \`else\`.␊
108+
3 | }␊
109+
`
110+
111+
## Invalid #6
92112
1 | if (a) if (b);
93113

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

107-
## Invalid #6
127+
## Invalid #7
108128
1 | if (a) {
109129
2 | if (b) {
110130
3 | // Should not report
@@ -140,7 +160,7 @@ Generated by [AVA](https://avajs.dev).
140160
8 | }␊
141161
`
142162

143-
## Invalid #7
163+
## Invalid #8
144164
1 | function * foo() {
145165
2 | if (a || b)
146166
3 | if (a ?? b)
@@ -325,7 +345,7 @@ Generated by [AVA](https://avajs.dev).
325345
11 | }␊
326346
`
327347

328-
## Invalid #8
348+
## Invalid #9
329349
1 | async function foo() {
330350
2 | if (a)
331351
3 | if (await a)
@@ -380,7 +400,7 @@ Generated by [AVA](https://avajs.dev).
380400
6 | }␊
381401
`
382402

383-
## Invalid #9
403+
## Invalid #10
384404
1 | if (((a || b))) if (((c || d)));
385405

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

399-
## Invalid #10
419+
## Invalid #11
400420
1 | if // 1
401421
2 | (
402422
3 | // 2
@@ -472,7 +492,7 @@ Generated by [AVA](https://avajs.dev).
472492
19 | }␊
473493
`
474494

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

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

520-
## Invalid #13
540+
## Invalid #14
521541
1 | if (a) {
522542
2 | if (b) foo()
523543
3 | }

test/snapshots/no-lonely-if.mjs.snap

41 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)