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

fix(es/parser): Re-lex << as two <-s if required #7439

Merged
merged 21 commits into from
Jun 30, 2023
4 changes: 4 additions & 0 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ impl Tokens for Lexer<'_> {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn reset_to(&mut self, to: BytePos) {
self.input.reset_to(to);
}
}

impl<'a> Iterator for Lexer<'a> {
Expand Down
3 changes: 3 additions & 0 deletions crates/swc_ecma_parser/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ macro_rules! tok {
('>') => {
crate::token::Token::BinOp(crate::token::BinOpToken::Gt)
};
("<<") => {
crate::token::Token::BinOp(crate::token::BinOpToken::LShift)
};
(">>") => {
crate::token::Token::BinOp(crate::token::BinOpToken::RShift)
};
Expand Down
23 changes: 21 additions & 2 deletions crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,12 @@ impl<I: Tokens> Parser<I> {
let obj = self.parse_primary_expr()?;
return_if_arrow!(self, obj);

let type_args = if self.syntax().typescript() && is!(self, '<') {
let type_args = if self.syntax().typescript() {
self.try_parse_ts_type_args()
} else {
None
};

let obj = if let Some(type_args) = type_args {
trace_cur!(self, parse_member_expr_or_new_expr__with_type_args);
Box::new(Expr::TsInstantiation(TsInstantiation {
Expand Down Expand Up @@ -1590,9 +1591,27 @@ impl<I: Tokens> Parser<I> {
let callee = self.parse_new_expr()?;
return_if_arrow!(self, callee);

let type_args = if self.input.syntax().typescript() && is!(self, '<') {
let type_args = if self.input.syntax().typescript() && is_one_of!(self, '<', "<<") {
let type_args_start = self.input.cur_pos();
self.try_parse_ts(|p| {
trace_cur!(p, parse_lhs_expr__type_args);

if is!(p, "<<") {
let ctx = Context {
should_not_lex_lt_or_gt_as_type: false,
in_type: true,
..p.ctx()
};
p.input.reset_to(type_args_start);
p.input.set_ctx(ctx);
}

trace_cur!(p, parse_lhs_expr__before_type_args);

let type_args = p.parse_ts_type_args()?;

trace_cur!(p, parse_lhs_expr__after_type_args);

if is!(p, '(') {
Ok(Some(type_args))
} else {
Expand Down
20 changes: 17 additions & 3 deletions crates/swc_ecma_parser/src/parser/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pub trait Tokens: Clone + Iterator<Item = TokenAndSpan> {
fn add_module_mode_error(&self, error: Error);

fn take_errors(&mut self) -> Vec<Error>;

fn reset_to(&mut self, to: BytePos);
}

#[derive(Clone)]
Expand Down Expand Up @@ -141,6 +143,8 @@ impl Tokens for TokensInput {
fn take_errors(&mut self) -> Vec<Error> {
take(&mut self.errors.borrow_mut())
}

fn reset_to(&mut self, _: BytePos) {}
}

/// Note: Lexer need access to parser's context to lex correctly.
Expand Down Expand Up @@ -253,6 +257,10 @@ impl<I: Tokens> Tokens for Capturing<I> {
fn take_errors(&mut self) -> Vec<Error> {
self.inner.take_errors()
}

fn reset_to(&mut self, to: BytePos) {
self.inner.reset_to(to);
}
}

/// This struct is responsible for managing current token and peeked token.
Expand Down Expand Up @@ -318,9 +326,9 @@ impl<I: Tokens> Buffer<I> {

#[cold]
#[inline(never)]
pub fn dump_cur(&mut self) -> String {
match self.cur() {
Some(v) => format!("{:?}", v),
pub fn dump_cur(&self) -> String {
match &self.cur {
Some(v) => format!("{:?}", v.token),
None => "<eof>".to_string(),
}
}
Expand Down Expand Up @@ -494,4 +502,10 @@ impl<I: Tokens> Buffer<I> {
pub(crate) fn set_token_context(&mut self, c: lexer::TokenContexts) {
self.iter.set_token_context(c)
}

#[inline]
pub(crate) fn reset_to(&mut self, to: BytePos) {
self.cur = None;
self.iter.reset_to(to)
}
}
29 changes: 27 additions & 2 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ impl<I: Tokens> Parser<I> {
permit_in_out: bool,
permit_const: bool,
) -> PResult<Box<TsTypeParamDecl>> {
self.in_type().parse_with(|p| {
trace_cur!(self, parse_ts_type_params);

let ret = self.in_type().parse_with(|p| {
p.ts_in_no_context(|p| {
let start = cur_pos!(p);

Expand All @@ -478,7 +480,11 @@ impl<I: Tokens> Parser<I> {
params,
}))
})
})
})?;

trace_cur!(self, parse_ts_type_params__end);

Ok(ret)
}

/// `tsParseTypeOrTypePredicateAnnotation`
Expand Down Expand Up @@ -574,11 +580,25 @@ impl<I: Tokens> Parser<I> {

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
pub(super) fn try_parse_ts_type_args(&mut self) -> Option<Box<TsTypeParamInstantiation>> {
let start = self.input.cur_pos();

trace_cur!(self, try_parse_ts_type_args);
debug_assert!(self.input.syntax().typescript());

self.try_parse_ts(|p| {
let nested = is!(p, "<<");
if nested {
let ctx = Context {
should_not_lex_lt_or_gt_as_type: false,
in_type: true,
..p.ctx()
};
p.input.reset_to(start);
p.input.set_ctx(ctx);
}

let type_args = p.parse_ts_type_args()?;
trace_cur!(p, try_parse_ts_type_args__after_type_args);

if is_one_of!(
p, '<', // invalid syntax
Expand Down Expand Up @@ -1840,9 +1860,12 @@ impl<I: Tokens> Parser<I> {

// ----- inlined `self.tsFillSignature(tt.arrow, node)`
let type_params = self.try_parse_ts_type_params(false, true)?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_type_params);
expect!(self, '(');
let params = self.parse_ts_binding_list_for_signature()?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_params);
let type_ann = self.parse_ts_type_or_type_predicate_ann(&tok!("=>"))?;
trace_cur!(self, parse_ts_fn_or_constructor_type__after_type_ann);
// ----- end

Ok(if is_fn_type {
Expand Down Expand Up @@ -2010,6 +2033,8 @@ impl<I: Tokens> Parser<I> {
permit_in_out: bool,
permit_const: bool,
) -> PResult<Option<Box<TsTypeParamDecl>>> {
trace_cur!(self, try_parse_ts_type_params);

if !cfg!(feature = "typescript") {
return Ok(None);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
assertType<<K>(key: K) => K>(foo);
169 changes: 169 additions & 0 deletions crates/swc_ecma_parser/tests/typescript/issue-7187/1/input.tsx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 35,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 1,
"end": 35,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 1,
"end": 34,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 1,
"end": 11,
"ctxt": 0
},
"value": "assertType",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "Identifier",
"span": {
"start": 30,
"end": 33,
"ctxt": 0
},
"value": "foo",
"optional": false
}
}
],
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 11,
"end": 29,
"ctxt": 0
},
"params": [
{
"type": "TsFunctionType",
"span": {
"start": 12,
"end": 28,
"ctxt": 0
},
"params": [
{
"type": "Identifier",
"span": {
"start": 16,
"end": 22,
"ctxt": 0
},
"value": "key",
"optional": false,
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 19,
"end": 22,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsTypeReference",
"span": {
"start": 21,
"end": 22,
"ctxt": 0
},
"typeName": {
"type": "Identifier",
"span": {
"start": 21,
"end": 22,
"ctxt": 0
},
"value": "K",
"optional": false
},
"typeParams": null
}
}
}
],
"typeParams": {
"type": "TsTypeParameterDeclaration",
"span": {
"start": 12,
"end": 15,
"ctxt": 0
},
"parameters": [
{
"type": "TsTypeParameter",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"name": {
"type": "Identifier",
"span": {
"start": 13,
"end": 14,
"ctxt": 0
},
"value": "K",
"optional": false
},
"in": false,
"out": false,
"const": false,
"constraint": null,
"default": null
}
]
},
"typeAnnotation": {
"type": "TsTypeAnnotation",
"span": {
"start": 24,
"end": 28,
"ctxt": 0
},
"typeAnnotation": {
"type": "TsTypeReference",
"span": {
"start": 27,
"end": 28,
"ctxt": 0
},
"typeName": {
"type": "Identifier",
"span": {
"start": 27,
"end": 28,
"ctxt": 0
},
"value": "K",
"optional": false
},
"typeParams": null
}
}
}
]
}
}
}
],
"interpreter": null
}