Skip to content

Commit

Permalink
fix(es/parser): Fix class getter/setter ASI bugs (#2409)
Browse files Browse the repository at this point in the history
  • Loading branch information
g-plane authored Oct 12, 2021
1 parent d114e7d commit 9446a03
Show file tree
Hide file tree
Showing 7 changed files with 228 additions and 28 deletions.
60 changes: 32 additions & 28 deletions ecmascript/parser/src/parser/class_and_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ impl<'a, I: Tokens> Parser<I> {

let declare_token = if declare {
// Handle declare(){}
if self.is_class_method()? {
if self.is_class_method() {
let key = Either::Right(PropName::Ident(Ident::new(
js_word!("declare"),
span!(self, start),
Expand All @@ -341,7 +341,7 @@ impl<'a, I: Tokens> Parser<I> {
kind: MethodKind::Method,
},
);
} else if self.is_class_property()? {
} else if self.is_class_property(/* asi */ true) {
// Property named `declare`

let key = Either::Right(PropName::Ident(Ident::new(
Expand Down Expand Up @@ -379,7 +379,7 @@ impl<'a, I: Tokens> Parser<I> {

if let Some(static_token) = static_token {
// Handle static(){}
if self.is_class_method()? {
if self.is_class_method() {
let key = Either::Right(PropName::Ident(Ident::new(
js_word!("static"),
static_token,
Expand All @@ -401,7 +401,7 @@ impl<'a, I: Tokens> Parser<I> {
kind: MethodKind::Method,
},
);
} else if self.is_class_property()? {
} else if self.is_class_property(/* asi */ true) {
// Property named `static`

// Avoid to parse
Expand Down Expand Up @@ -616,7 +616,7 @@ impl<'a, I: Tokens> Parser<I> {
}
};

if self.is_class_method()? {
if self.is_class_method() {
// handle a(){} / get(){} / set(){} / async(){}

trace_cur!(self, parse_class_member_with_is_static__normal_class_method);
Expand Down Expand Up @@ -736,21 +736,6 @@ impl<'a, I: Tokens> Parser<I> {
}
}

if self.is_class_property()? {
return self.make_property(
start,
decorators,
accessibility,
key,
is_static,
is_optional,
readonly.is_some(),
declare,
is_abstract,
is_override,
);
}

if match key {
Either::Right(PropName::Ident(ref i)) => i.sym == js_word!("async"),
_ => false,
Expand Down Expand Up @@ -802,6 +787,7 @@ impl<'a, I: Tokens> Parser<I> {
// `get\n*` is an uninitialized property named 'get' followed by a generator.
Either::Right(PropName::Ident(ref i))
if (i.sym == js_word!("get") || i.sym == js_word!("set"))
&& !self.is_class_property(/* asi */ false)
&& !is_next_line_generator =>
{
// handle get foo(){} / set foo(v){}
Expand Down Expand Up @@ -872,6 +858,21 @@ impl<'a, I: Tokens> Parser<I> {
_ => {}
}

if self.is_class_property(/* asi */ true) {
return self.make_property(
start,
decorators,
accessibility,
key,
is_static,
is_optional,
readonly.is_some(),
declare,
is_abstract,
is_override,
);
}

unexpected!(self, "* for generator, private key, identifier or async")
}

Expand Down Expand Up @@ -968,17 +969,20 @@ impl<'a, I: Tokens> Parser<I> {
})
}

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

fn is_class_property(&mut self) -> PResult<bool> {
Ok(
(self.input.syntax().typescript() && is_one_of!(self, '!', ':'))
|| is_one_of!(self, '=', ';', '}'),
)
fn is_class_property(&mut self, asi: bool) -> bool {
(self.input.syntax().typescript() && is_one_of!(self, '!', ':'))
|| is_one_of!(self, '=', '}')
|| if asi {
is!(self, ';')
} else {
is_exact!(self, ';')
}
}

fn parse_fn<T>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class C {
get
async x() { return 0 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: Expected '(', got 'x'
--> $DIR/tests/typescript-errors/class/get-set-asi-issue-1/input.ts:3:9
|
3 | async x() { return 0 }
| ^

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class C {
get
static x() { return 0 }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: Expected '(', got 'x'
--> $DIR/tests/typescript-errors/class/get-set-asi-issue-2/input.ts:3:10
|
3 | static x() { return 0 }
| ^

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class C {
get
x() { return 0 }

set
x(value) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"type": "Script",
"span": {
"start": 0,
"end": 57,
"ctxt": 0
},
"body": [
{
"type": "ClassDeclaration",
"identifier": {
"type": "Identifier",
"span": {
"start": 6,
"end": 7,
"ctxt": 0
},
"value": "C",
"optional": false
},
"declare": false,
"span": {
"start": 0,
"end": 57,
"ctxt": 0
},
"decorators": [],
"body": [
{
"type": "ClassMethod",
"span": {
"start": 12,
"end": 34,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 18,
"end": 19,
"ctxt": 0
},
"value": "x",
"optional": false
},
"function": {
"params": [],
"decorators": [],
"span": {
"start": 12,
"end": 34,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 22,
"end": 34,
"ctxt": 0
},
"stmts": [
{
"type": "ReturnStatement",
"span": {
"start": 24,
"end": 32,
"ctxt": 0
},
"argument": {
"type": "NumericLiteral",
"span": {
"start": 31,
"end": 32,
"ctxt": 0
},
"value": 0.0
}
}
]
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
},
"kind": "getter",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": false
},
{
"type": "ClassMethod",
"span": {
"start": 38,
"end": 55,
"ctxt": 0
},
"key": {
"type": "Identifier",
"span": {
"start": 44,
"end": 45,
"ctxt": 0
},
"value": "x",
"optional": false
},
"function": {
"params": [
{
"type": "Parameter",
"span": {
"start": 46,
"end": 51,
"ctxt": 0
},
"decorators": [],
"pat": {
"type": "Identifier",
"span": {
"start": 46,
"end": 51,
"ctxt": 0
},
"value": "value",
"optional": false,
"typeAnnotation": null
}
}
],
"decorators": [],
"span": {
"start": 38,
"end": 55,
"ctxt": 0
},
"body": {
"type": "BlockStatement",
"span": {
"start": 53,
"end": 55,
"ctxt": 0
},
"stmts": []
},
"generator": false,
"async": false,
"typeParameters": null,
"returnType": null
},
"kind": "setter",
"isStatic": false,
"accessibility": null,
"isAbstract": false,
"isOptional": false,
"isOverride": false
}
],
"superClass": null,
"isAbstract": false,
"typeParams": null,
"superTypeParams": null,
"implements": []
}
],
"interpreter": null
}

0 comments on commit 9446a03

Please sign in to comment.