Skip to content

Commit 29cd58b

Browse files
authored
Merge inferred return type like we do for params. Refs #359 (#604)
1 parent 4dc8cb0 commit 29cd58b

File tree

3 files changed

+13
-9
lines changed

3 files changed

+13
-9
lines changed

lib/infer/return.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ var findTarget = require('./finders').findTarget,
1414
*/
1515
module.exports = function () {
1616
return shouldSkipInference(function inferReturn(comment) {
17-
if (comment.returns) {
17+
if (Array.isArray(comment.returns) &&
18+
comment.returns.length &&
19+
comment.returns[0].type) {
1820
return comment;
1921
}
2022
var path = findTarget(comment.context.ast);
@@ -29,9 +31,14 @@ module.exports = function () {
2931
if (t.isFunction(fn) &&
3032
fn.returnType &&
3133
fn.returnType.typeAnnotation) {
32-
comment.returns = [{
33-
type: flowDoctrine(fn.returnType.typeAnnotation)
34-
}];
34+
var returnType = flowDoctrine(fn.returnType.typeAnnotation);
35+
if (comment.returns) {
36+
comment.returns[0].type = returnType;
37+
} else {
38+
comment.returns = [{
39+
type: returnType
40+
}];
41+
}
3542
}
3643
return comment;
3744
});

test/fixture/merge-infered-type.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Add five to `x`.
33
*
44
* @param x The number to add five to.
5-
* @returns {number} x plus five.
5+
* @returns x plus five.
66
*/
77
function addFive(x: number): number {
88
return x + 5;

test/fixture/merge-infered-type.output.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,7 @@
9898
"title": "returns",
9999
"description": "x plus five.",
100100
"lineNumber": 4,
101-
"type": {
102-
"type": "NameExpression",
103-
"name": "number"
104-
}
101+
"type": null
105102
}
106103
],
107104
"loc": {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy