Skip to content

Commit 5bd42d6

Browse files
BurntSushiarv
authored andcommitted
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
1 parent cd65dc3 commit 5bd42d6

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

lib/flow_doctrine.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ function flowDoctrine(type/*: Object */)/*: DoctrineType*/ {
9898
return {
9999
type: 'FunctionType',
100100
params: type.params.map(param => {
101+
let name = '';
102+
if (param.name && param.name.name) {
103+
name = param.name.name;
104+
}
101105
return {
102106
type: 'ParameterType',
103-
name: param.name.name,
107+
name: name,
104108
expression: flowDoctrine(param.typeAnnotation)
105109
};
106110
}),

lib/output/util/format_type.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ function formatType(getHref/*: Function*/, node/*: ?Object */) {
118118
case Syntax.NameExpression:
119119
return [link(node.name, getHref)];
120120
case Syntax.ParameterType:
121-
return [t(node.name + ': ')].concat(formatType(getHref, node.expression));
121+
if (node.name) {
122+
result.push(t(node.name + ': '));
123+
}
124+
return result.concat(formatType(getHref, node.expression));
122125

123126
case Syntax.TypeApplication:
124127
return formatType(getHref, node.expression)

test/lib/flow_doctrine.js

+13
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,19 @@ test('flowDoctrine', function (t) {
181181
name: 'boolean'
182182
}, 'boolean');
183183

184+
t.deepEqual(toDoctrineType('any => any'),
185+
{
186+
type: 'FunctionType',
187+
params: [
188+
{
189+
expression: {type: 'AllLiteral'},
190+
name: '',
191+
type: 'ParameterType'
192+
}
193+
],
194+
result: {type: 'AllLiteral'},
195+
}, '');
196+
184197
t.deepEqual(toDoctrineType('undefined'),
185198
{
186199
type: 'NameExpression',

0 commit comments

Comments
 (0)