Skip to content

Merge inferred return type like we do for params. Refs #359 #604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions lib/infer/return.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ var findTarget = require('./finders').findTarget,
*/
module.exports = function () {
return shouldSkipInference(function inferReturn(comment) {
if (comment.returns) {
if (Array.isArray(comment.returns) &&
comment.returns.length &&
comment.returns[0].type) {
return comment;
}
var path = findTarget(comment.context.ast);
Expand All @@ -29,9 +31,14 @@ module.exports = function () {
if (t.isFunction(fn) &&
fn.returnType &&
fn.returnType.typeAnnotation) {
comment.returns = [{
type: flowDoctrine(fn.returnType.typeAnnotation)
}];
var returnType = flowDoctrine(fn.returnType.typeAnnotation);
if (comment.returns) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to check that returns is an array of at least one element here too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We won't get to this point if it isn't, since we return early on L20 if comment.returns.length is 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't see it. The early return happens if it is a non empty array with a type.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, good point. I'm not sure that that returns: [] ever happens in practice, but I'll add a guard here just to be sure.

comment.returns[0].type = returnType;
} else {
comment.returns = [{
type: returnType
}];
}
}
return comment;
});
Expand Down
2 changes: 1 addition & 1 deletion test/fixture/merge-infered-type.input.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Add five to `x`.
*
* @param x The number to add five to.
* @returns {number} x plus five.
* @returns x plus five.
*/
function addFive(x: number): number {
return x + 5;
Expand Down
5 changes: 1 addition & 4 deletions test/fixture/merge-infered-type.output.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@
"title": "returns",
"description": "x plus five.",
"lineNumber": 4,
"type": {
"type": "NameExpression",
"name": "number"
}
"type": null
}
],
"loc": {
Expand Down