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

refactor(parser): move JSXIdentifier conversion code into parser #5345

Merged
Show file tree
Hide file tree
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: 0 additions & 6 deletions crates/oxc_ast/src/ast_impl/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,12 +351,6 @@ impl<'a> fmt::Display for IdentifierReference<'a> {
}
}

impl<'a> From<JSXIdentifier<'a>> for IdentifierReference<'a> {
fn from(value: JSXIdentifier<'a>) -> Self {
IdentifierReference { span: value.span, name: value.name, reference_id: Cell::default() }
}
}

impl<'a> Hash for BindingIdentifier<'a> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.name.hash(state);
Expand Down
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
23 changes: 19 additions & 4 deletions crates/oxc_parser/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,23 @@ impl<'a> ParserImpl<'a> {
.map(JSXElementName::MemberExpression);
}

let element_name = if identifier.is_reference() {
JSXElementName::IdentifierReference(self.ast.alloc(identifier.into()))
// 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 {
let identifier = self.ast.identifier_reference(identifier.span, identifier.name);
JSXElementName::IdentifierReference(self.ast.alloc(identifier))
} else {
JSXElementName::Identifier(self.ast.alloc(identifier))
};
Expand All @@ -171,8 +186,8 @@ impl<'a> ParserImpl<'a> {
object: JSXIdentifier<'a>,
) -> Result<Box<'a, JSXMemberExpression<'a>>> {
let mut span = span;
let mut object =
JSXMemberExpressionObject::IdentifierReference(self.ast.alloc(object.into()));
let object = self.ast.identifier_reference(object.span, object.name);
let mut object = JSXMemberExpressionObject::IdentifierReference(self.ast.alloc(object));
let mut property = None;

while self.eat(Kind::Dot) && !self.at(Kind::Eof) {
Expand Down