Allow await when it is not in AsyncArrowHead (#11148)

This commit is contained in:
Arun Kumar Mohan 2020-03-16 16:26:48 -05:00 committed by GitHub
parent 1ba41f2084
commit 84a9ea455b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 122 additions and 1 deletions

View File

@ -560,10 +560,15 @@ export default class ExpressionParser extends LValParser {
stop: false, stop: false,
}; };
do { do {
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
if (state.maybeAsyncArrow) {
this.state.maybeInAsyncArrowHead = true;
}
base = this.parseSubscript(base, startPos, startLoc, noCalls, state); base = this.parseSubscript(base, startPos, startLoc, noCalls, state);
// After parsing a subscript, this isn't "async" for sure. // After parsing a subscript, this isn't "async" for sure.
state.maybeAsyncArrow = false; state.maybeAsyncArrow = false;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
} while (!state.stop); } while (!state.stop);
return base; return base;
} }
@ -953,15 +958,18 @@ export default class ExpressionParser extends LValParser {
!this.canInsertSemicolon() !this.canInsertSemicolon()
) { ) {
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
const oldMaybeInAsyncArrowHead = this.state.maybeInAsyncArrowHead;
const oldYieldPos = this.state.yieldPos; const oldYieldPos = this.state.yieldPos;
const oldAwaitPos = this.state.awaitPos; const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true; this.state.maybeInArrowParameters = true;
this.state.maybeInAsyncArrowHead = true;
this.state.yieldPos = -1; this.state.yieldPos = -1;
this.state.awaitPos = -1; this.state.awaitPos = -1;
const params = [this.parseIdentifier()]; const params = [this.parseIdentifier()];
this.expect(tt.arrow); this.expect(tt.arrow);
this.checkYieldAwaitInDefaultParams(); this.checkYieldAwaitInDefaultParams();
this.state.maybeInArrowParameters = oldMaybeInArrowParameters; this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.maybeInAsyncArrowHead = oldMaybeInAsyncArrowHead;
this.state.yieldPos = oldYieldPos; this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos; this.state.awaitPos = oldAwaitPos;
// let foo = async bar => {}; // let foo = async bar => {};
@ -1296,6 +1304,9 @@ export default class ExpressionParser extends LValParser {
this.shouldParseArrow() && this.shouldParseArrow() &&
(arrowNode = this.parseArrow(arrowNode)) (arrowNode = this.parseArrow(arrowNode))
) { ) {
if (!this.isAwaitAllowed() && !this.state.maybeInAsyncArrowHead) {
this.state.awaitPos = oldAwaitPos;
}
this.checkYieldAwaitInDefaultParams(); this.checkYieldAwaitInDefaultParams();
this.state.yieldPos = oldYieldPos; this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos; this.state.awaitPos = oldAwaitPos;
@ -2128,7 +2139,7 @@ export default class ExpressionParser extends LValParser {
} }
if ( if (
this.state.awaitPos === -1 && this.state.awaitPos === -1 &&
(this.state.maybeInArrowParameters || this.isAwaitAllowed()) (this.state.maybeInAsyncArrowHead || this.isAwaitAllowed())
) { ) {
this.state.awaitPos = this.state.start; this.state.awaitPos = this.state.start;
} }

View File

@ -59,6 +59,11 @@ export default class State {
// Flags to track // Flags to track
inParameters: boolean = false; inParameters: boolean = false;
maybeInArrowParameters: boolean = false; maybeInArrowParameters: boolean = false;
// This flag is used to track async arrow head across function declarations.
// e.g. async (foo = function (await) {}) => {}
// When parsing `await` in this expression, `maybeInAsyncArrowHead` is true
// but `maybeInArrowParameters` is false
maybeInAsyncArrowHead: boolean = false;
inPipeline: boolean = false; inPipeline: boolean = false;
inType: boolean = false; inType: boolean = false;
noAnonFunctionType: boolean = false; noAnonFunctionType: boolean = false;

View File

@ -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": []
}
}