Skip to content

Commit

Permalink
Fix parsing type casted generic flow arrow exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
existentialism committed Aug 19, 2020
1 parent 96cc829 commit 0db9dba
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -115,6 +115,8 @@ const FlowErrors = Object.freeze({
'Unexpected token, expected "number" or "bigint"',
UnexpectedTokenAfterTypeParameter:
"Expected an arrow function after this type parameter declaration",
UnexpectedTypeParameterBeforeAsyncArrowFunction:
"Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}`",
UnsupportedDeclareExportKind:
"`declare export %0` is not supported. Use `%1` instead",
UnsupportedStatementInDeclareModule:
Expand Down Expand Up @@ -2720,6 +2722,15 @@ export default (superClass: Class<Parser>): Class<Parser> =>
"ArrowFunctionExpression"
) {
if (!arrow.error && !arrow.aborted) {
// <T> async () => {}
if (arrow.node.async) {
/*:: invariant(typeParameters) */
this.raise(
typeParameters.start,
FlowErrors.UnexpectedTypeParameterBeforeAsyncArrowFunction,
);
}

return arrow.node;
}

Expand Down
@@ -0,0 +1,7 @@
const f = async <T, R, S>(
x: T,
y: R,
z: S,
) => {
return null;
};
@@ -0,0 +1,134 @@
{
"type": "File",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}},
"program": {
"type": "Program",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "VariableDeclaration",
"start":0,"end":75,"loc":{"start":{"line":1,"column":0},"end":{"line":7,"column":2}},
"declarations": [
{
"type": "VariableDeclarator",
"start":6,"end":74,"loc":{"start":{"line":1,"column":6},"end":{"line":7,"column":1}},
"id": {
"type": "Identifier",
"start":6,"end":7,"loc":{"start":{"line":1,"column":6},"end":{"line":1,"column":7},"identifierName":"f"},
"name": "f"
},
"init": {
"type": "ArrowFunctionExpression",
"start":10,"end":74,"loc":{"start":{"line":1,"column":10},"end":{"line":7,"column":1}},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":16,"end":25,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":25}},
"params": [
{
"type": "TypeParameter",
"start":17,"end":18,"loc":{"start":{"line":1,"column":17},"end":{"line":1,"column":18}},
"name": "T",
"variance": null
},
{
"type": "TypeParameter",
"start":20,"end":21,"loc":{"start":{"line":1,"column":20},"end":{"line":1,"column":21}},
"name": "R",
"variance": null
},
{
"type": "TypeParameter",
"start":23,"end":24,"loc":{"start":{"line":1,"column":23},"end":{"line":1,"column":24}},
"name": "S",
"variance": null
}
]
},
"params": [
{
"type": "Identifier",
"start":29,"end":33,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":6},"identifierName":"x"},
"name": "x",
"typeAnnotation": {
"type": "TypeAnnotation",
"start":30,"end":33,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":6}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":32,"end":33,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":32,"end":33,"loc":{"start":{"line":2,"column":5},"end":{"line":2,"column":6},"identifierName":"T"},
"name": "T"
}
}
}
},
{
"type": "Identifier",
"start":37,"end":41,"loc":{"start":{"line":3,"column":2},"end":{"line":3,"column":6},"identifierName":"y"},
"name": "y",
"typeAnnotation": {
"type": "TypeAnnotation",
"start":38,"end":41,"loc":{"start":{"line":3,"column":3},"end":{"line":3,"column":6}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":40,"end":41,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":6}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":40,"end":41,"loc":{"start":{"line":3,"column":5},"end":{"line":3,"column":6},"identifierName":"R"},
"name": "R"
}
}
}
},
{
"type": "Identifier",
"start":45,"end":49,"loc":{"start":{"line":4,"column":2},"end":{"line":4,"column":6},"identifierName":"z"},
"name": "z",
"typeAnnotation": {
"type": "TypeAnnotation",
"start":46,"end":49,"loc":{"start":{"line":4,"column":3},"end":{"line":4,"column":6}},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start":48,"end":49,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":6}},
"typeParameters": null,
"id": {
"type": "Identifier",
"start":48,"end":49,"loc":{"start":{"line":4,"column":5},"end":{"line":4,"column":6},"identifierName":"S"},
"name": "S"
}
}
}
}
],
"id": null,
"generator": false,
"async": true,
"body": {
"type": "BlockStatement",
"start":56,"end":74,"loc":{"start":{"line":5,"column":5},"end":{"line":7,"column":1}},
"body": [
{
"type": "ReturnStatement",
"start":60,"end":72,"loc":{"start":{"line":6,"column":2},"end":{"line":6,"column":14}},
"argument": {
"type": "NullLiteral",
"start":67,"end":71,"loc":{"start":{"line":6,"column":9},"end":{"line":6,"column":13}}
}
}
],
"directives": []
}
}
}
],
"kind": "const"
}
],
"directives": []
}
}
@@ -0,0 +1 @@
<T> async () => {}
@@ -0,0 +1,46 @@
{
"type": "File",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"errors": [
"SyntaxError: Type parameters must come after the async keyword, e.g. instead of `<T> async () => {}`, use `async <T>() => {}` (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "ArrowFunctionExpression",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"id": null,
"generator": false,
"async": true,
"params": [],
"body": {
"type": "BlockStatement",
"start":16,"end":18,"loc":{"start":{"line":1,"column":16},"end":{"line":1,"column":18}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":0,"end":3,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":3}},
"params": [
{
"type": "TypeParameter",
"start":1,"end":2,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":2}},
"name": "T",
"variance": null
}
]
}
}
}
],
"directives": []
}
}

0 comments on commit 0db9dba

Please sign in to comment.