@@ -62,7 +62,12 @@ impl Rule for NoLoneBlocks {
62
62
} ;
63
63
64
64
if stmt. body . is_empty ( ) {
65
- if !matches ! ( parent_node. kind( ) , AstKind :: TryStatement ( _) | AstKind :: CatchClause ( _) ) {
65
+ let is_comment_in_stmt =
66
+ ctx. semantic ( ) . comments_range ( stmt. span . start ..stmt. span . end ) . last ( ) . is_some ( ) ;
67
+
68
+ if !is_comment_in_stmt
69
+ && !matches ! ( parent_node. kind( ) , AstKind :: TryStatement ( _) | AstKind :: CatchClause ( _) )
70
+ {
66
71
report ( ctx, node, parent_node) ;
67
72
}
68
73
return ;
@@ -155,30 +160,30 @@ fn test() {
155
160
"{ class Bar {} }" , // { "ecmaVersion": 6 },
156
161
"{ {let y = 1;} let x = 1; }" , // { "ecmaVersion": 6 },
157
162
"
158
- switch (foo) {
159
- case bar: {
160
- baz;
161
- }
162
- }
163
- ",
163
+ switch (foo) {
164
+ case bar: {
165
+ baz;
166
+ }
167
+ }
168
+ " ,
164
169
"
165
- switch (foo) {
166
- case bar: {
167
- baz;
168
- }
169
- case qux: {
170
- boop;
171
- }
172
- }
173
- ",
170
+ switch (foo) {
171
+ case bar: {
172
+ baz;
173
+ }
174
+ case qux: {
175
+ boop;
176
+ }
177
+ }
178
+ " ,
174
179
"
175
- switch (foo) {
176
- case bar:
177
- {
178
- baz;
179
- }
180
- }
181
- ",
180
+ switch (foo) {
181
+ case bar:
182
+ {
183
+ baz;
184
+ }
185
+ }
186
+ " ,
182
187
"function foo() { { const x = 4 } const x = 3 }" , // { "ecmaVersion": 6 },
183
188
"class C { static {} }" , // { "ecmaVersion": 2022 },
184
189
"class C { static { foo; } }" , // { "ecmaVersion": 2022 },
@@ -189,15 +194,29 @@ fn test() {
189
194
"class C { static { { function block(){} } something; } }" , // { "ecmaVersion": 2022 },
190
195
"class C { static { something; { class block {} } } }" , // { "ecmaVersion": 2022 },
191
196
"
192
- {
193
- using x = makeDisposable();
194
- }" , // { "parser": require(parser("typescript-parsers/no-lone-blocks/using")), "ecmaVersion": 2022 },
197
+ {
198
+ using x = makeDisposable();
199
+ }
200
+ " , // { "parser": require(parser("typescript-parsers/no-lone-blocks/using")), "ecmaVersion": 2022 },
195
201
"
196
- {
197
- await using x = makeDisposable();
198
- }" , // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 }
202
+ {
203
+ await using x = makeDisposable();
204
+ }
205
+ " , // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 }
199
206
// Issue: <https://github.com/oxc-project/oxc/issues/8515>
200
207
"try {} catch {}" ,
208
+ // Issue: https://github.com/oxc-project/oxc/issues/8697
209
+ "
210
+ if (foo) {
211
+ // do nothing
212
+ }
213
+ else if (bar) {
214
+ // do nothing again
215
+ }
216
+ else {
217
+ // do nothing
218
+ }
219
+ " ,
201
220
] ;
202
221
203
222
let fail = vec ! [
@@ -215,154 +234,154 @@ fn test() {
215
234
"{var x = 1;}" , // { "ecmaVersion": 6 },
216
235
"{
217
236
{var x = 1;}
218
- let y = 2; } {let z = 1;}" , // { "ecmaVersion": 6 },
237
+ let y = 2; } {let z = 1;}" , // { "ecmaVersion": 6 },
219
238
"{
220
239
{let x = 1;}
221
- var y = 2; } {let z = 1;}" , // { "ecmaVersion": 6 },
240
+ var y = 2; } {let z = 1;}" , // { "ecmaVersion": 6 },
222
241
"{
223
242
{var x = 1;}
224
- var y = 2; }
225
- {var z = 1;}" , // { "ecmaVersion": 6 },
243
+ var y = 2; }
244
+ {var z = 1;}" , // { "ecmaVersion": 6 },
226
245
"
227
- switch (foo) {
228
- case 1:
229
- foo();
230
- {
231
- bar;
232
- }
233
- }
234
- ",
246
+ switch (foo) {
247
+ case 1:
248
+ foo();
249
+ {
250
+ bar;
251
+ }
252
+ }
253
+ " ,
235
254
"
236
- switch (foo) {
237
- case 1:
238
- {
239
- bar;
240
- }
241
- foo();
242
- }
243
- ",
255
+ switch (foo) {
256
+ case 1:
257
+ {
258
+ bar;
259
+ }
260
+ foo();
261
+ }
262
+ " ,
244
263
"
245
- function foo () {
246
- {
247
- const x = 4;
248
- }
249
- }
250
- ", // { "ecmaVersion": 6 },
264
+ function foo () {
265
+ {
266
+ const x = 4;
267
+ }
268
+ }
269
+ " , // { "ecmaVersion": 6 },
251
270
"
252
- function foo () {
253
- {
254
- var x = 4;
255
- }
256
- }
257
- ",
271
+ function foo () {
272
+ {
273
+ var x = 4;
274
+ }
275
+ }
276
+ " ,
258
277
"
259
- class C {
260
- static {
261
- if (foo) {
262
- {
263
- let block;
264
- }
265
- }
266
- }
267
- }
268
- ", // { "ecmaVersion": 2022 },
278
+ class C {
279
+ static {
280
+ if (foo) {
281
+ {
282
+ let block;
283
+ }
284
+ }
285
+ }
286
+ }
287
+ " , // { "ecmaVersion": 2022 },
269
288
"
270
- class C {
271
- static {
272
- if (foo) {
273
- {
274
- block;
275
- }
276
- something;
277
- }
278
- }
279
- }
280
- ", // { "ecmaVersion": 2022 },
289
+ class C {
290
+ static {
291
+ if (foo) {
292
+ {
293
+ block;
294
+ }
295
+ something;
296
+ }
297
+ }
298
+ }
299
+ " , // { "ecmaVersion": 2022 },
281
300
"
282
- class C {
283
- static {
284
- {
285
- block;
286
- }
287
- }
288
- }
289
- ", // { "ecmaVersion": 2022 },
301
+ class C {
302
+ static {
303
+ {
304
+ block;
305
+ }
306
+ }
307
+ }
308
+ " , // { "ecmaVersion": 2022 },
290
309
"
291
- class C {
292
- static {
293
- {
294
- let block;
295
- }
296
- }
297
- }
298
- ", // { "ecmaVersion": 2022 },
310
+ class C {
311
+ static {
312
+ {
313
+ let block;
314
+ }
315
+ }
316
+ }
317
+ " , // { "ecmaVersion": 2022 },
299
318
"
300
- class C {
301
- static {
302
- {
303
- const block = 1;
304
- }
305
- }
306
- }
307
- ", // { "ecmaVersion": 2022 },
319
+ class C {
320
+ static {
321
+ {
322
+ const block = 1;
323
+ }
324
+ }
325
+ }
326
+ " , // { "ecmaVersion": 2022 },
308
327
"
309
- class C {
310
- static {
311
- {
312
- function block() {}
313
- }
314
- }
315
- }
316
- ", // { "ecmaVersion": 2022 },
328
+ class C {
329
+ static {
330
+ {
331
+ function block() {}
332
+ }
333
+ }
334
+ }
335
+ " , // { "ecmaVersion": 2022 },
317
336
"
318
- class C {
319
- static {
320
- {
321
- class block {}
322
- }
323
- }
324
- }
325
- ", // { "ecmaVersion": 2022 },
337
+ class C {
338
+ static {
339
+ {
340
+ class block {}
341
+ }
342
+ }
343
+ }
344
+ " , // { "ecmaVersion": 2022 },
326
345
"
327
- class C {
328
- static {
329
- {
330
- var block;
331
- }
332
- something;
333
- }
334
- }
335
- ", // { "ecmaVersion": 2022 },
346
+ class C {
347
+ static {
348
+ {
349
+ var block;
350
+ }
351
+ something;
352
+ }
353
+ }
354
+ " , // { "ecmaVersion": 2022 },
336
355
"
337
- class C {
338
- static {
339
- something;
340
- {
341
- var block;
342
- }
343
- }
344
- }
345
- ", // { "ecmaVersion": 2022 },
356
+ class C {
357
+ static {
358
+ something;
359
+ {
360
+ var block;
361
+ }
362
+ }
363
+ }
364
+ " , // { "ecmaVersion": 2022 },
346
365
"
347
- class C {
348
- static {
349
- {
350
- block;
351
- }
352
- something;
353
- }
354
- }
355
- ", // { "ecmaVersion": 2022 },
366
+ class C {
367
+ static {
368
+ {
369
+ block;
370
+ }
371
+ something;
372
+ }
373
+ }
374
+ " , // { "ecmaVersion": 2022 },
356
375
"
357
- class C {
358
- static {
359
- something;
360
- {
361
- block;
362
- }
363
- }
364
- }
365
- ", // { "ecmaVersion": 2022 }
376
+ class C {
377
+ static {
378
+ something;
379
+ {
380
+ block;
381
+ }
382
+ }
383
+ }
384
+ " , // { "ecmaVersion": 2022 }
366
385
] ;
367
386
368
387
Tester :: new ( NoLoneBlocks :: NAME , NoLoneBlocks :: PLUGIN , pass, fail) . test_and_snapshot ( ) ;
0 commit comments