Skip to content

Commit

Permalink
feat: Add flow inference for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored and tmcw committed Apr 18, 2019
1 parent 8e3cd47 commit 7947e97
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
22 changes: 22 additions & 0 deletions __tests__/lib/infer/return.js
Expand Up @@ -41,4 +41,26 @@ test('inferReturn', function() {
name: 'string',
type: 'NameExpression'
});
const generatorFn = evaluate(
'/** */function *a(): Generator<Foo, Bar, Baz> {}'
);
expect(generatorFn.generator).toBe(true);
expect(generatorFn.yields).toEqual([
{
title: 'yields',
type: {
name: 'Foo',
type: 'NameExpression'
}
}
]);
expect(generatorFn.returns).toEqual([
{
title: 'returns',
type: {
name: 'Bar',
type: 'NameExpression'
}
}
]);
});
19 changes: 18 additions & 1 deletion src/infer/return.js
Expand Up @@ -30,10 +30,27 @@ function inferReturn(comment) {
}

if (t.isFunction(fn) && fn.returnType && fn.returnType.typeAnnotation) {
const returnType = flowDoctrine(fn.returnType.typeAnnotation);
let returnType = flowDoctrine(fn.returnType.typeAnnotation);
if (comment.returns && comment.returns.length > 0) {
comment.returns[0].type = returnType;
} else {
if (
fn.generator &&
returnType.type === 'TypeApplication' &&
returnType.expression.name === 'Generator' &&
returnType.applications.length === 3
) {
comment.generator = true;
comment.yields = [
{
title: 'yields',
type: returnType.applications[0]
}
];

returnType = returnType.applications[1];
}

comment.returns = [
{
title: 'returns',
Expand Down

0 comments on commit 7947e97

Please sign in to comment.