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.
This commit is contained in:
Tyler Earls 2025-01-25 19:20:05 -06:00 committed by GitHub
parent 3e509e1c9b
commit 1de6f854cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 276 additions and 257 deletions

View file

@ -62,7 +62,12 @@ impl Rule for NoLoneBlocks {
};
if stmt.body.is_empty() {
if !matches!(parent_node.kind(), AstKind::TryStatement(_) | AstKind::CatchClause(_)) {
let is_comment_in_stmt =
ctx.semantic().comments_range(stmt.span.start..stmt.span.end).last().is_some();
if !is_comment_in_stmt
&& !matches!(parent_node.kind(), AstKind::TryStatement(_) | AstKind::CatchClause(_))
{
report(ctx, node, parent_node);
}
return;
@ -191,13 +196,27 @@ fn test() {
"
{
using x = makeDisposable();
}", // { "parser": require(parser("typescript-parsers/no-lone-blocks/using")), "ecmaVersion": 2022 },
}
", // { "parser": require(parser("typescript-parsers/no-lone-blocks/using")), "ecmaVersion": 2022 },
"
{
await using x = makeDisposable();
}", // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 }
}
", // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 }
// Issue: <https://github.com/oxc-project/oxc/issues/8515>
"try {} catch {}",
// Issue: https://github.com/oxc-project/oxc/issues/8697
"
if (foo) {
// do nothing
}
else if (bar) {
// do nothing again
}
else {
// do nothing
}
",
];
let fail = vec![

View file

@ -94,14 +94,14 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:4:5]
╭─[no_lone_blocks.tsx:4:4]
3 │ var y = 2; }
4 │ {var z = 1;}
· ────────────
╰────
⚠ eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:5:24]
╭─[no_lone_blocks.tsx:5:17]
4 │ foo();
5 │ ╭─▶ {
6 │ │ bar;
@ -110,7 +110,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:4:20]
╭─[no_lone_blocks.tsx:4:21]
3 │ case 1:
4 │ ╭─▶ {
5 │ │ bar;
@ -119,7 +119,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:3:20]
╭─[no_lone_blocks.tsx:3:17]
2 │ function foo () {
3 │ ╭─▶ {
4 │ │ const x = 4;
@ -128,7 +128,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:3:20]
╭─[no_lone_blocks.tsx:3:17]
2 │ function foo () {
3 │ ╭─▶ {
4 │ │ var x = 4;
@ -137,7 +137,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:5:24]
╭─[no_lone_blocks.tsx:5:25]
4 │ if (foo) {
5 │ ╭─▶ {
6 │ │ let block;
@ -146,7 +146,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:5:24]
╭─[no_lone_blocks.tsx:5:25]
4 │ if (foo) {
5 │ ╭─▶ {
6 │ │ block;
@ -155,7 +155,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ block;
@ -164,7 +164,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ let block;
@ -173,7 +173,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ const block = 1;
@ -182,7 +182,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ function block() {}
@ -191,7 +191,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ class block {}
@ -200,7 +200,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ var block;
@ -209,7 +209,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:5:22]
╭─[no_lone_blocks.tsx:5:21]
4 │ something;
5 │ ╭─▶ {
6 │ │ var block;
@ -218,7 +218,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:4:22]
╭─[no_lone_blocks.tsx:4:21]
3 │ static {
4 │ ╭─▶ {
5 │ │ block;
@ -227,7 +227,7 @@ source: crates/oxc_linter/src/tester.rs
╰────
⚠ eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:5:22]
╭─[no_lone_blocks.tsx:5:21]
4 │ something;
5 │ ╭─▶ {
6 │ │ block;