From 040278aa5072028d5a177ea112d20a75fe76dfd4 Mon Sep 17 00:00:00 2001 From: msdlisper <1170167213@qq.com> Date: Thu, 28 Dec 2023 14:43:47 +0800 Subject: [PATCH] refactor(linter): extract common code (#1848) extract common code --- crates/oxc_language_server/Cargo.toml | 10 +++++----- .../src/rules/jsx_a11y/anchor_has_content.rs | 15 ++++++--------- .../src/rules/jsx_a11y/html_has_lang.rs | 10 +--------- crates/oxc_linter/src/rules/jsx_a11y/lang.rs | 10 +--------- 4 files changed, 13 insertions(+), 32 deletions(-) diff --git a/crates/oxc_language_server/Cargo.toml b/crates/oxc_language_server/Cargo.toml index bdd3a48b7..87c08ded5 100644 --- a/crates/oxc_language_server/Cargo.toml +++ b/crates/oxc_language_server/Cargo.toml @@ -16,7 +16,7 @@ categories.workspace = true workspace = true [[bin]] -name = "oxc_language_server" +name = "oxc_language_server" test = false [dependencies] @@ -36,7 +36,7 @@ rayon = { workspace = true } ropey = { workspace = true } tokio = { workspace = true, features = ["full"] } tower-lsp = { workspace = true, features = ["proposed"] } -log = "0.4.20" -serde = { workspace = true, features = ["derive"] } -serde_json = { workspace = true } -globset = "0.4.14" +log = "0.4.20" +serde = { workspace = true, features = ["derive"] } +serde_json = { workspace = true } +globset = "0.4.14" diff --git a/crates/oxc_linter/src/rules/jsx_a11y/anchor_has_content.rs b/crates/oxc_linter/src/rules/jsx_a11y/anchor_has_content.rs index cfaed3118..b3435ee47 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/anchor_has_content.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/anchor_has_content.rs @@ -14,7 +14,12 @@ use oxc_span::Span; use oxc_allocator::Vec; -use crate::{context::LintContext, rule::Rule, utils::has_jsx_prop_lowercase, AstNode}; +use crate::{ + context::LintContext, + rule::Rule, + utils::{get_prop_value, has_jsx_prop_lowercase}, + AstNode, +}; #[derive(Debug, Error, Diagnostic)] enum AnchorHasContentDiagnostic { @@ -100,14 +105,6 @@ impl Rule for AnchorHasContent { } } -fn get_prop_value<'a, 'b>(item: &'b JSXAttributeItem<'a>) -> Option<&'b JSXAttributeValue<'a>> { - if let JSXAttributeItem::Attribute(attr) = item { - attr.0.value.as_ref() - } else { - None - } -} - fn match_valid_prop(attr_items: &Vec) -> bool { attr_items .into_iter() diff --git a/crates/oxc_linter/src/rules/jsx_a11y/html_has_lang.rs b/crates/oxc_linter/src/rules/jsx_a11y/html_has_lang.rs index f19f38710..c4f279c1f 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/html_has_lang.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/html_has_lang.rs @@ -14,7 +14,7 @@ use oxc_span::Span; use crate::{ context::LintContext, rule::Rule, - utils::{get_element_type, has_jsx_prop_lowercase}, + utils::{get_element_type, get_prop_value, has_jsx_prop_lowercase}, AstNode, }; @@ -56,14 +56,6 @@ declare_oxc_lint!( correctness ); -fn get_prop_value<'a, 'b>(item: &'b JSXAttributeItem<'a>) -> Option<&'b JSXAttributeValue<'a>> { - if let JSXAttributeItem::Attribute(attr) = item { - attr.0.value.as_ref() - } else { - None - } -} - impl Rule for HtmlHasLang { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { let AstKind::JSXOpeningElement(jsx_el) = node.kind() else { diff --git a/crates/oxc_linter/src/rules/jsx_a11y/lang.rs b/crates/oxc_linter/src/rules/jsx_a11y/lang.rs index ae6b0576c..4cc5f4f62 100644 --- a/crates/oxc_linter/src/rules/jsx_a11y/lang.rs +++ b/crates/oxc_linter/src/rules/jsx_a11y/lang.rs @@ -15,7 +15,7 @@ use oxc_span::Span; use crate::{ context::LintContext, rule::Rule, - utils::{get_element_type, has_jsx_prop_lowercase}, + utils::{get_element_type, get_prop_value, has_jsx_prop_lowercase}, AstNode, }; @@ -92,14 +92,6 @@ impl Rule for Lang { } } -fn get_prop_value<'a, 'b>(item: &'b JSXAttributeItem<'a>) -> Option<&'b JSXAttributeValue<'a>> { - if let JSXAttributeItem::Attribute(attr) = item { - attr.0.value.as_ref() - } else { - None - } -} - fn is_valid_lang_prop(item: &JSXAttributeItem) -> bool { match get_prop_value(item) { Some(JSXAttributeValue::ExpressionContainer(JSXExpressionContainer {