@@ -13,6 +13,24 @@ let errorLocation = (loc: Loc.t) =>
1313let dotTypedIdentifier = (id: FlowAst . Identifier . t )=>
1414DotTyped . Identifier (snd(id));
1515
16+ let dotTypedIdentifierOfPropertyKey = key=>
17+ switch (key) {
18+ | FlowAst . Expression . Object . Property . Identifier (id )=>
19+ dotTypedIdentifier(id)
20+ | FlowAst . Expression . Object . Property . Literal ((_loc , {value}))=>
21+ switch (value) {
22+ | String (s )=> DotTyped . Identifier (s)
23+ | _ => DotTyped . UnknownIdentifier
24+ }
25+ | FlowAst . Expression . Object . Property . Computed ((loc , _ ))=>
26+ raise (
27+ Errors2 . NotSupported ({
28+ message: "Computed object properties" ,
29+ loc: errorLocation(loc),
30+ }),
31+ )
32+ };
33+
1634/** Transforms a typed Flow AST to a DotTyped AST, extracting the types.*/
1735let rec flowTypeToTyped = (flowType: FlowAst . Type . t )=> {
1836let (loc , type_ )= flowType;
@@ -51,7 +69,7 @@ and functionTypeToTyped = (f: FlowAst.Type.Function.t) => {
5169Belt . Option . map(param. name, dotTypedIdentifier)
5270|. Belt . Option . getWithDefault(DotTyped . Identifier ("" )),
5371 type_: flowTypeToTyped(param. typeAnnotation),
54- optional: false ,
72+ optional: param . optional ,
5573 };
5674let parameters =
5775 formal|. Belt . List . toArray|. Belt . Array . map(paramToProperty);
@@ -64,6 +82,14 @@ and functionTypeToTyped = (f: FlowAst.Type.Function.t) => {
6482DotTyped . Function ({parameters, rest, returnType, typeParameters: [||] });
6583};
6684
85+ let objectPropertyValueToTyped = (value: FlowAst . Type . Object . Property . value )=>
86+ switch (value) {
87+ | FlowAst . Type . Object . Property . Init (t )=> flowTypeToTyped(t)
88+ | FlowAst . Type . Object . Property . Get ((_loc , func ))
89+ | FlowAst . Type . Object . Property . Set ((_loc , func ))=>
90+ functionTypeToTyped(func)
91+ };
92+
6793let typeAnnotationToTyped = (annotation: FlowAst . Type . annotation )=> {
6894let (_ , t )= annotation;
6995 flowTypeToTyped(t);
@@ -81,6 +107,45 @@ let flowAstToTypedAst = ((loc: Loc.t, s)) =>
81107 typeAnnotationToTyped,
82108 ),
83109 })
110+
111+ | FlowAst . Statement . DeclareFunction ({id, typeAnnotation})=>
112+ DotTyped . FunctionDeclaration ({
113+ name: dotTypedIdentifier(id),
114+ type_: typeAnnotationToTyped(typeAnnotation),
115+ })
116+
117+ | FlowAst . Statement . InterfaceDeclaration ({id, body})=>
118+ let (_bodyLoc , bodyType )= body;
119+ let properties = List . toArray(bodyType. properties);
120+ DotTyped . InterfaceDeclaration ({
121+ name: dotTypedIdentifier(id),
122+ type_:
123+ DotTyped . Object ({
124+ properties:
125+ Array . map(properties, prop=>
126+ switch (prop) {
127+ | FlowAst . Type . Object . Property ((_loc , {key, value, optional}))=>
128+ DotTyped . {
129+ name: dotTypedIdentifierOfPropertyKey(key),
130+ type_: objectPropertyValueToTyped(value),
131+ optional,
132+ }
133+ | FlowAst . Type . Object . CallProperty ((loc , _ ))
134+ | FlowAst . Type . Object . Indexer ((loc , _ ))
135+ | FlowAst . Type . Object . SpreadProperty ((loc , _ ))=>
136+ raise (
137+ Errors2 . NotSupported ({
138+ message: "Special object properties" ,
139+ loc: errorLocation(loc),
140+ }),
141+ )
142+ }
143+ ),
144+ typeParameters: [||] ,
145+ extends: None ,
146+ }),
147+ });
148+
84149| _ =>
85150raise (
86151Errors2 . NotSupported ({
@@ -94,6 +159,7 @@ let parse = (~name: string, ~source: string) => {
94159let (flowAst , _errors )=
95160Parser_flow . program_file(source, Some (Loc . SourceFile (name)));
96161let (_ , statements , _ )= flowAst;
162+
97163let typedModule =
98164DotTyped . ModuleDeclaration ({
99165 name: Identifier (name),