Skip to content

Commit

Permalink
refactor(parser): inline code from JSXIdentifier::is_reference
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Aug 30, 2024
1 parent 097f551 commit ec489bd
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
22 changes: 0 additions & 22 deletions crates/oxc_ast/src/ast_impl/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,6 @@ impl<'a> fmt::Display for JSXIdentifier<'a> {
}
}

impl<'a> JSXIdentifier<'a> {
/// Determines whether the given current identifier is a reference.
///
/// 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 {
// 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(),
}
}
}

impl<'a> fmt::Display for JSXNamespacedName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.namespace.name, self.property.name)
Expand Down
16 changes: 15 additions & 1 deletion crates/oxc_parser/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,21 @@ impl<'a> ParserImpl<'a> {
.map(JSXElementName::MemberExpression);
}

let element_name = if identifier.is_reference() {
// References begin with a capital letter, `_` or `$` e.g. `<Foo>`, `<_foo>`, `<$foo>`.
// https://babeljs.io/repl#?code_lz=DwMQ9mAED0B8DcAoYAzCMHIPpqnJwAJLhkkA&presets=react
// 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 = identifier.name.as_str();
let is_reference = match name.as_bytes()[0] {
b if b.is_ascii() => b < b'a',
_ => name.chars().next().unwrap().is_uppercase(),
};

let element_name = if is_reference {
JSXElementName::IdentifierReference(self.ast.alloc(identifier.into()))
} else {
JSXElementName::Identifier(self.ast.alloc(identifier))
Expand Down

0 comments on commit ec489bd

Please sign in to comment.