mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 04:08:41 +00:00
perf(linter): make img-redundant-alt only build a regex once (#4604)
This commit is contained in:
parent
74fa75aa87
commit
f259df0d62
1 changed files with 48 additions and 35 deletions
|
|
@ -4,8 +4,9 @@ use oxc_ast::{
|
|||
};
|
||||
use oxc_diagnostics::OxcDiagnostic;
|
||||
use oxc_macros::declare_oxc_lint;
|
||||
use oxc_span::Span;
|
||||
use regex::Regex;
|
||||
use oxc_span::{CompactStr, Span};
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::{
|
||||
context::LintContext,
|
||||
|
|
@ -23,10 +24,10 @@ fn img_redundant_alt_diagnostic(span0: Span) -> OxcDiagnostic {
|
|||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ImgRedundantAlt(Box<ImgRedundantAltConfig>);
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ImgRedundantAltConfig {
|
||||
types_to_validate: Vec<String>,
|
||||
redundant_words: Vec<String>,
|
||||
types_to_validate: Vec<CompactStr>,
|
||||
redundant_words: Regex,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for ImgRedundantAlt {
|
||||
|
|
@ -37,17 +38,28 @@ impl std::ops::Deref for ImgRedundantAlt {
|
|||
}
|
||||
}
|
||||
|
||||
const COMPONENTS_FIXED_TO_VALIDATE: [&str; 1] = ["img"];
|
||||
const REDUNDANT_WORDS: [&str; 3] = ["image", "photo", "picture"];
|
||||
impl Default for ImgRedundantAltConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
types_to_validate: COMPONENTS_FIXED_TO_VALIDATE
|
||||
.iter()
|
||||
.map(|&s| s.to_string())
|
||||
.collect(),
|
||||
redundant_words: REDUNDANT_WORDS.iter().map(|&s| s.to_string()).collect(),
|
||||
types_to_validate: vec![CompactStr::new("img")],
|
||||
redundant_words: Self::union(&REDUNDANT_WORDS).unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ImgRedundantAltConfig {
|
||||
fn new(types_to_validate: Vec<&str>, redundant_words: &[&str]) -> Result<Self, regex::Error> {
|
||||
Ok(Self {
|
||||
types_to_validate: types_to_validate.into_iter().map(Into::into).collect(),
|
||||
redundant_words: Self::union(redundant_words)?,
|
||||
})
|
||||
}
|
||||
|
||||
fn union(strs: &[&str]) -> Result<Regex, regex::Error> {
|
||||
RegexBuilder::new(&format!(r"(?i)\b({})\b", strs.join("|"))).case_insensitive(true).build()
|
||||
}
|
||||
}
|
||||
|
||||
declare_oxc_lint!(
|
||||
/// ### What it does
|
||||
|
|
@ -81,27 +93,27 @@ declare_oxc_lint!(
|
|||
ImgRedundantAlt,
|
||||
correctness
|
||||
);
|
||||
const COMPONENTS_FIXED_TO_VALIDATE: [&str; 1] = ["img"];
|
||||
const REDUNDANT_WORDS: [&str; 3] = ["image", "photo", "picture"];
|
||||
|
||||
impl Rule for ImgRedundantAlt {
|
||||
fn from_configuration(value: serde_json::Value) -> Self {
|
||||
let mut img_redundant_alt = ImgRedundantAltConfig::default();
|
||||
if let Some(config) = value.get(0) {
|
||||
if let Some(components) = config.get("components").and_then(|v| v.as_array()) {
|
||||
img_redundant_alt
|
||||
.types_to_validate
|
||||
.extend(components.iter().filter_map(|v| v.as_str().map(ToString::to_string)));
|
||||
}
|
||||
fn from_configuration(value: Value) -> Self {
|
||||
let Some(config) = value.get(0) else {
|
||||
return Self::default();
|
||||
};
|
||||
let components = config.get("components").and_then(Value::as_array).map_or(
|
||||
Vec::from(COMPONENTS_FIXED_TO_VALIDATE),
|
||||
|v| {
|
||||
v.iter()
|
||||
.filter_map(Value::as_str)
|
||||
.chain(COMPONENTS_FIXED_TO_VALIDATE)
|
||||
.collect::<Vec<_>>()
|
||||
},
|
||||
);
|
||||
let words =
|
||||
config.get("words").and_then(Value::as_array).map_or(Vec::from(REDUNDANT_WORDS), |v| {
|
||||
v.iter().filter_map(Value::as_str).chain(REDUNDANT_WORDS).collect::<Vec<_>>()
|
||||
});
|
||||
|
||||
if let Some(words) = config.get("words").and_then(|v| v.as_array()) {
|
||||
img_redundant_alt
|
||||
.redundant_words
|
||||
.extend(words.iter().filter_map(|v| v.as_str().map(ToString::to_string)));
|
||||
}
|
||||
}
|
||||
|
||||
Self(Box::new(img_redundant_alt))
|
||||
Self(Box::new(ImgRedundantAltConfig::new(components, words.as_slice()).unwrap()))
|
||||
}
|
||||
|
||||
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
|
||||
|
|
@ -144,7 +156,7 @@ impl Rule for ImgRedundantAlt {
|
|||
JSXAttributeValue::StringLiteral(lit) => {
|
||||
let alt_text = lit.value.as_str();
|
||||
|
||||
if is_redundant_alt_text(alt_text, &self.redundant_words) {
|
||||
if self.is_redundant_alt_text(alt_text) {
|
||||
ctx.diagnostic(img_redundant_alt_diagnostic(alt_attribute_name_span));
|
||||
}
|
||||
}
|
||||
|
|
@ -152,7 +164,7 @@ impl Rule for ImgRedundantAlt {
|
|||
JSXExpression::StringLiteral(lit) => {
|
||||
let alt_text = lit.value.as_str();
|
||||
|
||||
if is_redundant_alt_text(alt_text, &self.redundant_words) {
|
||||
if self.is_redundant_alt_text(alt_text) {
|
||||
ctx.diagnostic(img_redundant_alt_diagnostic(alt_attribute_name_span));
|
||||
}
|
||||
}
|
||||
|
|
@ -160,7 +172,7 @@ impl Rule for ImgRedundantAlt {
|
|||
for quasi in &lit.quasis {
|
||||
let alt_text = quasi.value.raw.as_str();
|
||||
|
||||
if is_redundant_alt_text(alt_text, &self.redundant_words) {
|
||||
if self.is_redundant_alt_text(alt_text) {
|
||||
ctx.diagnostic(img_redundant_alt_diagnostic(alt_attribute_name_span));
|
||||
}
|
||||
}
|
||||
|
|
@ -172,10 +184,11 @@ impl Rule for ImgRedundantAlt {
|
|||
}
|
||||
}
|
||||
|
||||
fn is_redundant_alt_text(alt_text: &str, redundant_words: &[String]) -> bool {
|
||||
let regexp = Regex::new(&format!(r"(?i)\b({})\b", redundant_words.join("|"),)).unwrap();
|
||||
|
||||
regexp.is_match(alt_text)
|
||||
impl ImgRedundantAlt {
|
||||
#[inline]
|
||||
fn is_redundant_alt_text(&self, alt_text: &str) -> bool {
|
||||
self.redundant_words.is_match(alt_text)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
|||
Loading…
Reference in a new issue