documentation/lib/flow_doctrine.js
Tom MacWright 70cc7d0718 Infer Flow type aliases into typedefs. Fixes #227
Given the `type` tag introduced with Flow, this can infer
a typedef statement, as well as infer its potentially
nested properties and their types.

This also includes

* Refactor of Markdown AST generation that fixes #228
* Refactor of nest.js to handle multi-level nesting
2015-11-04 11:25:32 -05:00

62 lines
1.1 KiB
JavaScript

var namedTypes = {
'NumberTypeAnnotation': 'number',
'BooleanTypeAnnotation': 'boolean',
'ObjectTypeAnnotation': 'Object',
'StringTypeAnnotation': 'string'
};
var oneToOne = {
'AnyTypeAnnotation': {
type: 'AllLiteral'
}
};
function flowDoctrine(type) {
if (type.type in namedTypes) {
return {
type: 'NameExpression',
name: namedTypes[type.type]
};
}
if (type.type in oneToOne) {
return oneToOne[type.type];
}
if (type.type === 'NullableTypeAnnotation') {
return {
type: 'OptionalType',
expression: flowDoctrine(type.typeAnnotation)
};
}
if (type.type === 'UnionTypeAnnotation') {
return {
type: 'UnionType',
elements: type.types.map(flowDoctrine)
};
}
if (type.type === 'GenericTypeAnnotation') {
if (type.typeParameters) {
return {
type: 'TypeApplication',
expression: {
type: 'NameExpression',
name: type.id.name
},
applications: type.typeParameters.params.map(flowDoctrine)
};
}
return {
type: 'NameExpression',
name: type.id.name
};
}
}
module.exports = flowDoctrine;