Skip to content

Commit

Permalink
Allow await when it is not in AsyncArrowHead
Browse files Browse the repository at this point in the history
  • Loading branch information
arku committed Mar 10, 2020
1 parent 2057d2b commit 99c07ae
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
13 changes: 12 additions & 1 deletion packages/babel-parser/src/parser/expression.js
Expand Up @@ -560,10 +560,15 @@ export default class ExpressionParser extends LValParser {
stop: false,
};
do {
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
if (state.maybeAsyncArrow) {
this.state.maybeInAsyncArrowHead = state.maybeAsyncArrow;
}
base = this.parseSubscript(base, startPos, startLoc, noCalls, state);

// After parsing a subscript, this isn't "async" for sure.
state.maybeAsyncArrow = false;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
} while (!state.stop);
return base;
}
Expand Down Expand Up @@ -953,15 +958,18 @@ export default class ExpressionParser extends LValParser {
!this.canInsertSemicolon()
) {
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true;
this.state.maybeInAsyncArrowHead = true;
this.state.yieldPos = -1;
this.state.awaitPos = -1;
const params = [this.parseIdentifier()];
this.expect(tt.arrow);
this.checkYieldAwaitInDefaultParams();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
// let foo = async bar => {};
Expand Down Expand Up @@ -1296,6 +1304,9 @@ export default class ExpressionParser extends LValParser {
this.shouldParseArrow() &&
(arrowNode = this.parseArrow(arrowNode))
) {
if (!this.isAwaitAllowed() && !this.state.maybeInAsyncArrowHead) {
this.state.awaitPos = oldAwaitPos;
}
this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;
Expand Down Expand Up @@ -2128,7 +2139,7 @@ export default class ExpressionParser extends LValParser {
}
if (
this.state.awaitPos === -1 &&
(this.state.maybeInArrowParameters || this.isAwaitAllowed())
(this.state.maybeInAsyncArrowHead || this.isAwaitAllowed())
) {
this.state.awaitPos = this.state.start;
}
Expand Down
1 change: 1 addition & 0 deletions packages/babel-parser/src/tokenizer/state.js
Expand Up @@ -59,6 +59,7 @@ export default class State {
// Flags to track
inParameters: boolean = false;
maybeInArrowParameters: boolean = false;
maybeInAsyncArrowHead: boolean = false;
inPipeline: boolean = false;
inType: boolean = false;
noAnonFunctionType: boolean = false;
Expand Down
@@ -0,0 +1 @@
(await) => {}
@@ -0,0 +1,104 @@
{
"type": "File",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"program": {
"type": "Program",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 13
}
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 1,
"end": 6,
"loc": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 6
},
"identifierName": "await"
},
"name": "await"
}
],
"body": {
"type": "BlockStatement",
"start": 11,
"end": 13,
"loc": {
"start": {
"line": 1,
"column": 11
},
"end": {
"line": 1,
"column": 13
}
},
"body": [],
"directives": []
}
}
}
],
"directives": []
}
}

0 comments on commit 99c07ae

Please sign in to comment.