diff --git a/crates/oxc_linter/src/rules.rs b/crates/oxc_linter/src/rules.rs index 7750b5f7c..873f2a7ad 100644 --- a/crates/oxc_linter/src/rules.rs +++ b/crates/oxc_linter/src/rules.rs @@ -54,6 +54,7 @@ mod eslint { pub mod no_empty; pub mod no_empty_character_class; pub mod no_empty_pattern; + pub mod no_empty_static_block; pub mod no_eval; pub mod no_ex_assign; pub mod no_extra_boolean_cast; @@ -249,6 +250,7 @@ oxc_macros::declare_all_lint_rules! { eslint::no_empty, eslint::no_empty_character_class, eslint::no_empty_pattern, + eslint::no_empty_static_block, eslint::no_eval, eslint::no_ex_assign, eslint::no_extra_boolean_cast, diff --git a/crates/oxc_linter/src/rules/eslint/no_empty_static_block.rs b/crates/oxc_linter/src/rules/eslint/no_empty_static_block.rs new file mode 100644 index 000000000..64ba4df03 --- /dev/null +++ b/crates/oxc_linter/src/rules/eslint/no_empty_static_block.rs @@ -0,0 +1,76 @@ +use oxc_ast::AstKind; +use oxc_diagnostics::{ + miette::{self, Diagnostic}, + thiserror::Error, +}; +use oxc_macros::declare_oxc_lint; +use oxc_span::Span; + +use crate::{context::LintContext, rule::Rule, AstNode}; + +#[derive(Debug, Error, Diagnostic)] +#[error("eslint(no-empty-static-block): Disallow empty static blocks")] +#[diagnostic(severity(warning), help("Unexpected empty static block."))] +struct NoEmptyStaticBlockDiagnostic(#[label] pub Span); + +#[derive(Debug, Default, Clone)] +pub struct NoEmptyStaticBlock; + +declare_oxc_lint!( + /// ### What it does + /// Disallows the usages of empty static blocks + /// + /// ### Why is this bad? + /// Empty block statements, while not technically errors, usually occur due to refactoring that wasn’t completed. + /// They can cause confusion when reading code. + /// + /// ### Example + /// ```javascript + /// + /// class Foo { + /// static { + /// } + /// } + /// + /// ``` + NoEmptyStaticBlock, + suspicious +); + +impl Rule for NoEmptyStaticBlock { + fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { + if let AstKind::StaticBlock(static_block) = node.kind() { + if static_block.body.is_empty() { + if ctx.semantic().trivias().has_comments_between(static_block.span) { + return; + } + ctx.diagnostic(NoEmptyStaticBlockDiagnostic(static_block.span)); + } + } + } +} + +#[test] +fn test() { + use crate::tester::Tester; + + let pass = vec![ + "class Foo { static { bar(); } }", + "class Foo { static { /* comments */ } }", + "class Foo { static { + // comment + } }", + "class Foo { static { bar(); } static { bar(); } }", + ]; + + let fail = vec![ + "class Foo { static {} }", + "class Foo { static { } }", + "class Foo { static { + + } }", + "class Foo { static { bar(); } static {} }", + ]; + + Tester::new_without_config(NoEmptyStaticBlock::NAME, pass, fail).test_and_snapshot(); +} diff --git a/crates/oxc_linter/src/snapshots/no_empty_static_block.snap b/crates/oxc_linter/src/snapshots/no_empty_static_block.snap new file mode 100644 index 000000000..e98d1afc7 --- /dev/null +++ b/crates/oxc_linter/src/snapshots/no_empty_static_block.snap @@ -0,0 +1,34 @@ +--- +source: crates/oxc_linter/src/tester.rs +expression: no_empty_static_block +--- + ⚠ eslint(no-empty-static-block): Disallow empty static blocks + ╭─[no_empty_static_block.tsx:1:1] + 1 │ class Foo { static {} } + · ───────── + ╰──── + help: Unexpected empty static block. + + ⚠ eslint(no-empty-static-block): Disallow empty static blocks + ╭─[no_empty_static_block.tsx:1:1] + 1 │ class Foo { static { } } + · ────────── + ╰──── + help: Unexpected empty static block. + + ⚠ eslint(no-empty-static-block): Disallow empty static blocks + ╭─[no_empty_static_block.tsx:1:1] + 1 │ ╭─▶ class Foo { static { + 2 │ │ + 3 │ ╰─▶ } } + ╰──── + help: Unexpected empty static block. + + ⚠ eslint(no-empty-static-block): Disallow empty static blocks + ╭─[no_empty_static_block.tsx:1:1] + 1 │ class Foo { static { bar(); } static {} } + · ───────── + ╰──── + help: Unexpected empty static block. + +