Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit59d5708

Browse files
committed
Flow can parse interfaces now!
1 parentdcc30ae commit59d5708

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

‎2/parseFlow.re‎

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,24 @@ let errorLocation = (loc: Loc.t) =>
1313
letdotTypedIdentifier= (id:FlowAst.Identifier.t)=>
1414
DotTyped.Identifier(snd(id));
1515

16+
letdotTypedIdentifierOfPropertyKey= 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.*/
1735
letrecflowTypeToTyped= (flowType:FlowAst.Type.t)=> {
1836
let (loc,type_)= flowType;
@@ -51,7 +69,7 @@ and functionTypeToTyped = (f: FlowAst.Type.Function.t) => {
5169
Belt.Option.map(param.name, dotTypedIdentifier)
5270
|.Belt.Option.getWithDefault(DotTyped.Identifier("")),
5371
type_: flowTypeToTyped(param.typeAnnotation),
54-
optional:false,
72+
optional:param.optional,
5573
};
5674
letparameters=
5775
formal|.Belt.List.toArray|.Belt.Array.map(paramToProperty);
@@ -64,6 +82,14 @@ and functionTypeToTyped = (f: FlowAst.Type.Function.t) => {
6482
DotTyped.Function({parameters, rest, returnType, typeParameters:[||]});
6583
};
6684

85+
letobjectPropertyValueToTyped= (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+
6793
lettypeAnnotationToTyped= (annotation:FlowAst.Type.annotation)=> {
6894
let (_,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+
letproperties=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
|_=>
85150
raise(
86151
Errors2.NotSupported({
@@ -94,6 +159,7 @@ let parse = (~name: string, ~source: string) => {
94159
let (flowAst,_errors)=
95160
Parser_flow.program_file(source,Some(Loc.SourceFile(name)));
96161
let (_,statements,_)= flowAst;
162+
97163
lettypedModule=
98164
DotTyped.ModuleDeclaration({
99165
name:Identifier(name),

‎2/parseTypescript.re‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ let rec astToDotTyped = ast =>
5454
declarations:Array.map(sourceFile.statements, astToDotTyped),
5555
});
5656
}
57+
5758
|Typescript.FunctionDeclaration(func)=>
5859
DotTyped.FunctionDeclaration({
5960
name: getName(func.name),
@@ -72,6 +73,7 @@ let rec astToDotTyped = ast =>
7273
returnType: typescriptAstToDotTyped(func.type_),
7374
}),
7475
})
76+
7577
|Typescript.InterfaceDeclaration(interface)=>
7678
DotTyped.InterfaceDeclaration({
7779
name: getName(interface.name),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp