Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(linter/no-unescaped-entities): add fast path to check if char should be replaced #6594

Merged
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
6 changes: 6 additions & 0 deletions crates/oxc_linter/src/rules/react/no_unescaped_entities.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ impl Rule for NoUnescapedEntities {
if let AstKind::JSXText(jsx_text) = node.kind() {
let source = jsx_text.span.source_text(ctx.source_text());
for (i, char) in source.char_indices() {
if !CHARS.contains(&char) {
continue;
}
if let Some(escapes) = DEFAULTS.get(&char) {
#[allow(clippy::cast_possible_truncation)]
ctx.diagnostic(no_unescaped_entities_diagnostic(
Expand All @@ -71,6 +74,9 @@ impl Rule for NoUnescapedEntities {
}
}

// NOTE: If we add substantially more characters, we should consider using a hash set instead.
pub const CHARS: [char; 4] = ['>', '"', '\'', '}'];

pub const DEFAULTS: Map<char, &'static [&'static str]> = phf_map! {
'>' => &["&gt;"],
'"' => &["&quot;", "&ldquo;", "&#34;", "&rdquo;"],
Expand Down
Loading