Skip to content

Commit

Permalink
Allow leading pipes for type aliases of unions
Browse files Browse the repository at this point in the history
Summary:
Ocaml allows this on pattern matches and it's convenient for larger unions that span one-per-line.

This adds support for this to the flow parser:

```javascript
type Nodes =
  | {type: "Identifier", ... }
  | {type: "Expression", ... }
  | {type: "Literal", ... }
  ...
;
```

Note that this adds leading-`|` support for type aliases only for now. We could easily expand to other annotations, but I figured it's most useful for type aliases of large unions -- so might as well start there and leave other things constrained for now. Should be easy to open this up to all annotations in the future if we have a good reason to.
Closes #1353

Reviewed By: samwgoldman

Differential Revision: D2897790

Pulled By: jeffmo

fb-gh-sync-id: edef99adca2876747ea330fa9478b2ed5be36506
jeffmo authored and facebook-github-bot-9 committed Feb 4, 2016
1 parent 9510eab commit 7fb56ee
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/parser/parser_flow.ml
Original file line number Diff line number Diff line change
@@ -2943,6 +2943,9 @@ end = struct
let id = Parse.identifier env in
let typeParameters = Type.type_parameter_declaration env in
Expect.token env T_ASSIGN;
(match Peek.token env with
| T_BIT_OR | T_BIT_AND -> Eat.token env
| _ -> ());
let right = Type._type env in
let end_loc = match Peek.semicolon_loc env with
| None -> fst right
31 changes: 31 additions & 0 deletions src/parser/test/hardcoded_tests.js
Original file line number Diff line number Diff line change
@@ -941,6 +941,37 @@ module.exports = {
},
],
},
'type union = | A | B | C': {
'body': [
{
'type': 'TypeAlias',
'id.name': 'union',
'right': {
'type': 'UnionTypeAnnotation',
'types': [
{'type': 'GenericTypeAnnotation', 'id.name': 'A'},
{'type': 'GenericTypeAnnotation', 'id.name': 'B'},
{'type': 'GenericTypeAnnotation', 'id.name': 'C'},
]
},
},
],
},
'type overloads = & ((x: string) => number) & ((x: number) => string);': {
'body': [
{
'type': 'TypeAlias',
'id.name': 'overloads',
'right': {
'type': 'IntersectionTypeAnnotation',
'types': [
{'type': 'FunctionTypeAnnotation'},
{'type': 'FunctionTypeAnnotation'}
]
},
},
],
}
},
'Interfaces': {
'interface A {}': {

0 comments on commit 7fb56ee

Please sign in to comment.