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(codemod/biome_js_analyze): derive Applicability for all lint rule actions #2889

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
2 changes: 1 addition & 1 deletion crates/biome_analyze/src/rule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl RuleMetadata {
self
}

pub fn to_applicability(&self) -> Applicability {
pub fn applicability(&self) -> Applicability {
self.fix_kind
.try_into()
.expect("Fix kind is not set in the rule metadata")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Rule for OrganizeImports {

Some(JsRuleAction::new(
ActionCategory::Source(SourceActionKind::OrganizeImports),
ctx.metadata().to_applicability(),
ctx.metadata().applicability(),
markup! { "Organize Imports (Biome)" },
mutation,
))
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_access_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{jsx_ext::AnyJsxElement, JsxAttribute, JsxAttributeList};
use biome_rowan::{AstNode, BatchMutationExt};

Expand Down Expand Up @@ -102,7 +101,7 @@ impl Rule for NoAccessKey {
mutation.remove_node(node.clone());
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"accessKey"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, BatchMutationExt};

Expand Down Expand Up @@ -116,7 +115,7 @@ impl Rule for NoAriaHiddenOnFocusable {
mutation.remove_node(aria_hidden_attr);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the aria-hidden attribute from the element." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, AstNodeList, BatchMutationExt};

Expand Down Expand Up @@ -126,9 +125,9 @@ impl Rule for NoAriaUnsupportedElements {
)
}

fn action(_ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
let element = _ctx.query();
let mut mutation = _ctx.root().begin();
fn action(ctx: &RuleContext<Self>, _state: &Self::State) -> Option<JsRuleAction> {
let element = ctx.query();
let mut mutation = ctx.root().begin();

let attribute = element.attributes().into_iter().find_map(|attribute| {
let jsx_attribute = attribute.as_jsx_attribute()?;
Expand All @@ -147,7 +146,7 @@ impl Rule for NoAriaUnsupportedElements {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>""{removed_attribute}""</Emphasis>" attribute." }
.to_owned(),
mutation,
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_autofocus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{jsx_ext::AnyJsxElement, JsxAttribute};
use biome_rowan::{AstNode, BatchMutationExt};

Expand Down Expand Up @@ -102,7 +101,7 @@ impl Rule for NoAutofocus {
mutation.remove_node(attr.clone());
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"autoFocus"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_blank_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::JsRuleAction;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make::{
jsx_attribute, jsx_attribute_initializer_clause, jsx_attribute_list, jsx_ident, jsx_name,
jsx_string, jsx_string_literal, token,
Expand Down Expand Up @@ -152,7 +151,7 @@ impl Rule for NoBlankTarget {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::Always,
ctx.metadata().applicability(),
message,
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use biome_analyze::context::RuleContext;
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;
use biome_js_syntax::*;
use biome_rowan::{AstNode, BatchMutationExt};
Expand Down Expand Up @@ -83,7 +82,7 @@ impl Rule for NoDistractingElements {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the '"{name.text_trimmed()}"' element." }.to_owned(),
mutation,
))
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_header_scope.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use biome_analyze::context::RuleContext;
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;
use biome_rowan::{AstNode, BatchMutationExt};

Expand Down Expand Up @@ -96,7 +95,7 @@ impl Rule for NoHeaderScope {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"scope"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, BatchMutationExt};

Expand Down Expand Up @@ -109,7 +108,7 @@ impl Rule for NoInteractiveElementToNoninteractiveRole {
mutation.remove_node(role_attribute);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"role"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::JsRuleAction;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, BatchMutationExt, TextRange};

Expand Down Expand Up @@ -117,7 +116,7 @@ impl Rule for NoNoninteractiveElementToInteractiveRole {
mutation.remove_node(role_attribute);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"role"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_analyze::{
};
use biome_aria::AriaRoles;
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{
jsx_ext::AnyJsxElement, AnyJsxAttributeValue, JsNumberLiteralExpression,
JsStringLiteralExpression, JsUnaryExpression, TextRange,
Expand Down Expand Up @@ -165,7 +164,7 @@ impl Rule for NoNoninteractiveTabindex {
mutation.remove_node(tabindex_attribute);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"tabIndex"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::JsRuleAction;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_factory::make::{jsx_string, jsx_string_literal};
use biome_js_semantic::SemanticModel;
Expand Down Expand Up @@ -197,7 +196,7 @@ impl Rule for NoPositiveTabindex {

Some(JsRuleAction::new(
biome_analyze::ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Replace the "<Emphasis>"tableIndex"</Emphasis>" prop value with 0." }
.to_owned(),
mutation,
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/no_redundant_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_analyze::{
};
use biome_aria::{roles::AriaRoleDefinition, AriaRoles};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{
jsx_ext::AnyJsxElement, AnyJsxAttributeValue, JsxAttribute, JsxAttributeList,
};
Expand Down Expand Up @@ -107,7 +106,7 @@ impl Rule for NoRedundantRoles {
mutation.remove_node(state.redundant_attribute.clone());
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"role"</Emphasis>" attribute." }.to_owned(),
mutation,
))
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/use_anchor_content.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use biome_analyze::context::RuleContext;
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;
use biome_js_syntax::JsxElement;
use biome_rowan::{AstNode, BatchMutationExt};
Expand Down Expand Up @@ -144,7 +143,7 @@ impl Rule for UseAnchorContent {

return Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the "<Emphasis>"aria-hidden"</Emphasis>" attribute to allow the anchor element and its content visible to assistive technologies." }.to_owned(),
mutation,
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
context::RuleContext, declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make::{
jsx_attribute, jsx_attribute_initializer_clause, jsx_attribute_list, jsx_ident, jsx_name,
jsx_string, jsx_string_literal, token,
Expand Down Expand Up @@ -125,7 +124,7 @@ impl Rule for UseAriaActivedescendantWithTabindex {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Add the tabIndex attribute." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::JsRuleAction;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_js_syntax::JsxAttribute;
use biome_rowan::{AstNode, AstNodeList, BatchMutationExt};
Expand Down Expand Up @@ -96,7 +95,7 @@ impl Rule for UseValidAriaProps {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),

markup! { "Remove the invalid "<Emphasis>"aria-*"</Emphasis>" attribute.
Check the list of all "<Hyperlink href="https://developer.mozilla.org/en-US/docs/web/Accessibility/ARIA/Attributes#aria_attribute_types">"valid"</Hyperlink>" aria-* attributes." }
Expand Down
3 changes: 1 addition & 2 deletions crates/biome_js_analyze/src/lint/a11y/use_valid_aria_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use biome_analyze::{
};
use biome_console::markup;
use biome_deserialize_macros::Deserializable;
use biome_diagnostics::Applicability;
use biome_js_syntax::jsx_ext::AnyJsxElement;
use biome_rowan::{AstNode, BatchMutationExt};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -140,7 +139,7 @@ impl Rule for UseValidAriaRole {
mutation.remove_node(role_attribute);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),

markup! { "Remove the invalid "<Emphasis>"role"</Emphasis>" attribute.\n Check the list of all "<Hyperlink href="https://www.w3.org/TR/wai-aria/#role_definitions">"valid"</Hyperlink>" role attributes." }
.to_owned(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::fmt::Display;
use biome_analyze::context::RuleContext;
use biome_analyze::{declare_rule, ActionCategory, FixKind, Rule, RuleDiagnostic, RuleSource};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{
JsReferenceIdentifier, JsSyntaxKind, TextRange, TsIntersectionTypeElementList, TsObjectType,
Expand Down Expand Up @@ -178,7 +177,7 @@ impl Rule for NoBannedTypes {
mutation.replace_node(reference_identifier.clone()?, banned_type.fix_with()?);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::Always,
ctx.metadata().applicability(),
markup! { "Use '"{suggested_type}"' instead" }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{
is_in_boolean_context, is_negation, AnyJsExpression, JsCallArgumentList, JsCallArguments,
JsCallExpression, JsNewExpression, JsSyntaxNode, JsUnaryOperator,
Expand Down Expand Up @@ -185,7 +184,7 @@ impl Rule for NoExtraBooleanCast {

Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { {message} }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{JsRegexLiteralExpression, JsSyntaxKind, JsSyntaxToken, TextRange, TextSize};
use biome_rowan::BatchMutationExt;
use std::{fmt::Write, ops::Range};
Expand Down Expand Up @@ -179,7 +178,7 @@ impl Rule for NoMultipleSpacesInRegularExpressionLiterals {
mutation.replace_token(token, next_trimmed_token);
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::Always,
ctx.metadata().applicability(),
markup! { "Use a quantifier instead." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_factory::make;
use biome_js_syntax::{
AnyJsClass, AnyJsClassMember, AnyJsExpression, JsArrowFunctionExpression, JsSuperExpression,
Expand Down Expand Up @@ -173,7 +172,7 @@ impl Rule for NoThisInStatic {
mutation.replace_node(expr, suggested_class_name.into());
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Use the class name instead." }.to_owned(),
mutation,
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use biome_analyze::{
RuleSource,
};
use biome_console::markup;
use biome_diagnostics::Applicability;
use biome_js_syntax::{
AnyJsCallArgument, AnyJsClass, AnyJsConstructorParameter, AnyJsFormalParameter,
JsCallExpression, JsConstructorClassMember,
Expand Down Expand Up @@ -215,7 +214,7 @@ impl Rule for NoUselessConstructor {
mutation.remove_node(constructor.clone());
Some(JsRuleAction::new(
ActionCategory::QuickFix,
Applicability::MaybeIncorrect,
ctx.metadata().applicability(),
markup! { "Remove the unnecessary constructor." }.to_owned(),
mutation,
))
Expand Down
Loading