Skip to content

Commit

Permalink
Fixed parsing of async keyword as class method and field. Fixes #62
Browse files Browse the repository at this point in the history
  • Loading branch information
mck89 committed Aug 16, 2023
1 parent 1df4dc2 commit 982271d
Show file tree
Hide file tree
Showing 11 changed files with 861 additions and 4 deletions.
13 changes: 9 additions & 4 deletions lib/Peast/Syntax/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2611,14 +2611,14 @@ protected function parseMethodDefinition()
}
}

//Handle the case where get and set are methods name and not the
//definition of a getter/setter
if ($kind !== Node\MethodDefinition::KIND_METHOD &&
//Handle the case where get, set and async are methods name and not the
//definition of a getter/setter or the start of an async function
if (($kind !== Node\MethodDefinition::KIND_METHOD || ($async && !$generator)) &&
$this->scanner->consume("(")
) {
$this->scanner->setState($state);
$kind = Node\MethodDefinition::KIND_METHOD;
$error = false;
$error = $async = false;
}

if ($prop = $this->parseClassElementName()) {
Expand Down Expand Up @@ -2688,6 +2688,11 @@ protected function parseMethodDefinition()
}
}
}
//Handle the case where "async" is a class field name
elseif ($this->features->classFields && $async && !$generator) {
$this->scanner->setState($state);
$error = $async = false;
}

if ($error) {
$this->error();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class test {
async () {
}
}
/**************************************************/
class test{async(){}}
/**************************************************/
class test
{
async ( )
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
[
{
"type": "Keyword",
"value": "class",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 1,
"column": 5,
"index": 5
}
}
},
{
"type": "Identifier",
"value": "test",
"location": {
"start": {
"line": 1,
"column": 6,
"index": 6
},
"end": {
"line": 1,
"column": 10,
"index": 10
}
}
},
{
"type": "Punctuator",
"value": "{",
"location": {
"start": {
"line": 1,
"column": 11,
"index": 11
},
"end": {
"line": 1,
"column": 12,
"index": 12
}
}
},
{
"type": "Identifier",
"value": "async",
"location": {
"start": {
"line": 2,
"column": 4,
"index": 17
},
"end": {
"line": 2,
"column": 9,
"index": 22
}
}
},
{
"type": "Punctuator",
"value": "(",
"location": {
"start": {
"line": 2,
"column": 10,
"index": 23
},
"end": {
"line": 2,
"column": 11,
"index": 24
}
}
},
{
"type": "Punctuator",
"value": ")",
"location": {
"start": {
"line": 2,
"column": 11,
"index": 24
},
"end": {
"line": 2,
"column": 12,
"index": 25
}
}
},
{
"type": "Punctuator",
"value": "{",
"location": {
"start": {
"line": 2,
"column": 12,
"index": 25
},
"end": {
"line": 2,
"column": 13,
"index": 26
}
}
},
{
"type": "Punctuator",
"value": "}",
"location": {
"start": {
"line": 2,
"column": 13,
"index": 26
},
"end": {
"line": 2,
"column": 14,
"index": 27
}
}
},
{
"type": "Punctuator",
"value": "}",
"location": {
"start": {
"line": 3,
"column": 0,
"index": 28
},
"end": {
"line": 3,
"column": 1,
"index": 29
}
}
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class test {
async (){}
}
154 changes: 154 additions & 0 deletions test/Peast/Syntax/ES2017/files/AsyncAwait/ClassMethodNamedAsync.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
{
"type": "Program",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 3,
"column": 1,
"index": 29
}
},
"leadingComments": [],
"trailingComments": [],
"body": [
{
"type": "ClassDeclaration",
"location": {
"start": {
"line": 1,
"column": 0,
"index": 0
},
"end": {
"line": 3,
"column": 1,
"index": 29
}
},
"leadingComments": [],
"trailingComments": [],
"id": {
"type": "Identifier",
"location": {
"start": {
"line": 1,
"column": 6,
"index": 6
},
"end": {
"line": 1,
"column": 10,
"index": 10
}
},
"leadingComments": [],
"trailingComments": [],
"name": "test",
"rawName": "test"
},
"superClass": null,
"body": {
"type": "ClassBody",
"location": {
"start": {
"line": 1,
"column": 11,
"index": 11
},
"end": {
"line": 3,
"column": 1,
"index": 29
}
},
"leadingComments": [],
"trailingComments": [],
"body": [
{
"type": "MethodDefinition",
"location": {
"start": {
"line": 2,
"column": 4,
"index": 17
},
"end": {
"line": 2,
"column": 14,
"index": 27
}
},
"leadingComments": [],
"trailingComments": [],
"key": {
"type": "Identifier",
"location": {
"start": {
"line": 2,
"column": 4,
"index": 17
},
"end": {
"line": 2,
"column": 9,
"index": 22
}
},
"leadingComments": [],
"trailingComments": [],
"name": "async",
"rawName": "async"
},
"value": {
"type": "FunctionExpression",
"location": {
"start": {
"line": 2,
"column": 10,
"index": 23
},
"end": {
"line": 2,
"column": 14,
"index": 27
}
},
"leadingComments": [],
"trailingComments": [],
"id": null,
"params": [],
"body": {
"type": "BlockStatement",
"location": {
"start": {
"line": 2,
"column": 12,
"index": 25
},
"end": {
"line": 2,
"column": 14,
"index": 27
}
},
"leadingComments": [],
"trailingComments": [],
"body": []
},
"generator": false,
"async": false
},
"kind": "method",
"computed": false,
"static": false
}
]
}
}
],
"sourceType": "script"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class test {
async *(){}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
async function *(param) {
}
17 changes: 17 additions & 0 deletions test/Peast/Syntax/ES2022/files/ClassFields/ClassFields6.Render.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class test {
async;
async = 1;
async fn () {
}
}
/**************************************************/
class test{async;async=1;async fn(){}}
/**************************************************/
class test
{
async;
async = 1;
async fn ( )
{
}
}
Loading

0 comments on commit 982271d

Please sign in to comment.