Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Async Generic After Await Parsing Error #11092

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.js
Expand Up @@ -1528,6 +1528,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!this.isRelational("<")) {
return undefined;
}

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
Copy link
Contributor Author

@liamfd liamfd Feb 4, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't 100% sure where to put this change, and the one below to reset things.

For resetting these values, it seemed best to do it before any attempt to parse is started, but strictly speaking it only seemed to need to happen before parseFunctionParams.

For setting state back to oldState below, it looks like typically this is done before parseArrowExpression is called (e.g. https://github.com/liamfd/babel/blob/master/packages/babel-parser/src/parser/expression.js#L972), so I matched that.

const oldYieldPos = this.state.yieldPos;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added oldMaybeInArrowParameters and oldYieldPos at @JLHwung 's suggestion. From looking around, it does seem like these are generally reset together.

I also wasn't entirely sure whether oldMaybeInArrowParameters should be set here, or whether it should be true or false. I opted to match https://github.com/liamfd/babel/blob/master/packages/babel-parser/src/parser/expression.js#L972, true seemed more appropriate.

const oldAwaitPos = this.state.awaitPos;
this.state.maybeInArrowParameters = true;
this.state.yieldPos = -1;
this.state.awaitPos = -1;

const res: ?N.ArrowFunctionExpression = this.tsTryParseAndCatch(() => {
const node: N.ArrowFunctionExpression = this.startNodeAt(
startPos,
Expand All @@ -1541,6 +1549,10 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node;
});

this.state.maybeInArrowParameters = oldMaybeInArrowParameters;
this.state.yieldPos = oldYieldPos;
this.state.awaitPos = oldAwaitPos;

if (!res) {
return undefined;
}
Expand Down
@@ -0,0 +1,4 @@
async () => {
await null;
async <T>() => null;
};
@@ -0,0 +1,214 @@
{
"type": "File",
"start": 0,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 2
}
},
"program": {
"type": "Program",
"start": 0,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 2
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 53,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 2
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 0,
"end": 52,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 4,
"column": 1
}
},
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start": 12,
"end": 52,
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 4,
"column": 1
}
},
"body": [
{
"type": "ExpressionStatement",
"start": 16,
"end": 27,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 13
}
},
"expression": {
"type": "AwaitExpression",
"start": 16,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 2
},
"end": {
"line": 2,
"column": 12
}
},
"argument": {
"type": "NullLiteral",
"start": 22,
"end": 26,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 12
}
}
}
}
},
{
"type": "ExpressionStatement",
"start": 30,
"end": 50,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 22
}
},
"expression": {
"type": "ArrowFunctionExpression",
"start": 30,
"end": 49,
"loc": {
"start": {
"line": 3,
"column": 2
},
"end": {
"line": 3,
"column": 21
}
},
"typeParameters": {
"type": "TSTypeParameterDeclaration",
"start": 36,
"end": 39,
"loc": {
"start": {
"line": 3,
"column": 8
},
"end": {
"line": 3,
"column": 11
}
},
"params": [
{
"type": "TSTypeParameter",
"start": 37,
"end": 38,
"loc": {
"start": {
"line": 3,
"column": 9
},
"end": {
"line": 3,
"column": 10
}
},
"name": "T"
}
]
},
"params": [],
"id": null,
"generator": false,
"async": true,
"body": {
"type": "NullLiteral",
"start": 45,
"end": 49,
"loc": {
"start": {
"line": 3,
"column": 17
},
"end": {
"line": 3,
"column": 21
}
}
}
}
}
],
"directives": []
}
}
}
],
"directives": []
}
}