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

perf(parser): lex JSXText with memchr #2558

Merged
merged 1 commit into from
Mar 1, 2024
Merged
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
25 changes: 13 additions & 12 deletions crates/oxc_parser/src/lexer/jsx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{Kind, Lexer, Token};
use crate::diagnostics;

use memchr::memchr;
use memchr::{memchr, memchr2};
use oxc_syntax::identifier::{is_identifier_part, is_identifier_start};

impl<'a> Lexer<'a> {
Expand Down Expand Up @@ -71,17 +71,18 @@ impl<'a> Lexer<'a> {
Kind::LCurly
}
Some(_) => {
loop {
// The tokens `{`, `<`, `>` and `}` cannot appear in a jsx text.
// The TypeScript compiler raises the error "Unexpected token. Did you mean `{'>'}` or `&gt;`?".
// Where as the Babel compiler does not raise any errors.
// The following check omits `>` and `}` so that more Babel tests can be passed.
if self.peek().is_some_and(|c| c == '{' || c == '<') {
break;
}
if self.next_char().is_none() {
break;
}
// The tokens `{`, `<`, `>` and `}` cannot appear in JSX text.
// The TypeScript compiler raises the error "Unexpected token. Did you mean `{'>'}` or `&gt;`?".
// Where as the Babel compiler does not raise any errors.
// The following check omits `>` and `}` so that more Babel tests can be passed.
let len = memchr2(b'{', b'<', self.remaining().as_bytes());
if let Some(len) = len {
// SAFETY: `memchr2` guarantees `len` will be offset from current position
// of a `{` or `<` byte. So must be a valid UTF-8 boundary, and within bounds of source.
let end = unsafe { self.source.position().add(len) };
self.source.set_position(end);
} else {
self.source.advance_to_end();
}
Kind::JSXText
}
Expand Down
Loading