feat(linter): eslint 9.0 no empty static block (#1543)

Related [issue](https://github.com/oxc-project/oxc/issues/1191)

---------

Co-authored-by: Radu Baston <radu.baston@sectorlabs.ro>
This commit is contained in:
Radu Baston 2023-11-25 19:09:50 +02:00 committed by GitHub
parent 9ff0ffcc6f
commit 59d0428eb5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 112 additions and 0 deletions

View file

@ -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,

View file

@ -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 wasnt 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();
}

View file

@ -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.