Fix bug parsing anonymous closure types from Flow. (#672)

If no parameter name exists in a function type, then set it to empty. Also,
fix the formatter so that `{param}: {ty}` is formatted as `{ty}` when
`{param}` is empty.

Fixes #671
This commit is contained in:
Andrew Gallant 2017-02-19 16:26:24 -05:00 committed by Erik Arvidsson
parent cd65dc39cf
commit 5bd42d6815
3 changed files with 22 additions and 2 deletions

View File

@ -98,9 +98,13 @@ function flowDoctrine(type/*: Object */)/*: DoctrineType*/ {
return {
type: 'FunctionType',
params: type.params.map(param => {
let name = '';
if (param.name && param.name.name) {
name = param.name.name;
}
return {
type: 'ParameterType',
name: param.name.name,
name: name,
expression: flowDoctrine(param.typeAnnotation)
};
}),

View File

@ -118,7 +118,10 @@ function formatType(getHref/*: Function*/, node/*: ?Object */) {
case Syntax.NameExpression:
return [link(node.name, getHref)];
case Syntax.ParameterType:
return [t(node.name + ': ')].concat(formatType(getHref, node.expression));
if (node.name) {
result.push(t(node.name + ': '));
}
return result.concat(formatType(getHref, node.expression));
case Syntax.TypeApplication:
return formatType(getHref, node.expression)

View File

@ -181,6 +181,19 @@ test('flowDoctrine', function (t) {
name: 'boolean'
}, 'boolean');
t.deepEqual(toDoctrineType('any => any'),
{
type: 'FunctionType',
params: [
{
expression: {type: 'AllLiteral'},
name: '',
type: 'ParameterType'
}
],
result: {type: 'AllLiteral'},
}, '');
t.deepEqual(toDoctrineType('undefined'),
{
type: 'NameExpression',