Skip to content

Commit 0a97e0e

Browse files
authored
Continue to close the gap with flow (#520)
* Adds test support for flow difference * New support for FunctionTypeAnnotation * New support for ObjectTypeAnnotation * New support for MixedTypeAnnotation
1 parent d2b93ae commit 0a97e0e

File tree

6 files changed

+759
-28
lines changed

6 files changed

+759
-28
lines changed

lib/flow_doctrine.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
var namedTypes = {
22
'NumberTypeAnnotation': 'number',
33
'BooleanTypeAnnotation': 'boolean',
4-
'ObjectTypeAnnotation': 'Object',
54
'StringTypeAnnotation': 'string'
65
};
76

87
var oneToOne = {
98
'AnyTypeAnnotation': 'AllLiteral',
9+
'MixedTypeAnnotation': 'AllLiteral',
1010
'NullLiteralTypeAnnotation': 'NullLiteral',
1111
'VoidTypeAnnotation': 'VoidLiteral'
1212
};
@@ -17,6 +17,14 @@ var literalTypes = {
1717
'StringLiteralTypeAnnotation': 'StringLiteral'
1818
};
1919

20+
function propertyToField(property) {
21+
return {
22+
type: 'FieldType',
23+
key: property.key.name,
24+
value: flowDoctrine(property.value)
25+
};
26+
}
27+
2028
/**
2129
* Babel parses Flow annotations in JavaScript into AST nodes. documentation.js uses
2230
* Babel to parse JavaScript. This method restructures those Babel-generated
@@ -71,6 +79,20 @@ function flowDoctrine(type) {
7179
applications: [flowDoctrine(type.elementType)]
7280
};
7381

82+
// (y: number) => bool
83+
case 'FunctionTypeAnnotation':
84+
return {
85+
type: 'FunctionType',
86+
params: type.params.map(function (param) {
87+
return {
88+
type: 'ParameterType',
89+
name: param.name.name,
90+
expression: flowDoctrine(param.typeAnnotation)
91+
};
92+
}),
93+
result: flowDoctrine(type.returnType)
94+
};
95+
7496
case 'GenericTypeAnnotation':
7597
if (type.typeParameters) {
7698
return {
@@ -83,6 +105,19 @@ function flowDoctrine(type) {
83105
};
84106
}
85107

108+
return {
109+
type: 'NameExpression',
110+
name: type.id.name
111+
};
112+
113+
case 'ObjectTypeAnnotation':
114+
if (type.properties) {
115+
return {
116+
type: 'RecordType',
117+
fields: type.properties.map(propertyToField)
118+
};
119+
}
120+
86121
return {
87122
type: 'NameExpression',
88123
name: type.id.name

test/fixture/sync/flow-types.input.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,11 @@ function veryImportantTransform(
5252
* Function with optional parameter.
5353
*/
5454
function optionalFunc(x: number = 42) {}
55+
56+
/**
57+
* Function with object parameter.
58+
*/
59+
function objectParamFn(x: { a: number }) {}
60+
61+
/** hi */
62+
function objectParamFn(x: (y:Foo) => Bar) {}

0 commit comments

Comments
 (0)