-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[parser] Add parser support for _ in constructors and calls
Summary: This diff adds support for parsing `_`, which will be used to specify that a type parameter should be implicitly instantiated. We add a new ast node to the Expression module, `TypeParameterInstantiation`. This is reminiscent of `Type`'s `ParameterInstantiation`, except that instead of a list of `Type.t`s, it contains a list of `Implicit of Loc.t | Explicit of Type.t`. When parsing `New` and `Call` type arguments, we parse `_` as an `Implicit` and all other types `t` as `Explicit t`. Creating the new ast node comes with a number of benefits: 1. External consumers of our AST currently consume `ParameterInstantiation` as it was originally designed: for type arguments to polymorphic types. Creating a new AST node makes sure we don't need to update all of those external consumers. 2. This new AST node allows OCaml to enforce the invariant that `_` can only appear in `Call` and `New`. 3. Related to 2., this more faithfully models the structure of our AST. `Call` and `New` have type arguments, but the fact that we used the same representation as we did for `Type` was just because they happened to have the same internal representation. These types are types we come across when parsing an expression, not a type annotation. We also now say that `_` is a reserved keyword when it's found in type positions, so it cannot be used unless it means implicit instantiation. We need to update babel to consider "_" to be a reserved identifier in type positions and to parse `_` as an `ImplicitInstantiationAnnotation`. Reviewed By: samwgoldman Differential Revision: D10105957 fbshipit-source-id: 3003b0cadbb1f9163a07bae1957710e9fbdeccdd
- Loading branch information
1 parent
d2b32ea
commit fb23b93
Showing
28 changed files
with
887 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/parser/test/flow/types/annotations/underscore_is_allowed_trailing_commas.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test< | ||
_, | ||
_, | ||
number, | ||
_, | ||
_, | ||
>(); |
96 changes: 96 additions & 0 deletions
96
src/parser/test/flow/types/annotations/underscore_is_allowed_trailing_commas.tree.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{ | ||
"type":"Program", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":7,"column":4}}, | ||
"range":[0,40], | ||
"body":[ | ||
{ | ||
"type":"ExpressionStatement", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":7,"column":4}}, | ||
"range":[0,40], | ||
"expression":{ | ||
"type":"CallExpression", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":7,"column":3}}, | ||
"range":[0,39], | ||
"callee":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, | ||
"range":[0,4], | ||
"name":"test", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeArguments":{ | ||
"type":"TypeParameterInstantiation", | ||
"loc":{"source":null,"start":{"line":1,"column":4},"end":{"line":7,"column":1}}, | ||
"range":[4,37], | ||
"params":[ | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":3}}, | ||
"range":[8,9], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":2,"column":2},"end":{"line":2,"column":3}}, | ||
"range":[8,9], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":3,"column":2},"end":{"line":3,"column":3}}, | ||
"range":[13,14], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":3,"column":2},"end":{"line":3,"column":3}}, | ||
"range":[13,14], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"NumberTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":4,"column":2},"end":{"line":4,"column":8}}, | ||
"range":[18,24] | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":5,"column":2},"end":{"line":5,"column":3}}, | ||
"range":[28,29], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":5,"column":2},"end":{"line":5,"column":3}}, | ||
"range":[28,29], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":6,"column":2},"end":{"line":6,"column":3}}, | ||
"range":[33,34], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":6,"column":2},"end":{"line":6,"column":3}}, | ||
"range":[33,34], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
} | ||
] | ||
}, | ||
"arguments":[] | ||
}, | ||
"directive":null | ||
} | ||
], | ||
"comments":[] | ||
} |
1 change: 1 addition & 0 deletions
1
src/parser/test/flow/types/annotations/underscore_is_implicit_anywhere_in_list.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
test<number, _, string, _, _, _, Foo, Bar, Baz>(); |
143 changes: 143 additions & 0 deletions
143
src/parser/test/flow/types/annotations/underscore_is_implicit_anywhere_in_list.tree.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
{ | ||
"type":"Program", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":50}}, | ||
"range":[0,50], | ||
"body":[ | ||
{ | ||
"type":"ExpressionStatement", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":50}}, | ||
"range":[0,50], | ||
"expression":{ | ||
"type":"CallExpression", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":49}}, | ||
"range":[0,49], | ||
"callee":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":0},"end":{"line":1,"column":4}}, | ||
"range":[0,4], | ||
"name":"test", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeArguments":{ | ||
"type":"TypeParameterInstantiation", | ||
"loc":{"source":null,"start":{"line":1,"column":4},"end":{"line":1,"column":47}}, | ||
"range":[4,47], | ||
"params":[ | ||
{ | ||
"type":"NumberTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":5},"end":{"line":1,"column":11}}, | ||
"range":[5,11] | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":14}}, | ||
"range":[13,14], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":13},"end":{"line":1,"column":14}}, | ||
"range":[13,14], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"StringTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":16},"end":{"line":1,"column":22}}, | ||
"range":[16,22] | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":25}}, | ||
"range":[24,25], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":24},"end":{"line":1,"column":25}}, | ||
"range":[24,25], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":27},"end":{"line":1,"column":28}}, | ||
"range":[27,28], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":27},"end":{"line":1,"column":28}}, | ||
"range":[27,28], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":30},"end":{"line":1,"column":31}}, | ||
"range":[30,31], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":30},"end":{"line":1,"column":31}}, | ||
"range":[30,31], | ||
"name":"_", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":33},"end":{"line":1,"column":36}}, | ||
"range":[33,36], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":33},"end":{"line":1,"column":36}}, | ||
"range":[33,36], | ||
"name":"Foo", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":38},"end":{"line":1,"column":41}}, | ||
"range":[38,41], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":38},"end":{"line":1,"column":41}}, | ||
"range":[38,41], | ||
"name":"Bar", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
}, | ||
{ | ||
"type":"GenericTypeAnnotation", | ||
"loc":{"source":null,"start":{"line":1,"column":43},"end":{"line":1,"column":46}}, | ||
"range":[43,46], | ||
"id":{ | ||
"type":"Identifier", | ||
"loc":{"source":null,"start":{"line":1,"column":43},"end":{"line":1,"column":46}}, | ||
"range":[43,46], | ||
"name":"Baz", | ||
"typeAnnotation":null, | ||
"optional":false | ||
}, | ||
"typeParameters":null | ||
} | ||
] | ||
}, | ||
"arguments":[] | ||
}, | ||
"directive":null | ||
} | ||
], | ||
"comments":[] | ||
} |
Oops, something went wrong.