Skip to content

Commit

Permalink
Create parser plugin "topLevelAwait"
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Oct 8, 2019
1 parent 25b3526 commit 803d331
Show file tree
Hide file tree
Showing 22 changed files with 489 additions and 5 deletions.
1 change: 1 addition & 0 deletions packages/babel-parser/src/parser/expression.js
Expand Up @@ -2183,6 +2183,7 @@ export default class ExpressionParser extends LValParser {
isAwaitAllowed(): boolean {
if (this.scope.inFunction) return this.scope.inAsync;
if (this.options.allowAwaitOutsideFunction) return true;
if (this.hasPlugin("topLevelAwait")) return this.inModule;
return false;
}

Expand Down
6 changes: 1 addition & 5 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -495,11 +495,7 @@ export default class StatementParser extends ExpressionParser {
this.state.labels.push(loopLabel);

let awaitAt = -1;
if (
(this.scope.inAsync ||
(!this.scope.inFunction && this.options.allowAwaitOutsideFunction)) &&
this.eatContextual("await")
) {
if (this.isAwaitAllowed() && this.eatContextual("await")) {
awaitAt = this.state.lastTokStart;
}
this.scope.enter(SCOPE_OTHER);
Expand Down
@@ -0,0 +1 @@
export default await 0;
@@ -0,0 +1,4 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}
@@ -0,0 +1,85 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExportDefaultDeclaration",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"declaration": {
"type": "AwaitExpression",
"start": 15,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 15
},
"end": {
"line": 1,
"column": 22
}
},
"argument": {
"type": "NumericLiteral",
"start": 21,
"end": 22,
"loc": {
"start": {
"line": 1,
"column": 21
},
"end": {
"line": 1,
"column": 22
}
},
"extra": {
"rawValue": 0,
"raw": "0"
},
"value": 0
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
for await (const a of b);
@@ -0,0 +1,4 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}
@@ -0,0 +1,134 @@
{
"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": "module",
"interpreter": null,
"body": [
{
"type": "ForOfStatement",
"start": 0,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 25
}
},
"await": true,
"left": {
"type": "VariableDeclaration",
"start": 11,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 18
}
},
"declarations": [
{
"type": "VariableDeclarator",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 18
}
},
"id": {
"type": "Identifier",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 1,
"column": 17
},
"end": {
"line": 1,
"column": 18
},
"identifierName": "a"
},
"name": "a"
},
"init": null
}
],
"kind": "const"
},
"right": {
"type": "Identifier",
"start": 22,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 23
},
"identifierName": "b"
},
"name": "b"
},
"body": {
"type": "EmptyStatement",
"start": 24,
"end": 25,
"loc": {
"start": {
"line": 1,
"column": 24
},
"end": {
"line": 1,
"column": 25
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
for await (const a of b);
@@ -0,0 +1,5 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "script",
"throws": "Unexpected token, expected \"(\" (1:4)"
}
@@ -0,0 +1 @@
() => await 0;
@@ -0,0 +1,5 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module",
"throws": "Can not use keyword 'await' outside an async function (1:6)"
}
@@ -0,0 +1,3 @@
if (true) {
await 0;
}
@@ -0,0 +1,4 @@
{
"plugins": ["topLevelAwait"],
"sourceType": "module"
}

0 comments on commit 803d331

Please sign in to comment.