Skip to content

Commit

Permalink
refactor(linter): add source metadata (#1519)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Jan 11, 2024
1 parent f423ed0 commit 7f78594
Show file tree
Hide file tree
Showing 359 changed files with 787 additions and 526 deletions.
2 changes: 1 addition & 1 deletion crates/biome_analyze/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub use crate::registry::{
};
pub use crate::rule::{
CategoryLanguage, FixKind, GroupCategory, GroupLanguage, Rule, RuleAction, RuleDiagnostic,
RuleGroup, RuleMeta, RuleMetadata, Source, SourceKind, SuppressAction,
RuleGroup, RuleMeta, RuleMetadata, RuleSource, RuleSourceKind, SuppressAction,
};
pub use crate::services::{FromServices, MissingServicesDiagnostic, ServiceBag};
pub use crate::signals::{
Expand Down
58 changes: 39 additions & 19 deletions crates/biome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ pub struct RuleMetadata {
/// The kind of fix
pub fix_kind: Option<FixKind>,
/// The source URL of the rule
pub source: Option<Source>,
pub source: Option<RuleSource>,
/// The source kind of the rule
pub source_kind: Option<SourceKind>,
pub source_kind: Option<RuleSourceKind>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
Expand All @@ -57,11 +57,15 @@ impl Display for FixKind {
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum Source {
pub enum RuleSource {
/// Rules from [Rust Clippy](https://rust-lang.github.io/rust-clippy/master/index.html)
Clippy(&'static str),
/// Rules from [Eslint](https://eslint.org/)
Eslint(&'static str),
/// Rules from [Eslint Plugin Import](https://github.com/import-js/eslint-plugin-import)
EslintImport(&'static str),
/// Rules from [Eslint Plugin Import Access](https://github.com/uhyo/eslint-plugin-import-access)
EslintImportAccess(&'static str),
/// Rules from [Eslint Plugin Jest](https://github.com/jest-community/eslint-plugin-jest)
EslintJest(&'static str),
/// Rules from [Eslint Plugin JSX A11y](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y)
Expand All @@ -70,38 +74,50 @@ pub enum Source {
EslintReact(&'static str),
/// Rules from [Eslint Plugin React Hooks](https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md)
EslintReactHooks(&'static str),
/// Rules from [Typescript Eslint Plugin](https://typescript-eslint.io)
/// Rules from [Eslint Plugin Sonar](https://github.com/SonarSource/eslint-plugin-sonarjs)
EslintSonarJs(&'static str),
/// Rules from [Eslint Plugin Stylistic](https://eslint.style)
EslintStylistic(&'static str),
/// Rules from [Eslint Plugin Typescript](https://typescript-eslint.io)
EslintTypeScript(&'static str),
/// Rules from [Eslint Plugin Unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn)
EslintUnicorn(&'static str),
/// Rules from [Eslint Plugin Mysticatea](https://github.com/mysticatea/eslint-plugin)
EslintMysticatea(&'static str),
}

impl Source {
impl RuleSource {
pub fn as_rule_name(&self) -> &'static str {
match self {
Self::Clippy(rule_name) => rule_name,
Self::Eslint(rule_name) => rule_name,
Self::EslintJest(rule_name) => rule_name,
Self::EslintJsxA11y(rule_name) => rule_name,
Self::EslintReact(rule_name) => rule_name,
Self::EslintReactHooks(rule_name) => rule_name,
Self::EslintTypeScript(rule_name) => rule_name,
Self::EslintUnicorn(rule_name) => rule_name,
Self::EslintMysticatea(rule_name) => rule_name,
Self::Clippy(rule_name)
| Self::Eslint(rule_name)
| Self::EslintImport(rule_name)
| Self::EslintImportAccess(rule_name)
| Self::EslintJest(rule_name)
| Self::EslintJsxA11y(rule_name)
| Self::EslintReact(rule_name)
| Self::EslintReactHooks(rule_name)
| Self::EslintTypeScript(rule_name)
| Self::EslintSonarJs(rule_name)
| Self::EslintStylistic(rule_name)
| Self::EslintUnicorn(rule_name)
| Self::EslintMysticatea(rule_name) => rule_name,
}
}

pub fn as_rule_url(&self) -> String {
match self {
Self::Clippy(rule_name) => format!("https://rust-lang.github.io/rust-clippy/master/#/{rule_name}"),
Self::Eslint(rule_name) => format!( "https://eslint.org/docs/latest/rules/{rule_name}"),
Self::Eslint(rule_name) => format!("https://eslint.org/docs/latest/rules/{rule_name}"),
Self::EslintImport(rule_name) => format!("https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/{rule_name}.md"),
Self::EslintImportAccess(rule_name) => format!("https://github.com/uhyo/eslint-plugin-import-access/blob/main/docs/rules/{rule_name}.md"),
Self::EslintJest(rule_name) => format!("https://github.com/jest-community/eslint-plugin-jest/blob/main/docs/rules/{rule_name}.md"),
Self::EslintJsxA11y(rule_name) => format!("https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/{rule_name}.md"),
Self::EslintReact(rule_name) => format!("https://github.com/jsx-eslint/eslint-plugin-react/blob/master/docs/rules/{rule_name}.md"),
Self::EslintReactHooks(_) => "https://github.com/facebook/react/blob/main/packages/eslint-plugin-react-hooks/README.md".to_string(),
Self::EslintTypeScript(rule_name) => format!("https://typescript-eslint.io/rules/{rule_name}"),
Self::EslintSonarJs(rule_name) => format!("https://github.com/SonarSource/eslint-plugin-sonarjs/blob/HEAD/docs/rules/{rule_name}.md"),
Self::EslintStylistic(rule_name) => format!("https://eslint.style/rules/default/{rule_name}"),
Self::EslintUnicorn(rule_name) => format!("https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/{rule_name}.md"),
Self::EslintMysticatea(rule_name) => format!("https://github.com/mysticatea/eslint-plugin/blob/master/docs/rules/{rule_name}.md"),
}
Expand All @@ -112,9 +128,10 @@ impl Source {
}
}

#[derive(Debug, Clone)]
pub enum SourceKind {
#[derive(Debug, Default, Clone)]
pub enum RuleSourceKind {
/// The rule implements the same logic of the source
#[default]
SameLogic,
/// The rule deviate of the logic of the source
Inspired,
Expand Down Expand Up @@ -149,12 +166,15 @@ impl RuleMetadata {
self
}

pub const fn source(mut self, source: Source) -> Self {
pub const fn source(mut self, source: RuleSource) -> Self {
self.source = Some(source);
//if self.source_kind.is_none() {
// self.source_kind = Some(RuleSourceKind::SameLogic);
//}
self
}

pub const fn source_kind(mut self, source_kind: SourceKind) -> Self {
pub const fn source_kind(mut self, source_kind: RuleSourceKind) -> Self {
self.source_kind = Some(source_kind);
self
}
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/analyzers/a11y/no_access_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::JsRuleAction;
use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic,
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
Expand Down Expand Up @@ -39,6 +40,7 @@ declare_rule! {
pub(crate) NoAccessKey {
version: "1.0.0",
name: "noAccessKey",
source: RuleSource::EslintJsxA11y("no-access-key"),
recommended: true,
fix_kind: FixKind::Unsafe,
}
Expand Down
2 changes: 2 additions & 0 deletions crates/biome_js_analyze/src/analyzers/a11y/no_auto_focus.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::JsRuleAction;
use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic,
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
Expand Down Expand Up @@ -59,6 +60,7 @@ declare_rule! {
pub(crate) NoAutoFocus {
version: "1.0.0",
name: "noAutofocus",
source: RuleSource::EslintJsxA11y("no-autofocus"),
recommended: true,
fix_kind: FixKind::Unsafe,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::JsRuleAction;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make::{
Expand Down Expand Up @@ -51,6 +51,7 @@ declare_rule! {
pub(crate) NoBlankTarget {
version: "1.0.0",
name: "noBlankTarget",
source: RuleSource::EslintJsxA11y("jsx-no-target-blank"),
recommended: true,
fix_kind: FixKind::Safe,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
Expand Down Expand Up @@ -40,6 +40,7 @@ declare_rule! {
pub(crate) NoDistractingElements {
version: "1.0.0",
name: "noDistractingElements",
source: RuleSource::EslintJsxA11y("no-distracting-elements"),
recommended: true,
fix_kind: FixKind::Unsafe,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
Expand Down Expand Up @@ -40,6 +40,7 @@ declare_rule! {
pub(crate) NoHeaderScope {
version: "1.0.0",
name: "noHeaderScope",
source: RuleSource::EslintJsxA11y("scope"),
recommended: true,
fix_kind: FixKind::Unsafe,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_js_syntax::{
Expand Down Expand Up @@ -42,6 +42,7 @@ declare_rule! {
pub(crate) NoRedundantAlt {
version: "1.0.0",
name: "noRedundantAlt",
source: RuleSource::EslintJsxA11y("no-redundant-roles"),
recommended: true,
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_js_analyze/src/analyzers/a11y/use_alt_text.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::{fmt::Display, fmt::Formatter, markup};
use biome_js_syntax::{jsx_ext::AnyJsxElement, TextRange};
use biome_rowan::AstNode;
Expand Down Expand Up @@ -46,6 +46,7 @@ declare_rule! {
pub(crate) UseAltText {
version: "1.0.0",
name: "useAltText",
source: RuleSource::EslintJsxA11y("alt-text"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
Expand Down Expand Up @@ -67,6 +67,7 @@ declare_rule! {
pub(crate) UseAnchorContent {
version: "1.0.0",
name: "useAnchorContent",
source: RuleSource::EslintJsxA11y("anchor-has-content"),
recommended: true,
fix_kind: FixKind::Unsafe,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::{jsx_ext::AnyJsxElement, JsxElement};
use biome_rowan::AstNode;
Expand Down Expand Up @@ -47,6 +47,7 @@ declare_rule! {
pub(crate) UseHeadingContent {
version: "1.0.0",
name: "useHeadingContent",
source: RuleSource::EslintJsxA11y("heading-has-content"),
recommended: true,
}
}
Expand Down
3 changes: 2 additions & 1 deletion crates/biome_js_analyze/src/analyzers/a11y/use_html_lang.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::{jsx_ext::AnyJsxElement, TextRange};
use biome_rowan::AstNode;
Expand Down Expand Up @@ -55,6 +55,7 @@ declare_rule! {
pub(crate) UseHtmlLang {
version: "1.0.0",
name: "useHtmlLang",
source: RuleSource::EslintJsxA11y("html-has-lang"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::AstNode;
Expand Down Expand Up @@ -61,6 +61,7 @@ declare_rule! {
pub(crate) UseIframeTitle {
version: "1.0.0",
name: "useIframeTitle",
source: RuleSource::EslintJsxA11y("iframe-has-title"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{context::RuleContext, declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::{jsx_ext::AnyJsxElement, AnyJsxAttribute, AnyJsxElementName};
use biome_rowan::AstNode;
Expand Down Expand Up @@ -59,6 +59,7 @@ declare_rule! {
pub(crate) UseKeyWithClickEvents {
version: "1.0.0",
name: "useKeyWithClickEvents",
source: RuleSource::EslintJsxA11y("click-events-have-key-events"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::semantic_services::Semantic;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, Rule, RuleDiagnostic, RuleSource};
use biome_console::{markup, MarkupBuf};
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::AstNode;
Expand Down Expand Up @@ -42,6 +42,7 @@ declare_rule! {
pub(crate) UseKeyWithMouseEvents {
version: "1.0.0",
name: "useKeyWithMouseEvents",
source: RuleSource::EslintJsxA11y("mouse-events-have-key-events"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_js_syntax::{AnyJsxChild, JsxElement, TextRange};
Expand All @@ -8,8 +8,6 @@ use biome_rowan::AstNode;
declare_rule! {
/// Enforces that `audio` and `video` elements must have a `track` for captions.
///
/// **ESLint Equivalent:** [media-has-caption](https://github.com/jsx-eslint/eslint-plugin-jsx-a11y/blob/main/docs/rules/media-has-caption.md)
///
/// ## Examples
///
/// ### Invalid
Expand All @@ -35,6 +33,7 @@ declare_rule! {
pub(crate) UseMediaCaption {
version: "1.0.0",
name: "useMediaCaption",
source: RuleSource::EslintJsxA11y("media-has-caption"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic};
use biome_analyze::{declare_rule, Ast, Rule, RuleDiagnostic, RuleSource};
use biome_console::{markup, MarkupBuf};
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, TextRange};
Expand Down Expand Up @@ -66,6 +66,7 @@ declare_rule! {
pub(crate) UseValidAnchor {
version: "1.0.0",
name: "useValidAnchor",
source: RuleSource::EslintJsxA11y("anchor-is-valid"),
recommended: true,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use biome_analyze::{
context::RuleContext, declare_rule, AddVisitor, Phases, QueryMatch, Queryable, Rule,
RuleDiagnostic, ServiceBag, Visitor, VisitorContext,
RuleDiagnostic, RuleSource, ServiceBag, Visitor, VisitorContext,
};
use biome_console::markup;
use biome_deserialize::{
Expand Down Expand Up @@ -37,10 +37,6 @@ declare_rule! {
/// The complexity score is calculated based on the Cognitive Complexity
/// algorithm: http://redirect.sonarsource.com/doc/cognitive-complexity.html
///
/// Source:
///
/// * https://github.com/SonarSource/eslint-plugin-sonarjs/blob/HEAD/docs/rules/cognitive-complexity.md
///
/// ## Examples
///
/// ### Invalid
Expand Down Expand Up @@ -79,6 +75,7 @@ declare_rule! {
pub(crate) NoExcessiveCognitiveComplexity {
version: "1.0.0",
name: "noExcessiveCognitiveComplexity",
source: RuleSource::EslintSonarJs("cognitive-complexity"),
recommended: false,
}
}
Expand Down
Loading

0 comments on commit 7f78594

Please sign in to comment.