Fix parsing type casted generic flow arrow exprs (#11955)
This commit is contained in:
parent
180e9c00e3
commit
96cc8292b7
@ -2674,28 +2674,57 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
|
|
||||||
let typeParameters;
|
let typeParameters;
|
||||||
|
|
||||||
const arrow = this.tryParse(() => {
|
const arrow = this.tryParse(abort => {
|
||||||
typeParameters = this.flowParseTypeParameterDeclaration();
|
typeParameters = this.flowParseTypeParameterDeclaration();
|
||||||
|
|
||||||
const arrowExpression = this.forwardNoArrowParamsConversionAt(
|
const arrowExpression = this.forwardNoArrowParamsConversionAt(
|
||||||
typeParameters,
|
typeParameters,
|
||||||
() =>
|
() => {
|
||||||
super.parseMaybeAssign(
|
const result = super.parseMaybeAssign(
|
||||||
refExpressionErrors,
|
refExpressionErrors,
|
||||||
afterLeftParse,
|
afterLeftParse,
|
||||||
refNeedsArrowPos,
|
refNeedsArrowPos,
|
||||||
),
|
|
||||||
);
|
);
|
||||||
arrowExpression.typeParameters = typeParameters;
|
|
||||||
this.resetStartLocationFromNode(arrowExpression, typeParameters);
|
this.resetStartLocationFromNode(result, typeParameters);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// <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);
|
||||||
|
expr.typeParameters = typeParameters;
|
||||||
|
this.resetStartLocationFromNode(expr, typeParameters);
|
||||||
|
|
||||||
return arrowExpression;
|
return arrowExpression;
|
||||||
}, state);
|
}, state);
|
||||||
|
|
||||||
const arrowExpression: ?N.ArrowFunctionExpression =
|
let arrowExpression: ?(
|
||||||
arrow.node?.type === "ArrowFunctionExpression" ? arrow.node : null;
|
| 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.
|
// If we are here, both JSX and Flow parsing attempts failed.
|
||||||
// Give the precedence to the JSX error, except if JSX had an
|
// Give the precedence to the JSX error, except if JSX had an
|
||||||
@ -3482,4 +3511,8 @@ export default (superClass: Class<Parser>): Class<Parser> =>
|
|||||||
}
|
}
|
||||||
return false;
|
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)"
|
||||||
|
}
|
||||||
1
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-invalid/input.js
vendored
Normal file
1
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-invalid/input.js
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
<T>(() => {}: any);
|
||||||
5
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-invalid/options.json
vendored
Normal file
5
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-invalid/options.json
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["flow"],
|
||||||
|
"throws": "Expected an arrow function after this type parameter declaration (1:0)"
|
||||||
|
}
|
||||||
2
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-parenthesized/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-parenthesized/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
(<T>() => {}: any);
|
||||||
|
((<T>() => {}): any);
|
||||||
@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"sourceType": "module",
|
||||||
|
"plugins": ["flow"],
|
||||||
|
"createParenthesizedExpressions": true
|
||||||
|
}
|
||||||
109
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-parenthesized/output.json
vendored
Normal file
109
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func-parenthesized/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
2
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func/input.js
vendored
Normal file
2
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func/input.js
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
(<T>() => {}: any);
|
||||||
|
((<T>() => {}): any);
|
||||||
109
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func/output.json
vendored
Normal file
109
packages/babel-parser/test/fixtures/flow/typecasts/generic-arrow-func/output.json
vendored
Normal file
@ -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": []
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user