Skip to content

Commit 11826ad

Browse files
committed
Add return type inference as well
1 parent 7730af7 commit 11826ad

File tree

7 files changed

+96
-6
lines changed

7 files changed

+96
-6
lines changed

index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ var sort = require('./lib/sort'),
1414
inferKind = require('./lib/infer/kind'),
1515
inferParams = require('./lib/infer/params'),
1616
inferMembership = require('./lib/infer/membership'),
17+
inferReturn = require('./lib/infer/return'),
1718
lint = require('./lib/lint');
1819

1920
/**
@@ -62,10 +63,11 @@ module.exports = function (indexes, options, callback) {
6263
// compose nesting & membership to avoid intermediate arrays
6364
comment = nestParams(
6465
inferMembership(
65-
inferParams(
66-
inferKind(
67-
inferName(
68-
lint(comment))))));
66+
inferReturn(
67+
inferParams(
68+
inferKind(
69+
inferName(
70+
lint(comment)))))));
6971
if (options.github) {
7072
comment = github(comment);
7173
}

lib/infer/return.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
var types = require('ast-types'),
4+
flowDoctrine = require('../flow_doctrine');
5+
6+
/**
7+
* Infers returns tags by using Flow return type annotations
8+
*
9+
* @name inferReturn
10+
* @param {Object} comment parsed comment
11+
* @returns {Object} comment with return tag inferred
12+
*/
13+
module.exports = function inferReturn(comment) {
14+
15+
types.visit(comment.context.ast, {
16+
visitFunction: function (path) {
17+
18+
if (!comment.returns &&
19+
path.value.returnType &&
20+
path.value.returnType.typeAnnotation) {
21+
comment.returns = [{
22+
type: flowDoctrine(path.value.returnType.typeAnnotation)
23+
}];
24+
}
25+
26+
this.abort();
27+
}
28+
});
29+
30+
return comment;
31+
};

test/fixture/flow-types.input.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This function returns the number one.
55
*/
6-
function addThem(a: number, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named) {
6+
function addThem(a: number, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {
77
return a + b + c + d + e;
88
};
99

test/fixture/flow-types.output.custom.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ This function returns the number one.
1919

2020

2121

22+
Returns **number**
23+
24+
25+

test/fixture/flow-types.output.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
"column": 1
2424
}
2525
},
26-
"code": "/* eslint-disable */\n\n/**\n * This function returns the number one.\n */\nfunction addThem(a: number, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named) {\n return a + b + c + d + e;\n};\n\n/* eslint-enable */\n"
26+
"code": "/* eslint-disable */\n\n/**\n * This function returns the number one.\n */\nfunction addThem(a: number, b: string, c: ?boolean, d: Array<number>, e: Object, f: Named): number {\n return a + b + c + d + e;\n};\n\n/* eslint-enable */\n"
2727
},
2828
"errors": [],
2929
"name": "addThem",
@@ -96,6 +96,14 @@
9696
}
9797
}
9898
],
99+
"returns": [
100+
{
101+
"type": {
102+
"type": "NameExpression",
103+
"name": "number"
104+
}
105+
}
106+
],
99107
"members": {
100108
"instance": [],
101109
"static": []

test/fixture/flow-types.output.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,7 @@ This function returns the number one.
1919

2020

2121

22+
Returns **number**
23+
24+
25+

test/fixture/flow-types.output.md.json

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,47 @@
349349
]
350350
}
351351
]
352+
},
353+
{
354+
"type": "root",
355+
"children": [
356+
{
357+
"type": "paragraph",
358+
"children": [
359+
{
360+
"type": "text",
361+
"value": "Returns "
362+
},
363+
{
364+
"type": "strong",
365+
"children": [
366+
{
367+
"type": "text",
368+
"value": "number"
369+
}
370+
]
371+
},
372+
{
373+
"type": "text",
374+
"value": " "
375+
},
376+
{
377+
"type": "root",
378+
"children": [],
379+
"position": {
380+
"start": {
381+
"line": 1,
382+
"column": 1
383+
},
384+
"end": {
385+
"line": 1,
386+
"column": 1
387+
}
388+
}
389+
}
390+
]
391+
}
392+
]
352393
}
353394
]
354395
}

0 commit comments

Comments
 (0)