Skip to content

Commit

Permalink
fix(parser): improve lexing of jsx identifier to fix duplicated comme…
Browse files Browse the repository at this point in the history
…nts after jsx name (#2687)
  • Loading branch information
Boshen authored Mar 12, 2024
1 parent 6c6adb4 commit cda9c93
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
9 changes: 9 additions & 0 deletions crates/oxc_parser/examples/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
println!("{comments:?}");

if ret.errors.is_empty() {
println!("Parsed Successfully.");
} else {
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<component-name>`
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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/jsx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<component-name />`
self.continue_lex_jsx_identifier();
self.bump_any();
let span = self.end_span(span);
let name = span.source_text(self.source_text);
Expand Down
32 changes: 14 additions & 18 deletions crates/oxc_parser/src/lexer/jsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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` ::
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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<Token> {
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))
}
}
8 changes: 0 additions & 8 deletions tasks/coverage/parser_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 │ <test1 32data={32} />;
· ────
13 │ <test2 -data={32} />;
╰────

× Invalid characters after number
╭─[conformance/jsx/tsxAttributeInvalidNames.tsx:12:10]
11 │ // Invalid names
12 │ <test1 32data={32} />;
· ────
13 │ <test2 -data={32} />;
Expand Down

0 comments on commit cda9c93

Please sign in to comment.