Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(analyze): add language field to rule metadata #2811

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions crates/biome_analyze/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ The documentation needs to adhere to the following rules:
- When adding _invalid_ snippets in the `### Invalid` section, you must use the `expect_diagnostic` code block property. We use this property to generate a diagnostic and attach it to the snippet. A snippet **must emit only ONE diagnostic**.
- When adding _valid_ snippets in the `### Valid` section, you can use one single snippet.
- You can use the code block property `ignore` to tell the code generation script to **not generate a diagnostic for an invalid snippet**.
- Update the `language` field in the `declare_rule!` macro to the language the rule primarily applies to.
- If your rule applies to any JavaScript, you can leave it as `js`.
- If your rule only makes sense in a specific JavaScript dialect, you should set it to `jsx`, `ts`, or `tsx`, whichever is most appropriate.

Here's an example of how the documentation could look like:

Expand Down Expand Up @@ -218,6 +221,7 @@ declare_rule! {
pub(crate) NoVar {
version: "next",
name: "noVar",
language: "js",
recommended: false,
}
}
Expand Down Expand Up @@ -373,6 +377,7 @@ declare_rule! {
pub(crate) NoVar {
version: "1.0.0",
name: "noVar",
language: "js",
deprecated: "Use the rule `noAnotherVar`",
recommended: false,
}
Expand Down Expand Up @@ -495,6 +500,7 @@ declare_rule! {
pub(crate) ExampleRule {
version: "next",
name: "myRuleName",
language: "js",
recommended: false,
}
}
Expand All @@ -516,6 +522,7 @@ declare_rule! {
pub(crate) ExampleRule {
version: "next",
name: "myRuleName",
language: "js",
recommended: false,
}
}
Expand Down
18 changes: 16 additions & 2 deletions crates/biome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub struct RuleMetadata {
pub name: &'static str,
/// The content of the documentation comments for this rule
pub docs: &'static str,
/// The language that the rule applies to.
pub language: &'static str,
/// Whether a rule is recommended or not
pub recommended: bool,
/// The kind of fix
Expand Down Expand Up @@ -242,12 +244,18 @@ impl RuleSourceKind {
}

impl RuleMetadata {
pub const fn new(version: &'static str, name: &'static str, docs: &'static str) -> Self {
pub const fn new(
version: &'static str,
name: &'static str,
docs: &'static str,
language: &'static str,
) -> Self {
Self {
deprecated: None,
version,
name,
docs,
language,
recommended: false,
fix_kind: None,
sources: &[],
Expand Down Expand Up @@ -282,6 +290,11 @@ impl RuleMetadata {
self.source_kind = Some(source_kind);
self
}

pub const fn language(mut self, language: &'static str) -> Self {
self.language = language;
self
}
}

pub trait RuleMeta {
Expand Down Expand Up @@ -315,6 +328,7 @@ macro_rules! declare_rule {
( $( #[doc = $doc:literal] )+ $vis:vis $id:ident {
version: $version:literal,
name: $name:tt,
language: $language:literal,
$( $key:ident: $value:expr, )*
} ) => {
$( #[doc = $doc] )*
Expand All @@ -323,7 +337,7 @@ macro_rules! declare_rule {
impl $crate::RuleMeta for $id {
type Group = super::Group;
const METADATA: $crate::RuleMetadata =
$crate::RuleMetadata::new($version, $name, concat!( $( $doc, "\n", )* )) $( .$key($value) )*;
$crate::RuleMetadata::new($version, $name, concat!( $( $doc, "\n", )* ), $language) $( .$key($value) )*;
}

// Declare a new `rule_category!` macro in the module context that
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare_rule! {
pub NoColorInvalidHex {
version: "next",
name: "noColorInvalidHex",
language: "css",
recommended: false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ declare_rule! {
pub NoCssEmptyBlock {
version: "next",
name: "noCssEmptyBlock",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("no-empty-block")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ declare_rule! {
pub NoDuplicateAtImportRules {
version: "next",
name: "noDuplicateAtImportRules",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("no-duplicate-at-import-rules")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ declare_rule! {
pub NoDuplicateFontNames {
version: "next",
name: "noDuplicateFontNames",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("font-family-no-duplicate-names")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare_rule! {
pub NoDuplicateSelectorsKeyframeBlock {
version: "next",
name: "noDuplicateSelectorsKeyframeBlock",
language: "css",
recommended: true,
sources:&[RuleSource::Stylelint("keyframe-block-no-duplicate-selectors")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare_rule! {
pub NoImportantInKeyframe {
version: "next",
name: "noImportantInKeyframe",
language: "css",
recommended: true,
sources:&[RuleSource::Stylelint("keyframe-declaration-no-important")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ declare_rule! {
pub NoInvalidPositionAtImportRule {
version: "next",
name: "noInvalidPositionAtImportRule",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("no-invalid-position-at-import-rule")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare_rule! {
pub NoUnknownFunction {
version: "next",
name: "noUnknownFunction",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("function-no-unknown")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ declare_rule! {
pub NoUnknownMediaFeatureName {
version: "next",
name: "noUnknownMediaFeatureName",
language: "css",
recommended: false,
sources: &[RuleSource::Stylelint("media-feature-name-no-unknown")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ declare_rule! {
pub NoUnknownProperty {
version: "next",
name: "noUnknownProperty",
language: "css",
recommended: false,
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ declare_rule! {
pub NoUnknownSelectorPseudoElement {
version: "next",
name: "noUnknownSelectorPseudoElement",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("selector-pseudo-element-no-unknown")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ declare_rule! {
pub NoUnknownUnit {
version: "next",
name: "noUnknownUnit",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("unit-no-unknown")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ declare_rule! {
pub NoUnmatchableAnbSelector {
version: "next",
name: "noUnmatchableAnbSelector",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("selector-anb-no-unmatchable")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare_rule! {
pub UseGenericFontNames {
version: "next",
name: "useGenericFontNames",
language: "css",
recommended: true,
sources: &[RuleSource::Stylelint("font-family-no-missing-generic-family-keyword")],
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ declare_rule! {
pub OrganizeImports {
version: "1.0.0",
name: "organizeImports",
language: "js",
recommended: false,
fix_kind: FixKind::Unsafe,
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/no_access_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare_rule! {
pub NoAccessKey {
version: "1.0.0",
name: "noAccessKey",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-access-key")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare_rule! {
pub NoAriaHiddenOnFocusable {
version: "1.4.0",
name: "noAriaHiddenOnFocusable",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-aria-hidden-on-focusable")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ declare_rule! {
pub NoAriaUnsupportedElements {
version: "1.0.0",
name: "noAriaUnsupportedElements",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("aria-unsupported-elements")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/no_autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare_rule! {
pub NoAutofocus {
version: "1.0.0",
name: "noAutofocus",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-autofocus")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/no_blank_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_rule! {
pub NoBlankTarget {
version: "1.0.0",
name: "noBlankTarget",
language: "jsx",
sources: &[RuleSource::EslintReact("jsx-no-target-blank")],
recommended: true,
fix_kind: FixKind::Safe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare_rule! {
pub NoDistractingElements {
version: "1.0.0",
name: "noDistractingElements",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-distracting-elements")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/no_header_scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ declare_rule! {
pub NoHeaderScope {
version: "1.0.0",
name: "noHeaderScope",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("scope")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ declare_rule! {
pub NoInteractiveElementToNoninteractiveRole {
version: "1.3.0",
name: "noInteractiveElementToNoninteractiveRole",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-interactive-element-to-noninteractive-role")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ declare_rule! {
pub NoNoninteractiveElementToInteractiveRole {
version: "1.0.0",
name: "noNoninteractiveElementToInteractiveRole",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-noninteractive-element-to-interactive-role")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_rule! {
pub NoNoninteractiveTabindex {
version: "1.0.0",
name: "noNoninteractiveTabindex",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-noninteractive-tabindex")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_rule! {
pub NoPositiveTabindex {
version: "1.0.0",
name: "noPositiveTabindex",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("tabindex-no-positive")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/no_redundant_alt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ declare_rule! {
pub NoRedundantAlt {
version: "1.0.0",
name: "noRedundantAlt",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("img-redundant-alt")],
recommended: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ declare_rule! {
pub NoRedundantRoles {
version: "1.0.0",
name: "noRedundantRoles",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("no-redundant-roles")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ declare_rule! {
pub NoSvgWithoutTitle {
version: "1.0.0",
name: "noSvgWithoutTitle",
language: "jsx",
recommended: true,
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/use_alt_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ declare_rule! {
pub UseAltText {
version: "1.0.0",
name: "useAltText",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("alt-text")],
recommended: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ declare_rule! {
pub UseAnchorContent {
version: "1.0.0",
name: "useAnchorContent",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("anchor-has-content")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare_rule! {
pub UseAriaActivedescendantWithTabindex {
version: "1.3.0",
name: "useAriaActivedescendantWithTabindex",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("aria-activedescendant-has-tabindex")],
recommended: true,
fix_kind: FixKind::Unsafe,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ declare_rule! {
pub UseAriaPropsForRole {
version: "1.0.0",
name: "useAriaPropsForRole",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("role-has-required-aria-props")],
recommended: true,
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/use_button_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ declare_rule! {
pub UseButtonType {
version: "1.0.0",
name: "useButtonType",
language: "jsx",
sources: &[RuleSource::EslintReact("button-has-type")],
recommended: true,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ declare_rule! {
pub UseHeadingContent {
version: "1.0.0",
name: "useHeadingContent",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("heading-has-content")],
recommended: true,
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/use_html_lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ declare_rule! {
pub UseHtmlLang {
version: "1.0.0",
name: "useHtmlLang",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("html-has-lang")],
recommended: true,
}
Expand Down
1 change: 1 addition & 0 deletions crates/biome_js_analyze/src/lint/a11y/use_iframe_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ declare_rule! {
pub UseIframeTitle {
version: "1.0.0",
name: "useIframeTitle",
language: "jsx",
sources: &[RuleSource::EslintJsxA11y("iframe-has-title")],
recommended: true,
}
Expand Down
Loading
Loading