refactor(linter): extract common code (#1848)

extract common code
This commit is contained in:
msdlisper 2023-12-28 14:43:47 +08:00 committed by GitHub
parent 497a0b8462
commit 040278aa50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 13 additions and 32 deletions

View file

@ -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"

View file

@ -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<JSXAttributeItem>) -> bool {
attr_items
.into_iter()

View file

@ -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 {

View file

@ -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 {