refactor(linter): remove duplicate get_jsx_attribute_name (#1971)

This commit is contained in:
Cameron 2024-01-10 03:45:34 +00:00 committed by GitHub
parent 78916703c5
commit 64310fa9b1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 14 additions and 23 deletions

View file

@ -7,7 +7,8 @@ use oxc_macros::declare_oxc_lint;
use oxc_span::Span; use oxc_span::Span;
use crate::{ use crate::{
context::LintContext, globals::VALID_ARIA_PROPS, rule::Rule, utils::get_attribute_name, AstNode, context::LintContext, globals::VALID_ARIA_PROPS, rule::Rule, utils::get_jsx_attribute_name,
AstNode,
}; };
#[derive(Debug, Error, Diagnostic)] #[derive(Debug, Error, Diagnostic)]
@ -41,7 +42,7 @@ declare_oxc_lint!(
impl Rule for AriaProps { impl Rule for AriaProps {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) { fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
if let AstKind::JSXAttributeItem(JSXAttributeItem::Attribute(attr)) = node.kind() { if let AstKind::JSXAttributeItem(JSXAttributeItem::Attribute(attr)) = node.kind() {
let name = get_attribute_name(&attr.name).to_lowercase(); let name = get_jsx_attribute_name(&attr.name).to_lowercase();
if name.starts_with("aria-") && !VALID_ARIA_PROPS.contains(&name) { if name.starts_with("aria-") && !VALID_ARIA_PROPS.contains(&name) {
ctx.diagnostic(AriaPropsDiagnostic(attr.span, name)); ctx.diagnostic(AriaPropsDiagnostic(attr.span, name));
} }

View file

@ -10,7 +10,7 @@ use phf::phf_set;
use crate::{ use crate::{
globals::RESERVED_HTML_TAG, globals::RESERVED_HTML_TAG,
rule::Rule, rule::Rule,
utils::{get_attribute_name, get_element_type}, utils::{get_element_type, get_jsx_attribute_name},
AstNode, LintContext, AstNode, LintContext,
}; };
@ -53,7 +53,7 @@ impl Rule for AriaUnsupportedElements {
JSXAttributeItem::Attribute(attr) => attr, JSXAttributeItem::Attribute(attr) => attr,
JSXAttributeItem::SpreadAttribute(_) => continue, JSXAttributeItem::SpreadAttribute(_) => continue,
}; };
let attr_name = get_attribute_name(&attr.name).to_lowercase(); let attr_name = get_jsx_attribute_name(&attr.name).to_lowercase();
if INVALID_ATTRIBUTES.contains(&attr_name) { if INVALID_ATTRIBUTES.contains(&attr_name) {
ctx.diagnostic(AriaUnsupportedElementsDiagnostic(attr.span, attr_name)); ctx.diagnostic(AriaUnsupportedElementsDiagnostic(attr.span, attr_name));
} }

View file

@ -15,7 +15,8 @@ use crate::{
globals::{VALID_ARIA_PROPS, VALID_ARIA_ROLES}, globals::{VALID_ARIA_PROPS, VALID_ARIA_ROLES},
rule::Rule, rule::Rule,
utils::{ utils::{
get_attribute_name, get_element_type, get_string_literal_prop_value, has_jsx_prop_lowercase, get_element_type, get_jsx_attribute_name, get_string_literal_prop_value,
has_jsx_prop_lowercase,
}, },
AstNode, AstNode,
}; };
@ -77,7 +78,7 @@ impl Rule for RoleSupportAriaProps {
let invalid_props = get_invalid_aria_props_for_role(role_value); let invalid_props = get_invalid_aria_props_for_role(role_value);
for attr in &jsx_el.attributes { for attr in &jsx_el.attributes {
if let JSXAttributeItem::Attribute(attr) = attr { if let JSXAttributeItem::Attribute(attr) = attr {
let name = get_attribute_name(&attr.name).to_lowercase(); let name = get_jsx_attribute_name(&attr.name).to_lowercase();
if invalid_props.contains(&&name.as_str()) { if invalid_props.contains(&&name.as_str()) {
ctx.diagnostic(if is_implicit { ctx.diagnostic(if is_implicit {
RoleSupportAriaPropsDiagnostic::IsImplicit( RoleSupportAriaPropsDiagnostic::IsImplicit(

View file

@ -19,7 +19,7 @@ use std::collections::hash_set::HashSet;
use crate::{ use crate::{
context::LintContext, context::LintContext,
rule::Rule, rule::Rule,
utils::{get_element_type, get_prop_name}, utils::{get_element_type, get_jsx_attribute_name},
AstNode, AstNode,
}; };
@ -482,7 +482,7 @@ impl Rule for NoUnknownProperty {
}) })
.for_each(|attr| { .for_each(|attr| {
let span = attr.name.span(); let span = attr.name.span();
let actual_name = get_prop_name(&attr.name); let actual_name = get_jsx_attribute_name(&attr.name);
if self.0.ignore.contains(&(actual_name)) { if self.0.ignore.contains(&(actual_name)) {
return; return;
}; };

View file

@ -1,10 +0,0 @@
use oxc_ast::ast::JSXAttributeName;
pub fn get_attribute_name(attr: &JSXAttributeName) -> String {
match attr {
JSXAttributeName::Identifier(ident) => ident.name.to_string(),
JSXAttributeName::NamespacedName(namespaced_name) => {
format!("{}:{}", namespaced_name.namespace.name, namespaced_name.property.name)
}
}
}

View file

@ -1,7 +1,6 @@
mod jest; mod jest;
mod jsx_a11y;
mod node; mod node;
mod react; mod react;
mod unicorn; mod unicorn;
pub use self::{jest::*, jsx_a11y::*, node::*, react::*, unicorn::*}; pub use self::{jest::*, node::*, react::*, unicorn::*};

View file

@ -54,10 +54,10 @@ pub fn get_prop_value<'a, 'b>(item: &'b JSXAttributeItem<'a>) -> Option<&'b JSXA
} }
} }
pub fn get_prop_name(item: &JSXAttributeName) -> String { pub fn get_jsx_attribute_name(attr: &JSXAttributeName) -> String {
match item { match attr {
JSXAttributeName::NamespacedName(name) => { JSXAttributeName::NamespacedName(name) => {
format!("{}:{}", name.namespace.name.as_str(), name.property.name.as_str()) format!("{}:{}", name.namespace.name, name.property.name)
} }
JSXAttributeName::Identifier(ident) => ident.name.to_string(), JSXAttributeName::Identifier(ident) => ident.name.to_string(),
} }