Skip to content

Commit 384ccbc

Browse files
arvtmcw
authored andcommitted
Fix inferring return type of var function (#545)
I accidentally broke the detection of the return type in: ```js var f = function(x: number): boolean {} ```
1 parent df8efa4 commit 384ccbc

File tree

5 files changed

+182
-2
lines changed

5 files changed

+182
-2
lines changed

lib/infer/return.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var finders = require('./finders'),
3+
var findTarget = require('./finders').findTarget,
44
t = require('babel-types'),
55
shouldSkipInference = require('./should_skip_inference'),
66
flowDoctrine = require('../flow_doctrine');
@@ -17,8 +17,15 @@ module.exports = function () {
1717
if (comment.returns) {
1818
return comment;
1919
}
20-
var path = finders.findTarget(comment.context.ast);
20+
var path = findTarget(comment.context.ast);
2121
var fn = path && path.node;
22+
23+
// In case of `/** */ var x = function () {}` findTarget returns
24+
// the declarator.
25+
if (t.isVariableDeclarator(fn)) {
26+
fn = fn.init;
27+
}
28+
2229
if (t.isFunction(fn) &&
2330
fn.returnType &&
2431
fn.returnType.typeAnnotation) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/** */
2+
var f = function(x: number): boolean {}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
[
2+
{
3+
"description": "",
4+
"tags": [],
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 1,
12+
"column": 6
13+
}
14+
},
15+
"context": {
16+
"loc": {
17+
"start": {
18+
"line": 2,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 2,
23+
"column": 39
24+
}
25+
}
26+
},
27+
"errors": [],
28+
"name": "f",
29+
"kind": "function",
30+
"params": [
31+
{
32+
"title": "param",
33+
"name": "x",
34+
"lineNumber": 2,
35+
"type": {
36+
"type": "NameExpression",
37+
"name": "number"
38+
}
39+
}
40+
],
41+
"returns": [
42+
{
43+
"type": {
44+
"type": "NameExpression",
45+
"name": "boolean"
46+
}
47+
}
48+
],
49+
"members": {
50+
"instance": [],
51+
"static": []
52+
},
53+
"path": [
54+
{
55+
"name": "f",
56+
"kind": "function"
57+
}
58+
],
59+
"namespace": "f"
60+
}
61+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
2+
3+
# f
4+
5+
**Parameters**
6+
7+
- `x` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**
8+
9+
Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "html",
6+
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
7+
},
8+
{
9+
"depth": 1,
10+
"type": "heading",
11+
"children": [
12+
{
13+
"type": "text",
14+
"value": "f"
15+
}
16+
]
17+
},
18+
{
19+
"type": "strong",
20+
"children": [
21+
{
22+
"type": "text",
23+
"value": "Parameters"
24+
}
25+
]
26+
},
27+
{
28+
"ordered": false,
29+
"type": "list",
30+
"children": [
31+
{
32+
"type": "listItem",
33+
"children": [
34+
{
35+
"type": "paragraph",
36+
"children": [
37+
{
38+
"type": "inlineCode",
39+
"value": "x"
40+
},
41+
{
42+
"type": "text",
43+
"value": " "
44+
},
45+
{
46+
"type": "strong",
47+
"children": [
48+
{
49+
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
50+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
51+
"type": "link",
52+
"children": [
53+
{
54+
"type": "text",
55+
"value": "number"
56+
}
57+
]
58+
}
59+
]
60+
},
61+
{
62+
"type": "text",
63+
"value": " "
64+
}
65+
]
66+
}
67+
]
68+
}
69+
]
70+
},
71+
{
72+
"type": "paragraph",
73+
"children": [
74+
{
75+
"type": "text",
76+
"value": "Returns "
77+
},
78+
{
79+
"type": "strong",
80+
"children": [
81+
{
82+
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
83+
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
84+
"type": "link",
85+
"children": [
86+
{
87+
"type": "text",
88+
"value": "boolean"
89+
}
90+
]
91+
}
92+
]
93+
},
94+
{
95+
"type": "text",
96+
"value": " "
97+
}
98+
]
99+
}
100+
]
101+
}

0 commit comments

Comments
 (0)