Skip to content

Commit

Permalink
Fix await as function name
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 5, 2018
1 parent b4d80b6 commit 42de14d
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 3 deletions.
12 changes: 9 additions & 3 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -847,7 +847,6 @@ export default class StatementParser extends ExpressionParser {
const oldInClassProperty = this.state.inClassProperty;
this.state.inFunction = true;
this.state.inMethod = false;
this.state.inAsync = isAsync;
this.state.inClassProperty = false;

this.initFunction(node, isAsync);
Expand All @@ -874,11 +873,18 @@ export default class StatementParser extends ExpressionParser {
// valid because yield is parsed as if it was outside the generator.
// Therefore, this.state.inGenerator is set before or after parsing the
// function id according to the "isStatement" parameter.
if (!isStatement) this.state.inGenerator = node.generator;
// The same applies to await & async functions.
if (!isStatement) {
this.state.inAsync = isAsync;
this.state.inGenerator = node.generator;
}
if (this.match(tt.name) || this.match(tt._yield)) {
node.id = this.parseBindingIdentifier();
}
if (isStatement) this.state.inGenerator = node.generator;
if (isStatement) {
this.state.inAsync = isAsync;
this.state.inGenerator = node.generator;
}

this.parseFunctionParams(node);
this.parseFunctionBodyAndFinish(
Expand Down
@@ -0,0 +1 @@
async function await() {}
@@ -0,0 +1,86 @@
{
"type": "File",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"program": {
"type": "Program",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 20,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 20
},
"identifierName": "await"
},
"name": "await"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 23,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 23
},
"end": {
"line": 1,
"column": 25
}
},
"body": [],
"directives": []
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
(async function await() {});
@@ -0,0 +1,3 @@
{
"throws": "invalid use of await inside of an async function (1:16)"
}
@@ -0,0 +1,3 @@
async function foo() {
function await() {}
}
@@ -0,0 +1,3 @@
{
"throws": "invalid use of await inside of an async function (2:11)"
}
@@ -0,0 +1,3 @@
async function fn() {
(function await() {});
}
@@ -0,0 +1,158 @@
{
"type": "File",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 15,
"end": 17,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 17
},
"identifierName": "fn"
},
"name": "fn"
},
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 20,
"end": 48,
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 24,
"end": 46,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 24
}
},
"expression": {
"type": "FunctionExpression",
"start": 25,
"end": 44,
"loc": {
"start": {
"line": 2,
"column": 3
},
"end": {
"line": 2,
"column": 22
}
},
"id": {
"type": "Identifier",
"start": 34,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 17
},
"identifierName": "await"
},
"name": "await"
},
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 42,
"end": 44,
"loc": {
"start": {
"line": 2,
"column": 20
},
"end": {
"line": 2,
"column": 22
}
},
"body": [],
"directives": []
},
"extra": {
"parenthesized": true,
"parenStart": 24
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}

0 comments on commit 42de14d

Please sign in to comment.