From c15af02e52e124f3968b098d487e28b49d937b56 Mon Sep 17 00:00:00 2001 From: dalaoshu Date: Sun, 19 Jan 2025 00:08:57 +0800 Subject: [PATCH] fix(linter): false positive in `eslint/no-lone-blocks` (#8587) closes #8515 --- .../oxc_linter/src/rules/eslint/no_lone_blocks.rs | 14 +++++++++----- .../src/snapshots/eslint_no_lone_blocks.snap | 13 ++++++------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs b/crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs index 0f016eff87507..88ae706df7a06 100644 --- a/crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs +++ b/crates/oxc_linter/src/rules/eslint/no_lone_blocks.rs @@ -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; } @@ -206,6 +208,8 @@ fn test() { { await using x = makeDisposable(); }", // { "parser": require(parser("typescript-parsers/no-lone-blocks/await-using")), "ecmaVersion": 2022 } + // Issue: + "try {} catch {}", ]; let fail = vec![ @@ -213,20 +217,20 @@ fn test() { "{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 }, diff --git a/crates/oxc_linter/src/snapshots/eslint_no_lone_blocks.snap b/crates/oxc_linter/src/snapshots/eslint_no_lone_blocks.snap index a1677b9f013d7..3bababa79d1a1 100644 --- a/crates/oxc_linter/src/snapshots/eslint_no_lone_blocks.snap +++ b/crates/oxc_linter/src/snapshots/eslint_no_lone_blocks.snap @@ -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] @@ -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 │ { } } · ─── ╰──── @@ -59,7 +58,7 @@ snapshot_kind: text ⚠ eslint(no-lone-blocks): Nested block is redundant. ╭─[no_lone_blocks.tsx:2:4] - 1 │ { + 1 │ { 2 │ {var x = 1;} · ──────────── 3 │ let y = 2; } {let z = 1;} @@ -67,14 +66,14 @@ snapshot_kind: text ⚠ 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;} @@ -82,7 +81,7 @@ snapshot_kind: text ⚠ eslint(no-lone-blocks): Nested block is redundant. ╭─[no_lone_blocks.tsx:2:4] - 1 │ { + 1 │ { 2 │ {var x = 1;} · ──────────── 3 │ var y = 2; }