feat(linter) eslint-plugin-unicorn no-static-only-class (#1161)

This commit is contained in:
Cameron 2023-11-06 09:22:13 +00:00 committed by GitHub
parent a5b87c424a
commit de6150a071
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 233 additions and 0 deletions

View file

@ -149,6 +149,7 @@ mod unicorn {
pub mod no_invalid_remove_event_listener;
pub mod no_new_array;
pub mod no_object_as_default_parameter;
pub mod no_static_only_class;
pub mod no_thenable;
pub mod no_unnecessary_await;
pub mod prefer_array_flat_map;
@ -278,6 +279,7 @@ oxc_macros::declare_all_lint_rules! {
unicorn::no_invalid_remove_event_listener,
unicorn::no_new_array,
unicorn::no_object_as_default_parameter,
unicorn::no_static_only_class,
unicorn::no_thenable,
unicorn::no_unnecessary_await,
unicorn::prefer_array_flat_map,

View file

@ -0,0 +1,156 @@
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-plugin-unicorn(no-static-only-class): Disallow classes that only have static members."
)]
#[diagnostic(
severity(warning),
help("A class with only static members could just be an object instead.")
)]
struct NoStaticOnlyClassDiagnostic(#[label] pub Span);
#[derive(Debug, Default, Clone)]
pub struct NoStaticOnlyClass;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow classes that only have static members.
///
/// ### Why is this bad?
///
/// A class with only static members could just be an object instead.
///
///
/// ### Example
/// ```javascript
/// // Bad
/// class A {
/// static a() {}
/// }
///
/// // Good
/// class A {
/// static a() {}
///
/// constructor() {}
/// }
/// ```
NoStaticOnlyClass,
pedantic
);
impl Rule for NoStaticOnlyClass {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
let AstKind::Class(class) = node.kind() else { return };
if class.super_class.is_some() {
return;
}
if class.decorators.len() > 0 {
return;
}
if class.body.body.len() == 0 {
return;
}
if class.body.body.iter().any(|node| {
match node {
oxc_ast::ast::ClassElement::MethodDefinition(v) => {
if v.accessibility.is_some() {
return true;
}
}
oxc_ast::ast::ClassElement::PropertyDefinition(v) => {
if v.accessibility.is_some() || v.readonly || v.declare {
return true;
}
}
oxc_ast::ast::ClassElement::TSAbstractMethodDefinition(v) => {
if v.method_definition.accessibility.is_some() {
return true;
}
}
oxc_ast::ast::ClassElement::TSAbstractPropertyDefinition(v) => {
if v.property_definition.accessibility.is_some() {
return true;
}
}
oxc_ast::ast::ClassElement::AccessorProperty(_)
| oxc_ast::ast::ClassElement::StaticBlock(_)
| oxc_ast::ast::ClassElement::TSIndexSignature(_) => {}
}
if node.r#static() {
if let Some(k) = node.property_key() {
return k.is_private_identifier();
}
return false;
}
true
}) {
return;
}
ctx.diagnostic(NoStaticOnlyClassDiagnostic(class.span));
}
}
#[test]
fn test() {
use crate::tester::Tester;
let pass = vec![
r#"class A {}"#,
r#"const A = class {}"#,
r#"class A extends B { static a() {}; }"#,
r#"const A = class extends B { static a() {}; }"#,
r#"class A { a() {} }"#,
r#"class A { constructor() {} }"#,
r#"class A { get a() {} }"#,
r#"class A { set a(value) {} }"#,
r#"class A3 { static #a() {}; }"#,
r#"class A3 { static #a = 1; }"#,
r#"const A3 = class { static #a() {}; }"#,
r#"const A3 = class { static #a = 1; }"#,
r#"class A2 { static {}; }"#,
r#"class A { static #a() {}; }"#,
r#"class A { static #a = 1; }"#,
r#"const A = class { static #a() {}; }"#,
r#"const A = class { static #a = 1; }"#,
r#"@decorator class A { static a = 1; }"#,
r#"class A { static public a = 1; }"#,
r#"class A { static private a = 1; }"#,
r#"class A { static readonly a = 1; }"#,
r#"class A { static declare a = 1; }"#,
r#"class A { static {}; }"#,
r#"class A2 { static #a() {}; }"#,
r#"class A2 { static #a = 1; }"#,
r#"const A2 = class { static #a() {}; }"#,
r#"const A2 = class { static #a = 1; }"#,
r#"class A2 { static {}; }"#,
];
let fail = vec![
r#"class A { static a() {}; }"#,
r#"class A { static a() {} }"#,
r#"const A = class A { static a() {}; }"#,
r#"const A = class { static a() {}; }"#,
r#"class A { static constructor() {}; }"#,
r#"export default class A { static a() {}; }"#,
r#"export default class { static a() {}; }"#,
r#"export class A { static a() {}; }"#,
r#"class A {static [this.a] = 1}"#,
r#"class A { static a() {} }"#,
];
Tester::new_without_config(NoStaticOnlyClass::NAME, pass, fail).test_and_snapshot();
}

View file

@ -0,0 +1,75 @@
---
source: crates/oxc_linter/src/tester.rs
expression: no_static_only_class
---
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ class A { static a() {}; }
· ──────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ class A { static a() {} }
· ─────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ const A = class A { static a() {}; }
· ──────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ const A = class { static a() {}; }
· ────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ class A { static constructor() {}; }
· ────────────────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ export default class A { static a() {}; }
· ──────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ export default class { static a() {}; }
· ────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ export class A { static a() {}; }
· ──────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ class A {static [this.a] = 1}
· ─────────────────────────────
╰────
help: A class with only static members could just be an object instead.
⚠ eslint-plugin-unicorn(no-static-only-class): Disallow classes that only have static members.
╭─[no_static_only_class.tsx:1:1]
1 │ class A { static a() {} }
· ─────────────────────────
╰────
help: A class with only static members could just be an object instead.