diff --git a/crates/oxc_parser/src/cursor.rs b/crates/oxc_parser/src/cursor.rs index 2f97590cca557..7947efcdf379c 100644 --- a/crates/oxc_parser/src/cursor.rs +++ b/crates/oxc_parser/src/cursor.rs @@ -3,7 +3,7 @@ use oxc_allocator::Vec; use oxc_ast::ast::{BindingRestElement, RegExpFlags}; use oxc_diagnostics::OxcDiagnostic; -use oxc_span::{GetSpan, Span}; +use oxc_span::{GetSpan, Ident, Span}; use crate::{ Context, ParserImpl, diagnostics, @@ -74,6 +74,12 @@ impl<'a> ParserImpl<'a> { self.lexer.get_string(self.token) } + /// Get current identifier with precomputed hash + #[inline] + pub(crate) fn cur_ident(&self) -> Ident<'a> { + self.lexer.get_ident(self.token) + } + /// Get current template string pub(crate) fn cur_template_string(&self) -> Option<&'a str> { self.lexer.get_template_string(self.token.start()) diff --git a/crates/oxc_parser/src/js/expression.rs b/crates/oxc_parser/src/js/expression.rs index 24f14cd3de94c..7f75b402e34c0 100644 --- a/crates/oxc_parser/src/js/expression.rs +++ b/crates/oxc_parser/src/js/expression.rs @@ -3,7 +3,7 @@ use oxc_allocator::{Box, TakeIn, Vec}; use oxc_ast::ast::*; #[cfg(feature = "regular_expression")] use oxc_regular_expression::ast::Pattern; -use oxc_span::{Atom, GetSpan, Span}; +use oxc_span::{GetSpan, Ident, Span}; use oxc_syntax::{ number::{BigintBase, NumberBase}, precedence::Precedence, @@ -117,11 +117,11 @@ impl<'a> ParserImpl<'a> { } #[inline] - pub(crate) fn parse_identifier_kind(&mut self, kind: Kind) -> (Span, Atom<'a>) { + pub(crate) fn parse_identifier_kind(&mut self, kind: Kind) -> (Span, Ident<'a>) { let span = self.cur_token().span(); - let name = self.cur_string(); + let ident = self.cur_ident(); self.bump_remap(kind); - (span, Atom::from(name)) + (span, ident) } pub(crate) fn check_identifier(&mut self, kind: Kind, ctx: Context) { diff --git a/crates/oxc_parser/src/lexer/string.rs b/crates/oxc_parser/src/lexer/string.rs index 74a8b7aed16bb..ded5fcbe31855 100644 --- a/crates/oxc_parser/src/lexer/string.rs +++ b/crates/oxc_parser/src/lexer/string.rs @@ -1,6 +1,7 @@ use std::cmp::max; use oxc_allocator::StringBuilder; +use oxc_span::Ident; use crate::diagnostics; @@ -272,4 +273,10 @@ impl<'a> Lexer<'a> { } &source_text[start..end] } + + /// Get the current identifier with precomputed hash. + #[inline] + pub(crate) fn get_ident(&self, token: Token) -> Ident<'a> { + Ident::new(self.get_string(token)) + } }