Skip to content

Commit ed57fa3

Browse files
refactor(parser): Reduce backtracking for assertion signature parsing (#11424)
- Replace use of `lookahead` when parsing assertion signatures in favour of checkpoint and rewinds. - Remove unnecessary backtracking when parsing assertion signatures for one of the code paths. Relates to #11334 as part of reducing unnecessary backtracking from the parser.
1 parent dc110f5 commit ed57fa3

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

crates/oxc_parser/src/ts/types.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,14 @@ impl<'a> ParserImpl<'a> {
406406
Kind::LParen => self.parse_parenthesized_type(),
407407
Kind::Import => TSType::TSImportType(self.parse_ts_import_type()),
408408
Kind::Asserts => {
409-
if self.lookahead(Self::is_next_token_identifier_or_keyword_on_same_line) {
410-
self.parse_asserts_type_predicate()
409+
let checkpoint = self.checkpoint();
410+
let asserts_start_span = self.start_span();
411+
self.bump_any(); // bump `asserts`
412+
413+
if self.is_token_identifier_or_keyword_on_same_line() {
414+
self.parse_asserts_type_predicate(asserts_start_span)
411415
} else {
416+
self.rewind(checkpoint);
412417
self.parse_type_reference()
413418
}
414419
}
@@ -417,8 +422,7 @@ impl<'a> ParserImpl<'a> {
417422
}
418423
}
419424

420-
fn is_next_token_identifier_or_keyword_on_same_line(&mut self) -> bool {
421-
self.bump_any();
425+
fn is_token_identifier_or_keyword_on_same_line(&self) -> bool {
422426
self.cur_kind().is_identifier_name() && !self.cur_token().is_on_new_line()
423427
}
424428

@@ -729,9 +733,7 @@ impl<'a> ParserImpl<'a> {
729733
self.ast.ts_type_template_literal_type(self.end_span(span), quasis, types)
730734
}
731735

732-
fn parse_asserts_type_predicate(&mut self) -> TSType<'a> {
733-
let span = self.start_span();
734-
self.bump_any(); // bump `asserts`
736+
fn parse_asserts_type_predicate(&mut self, asserts_start_span: u32) -> TSType<'a> {
735737
let parameter_name = if self.at(Kind::This) {
736738
TSTypePredicateName::This(self.parse_this_type_node())
737739
} else {
@@ -745,7 +747,7 @@ impl<'a> ParserImpl<'a> {
745747
type_annotation = Some(self.ast.ts_type_annotation(self.end_span(type_span), ty));
746748
}
747749
self.ast.ts_type_type_predicate(
748-
self.end_span(span),
750+
self.end_span(asserts_start_span),
749751
parameter_name,
750752
/* asserts */ true,
751753
type_annotation,

0 commit comments

Comments
 (0)