Skip to content

Commit

Permalink
Allow function types inside tuples inside arrow return types
Browse files Browse the repository at this point in the history
Summary:
To avoid ambiguity, Flow disallows using a function type in the return type, like

  const f = (): boolean => string => { return (b: boolean): string => "hello"; }

unless you wrap it in parens

  const f = (): (boolean => string) => { return (b: boolean): string => "hello"; }

We forgot to allow it inside tuples too, like

  const f = (): [boolean => string] => { return [(b: boolean): string => "hello"]; }

Reviewed By: dsainati1

Differential Revision: D15150181

fbshipit-source-id: dbc9292ac0935ea79373437501b22373e538979a
  • Loading branch information
gabelevi authored and facebook-github-bot committed Apr 30, 2019
1 parent 52bbf66 commit 65e6d1d
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/parser/test/flow/arrow_function/tuple_return_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(): [number => void] => {}
70 changes: 70 additions & 0 deletions src/parser/test/flow/arrow_function/tuple_return_type.tree.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"type":"Program",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"range":[0,26],
"body":[
{
"type":"ExpressionStatement",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"range":[0,26],
"expression":{
"type":"ArrowFunctionExpression",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":26}},
"range":[0,26],
"id":null,
"params":[],
"body":{
"type":"BlockStatement",
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":26}},
"range":[24,26],
"body":[]
},
"async":false,
"generator":false,
"predicate":null,
"expression":false,
"returnType":{
"type":"TypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":2},"end":{"line":1,"column":20}},
"range":[2,20],
"typeAnnotation":{
"type":"TupleTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":4},"end":{"line":1,"column":20}},
"range":[4,20],
"types":[
{
"type":"FunctionTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":5},"end":{"line":1,"column":19}},
"range":[5,19],
"params":[
{
"type":"FunctionTypeParam",
"loc":{"source":null,"start":{"line":1,"column":5},"end":{"line":1,"column":11}},
"range":[5,11],
"name":null,
"typeAnnotation":{
"type":"NumberTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":5},"end":{"line":1,"column":11}},
"range":[5,11]
},
"optional":false
}
],
"returnType":{
"type":"VoidTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":15},"end":{"line":1,"column":19}},
"range":[15,19]
},
"rest":null,
"typeParameters":null
}
]
}
},
"typeParameters":null
},
"directive":null
}
],
"comments":[]
}
2 changes: 1 addition & 1 deletion src/parser/type_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ module Type (Parse: Parser_common.PARSER) : TYPE = struct
in fun env ->
with_loc (fun env ->
Expect.token env T_LBRACKET;
let tl = types env [] in
let tl = types (with_no_anon_function_type false env) [] in
Expect.token env T_RBRACKET;
Type.Tuple tl
) env
Expand Down

1 comment on commit 65e6d1d

@TrySound
Copy link
Contributor

Choose a reason for hiding this comment

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

🎉

Please sign in to comment.