Skip to content

Commit dcd5e06

Browse files
committed
Merge yields description with inferred type
1 parent db42ac2 commit dcd5e06

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

declarations/comment.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ type Comment = {
101101
access?: Access,
102102
readonly?: boolean,
103103
abstract?: boolean,
104+
generator?: boolean,
104105
alias?: string,
105106

106107
copyright?: string,

src/infer/return.js

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,47 @@ function inferReturn(comment) {
4545

4646
if ((t.isFunction(fn) || t.isTSDeclareFunction(fn) || t.isTSDeclareMethod(fn)) && fn.returnType) {
4747
let returnType = typeAnnotation(fn.returnType);
48-
if (comment.returns && comment.returns.length > 0) {
49-
comment.returns[0].type = returnType;
50-
} else {
51-
if (fn.generator && returnType.type === 'TypeApplication') {
52-
comment.generator = true;
53-
let numArgs;
48+
let yieldsType = null;
49+
50+
if (fn.generator && returnType.type === 'TypeApplication') {
51+
comment.generator = true;
52+
let numArgs;
53+
54+
if (t.isFlow(fn.returnType)) {
55+
numArgs = FLOW_GENERATORS[returnType.expression.name];
56+
} else if (t.isTSTypeAnnotation(fn.returnType)) {
57+
numArgs = TS_GENERATORS[returnType.expression.name];
58+
}
59+
60+
if (returnType.applications.length === numArgs) {
61+
yieldsType = returnType.applications[0];
5462

55-
if (t.isFlow(fn.returnType)) {
56-
numArgs = FLOW_GENERATORS[returnType.expression.name];
57-
} else if (t.isTSTypeAnnotation(fn.returnType)) {
58-
numArgs = TS_GENERATORS[returnType.expression.name];
63+
if (numArgs > 1) {
64+
returnType = returnType.applications[1];
65+
} else {
66+
returnType = {
67+
type: 'VoidLiteral'
68+
};
5969
}
70+
}
71+
}
6072

61-
if (returnType.applications.length === numArgs) {
62-
comment.yields = [
63-
{
64-
title: 'yields',
65-
type: returnType.applications[0]
66-
}
67-
];
68-
69-
if (numArgs > 1) {
70-
returnType = returnType.applications[1];
71-
} else {
72-
returnType = {
73-
type: 'VoidLiteral'
74-
};
73+
if (yieldsType) {
74+
if (comment.yields && comment.yields.length > 0) {
75+
comment.yields[0].type = yieldsType;
76+
} else {
77+
comment.yields = [
78+
{
79+
title: 'yields',
80+
type: yieldsType
7581
}
76-
}
82+
];
7783
}
84+
}
7885

86+
if (comment.returns && comment.returns.length > 0) {
87+
comment.returns[0].type = returnType;
88+
} else {
7989
comment.returns = [
8090
{
8191
title: 'returns',

0 commit comments

Comments
 (0)