Skip to content

Commit

Permalink
fix(es/parser): Fix bugs (#1405)
Browse files Browse the repository at this point in the history
swc_ecma_parser:
 - Parse generics in class methods with special name properly.
  • Loading branch information
kdy1 authored Feb 16, 2021
1 parent a53186c commit 5ad57b0
Show file tree
Hide file tree
Showing 6 changed files with 1,370 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ecmascript/parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ include = ["Cargo.toml", "src/**/*.rs", "examples/**/*.rs"]
license = "Apache-2.0/MIT"
name = "swc_ecma_parser"
repository = "https://github.com/swc-project/swc.git"
version = "0.47.1"
version = "0.47.2"

[features]
default = []
Expand Down
14 changes: 13 additions & 1 deletion ecmascript/parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ impl<'a, I: Tokens> Parser<I> {
}

fn parse_class_member(&mut self) -> PResult<ClassMember> {
trace_cur!(self, parse_class_member);

let start = cur_pos!(self);
let decorators = self.parse_decorators(false)?;
let declare = self.syntax().typescript() && eat!(self, "declare");
Expand Down Expand Up @@ -476,6 +478,7 @@ impl<'a, I: Tokens> Parser<I> {
);
}

trace_cur!(self, parse_class_member_with_is_static__normal_class_member);
let mut key = self.parse_class_prop_name()?;
let is_optional = self.input.syntax().typescript() && eat!(self, '?');

Expand All @@ -496,6 +499,8 @@ impl<'a, I: Tokens> Parser<I> {
if self.is_class_method()? {
// handle a(){} / get(){} / set(){} / async(){}

trace_cur!(self, parse_class_member_with_is_static__normal_class_method);

if readonly {
syntax_error!(self, span!(self, start), SyntaxError::ReadOnlyMethod);
}
Expand Down Expand Up @@ -819,7 +824,9 @@ impl<'a, I: Tokens> Parser<I> {
}

fn is_class_method(&mut self) -> PResult<bool> {
Ok(is!(self, '(') || (self.input.syntax().typescript() && is!(self, '<')))
Ok(is!(self, '(')
|| (self.input.syntax().typescript() && is!(self, '<'))
|| (self.input.syntax().typescript() && is!(self, JSXTagStart)))
}

fn is_class_property(&mut self) -> PResult<bool> {
Expand Down Expand Up @@ -912,6 +919,8 @@ impl<'a, I: Tokens> Parser<I> {
where
F: FnOnce(&mut Self) -> PResult<Vec<Param>>,
{
trace_cur!(self, parse_fn_args_body);

// let prev_in_generator = self.ctx().in_generator;
let ctx = Context {
in_async: is_async,
Expand All @@ -922,6 +931,7 @@ impl<'a, I: Tokens> Parser<I> {
self.with_ctx(ctx).parse_with(|p| {
let type_params = if p.syntax().typescript() && is_one_of!(p, '<', JSXTagStart) {
//
trace_cur!(p, parse_fn_args_body__type_params);
Some(p.parse_ts_type_params()?)
} else {
None
Expand Down Expand Up @@ -1033,6 +1043,8 @@ impl<'a, I: Tokens> Parser<I> {
where
F: FnOnce(&mut Self) -> PResult<Vec<Param>>,
{
trace_cur!(self, make_method);

let is_static = static_token.is_some();
let ctx = Context {
span_of_fn_name: Some(key.span()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function foo<P extends RouteParams = RP, S extends State = RS>(
name: string,
path: string,
...middleware: RouterMiddleware<P, S>[]
): Router<P extends RP ? P : (P & RP), S extends RS ? S : (S & RS)>; { }
Loading

0 comments on commit 5ad57b0

Please sign in to comment.