mirror of
https://github.com/danbulant/oxc
synced 2026-05-19 20:28:58 +00:00
perf(linter/no-unescaped-entities): add fast path to check if char should 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.
This commit is contained in:
parent
ee73f568b3
commit
b3d0cce2c2
1 changed files with 6 additions and 0 deletions
|
|
@ -51,6 +51,9 @@ impl Rule for NoUnescapedEntities {
|
||||||
if let AstKind::JSXText(jsx_text) = node.kind() {
|
if let AstKind::JSXText(jsx_text) = node.kind() {
|
||||||
let source = jsx_text.span.source_text(ctx.source_text());
|
let source = jsx_text.span.source_text(ctx.source_text());
|
||||||
for (i, char) in source.char_indices() {
|
for (i, char) in source.char_indices() {
|
||||||
|
if !CHARS.contains(&char) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if let Some(escapes) = DEFAULTS.get(&char) {
|
if let Some(escapes) = DEFAULTS.get(&char) {
|
||||||
#[allow(clippy::cast_possible_truncation)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
ctx.diagnostic(no_unescaped_entities_diagnostic(
|
ctx.diagnostic(no_unescaped_entities_diagnostic(
|
||||||
|
|
@ -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! {
|
pub const DEFAULTS: Map<char, &'static [&'static str]> = phf_map! {
|
||||||
'>' => &[">"],
|
'>' => &[">"],
|
||||||
'"' => &[""", "“", """, "”"],
|
'"' => &[""", "“", """, "”"],
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue