diff --git a/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs b/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs index 4452be0950c6c..676d2e33c3c7d 100644 --- a/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs +++ b/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs @@ -4,6 +4,7 @@ use oxc_ast::{ }; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; +use oxc_regular_expression::ast::Term; use oxc_span::{CompactStr, GetSpan, Span}; use crate::{ast_util::extract_regex_flags, context::LintContext, rule::Rule, AstNode}; @@ -125,18 +126,21 @@ fn get_pattern_replacement<'a>( return None; } - let pattern_text = reg_exp_literal.regex.pattern.source_text(ctx.source_text()); - let pattern_text = pattern_text.as_ref(); - if !is_simple_string(pattern_text) { + let pattern_terms = reg_exp_literal + .regex + .pattern + .as_pattern() + .and_then(|pattern| pattern.body.body.first().map(|it| &it.body))?; + let is_simple_string = pattern_terms.iter().all(|term| matches!(term, Term::Character(_))); + + if !is_simple_string { return None; } - Some(CompactStr::new(pattern_text)) -} + let pattern_text = reg_exp_literal.regex.pattern.source_text(ctx.source_text()); + let pattern_text = pattern_text.as_ref(); -fn is_simple_string(str: &str) -> bool { - str.chars() - .all(|c| !matches!(c, '^' | '$' | '+' | '[' | '{' | '(' | '\\' | '.' | '?' | '*' | '|')) + Some(CompactStr::new(pattern_text)) } #[test]