Skip to content

Commit

Permalink
refactor(ast): optimize JSXIdentifier::is_reference
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 30, 2024
1 parent 16a2143 commit 097f551
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion crates/oxc_ast/src/ast_impl/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,20 @@ impl<'a> JSXIdentifier<'a> {
///
/// References begin with a capital letter, `_` or `$`.
/// <https://babeljs.io/repl#?code_lz=DwMQ9mAED0B8DcAoYAzCMHIPpqnJwAJLhkkA&presets=react>
// `name.chars().next().unwrap()` cannot panic because name is never an empty string.
#[allow(clippy::missing_panics_doc)]
pub fn is_reference(&self) -> bool {
self.name.chars().next().map_or(false, |c| c.is_uppercase() || c == '_' || c == '$')
// The identifier has already been checked to be valid, so when first char is ASCII, it can only
// be `a-z`, `A-Z`, `_` or `$`. But compiler doesn't know that, so we can help it create faster
// code by taking that invariant into account.
// `b < b'a'` matches `A-Z`, `_` and `$`.
// Use a fast path for common case of ASCII characters, to avoid the more expensive
// `char::is_uppercase` in most cases.
let name = self.name.as_str();
match name.as_bytes()[0] {
b if b.is_ascii() => b < b'a',
_ => name.chars().next().unwrap().is_uppercase(),
}
}
}

Expand Down

0 comments on commit 097f551

Please sign in to comment.