Skip to content

Commit

Permalink
perf(linter/no-unescaped-entities): add fast path to check if char sh…
Browse files Browse the repository at this point in the history
…ould be replaced (#6594)

Flamegraph shows a lot of time spent hashing here, so I added a faster check to see if it is a char that we need to replace.
  • Loading branch information
camchenry committed Oct 16, 2024
1 parent ee73f56 commit b3d0cce
Showing 1 changed file with 6 additions and 0 deletions.
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

0 comments on commit b3d0cce

Please sign in to comment.