Skip to content

Commit

Permalink
[flow][parser] Fix parsing of parenthesized component types
Browse files Browse the repository at this point in the history
Summary:
Parse parenthesized component types as component types

Changelog: [parser] Component type in parentheses can now be correctly parsed. e.g. `type Foo = (component(x: number) renders Bar);`

Reviewed By: SamChou19815

Differential Revision: D64196210

fbshipit-source-id: 1abbfca38a2baa45656cd6d80abbbc28508cf941
  • Loading branch information
mvitousek authored and facebook-github-bot committed Oct 10, 2024
1 parent 2830c6a commit 13a349f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/parser/test/flow/components/component_type_paren.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type Foo = (component(x: number) renders Bar);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"components": true
}
70 changes: 70 additions & 0 deletions src/parser/test/flow/components/component_type_paren.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":46}},
"range":[0,46],
"body":[
{
"type":"TypeAlias",
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":46}},
"range":[0,46],
"id":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":1,"column":5},"end":{"line":1,"column":8}},
"range":[5,8],
"name":"Foo",
"typeAnnotation":null,
"optional":false
},
"typeParameters":null,
"right":{
"type":"ComponentTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":12},"end":{"line":1,"column":44}},
"range":[12,44],
"params":[
{
"type":"ComponentTypeParameter",
"loc":{"source":null,"start":{"line":1,"column":22},"end":{"line":1,"column":31}},
"range":[22,31],
"name":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":1,"column":22},"end":{"line":1,"column":23}},
"range":[22,23],
"name":"x",
"typeAnnotation":null,
"optional":false
},
"typeAnnotation":{
"type":"NumberTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":25},"end":{"line":1,"column":31}},
"range":[25,31]
},
"optional":false
}
],
"rest":null,
"rendersType":{
"type":"TypeOperator",
"loc":{"source":null,"start":{"line":1,"column":33},"end":{"line":1,"column":44}},
"range":[33,44],
"operator":"renders",
"typeAnnotation":{
"type":"GenericTypeAnnotation",
"loc":{"source":null,"start":{"line":1,"column":41},"end":{"line":1,"column":44}},
"range":[41,44],
"id":{
"type":"Identifier",
"loc":{"source":null,"start":{"line":1,"column":41},"end":{"line":1,"column":44}},
"range":[41,44],
"name":"Bar",
"typeAnnotation":null,
"optional":false
},
"typeParameters":null
}
},
"typeParameters":null
}
}
],
"comments":[]
}
6 changes: 6 additions & 0 deletions src/parser/type_parser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,12 @@ module Type (Parse : Parser_common.PARSER) : Parser_common.TYPE = struct
(* Ok this is definitely a parameter *)
ParamList (function_param_list_without_parens env [])
| _ -> Type (_type env))
| T_IDENTIFIER { raw = "component"; _ } when (parse_options env).components ->
(match Peek.ith_token ~i:1 env with
| T_LESS_THAN
| T_LPAREN ->
Type (_type env)
| _ -> function_param_or_generic_type env)
| T_IDENTIFIER _
| T_STATIC (* `static` is reserved in strict mode, but still an identifier *) ->
(* This could be a function parameter or a generic type *)
Expand Down
4 changes: 4 additions & 0 deletions tests/component_type/parse_component_type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare component Foo(bar: string) renders 'svg';
type FooTy = (component(bar: string) renders 'svg');

Foo as FooTy; // ok

0 comments on commit 13a349f

Please sign in to comment.