Skip to content

Commit 1de6f85

Browse files
authoredJan 26, 2025··
fix(linter): no-lone-blocks erroring on block statements containing comments (#8720)
This PR fixes #8697 by checking if semantic detects a comment in the span of an otherwise empty block statement. I also formatted some of the existing test cases to improve readability.
1 parent 3e509e1 commit 1de6f85

File tree

2 files changed

+276
-257
lines changed

2 files changed

+276
-257
lines changed
 

Diff for: ‎crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs

+177-158
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,12 @@ impl Rule for NoLoneBlocks {
6262
};
6363

6464
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+
{
6671
report(ctx, node, parent_node);
6772
}
6873
return;
@@ -155,30 +160,30 @@ fn test() {
155160
"{ class Bar {} }", // { "ecmaVersion": 6 },
156161
"{ {let y = 1;} let x = 1; }", // { "ecmaVersion": 6 },
157162
"
158-
switch (foo) {
159-
case bar: {
160-
baz;
161-
}
162-
}
163-
",
163+
switch (foo) {
164+
case bar: {
165+
baz;
166+
}
167+
}
168+
",
164169
"
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+
",
174179
"
175-
switch (foo) {
176-
case bar:
177-
{
178-
baz;
179-
}
180-
}
181-
",
180+
switch (foo) {
181+
case bar:
182+
{
183+
baz;
184+
}
185+
}
186+
",
182187
"function foo() { { const x = 4 } const x = 3 }", // { "ecmaVersion": 6 },
183188
"class C { static {} }", // { "ecmaVersion": 2022 },
184189
"class C { static { foo; } }", // { "ecmaVersion": 2022 },
@@ -189,15 +194,29 @@ fn test() {
189194
"class C { static { { function block(){} } something; } }", // { "ecmaVersion": 2022 },
190195
"class C { static { something; { class block {} } } }", // { "ecmaVersion": 2022 },
191196
"
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 },
195201
"
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 }
199206
// Issue: <https://github.com/oxc-project/oxc/issues/8515>
200207
"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+
",
201220
];
202221

203222
let fail = vec![
@@ -215,154 +234,154 @@ fn test() {
215234
"{var x = 1;}", // { "ecmaVersion": 6 },
216235
"{
217236
{var x = 1;}
218-
let y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
237+
let y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
219238
"{
220239
{let x = 1;}
221-
var y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
240+
var y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
222241
"{
223242
{var x = 1;}
224-
var y = 2; }
225-
{var z = 1;}", // { "ecmaVersion": 6 },
243+
var y = 2; }
244+
{var z = 1;}", // { "ecmaVersion": 6 },
226245
"
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+
",
235254
"
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+
",
244263
"
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 },
251270
"
252-
function foo () {
253-
{
254-
var x = 4;
255-
}
256-
}
257-
",
271+
function foo () {
272+
{
273+
var x = 4;
274+
}
275+
}
276+
",
258277
"
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 },
269288
"
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 },
281300
"
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 },
290309
"
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 },
299318
"
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 },
308327
"
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 },
317336
"
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 },
326345
"
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 },
336355
"
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 },
346365
"
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 },
356375
"
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 }
366385
];
367386

368387
Tester::new(NoLoneBlocks::NAME, NoLoneBlocks::PLUGIN, pass, fail).test_and_snapshot();

0 commit comments

Comments
 (0)
Please sign in to comment.