Skip to content
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
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ quote = "1"
syn = { version = "2", default-features = false }
unicode-id-start = "1"

#
oxc-browserslist = "1.1.0"
oxc_index = "1.0.1"
oxc_resolver = "2.1.1"
oxc_sourcemap = "1.0.3"

aho-corasick = "1.1.3"
#
allocator-api2 = "0.2.21"
assert-unchecked = "0.1.2"
base64 = "0.22.1"
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ oxc_semantic = { workspace = true }
oxc_span = { workspace = true, features = ["schemars", "serialize"] }
oxc_syntax = { workspace = true, features = ["serialize"] }

aho-corasick = { workspace = true }
#
bitflags = { workspace = true }
convert_case = { workspace = true }
cow-utils = { workspace = true }
Expand Down
9 changes: 1 addition & 8 deletions crates/oxc_linter/src/rules/eslint/no_regex_spaces.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use aho_corasick::AhoCorasick;
use lazy_static::lazy_static;
use oxc_allocator::{Allocator, Vec};
use oxc_ast::{
ast::{Argument, CallExpression, NewExpression, RegExpLiteral},
Expand Down Expand Up @@ -48,11 +46,6 @@ declare_oxc_lint!(
pending // TODO: This is somewhat autofixable, but the fixer does not exist yet.
);

lazy_static! {
static ref DOUBLE_SPACE: AhoCorasick =
AhoCorasick::new([" "]).expect("no-regex-spaces: Unable to build AhoCorasick");
}

impl Rule for NoRegexSpaces {
fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
match node.kind() {
Expand Down Expand Up @@ -127,7 +120,7 @@ impl NoRegexSpaces {
// For skipping if there aren't any consecutive spaces in the source, to avoid reporting cases
// where the space is explicitly escaped, like: `RegExp(' \ ')``.
fn has_double_space(input: &str) -> bool {
DOUBLE_SPACE.is_match(input)
input.contains(" ")
}
}

Expand Down
42 changes: 22 additions & 20 deletions crates/oxc_linter/src/rules/jsx_a11y/img_redundant_alt.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use aho_corasick::AhoCorasick;
use std::borrow::Cow;

use cow_utils::CowUtils;
use oxc_ast::{
ast::{JSXAttributeItem, JSXAttributeName, JSXAttributeValue, JSXExpression},
AstKind,
Expand Down Expand Up @@ -28,7 +30,7 @@ pub struct ImgRedundantAlt(Box<ImgRedundantAltConfig>);
#[derive(Debug, Clone)]
pub struct ImgRedundantAltConfig {
types_to_validate: Vec<CompactStr>,
redundant_words: AhoCorasick,
redundant_words: Vec<Cow<'static, str>>,
}

impl std::ops::Deref for ImgRedundantAlt {
Expand All @@ -45,24 +47,19 @@ impl Default for ImgRedundantAltConfig {
fn default() -> Self {
Self {
types_to_validate: vec![CompactStr::new("img")],
redundant_words: AhoCorasick::builder()
.ascii_case_insensitive(true)
.build(REDUNDANT_WORDS)
.expect("Could not build AhoCorasick"),
redundant_words: vec!["image".into(), "photo".into(), "picture".into()],
}
}
}
impl ImgRedundantAltConfig {
fn new(
types_to_validate: Vec<&str>,
redundant_words: &[&str],
) -> Result<Self, aho_corasick::BuildError> {
Ok(Self {
fn new(types_to_validate: Vec<&str>, redundant_words: &[&str]) -> Self {
Self {
types_to_validate: types_to_validate.into_iter().map(Into::into).collect(),
redundant_words: AhoCorasick::builder()
.ascii_case_insensitive(true)
.build(redundant_words)?,
})
redundant_words: redundant_words
.iter()
.map(|w| Cow::Owned(w.cow_to_ascii_lowercase().to_string()))
.collect::<Vec<_>>(),
}
}
}

Expand Down Expand Up @@ -121,7 +118,7 @@ impl Rule for ImgRedundantAlt {
v.iter().filter_map(Value::as_str).chain(REDUNDANT_WORDS).collect::<Vec<_>>()
});

Self(Box::new(ImgRedundantAltConfig::new(components, words.as_slice()).unwrap()))
Self(Box::new(ImgRedundantAltConfig::new(components, words.as_slice())))
}

fn run<'a>(&self, node: &AstNode<'a>, ctx: &LintContext<'a>) {
Expand Down Expand Up @@ -195,10 +192,15 @@ impl Rule for ImgRedundantAlt {
impl ImgRedundantAlt {
#[inline]
fn is_redundant_alt_text(&self, alt_text: &str) -> bool {
for mat in self.redundant_words.find_iter(alt_text) {
// check if followed by space or is whole text
if mat.end() == alt_text.len() || alt_text.as_bytes()[mat.end()] == b' ' {
return true;
let alt_text = alt_text.cow_to_ascii_lowercase();
for word in &self.redundant_words {
if let Some(index) = alt_text.find(word.as_ref()) {
// check if followed by space or is whole text
if index + word.len() == alt_text.len()
|| alt_text.as_bytes().get(index + word.len()) == Some(&b' ')
{
return true;
}
}
}
false
Expand Down