diff --git a/crates/oxc_parser/examples/parser.rs b/crates/oxc_parser/examples/parser.rs index 43d83f0d7..e50490448 100644 --- a/crates/oxc_parser/examples/parser.rs +++ b/crates/oxc_parser/examples/parser.rs @@ -20,6 +20,15 @@ fn main() -> Result<(), String> { println!("AST:"); println!("{}", serde_json::to_string_pretty(&ret.program).unwrap()); + println!("Comments:"); + let comments = ret + .trivias + .comments + .into_iter() + .map(|(start, end, _)| &source_text[start as usize..end as usize]) + .collect::>(); + println!("{comments:?}"); + if ret.errors.is_empty() { println!("Parsed Successfully."); } else { diff --git a/crates/oxc_parser/src/cursor.rs b/crates/oxc_parser/src/cursor.rs index 8bf3d9f71..c01fd7558 100644 --- a/crates/oxc_parser/src/cursor.rs +++ b/crates/oxc_parser/src/cursor.rs @@ -221,9 +221,11 @@ impl<'a> ParserImpl<'a> { } } - /// Tell lexer to re-read a jsx identifier - pub(crate) fn re_lex_jsx_identifier(&mut self) { - self.token = self.lexer.next_jsx_identifier(self.cur_token().start); + /// Tell lexer to continue reading jsx identifier if the lexer character position is at `-` for `` + pub(crate) fn continue_lex_jsx_identifier(&mut self) { + if let Some(token) = self.lexer.continue_lex_jsx_identifier() { + self.token = token; + } } pub(crate) fn re_lex_right_angle(&mut self) -> Kind { diff --git a/crates/oxc_parser/src/jsx/mod.rs b/crates/oxc_parser/src/jsx/mod.rs index 2262f6e67..7bcfe1b02 100644 --- a/crates/oxc_parser/src/jsx/mod.rs +++ b/crates/oxc_parser/src/jsx/mod.rs @@ -362,8 +362,8 @@ impl<'a> ParserImpl<'a> { if !self.at(Kind::Ident) && !self.cur_kind().is_all_keyword() { return Err(self.unexpected()); } - // we are at a valid normal Ident or Keyword, let's keep on lexing for `-` - self.re_lex_jsx_identifier(); + // Currently at a valid normal Ident or Keyword, keep on lexing for `-` in `` + self.continue_lex_jsx_identifier(); self.bump_any(); let span = self.end_span(span); let name = span.source_text(self.source_text); diff --git a/crates/oxc_parser/src/lexer/jsx.rs b/crates/oxc_parser/src/lexer/jsx.rs index 7be58232f..142bd95ae 100644 --- a/crates/oxc_parser/src/lexer/jsx.rs +++ b/crates/oxc_parser/src/lexer/jsx.rs @@ -2,7 +2,7 @@ use super::{Kind, Lexer, Token}; use crate::diagnostics; use memchr::{memchr, memchr2}; -use oxc_syntax::identifier::{is_identifier_part, is_identifier_start}; +use oxc_syntax::identifier::is_identifier_part; impl<'a> Lexer<'a> { /// `JSXDoubleStringCharacters` :: @@ -47,13 +47,6 @@ impl<'a> Lexer<'a> { self.finish_next(kind) } - /// Expand the current token for `JSXIdentifier` - pub(crate) fn next_jsx_identifier(&mut self, start_offset: u32) -> Token { - let kind = self.read_jsx_identifier(start_offset); - self.lookahead.clear(); - self.finish_next(kind) - } - /// [`JSXChild`](https://facebook.github.io/jsx/#prod-JSXChild) /// `JSXChild` : /// `JSXText` @@ -90,25 +83,28 @@ impl<'a> Lexer<'a> { } } + /// Expand the current `Ident` token for `JSXIdentifier` + /// + /// The current character is at `Ident`, continue reading for `JSXIdentifier` if it has a `-` + /// /// `JSXIdentifier` : /// `IdentifierStart` /// `JSXIdentifier` `IdentifierPart` /// `JSXIdentifier` [no `WhiteSpace` or Comment here] - - fn read_jsx_identifier(&mut self, _start_offset: u32) -> Kind { + pub(crate) fn continue_lex_jsx_identifier(&mut self) -> Option { + if self.peek() != Some('-') { + return None; + } + self.consume_char(); while let Some(c) = self.peek() { - if c == '-' || is_identifier_start(c) { + if c == '-' || is_identifier_part(c) { self.consume_char(); - while let Some(c) = self.peek() { - if is_identifier_part(c) { - self.consume_char(); - } else { - break; - } - } } else { break; } } - Kind::Ident + // Clear the current lookahead `Minus` Token + self.lookahead.clear(); + Some(self.finish_next(Kind::Ident)) } } diff --git a/tasks/coverage/parser_typescript.snap b/tasks/coverage/parser_typescript.snap index 21b4aef40..010e4878d 100644 --- a/tasks/coverage/parser_typescript.snap +++ b/tasks/coverage/parser_typescript.snap @@ -16182,14 +16182,6 @@ Expect to Parse: "conformance/salsa/plainJSRedeclare3.ts" × Invalid characters after number ╭─[conformance/jsx/tsxAttributeInvalidNames.tsx:12:10] 11 │ // Invalid names - 12 │ ; - · ──── - 13 │ ; - ╰──── - - × Invalid characters after number - ╭─[conformance/jsx/tsxAttributeInvalidNames.tsx:12:10] - 11 │ // Invalid names 12 │ ; · ──── 13 │ ;