From a7c8a574380bde30a5dba69551db5681140613c6 Mon Sep 17 00:00:00 2001 From: Erik Arvidsson Date: Sun, 19 Feb 2017 13:33:15 -0800 Subject: [PATCH] Fix(flow): Allow unnamed parameters This allows `type T = number => string`. Previously we were only handling `type T = (x: number) => string` correctly. Followup to #672 that: - Adds a test fixture - Fixes lint error --- lib/flow_doctrine.js | 4 +- lib/output/util/format_type.js | 2 +- test/fixture/flow-unnamed-params.input.js | 15 + test/fixture/flow-unnamed-params.output.json | 510 ++++++++++++++++++ test/fixture/flow-unnamed-params.output.md | 32 ++ .../flow-unnamed-params.output.md.json | 431 +++++++++++++++ 6 files changed, 991 insertions(+), 3 deletions(-) create mode 100644 test/fixture/flow-unnamed-params.input.js create mode 100644 test/fixture/flow-unnamed-params.output.json create mode 100644 test/fixture/flow-unnamed-params.output.md create mode 100644 test/fixture/flow-unnamed-params.output.md.json diff --git a/lib/flow_doctrine.js b/lib/flow_doctrine.js index 49a28d83e..3e7e079ae 100644 --- a/lib/flow_doctrine.js +++ b/lib/flow_doctrine.js @@ -100,11 +100,11 @@ function flowDoctrine(type/*: Object */)/*: DoctrineType*/ { params: type.params.map(param => { let name = ''; if (param.name && param.name.name) { - name = param.name.name; + name = param.name.name; } return { type: 'ParameterType', - name: name, + name, expression: flowDoctrine(param.typeAnnotation) }; }), diff --git a/lib/output/util/format_type.js b/lib/output/util/format_type.js index 242954623..c1fcee638 100644 --- a/lib/output/util/format_type.js +++ b/lib/output/util/format_type.js @@ -119,7 +119,7 @@ function formatType(getHref/*: Function*/, node/*: ?Object */) { return [link(node.name, getHref)]; case Syntax.ParameterType: if (node.name) { - result.push(t(node.name + ': ')); + result.push(t(node.name + ': ')); } return result.concat(formatType(getHref, node.expression)); diff --git a/test/fixture/flow-unnamed-params.input.js b/test/fixture/flow-unnamed-params.input.js new file mode 100644 index 000000000..08b022071 --- /dev/null +++ b/test/fixture/flow-unnamed-params.input.js @@ -0,0 +1,15 @@ +// @flow + +'use strict'; + +/** x */ +let x: T => string; + +/** x2 */ +let x2: (a: T) => string; + +/** T */ +type T = string[] => {num: number}; + +/** T2 */ +type T2 = (a: string[]) => {num: number}; diff --git a/test/fixture/flow-unnamed-params.output.json b/test/fixture/flow-unnamed-params.output.json new file mode 100644 index 000000000..e2f410f8b --- /dev/null +++ b/test/fixture/flow-unnamed-params.output.json @@ -0,0 +1,510 @@ +[ + { + "description": { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "x", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + } + } + }, + "tags": [], + "loc": { + "start": { + "line": 5, + "column": 0 + }, + "end": { + "line": 5, + "column": 8 + } + }, + "context": { + "loc": { + "start": { + "line": 6, + "column": 0 + }, + "end": { + "line": 6, + "column": 19 + } + } + }, + "augments": [], + "errors": [], + "examples": [], + "params": [], + "properties": [], + "returns": [], + "sees": [], + "throws": [], + "todos": [], + "name": "x", + "type": { + "type": "FunctionType", + "params": [ + { + "type": "ParameterType", + "name": "", + "expression": { + "type": "NameExpression", + "name": "T" + } + } + ], + "result": { + "type": "NameExpression", + "name": "string" + } + }, + "members": { + "global": [], + "inner": [], + "instance": [], + "events": [], + "static": [] + }, + "path": [ + { + "name": "x" + } + ], + "namespace": "x" + }, + { + "description": { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "x2", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + } + } + }, + "tags": [], + "loc": { + "start": { + "line": 8, + "column": 0 + }, + "end": { + "line": 8, + "column": 9 + } + }, + "context": { + "loc": { + "start": { + "line": 9, + "column": 0 + }, + "end": { + "line": 9, + "column": 25 + } + } + }, + "augments": [], + "errors": [], + "examples": [], + "params": [], + "properties": [], + "returns": [], + "sees": [], + "throws": [], + "todos": [], + "name": "x2", + "type": { + "type": "FunctionType", + "params": [ + { + "type": "ParameterType", + "name": "a", + "expression": { + "type": "NameExpression", + "name": "T" + } + } + ], + "result": { + "type": "NameExpression", + "name": "string" + } + }, + "members": { + "global": [], + "inner": [], + "instance": [], + "events": [], + "static": [] + }, + "path": [ + { + "name": "x2" + } + ], + "namespace": "x2" + }, + { + "description": { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "T", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + } + } + }, + "tags": [], + "loc": { + "start": { + "line": 11, + "column": 0 + }, + "end": { + "line": 11, + "column": 8 + } + }, + "context": { + "loc": { + "start": { + "line": 12, + "column": 0 + }, + "end": { + "line": 12, + "column": 35 + } + } + }, + "augments": [], + "errors": [], + "examples": [], + "params": [], + "properties": [], + "returns": [], + "sees": [], + "throws": [], + "todos": [], + "name": "T", + "kind": "typedef", + "type": { + "type": "FunctionType", + "params": [ + { + "type": "ParameterType", + "name": "", + "expression": { + "type": "TypeApplication", + "expression": { + "type": "NameExpression", + "name": "Array" + }, + "applications": [ + { + "type": "NameExpression", + "name": "string" + } + ] + } + } + ], + "result": { + "type": "RecordType", + "fields": [ + { + "type": "FieldType", + "key": "num", + "value": { + "type": "NameExpression", + "name": "number" + } + } + ] + } + }, + "members": { + "global": [], + "inner": [], + "instance": [], + "events": [], + "static": [] + }, + "path": [ + { + "name": "T", + "kind": "typedef" + } + ], + "namespace": "T" + }, + { + "description": { + "type": "root", + "children": [ + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "T2", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + } + } + }, + "tags": [], + "loc": { + "start": { + "line": 14, + "column": 0 + }, + "end": { + "line": 14, + "column": 9 + } + }, + "context": { + "loc": { + "start": { + "line": 15, + "column": 0 + }, + "end": { + "line": 15, + "column": 41 + } + } + }, + "augments": [], + "errors": [], + "examples": [], + "params": [], + "properties": [], + "returns": [], + "sees": [], + "throws": [], + "todos": [], + "name": "T2", + "kind": "typedef", + "type": { + "type": "FunctionType", + "params": [ + { + "type": "ParameterType", + "name": "a", + "expression": { + "type": "TypeApplication", + "expression": { + "type": "NameExpression", + "name": "Array" + }, + "applications": [ + { + "type": "NameExpression", + "name": "string" + } + ] + } + } + ], + "result": { + "type": "RecordType", + "fields": [ + { + "type": "FieldType", + "key": "num", + "value": { + "type": "NameExpression", + "name": "number" + } + } + ] + } + }, + "members": { + "global": [], + "inner": [], + "instance": [], + "events": [], + "static": [] + }, + "path": [ + { + "name": "T2", + "kind": "typedef" + } + ], + "namespace": "T2" + } +] \ No newline at end of file diff --git a/test/fixture/flow-unnamed-params.output.md b/test/fixture/flow-unnamed-params.output.md new file mode 100644 index 000000000..44c844114 --- /dev/null +++ b/test/fixture/flow-unnamed-params.output.md @@ -0,0 +1,32 @@ + + +### Table of Contents + +- [x](#x) +- [x2](#x2) +- [T](#t) +- [T2](#t2) + +## x + +x + +Type: function ([T](#t)): [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) + +## x2 + +x2 + +Type: function (a: [T](#t)): [string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String) + +## T + +T + +Type: function ([Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>): {num: [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)} + +## T2 + +T2 + +Type: function (a: [Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)<[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)>): {num: [number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)} diff --git a/test/fixture/flow-unnamed-params.output.md.json b/test/fixture/flow-unnamed-params.output.md.json new file mode 100644 index 000000000..a21691184 --- /dev/null +++ b/test/fixture/flow-unnamed-params.output.md.json @@ -0,0 +1,431 @@ +{ + "type": "root", + "children": [ + { + "type": "html", + "value": "" + }, + { + "depth": 2, + "type": "heading", + "children": [ + { + "type": "text", + "value": "x" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "x", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Type: " + }, + { + "type": "text", + "value": "function (" + }, + { + "href": "#t", + "url": "#t", + "type": "link", + "children": [ + { + "type": "text", + "value": "T" + } + ] + }, + { + "type": "text", + "value": ")" + }, + { + "type": "text", + "value": ": " + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "type": "link", + "children": [ + { + "type": "text", + "value": "string" + } + ] + } + ] + }, + { + "depth": 2, + "type": "heading", + "children": [ + { + "type": "text", + "value": "x2" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "x2", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Type: " + }, + { + "type": "text", + "value": "function (" + }, + { + "type": "text", + "value": "a: " + }, + { + "href": "#t", + "url": "#t", + "type": "link", + "children": [ + { + "type": "text", + "value": "T" + } + ] + }, + { + "type": "text", + "value": ")" + }, + { + "type": "text", + "value": ": " + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "type": "link", + "children": [ + { + "type": "text", + "value": "string" + } + ] + } + ] + }, + { + "depth": 2, + "type": "heading", + "children": [ + { + "type": "text", + "value": "T" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "T", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 2, + "offset": 1 + }, + "indent": [] + } + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Type: " + }, + { + "type": "text", + "value": "function (" + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array", + "type": "link", + "children": [ + { + "type": "text", + "value": "Array" + } + ] + }, + { + "type": "text", + "value": "<" + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "type": "link", + "children": [ + { + "type": "text", + "value": "string" + } + ] + }, + { + "type": "text", + "value": ">" + }, + { + "type": "text", + "value": ")" + }, + { + "type": "text", + "value": ": " + }, + { + "type": "text", + "value": "{" + }, + { + "type": "text", + "value": "num: " + }, + { + "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": "}" + } + ] + }, + { + "depth": 2, + "type": "heading", + "children": [ + { + "type": "text", + "value": "T2" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "T2", + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1, + "offset": 0 + }, + "end": { + "line": 1, + "column": 3, + "offset": 2 + }, + "indent": [] + } + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Type: " + }, + { + "type": "text", + "value": "function (" + }, + { + "type": "text", + "value": "a: " + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array", + "type": "link", + "children": [ + { + "type": "text", + "value": "Array" + } + ] + }, + { + "type": "text", + "value": "<" + }, + { + "href": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "url": "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String", + "type": "link", + "children": [ + { + "type": "text", + "value": "string" + } + ] + }, + { + "type": "text", + "value": ">" + }, + { + "type": "text", + "value": ")" + }, + { + "type": "text", + "value": ": " + }, + { + "type": "text", + "value": "{" + }, + { + "type": "text", + "value": "num: " + }, + { + "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": "}" + } + ] + } + ] +} \ No newline at end of file