Skip to content

Fix inferring return type of var function #545

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
Oct 10, 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
11 changes: 9 additions & 2 deletions lib/infer/return.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var finders = require('./finders'),
var findTarget = require('./finders').findTarget,
t = require('babel-types'),
shouldSkipInference = require('./should_skip_inference'),
flowDoctrine = require('../flow_doctrine');
Expand All @@ -17,8 +17,15 @@ module.exports = function () {
if (comment.returns) {
return comment;
}
var path = finders.findTarget(comment.context.ast);
var path = findTarget(comment.context.ast);
var fn = path && path.node;

// In case of `/** */ var x = function () {}` findTarget returns
// the declarator.
if (t.isVariableDeclarator(fn)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like my change to findTarget might have been bad. However, my other PR makes findTarget return a Path and then it would work to have it return the init again (because paths allows getting the parent).

Choose a reason for hiding this comment

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

I'm not sure, but do you want to update the top-level comment: Infers returns tags by using Flow return type annotations I'm not sure if this comment still applies int he same way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It still applies. The purpose of this function is to look at the ast node of the comment and infer the return tag based on the Flow return type annotation.

fn = fn.init;
}

if (t.isFunction(fn) &&
fn.returnType &&
fn.returnType.typeAnnotation) {
Expand Down
2 changes: 2 additions & 0 deletions test/fixture/var-function-param-return.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/** */
var f = function(x: number): boolean {}
61 changes: 61 additions & 0 deletions test/fixture/var-function-param-return.output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
[
{
"description": "",
"tags": [],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 6
}
},
"context": {
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 39
}
}
},
"errors": [],
"name": "f",
"kind": "function",
"params": [
{
"title": "param",
"name": "x",
"lineNumber": 2,
"type": {
"type": "NameExpression",
"name": "number"
}
}
],
"returns": [
{
"type": {
"type": "NameExpression",
"name": "boolean"
}
}
],
"members": {
"instance": [],
"static": []
},
"path": [
{
"name": "f",
"kind": "function"
}
],
"namespace": "f"
}
]
9 changes: 9 additions & 0 deletions test/fixture/var-function-param-return.output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

# f

**Parameters**

- `x` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)**

Returns **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
101 changes: 101 additions & 0 deletions test/fixture/var-function-param-return.output.md.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"type": "root",
"children": [
{
"type": "html",
"value": "<!-- Generated by documentation.js. Update this documentation by updating the source code. -->"
},
{
"depth": 1,
"type": "heading",
"children": [
{
"type": "text",
"value": "f"
}
]
},
{
"type": "strong",
"children": [
{
"type": "text",
"value": "Parameters"
}
]
},
{
"ordered": false,
"type": "list",
"children": [
{
"type": "listItem",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "inlineCode",
"value": "x"
},
{
"type": "text",
"value": " "
},
{
"type": "strong",
"children": [
{
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number",
"type": "link",
"children": [
{
"type": "text",
"value": "number"
}
]
}
]
},
{
"type": "text",
"value": " "
}
]
}
]
}
]
},
{
"type": "paragraph",
"children": [
{
"type": "text",
"value": "Returns "
},
{
"type": "strong",
"children": [
{
"href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
"url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean",
"type": "link",
"children": [
{
"type": "text",
"value": "boolean"
}
]
}
]
},
{
"type": "text",
"value": " "
}
]
}
]
}
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