From 36610b0d663901525ae8d47d0dd22d4008601707 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Tue, 13 Sep 2016 14:24:14 -0700 Subject: [PATCH] Fix inferring return type of var function I accidentally broke the detection of the return type in: ```js var f = function(x: number): boolean {} ``` --- lib/infer/return.js | 11 +- .../var-function-param-return.input.js | 2 + .../var-function-param-return.output.json | 61 +++++++++++ .../var-function-param-return.output.md | 9 ++ .../var-function-param-return.output.md.json | 101 ++++++++++++++++++ 5 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 test/fixture/var-function-param-return.input.js create mode 100644 test/fixture/var-function-param-return.output.json create mode 100644 test/fixture/var-function-param-return.output.md create mode 100644 test/fixture/var-function-param-return.output.md.json diff --git a/lib/infer/return.js b/lib/infer/return.js index d6bdcb01b..5ee4b9350 100644 --- a/lib/infer/return.js +++ b/lib/infer/return.js @@ -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'); @@ -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)) { + fn = fn.init; + } + if (t.isFunction(fn) && fn.returnType && fn.returnType.typeAnnotation) { diff --git a/test/fixture/var-function-param-return.input.js b/test/fixture/var-function-param-return.input.js new file mode 100644 index 000000000..bc3fcce7d --- /dev/null +++ b/test/fixture/var-function-param-return.input.js @@ -0,0 +1,2 @@ +/** */ +var f = function(x: number): boolean {} diff --git a/test/fixture/var-function-param-return.output.json b/test/fixture/var-function-param-return.output.json new file mode 100644 index 000000000..3594637a0 --- /dev/null +++ b/test/fixture/var-function-param-return.output.json @@ -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" + } +] \ No newline at end of file diff --git a/test/fixture/var-function-param-return.output.md b/test/fixture/var-function-param-return.output.md new file mode 100644 index 000000000..dc2fd69e0 --- /dev/null +++ b/test/fixture/var-function-param-return.output.md @@ -0,0 +1,9 @@ + + +# 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)** diff --git a/test/fixture/var-function-param-return.output.md.json b/test/fixture/var-function-param-return.output.md.json new file mode 100644 index 000000000..ac9fb9874 --- /dev/null +++ b/test/fixture/var-function-param-return.output.md.json @@ -0,0 +1,101 @@ +{ + "type": "root", + "children": [ + { + "type": "html", + "value": "" + }, + { + "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": " " + } + ] + } + ] +} \ No newline at end of file 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