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

Parse arrows with params annotations in conditional expressions #10669

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
1 change: 1 addition & 0 deletions packages/babel-parser/src/plugins/flow.js
Expand Up @@ -2061,6 +2061,7 @@ export default (superClass: Class<Parser>): Class<Parser> =>
return node.operator === "=";

case "ParenthesizedExpression":
case "TypeCastExpression":
JLHwung marked this conversation as resolved.
Show resolved Hide resolved
return this.isAssignable(node.expression);

case "MemberExpression":
Expand Down
@@ -0,0 +1,2 @@
test
? (x: T): U => y
Copy link
Contributor

Choose a reason for hiding this comment

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

U => y must be parenthesized, otherwise flow could not parse it.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is a known bug with flow, because it also rejects this valid JS code:

test ? (x) : I => y

facebook/flow#2006

Copy link
Member Author

Choose a reason for hiding this comment

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

For context, here is one of my first PRs 😛
babel/babylon#573

@@ -0,0 +1,220 @@
{
"type": "File",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"program": {
"type": "Program",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"sourceType": "module",
"interpreter": null,
"body": [
{
"type": "ExpressionStatement",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"expression": {
"type": "ConditionalExpression",
"start": 0,
"end": 23,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 2,
"column": 18
}
},
"test": {
"type": "Identifier",
"start": 0,
"end": 4,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 4
},
"identifierName": "test"
},
"name": "test"
},
"consequent": {
"type": "TypeCastExpression",
"start": 10,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 9
}
},
"expression": {
"type": "Identifier",
"start": 10,
"end": 11,
"loc": {
"start": {
"line": 2,
"column": 5
},
"end": {
"line": 2,
"column": 6
},
"identifierName": "x"
},
"name": "x"
},
"typeAnnotation": {
"type": "TypeAnnotation",
"start": 11,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 6
},
"end": {
"line": 2,
"column": 9
}
},
"typeAnnotation": {
"type": "GenericTypeAnnotation",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
}
},
"typeParameters": null,
"id": {
"type": "Identifier",
"start": 13,
"end": 14,
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
},
"identifierName": "T"
},
"name": "T"
}
}
},
"extra": {
"parenthesized": true,
"parenStart": 9
}
},
"alternate": {
"type": "ArrowFunctionExpression",
"start": 17,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 18
}
},
"id": null,
"generator": false,
"async": false,
"params": [
{
"type": "Identifier",
"start": 17,
"end": 18,
"loc": {
"start": {
"line": 2,
"column": 12
},
"end": {
"line": 2,
"column": 13
},
"identifierName": "U"
},
"name": "U"
}
],
"body": {
"type": "Identifier",
"start": 22,
"end": 23,
"loc": {
"start": {
"line": 2,
"column": 17
},
"end": {
"line": 2,
"column": 18
},
"identifierName": "y"
},
"name": "y"
}
}
}
}
],
"directives": []
}
}
@@ -0,0 +1,3 @@
test
? (x: T): U => y
: z