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 parsing type casted generic flow arrow exprs #11955

Merged
merged 3 commits into from Aug 19, 2020
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
51 changes: 42 additions & 9 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2674,28 +2674,57 @@ export default (superClass: Class<Parser>): Class<Parser> =>

let typeParameters;

const arrow = this.tryParse(() => {
const arrow = this.tryParse(abort => {
typeParameters = this.flowParseTypeParameterDeclaration();

const arrowExpression = this.forwardNoArrowParamsConversionAt(
typeParameters,
() =>
super.parseMaybeAssign(
() => {
const result = super.parseMaybeAssign(
refExpressionErrors,
afterLeftParse,
refNeedsArrowPos,
),
);

this.resetStartLocationFromNode(result, typeParameters);

return result;
},
);
arrowExpression.typeParameters = typeParameters;
this.resetStartLocationFromNode(arrowExpression, typeParameters);

// <T>(() => {}: any);
if (
arrowExpression.type !== "ArrowFunctionExpression" &&
arrowExpression.extra?.parenthesized
) {
abort();
}

// The above can return a TypeCastExpression when the arrow
// expression is not wrapped in parens. See also `this.parseParenItem`.
const expr = this.maybeUnwrapTypeCastExpression(arrowExpression);
existentialism marked this conversation as resolved.
Show resolved Hide resolved
expr.typeParameters = typeParameters;
this.resetStartLocationFromNode(expr, typeParameters);

return arrowExpression;
}, state);

const arrowExpression: ?N.ArrowFunctionExpression =
arrow.node?.type === "ArrowFunctionExpression" ? arrow.node : null;
let arrowExpression: ?(
| N.ArrowFunctionExpression
| N.TypeCastExpression
) = null;

if (!arrow.error && arrowExpression) return arrowExpression;
if (
arrow.node &&
this.maybeUnwrapTypeCastExpression(arrow.node).type ===
"ArrowFunctionExpression"
) {
if (!arrow.error && !arrow.aborted) {
return arrow.node;
}

arrowExpression = arrow.node;
}

// If we are here, both JSX and Flow parsing attempts failed.
// Give the precedence to the JSX error, except if JSX had an
Expand Down Expand Up @@ -3482,4 +3511,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
}
return false;
}

maybeUnwrapTypeCastExpression(node: N.Node) {
return node.type === "TypeCastExpression" ? node.expression : node;
}
};
@@ -0,0 +1 @@
<T>(() => {}: any);
@@ -0,0 +1,6 @@
{
"sourceType": "module",
"plugins": ["flow"],
"createParenthesizedExpressions": true,
"throws": "Expected an arrow function after this type parameter declaration (1:0)"
}
@@ -0,0 +1 @@
<T>(() => {}: any);
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["flow"],
"throws": "Expected an arrow function after this type parameter declaration (1:0)"
}
@@ -0,0 +1,2 @@
(<T>() => {}: any);
((<T>() => {}): any);
@@ -0,0 +1,5 @@
{
"sourceType": "module",
"plugins": ["flow"],
"createParenthesizedExpressions": true
}
@@ -0,0 +1,109 @@
{
"type": "File",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":21}},
"program": {
"type": "Program",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":21}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"expression": {
"type": "ParenthesizedExpression",
"start":0,"end":18,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":18}},
"expression": {
"type": "TypeCastExpression",
"start":1,"end":17,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":17}},
"expression": {
"type": "ArrowFunctionExpression",
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"params": [
{
"type": "TypeParameter",
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3}},
"name": "T",
"variance": null
}
]
}
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start":12,"end":17,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":17}},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17}}
}
}
}
}
},
{
"type": "ExpressionStatement",
"start":20,"end":41,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":21}},
"expression": {
"type": "ParenthesizedExpression",
"start":20,"end":40,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":20}},
"expression": {
"type": "TypeCastExpression",
"start":21,"end":39,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":19}},
"expression": {
"type": "ParenthesizedExpression",
"start":21,"end":34,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":14}},
"expression": {
"type": "ArrowFunctionExpression",
"start":22,"end":33,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":13}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":31,"end":33,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":13}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":22,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}},
"params": [
{
"type": "TypeParameter",
"start":23,"end":24,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4}},
"name": "T",
"variance": null
}
]
}
}
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start":34,"end":39,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start":36,"end":39,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":19}}
}
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,2 @@
(<T>() => {}: any);
((<T>() => {}): any);
@@ -0,0 +1,109 @@
{
"type": "File",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":21}},
"program": {
"type": "Program",
"start":0,"end":41,"loc":{"start":{"line":1,"column":0},"end":{"line":2,"column":21}},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start":0,"end":19,"loc":{"start":{"line":1,"column":0},"end":{"line":1,"column":19}},
"expression": {
"type": "TypeCastExpression",
"start":1,"end":17,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":17}},
"expression": {
"type": "ArrowFunctionExpression",
"start":1,"end":12,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":12}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":10,"end":12,"loc":{"start":{"line":1,"column":10},"end":{"line":1,"column":12}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":1,"end":4,"loc":{"start":{"line":1,"column":1},"end":{"line":1,"column":4}},
"params": [
{
"type": "TypeParameter",
"start":2,"end":3,"loc":{"start":{"line":1,"column":2},"end":{"line":1,"column":3}},
"name": "T",
"variance": null
}
]
}
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start":12,"end":17,"loc":{"start":{"line":1,"column":12},"end":{"line":1,"column":17}},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start":14,"end":17,"loc":{"start":{"line":1,"column":14},"end":{"line":1,"column":17}}
}
},
"extra": {
"parenthesized": true,
"parenStart": 0
}
}
},
{
"type": "ExpressionStatement",
"start":20,"end":41,"loc":{"start":{"line":2,"column":0},"end":{"line":2,"column":21}},
"expression": {
"type": "TypeCastExpression",
"start":21,"end":39,"loc":{"start":{"line":2,"column":1},"end":{"line":2,"column":19}},
"expression": {
"type": "ArrowFunctionExpression",
"start":22,"end":33,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":13}},
"id": null,
"generator": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"start":31,"end":33,"loc":{"start":{"line":2,"column":11},"end":{"line":2,"column":13}},
"body": [],
"directives": []
},
"typeParameters": {
"type": "TypeParameterDeclaration",
"start":22,"end":25,"loc":{"start":{"line":2,"column":2},"end":{"line":2,"column":5}},
"params": [
{
"type": "TypeParameter",
"start":23,"end":24,"loc":{"start":{"line":2,"column":3},"end":{"line":2,"column":4}},
"name": "T",
"variance": null
}
]
},
"extra": {
"parenthesized": true,
"parenStart": 21
}
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start":34,"end":39,"loc":{"start":{"line":2,"column":14},"end":{"line":2,"column":19}},
"typeAnnotation": {
"type": "AnyTypeAnnotation",
"start":36,"end":39,"loc":{"start":{"line":2,"column":16},"end":{"line":2,"column":19}}
}
},
"extra": {
"parenthesized": true,
"parenStart": 20
}
}
}
],
"directives": []
}
}