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): Fix parsing of > in typescript mode #7407

Merged
merged 29 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/swc_ecma_parser/src/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,15 @@ impl<'a> Lexer<'a> {
let c = self.cur().unwrap();
self.bump();

if self.syntax.typescript() && self.ctx.in_type && !self.ctx.should_not_lex_lt_or_gt_as_type
{
if c == '<' {
return Ok(Some(tok!('<')));
} else if c == '>' {
return Ok(Some(tok!('>')));
}
}

// XML style comment. `<!--`
if c == '<' && self.is(b'!') && self.peek() == Some('-') && self.peek_ahead() == Some('-') {
self.skip_line_comment(3);
Expand Down
14 changes: 2 additions & 12 deletions crates/swc_ecma_parser/src/lexer/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ impl<'a> Iterator for Lexer<'a> {
start = self.input.cur_pos();
};

let c = match self.input.cur() {
Some(c) => c,
match self.input.cur() {
Some(..) => {}
// End of input.
None => {
if let Some(comments) = self.comments.as_mut() {
Expand Down Expand Up @@ -324,16 +324,6 @@ impl<'a> Iterator for Lexer<'a> {
return self.read_tmpl_token(start_pos_of_tpl).map(Some);
}

if self.syntax.typescript() && self.ctx.in_type {
if c == '<' {
self.input.bump();
return Ok(Some(tok!('<')));
} else if c == '>' {
self.input.bump();
return Ok(Some(tok!('>')));
}
}

self.read_token()
})();

Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ pub struct Context {

in_type: bool,
/// Typescript extension.
should_not_lex_lt_or_gt_as_type: bool,
/// Typescript extension.
in_declare: bool,

/// If true, `:` should not be treated as a type annotation.
Expand Down
32 changes: 31 additions & 1 deletion crates/swc_ecma_parser/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,15 @@ impl<I: Tokens> Parser<I> {
/// `is_new_expr`: true iff we are parsing production 'NewExpression'.
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn parse_member_expr_or_new_expr(&mut self, is_new_expr: bool) -> PResult<Box<Expr>> {
let ctx = Context {
should_not_lex_lt_or_gt_as_type: true,
..self.ctx()
};
self.with_ctx(ctx)
.parse_member_expr_or_new_expr_inner(is_new_expr)
}

fn parse_member_expr_or_new_expr_inner(&mut self, is_new_expr: bool) -> PResult<Box<Expr>> {
trace_cur!(self, parse_member_expr_or_new_expr);

let start = cur_pos!(self);
Expand Down Expand Up @@ -680,6 +689,7 @@ impl<I: Tokens> Parser<I> {
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 {
expr: obj,
type_args,
Expand Down Expand Up @@ -1102,6 +1112,7 @@ impl<I: Tokens> Parser<I> {
})
}

#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
pub(super) fn parse_subscripts(
&mut self,
mut obj: Callee,
Expand All @@ -1118,13 +1129,15 @@ impl<I: Tokens> Parser<I> {
}

/// returned bool is true if this method should be called again.
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
fn parse_subscript(
&mut self,
start: BytePos,
mut obj: Callee,
no_call: bool,
no_computed_member: bool,
) -> PResult<(Box<Expr>, bool)> {
trace_cur!(self, parse_subscript);
let _ = cur!(self, false);

if self.input.syntax().typescript() {
Expand Down Expand Up @@ -1169,7 +1182,11 @@ impl<I: Tokens> Parser<I> {

let mut_obj_opt = &mut obj_opt;

let result = self.try_parse_ts(|p| {
let ctx: Context = Context {
should_not_lex_lt_or_gt_as_type: true,
..self.ctx()
};
let result = self.with_ctx(ctx).try_parse_ts(|p| {
if !no_call
&& p.at_possible_async(match &mut_obj_opt {
Some(Callee::Expr(ref expr)) => expr,
Expand Down Expand Up @@ -1210,6 +1227,18 @@ impl<I: Tokens> Parser<I> {
)
.map(|expr| (Box::new(Expr::TaggedTpl(expr)), true))
.map(Some)
} else if is!(p, '=') {
Ok(Some((
Box::new(Expr::TsInstantiation(TsInstantiation {
span: span!(p, start),
expr: match mut_obj_opt {
Some(Callee::Expr(obj)) => obj.take(),
_ => unreachable!(),
},
type_args,
})),
true,
)))
} else if no_call {
unexpected!(p, "`")
} else {
Expand Down Expand Up @@ -1500,6 +1529,7 @@ impl<I: Tokens> Parser<I> {
}

/// Parse call, dot, and `[]`-subscript expressions.
#[cfg_attr(feature = "debug", tracing::instrument(skip_all))]
pub(super) fn parse_lhs_expr(&mut self) -> PResult<Box<Expr>> {
trace_cur!(self, parse_lhs_expr);

Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_parser/src/parser/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,9 @@ 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>> {
trace_cur!(self, try_parse_ts_type_args);
debug_assert!(self.input.syntax().typescript());

self.try_parse_ts(|p| {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn(z<Number, Object>(y))
124 changes: 124 additions & 0 deletions crates/swc_ecma_parser/tests/typescript/issue-7403/1/input.ts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
{
"type": "Script",
"span": {
"start": 1,
"end": 25,
"ctxt": 0
},
"body": [
{
"type": "ExpressionStatement",
"span": {
"start": 1,
"end": 25,
"ctxt": 0
},
"expression": {
"type": "CallExpression",
"span": {
"start": 1,
"end": 25,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 1,
"end": 3,
"ctxt": 0
},
"value": "fn",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "CallExpression",
"span": {
"start": 4,
"end": 24,
"ctxt": 0
},
"callee": {
"type": "Identifier",
"span": {
"start": 4,
"end": 5,
"ctxt": 0
},
"value": "z",
"optional": false
},
"arguments": [
{
"spread": null,
"expression": {
"type": "Identifier",
"span": {
"start": 22,
"end": 23,
"ctxt": 0
},
"value": "y",
"optional": false
}
}
],
"typeArguments": {
"type": "TsTypeParameterInstantiation",
"span": {
"start": 5,
"end": 21,
"ctxt": 0
},
"params": [
{
"type": "TsTypeReference",
"span": {
"start": 6,
"end": 12,
"ctxt": 0
},
"typeName": {
"type": "Identifier",
"span": {
"start": 6,
"end": 12,
"ctxt": 0
},
"value": "Number",
"optional": false
},
"typeParams": null
},
{
"type": "TsTypeReference",
"span": {
"start": 14,
"end": 20,
"ctxt": 0
},
"typeName": {
"type": "Identifier",
"span": {
"start": 14,
"end": 20,
"ctxt": 0
},
"value": "Object",
"optional": false
},
"typeParams": null
}
]
}
}
}
],
"typeArguments": null
}
}
],
"interpreter": null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fn(x < y, x < y, x >>>= y)