Skip to content

Commit

Permalink
fix(linter): false positive in eslint/no-lone-blocks (#8587)
Browse files Browse the repository at this point in the history
closes #8515
  • Loading branch information
shulaoda authored Jan 18, 2025
1 parent ee8ee55 commit c15af02
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
14 changes: 9 additions & 5 deletions crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ impl Rule for NoLoneBlocks {

let body = &stmt.body;
if body.is_empty() {
report(ctx, node, parent_node);
if !matches!(parent_node.kind(), AstKind::TryStatement(_) | AstKind::CatchClause(_)) {
report(ctx, node, parent_node);
}
return;
}

Expand Down Expand Up @@ -206,27 +208,29 @@ fn test() {
{
await using x = makeDisposable();
}", // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 }
// Issue: <https://github.com/oxc-project/oxc/issues/8515>
"try {} catch {}",
];

let fail = vec![
"{}",
"{var x = 1;}",
"foo(); {} bar();",
"if (foo) { bar(); {} baz(); }",
"{
"{
{ } }",
"function foo() { bar(); {} baz(); }",
"while (foo) { {} }",
// MEMO: Currently, this rule always analyzes in strict mode (as it cannot retrieve ecmaFeatures).
// "{ function bar() {} }", // { "ecmaVersion": 6 },
"{var x = 1;}", // { "ecmaVersion": 6 },
"{
"{
{var x = 1;}
let y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
"{
"{
{let x = 1;}
var y = 2; } {let z = 1;}", // { "ecmaVersion": 6 },
"{
"{
{var x = 1;}
var y = 2; }
{var z = 1;}", // { "ecmaVersion": 6 },
Expand Down
13 changes: 6 additions & 7 deletions crates/oxc_linter/src/snapshots/eslint_no_lone_blocks.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
---
source: crates/oxc_linter/src/tester.rs
snapshot_kind: text
---
eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:1:1]
Expand Down Expand Up @@ -28,13 +27,13 @@ snapshot_kind: text

eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:1:1]
1 │ ╭─▶ {
1 │ ╭─▶ {
2 │ ╰─▶ { } }
╰────

eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:2:4]
1 │ {
1 │ {
2 │ { } }
· ───
╰────
Expand All @@ -59,30 +58,30 @@ snapshot_kind: text

eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:2:4]
1 │ {
1 │ {
2 │ {var x = 1;}
· ────────────
3let y = 2; } {let z = 1;}
╰────

eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:1:1]
1 │ ╭─▶ {
1 │ ╭─▶ {
2 │ │ {let x = 1;}
3 │ ╰─▶ var y = 2; } {let z = 1;}
╰────

eslint(no-lone-blocks): Block is unnecessary.
╭─[no_lone_blocks.tsx:1:1]
1 │ ╭─▶ {
1 │ ╭─▶ {
2 │ │ {var x = 1;}
3 │ ╰─▶ var y = 2; }
4 │ {var z = 1;}
╰────

eslint(no-lone-blocks): Nested block is redundant.
╭─[no_lone_blocks.tsx:2:4]
1 │ {
1 │ {
2 │ {var x = 1;}
· ────────────
3var y = 2; }
Expand Down

0 comments on commit c15af02

Please sign in to comment.