Skip to content

Commit

Permalink
Better error messaging for await not inside an async function
Browse files Browse the repository at this point in the history
  • Loading branch information
kaicataldo committed Oct 23, 2016
1 parent 930cdd0 commit 8358cff
Show file tree
Hide file tree
Showing 32 changed files with 1,335 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/parser/expression.js
Expand Up @@ -416,7 +416,11 @@ pp.parseExprAtom = function (refShorthandDefaultPos) {
let id = this.parseIdentifier(allowAwait || allowYield);

if (id.name === "await") {
if (this.state.inAsync || this.inModule) {
if (!this.state.inAsync && (this.inModule || this.state.inFunction && !this.match(tt.eq))) {
this.raise(node.start, "'await' may only be used in async functions");
}

if (this.state.inAsync) {
return this.parseAwait(node);
}
} else if (id.name === "async" && this.match(tt._function) && !this.canInsertSemicolon()) {
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/39/actual.js
@@ -0,0 +1,3 @@
function test() {
await foo();
}
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/39/options.json
@@ -0,0 +1,3 @@
{
"throws": "'await' may only be used in async functions (2:2)"
}
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/40/actual.js
@@ -0,0 +1,3 @@
function test() {
await = foo();
}
169 changes: 169 additions & 0 deletions test/fixtures/es2017/async-functions/40/expected.json
@@ -0,0 +1,169 @@
{
"type": "File",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 36,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 20,
"end": 34,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 16
}
},
"expression": {
"type": "AssignmentExpression",
"start": 20,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 15
}
},
"operator": "=",
"left": {
"type": "Identifier",
"start": 20,
"end": 25,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 7
},
"identifierName": "await"
},
"name": "await"
},
"right": {
"type": "CallExpression",
"start": 28,
"end": 33,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 15
}
},
"callee": {
"type": "Identifier",
"start": 28,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 10
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": []
}
}
}
],
"directives": []
}
}
],
"directives": []
}
}
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/40/options.json
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/41/actual.js
@@ -0,0 +1,3 @@
function test() {
const await = foo();
}
171 changes: 171 additions & 0 deletions test/fixtures/es2017/async-functions/41/expected.json
@@ -0,0 +1,171 @@
{
"type": "File",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"program": {
"type": "Program",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"sourceType": "script",
"body": [
{
"type": "FunctionDeclaration",
"start": 0,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 3,
"column": 1
}
},
"id": {
"type": "Identifier",
"start": 9,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 13
},
"identifierName": "test"
},
"name": "test"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start": 16,
"end": 42,
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 3,
"column": 1
}
},
"body": [
{
"type": "VariableDeclaration",
"start": 20,
"end": 40,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 22
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 26,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 21
}
},
"id": {
"type": "Identifier",
"start": 26,
"end": 31,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "await"
},
"name": "await"
},
"init": {
"type": "CallExpression",
"start": 34,
"end": 39,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 21
}
},
"callee": {
"type": "Identifier",
"start": 34,
"end": 37,
"loc": {
"start": {
"line": 2,
"column": 16
},
"end": {
"line": 2,
"column": 19
},
"identifierName": "foo"
},
"name": "foo"
},
"arguments": []
}
}
],
"kind": "const"
}
],
"directives": []
}
}
],
"directives": []
}
}
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/41/options.json
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
1 change: 1 addition & 0 deletions test/fixtures/es2017/async-functions/42/actual.js
@@ -0,0 +1 @@
await foo();
4 changes: 4 additions & 0 deletions test/fixtures/es2017/async-functions/42/options.json
@@ -0,0 +1,4 @@
{
"sourceType": "module",
"throws": "'await' may only be used in async functions (1:0)"
}
1 change: 1 addition & 0 deletions test/fixtures/es2017/async-functions/43/actual.js
@@ -0,0 +1 @@
await foo();
3 changes: 3 additions & 0 deletions test/fixtures/es2017/async-functions/43/options.json
@@ -0,0 +1,3 @@
{
"throws": "Unexpected token, expected ; (1:6)"
}
1 change: 1 addition & 0 deletions test/fixtures/es2017/async-functions/44/actual.js
@@ -0,0 +1 @@
await = foo();

0 comments on commit 8358cff

Please sign in to comment.