Skip to content

Commit

Permalink
Continue to close the gap with flow (#520)
Browse files Browse the repository at this point in the history
* Adds test support for flow difference
* New support for FunctionTypeAnnotation
* New support for ObjectTypeAnnotation
* New support for MixedTypeAnnotation
  • Loading branch information
tmcw authored Sep 1, 2016
1 parent d2b93ae commit 0a97e0e
Show file tree
Hide file tree
Showing 6 changed files with 759 additions and 28 deletions.
37 changes: 36 additions & 1 deletion lib/flow_doctrine.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var namedTypes = {
'NumberTypeAnnotation': 'number',
'BooleanTypeAnnotation': 'boolean',
'ObjectTypeAnnotation': 'Object',
'StringTypeAnnotation': 'string'
};

var oneToOne = {
'AnyTypeAnnotation': 'AllLiteral',
'MixedTypeAnnotation': 'AllLiteral',
'NullLiteralTypeAnnotation': 'NullLiteral',
'VoidTypeAnnotation': 'VoidLiteral'
};
Expand All @@ -17,6 +17,14 @@ var literalTypes = {
'StringLiteralTypeAnnotation': 'StringLiteral'
};

function propertyToField(property) {
return {
type: 'FieldType',
key: property.key.name,
value: flowDoctrine(property.value)
};
}

/**
* Babel parses Flow annotations in JavaScript into AST nodes. documentation.js uses
* Babel to parse JavaScript. This method restructures those Babel-generated
Expand Down Expand Up @@ -71,6 +79,20 @@ function flowDoctrine(type) {
applications: [flowDoctrine(type.elementType)]
};

// (y: number) => bool
case 'FunctionTypeAnnotation':
return {
type: 'FunctionType',
params: type.params.map(function (param) {
return {
type: 'ParameterType',
name: param.name.name,
expression: flowDoctrine(param.typeAnnotation)
};
}),
result: flowDoctrine(type.returnType)
};

case 'GenericTypeAnnotation':
if (type.typeParameters) {
return {
Expand All @@ -83,6 +105,19 @@ function flowDoctrine(type) {
};
}

return {
type: 'NameExpression',
name: type.id.name
};

case 'ObjectTypeAnnotation':
if (type.properties) {
return {
type: 'RecordType',
fields: type.properties.map(propertyToField)
};
}

return {
type: 'NameExpression',
name: type.id.name
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/sync/flow-types.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,11 @@ function veryImportantTransform(
* Function with optional parameter.
*/
function optionalFunc(x: number = 42) {}

/**
* Function with object parameter.
*/
function objectParamFn(x: { a: number }) {}

/** hi */
function objectParamFn(x: (y:Foo) => Bar) {}
Loading

0 comments on commit 0a97e0e

Please sign in to comment.