diff --git a/lib/infer/params.js b/lib/infer/params.js index 4701727a8..b8efa193c 100644 --- a/lib/infer/params.js +++ b/lib/infer/params.js @@ -121,11 +121,20 @@ module.exports = function () { path.value.params.forEach(function (param, j) { if (existingParams[param.name] === undefined) { + // This type is not explicitly documented if (!comment.params) { comment.params = []; } comment.params = comment.params.concat(paramToDoc(param, j)); + } else if (!existingParams[param.name].type) { + // This param has a description, but potentially it can + // be have an inferred type. Infer its type without + // dropping the description. + var doc = paramToDoc(param, j); + if (doc.type) { + existingParams[param.name].type = doc.type; + } } paramOrder[param.name] = i++; }); diff --git a/test/fixture/merge-infered-type.input.js b/test/fixture/merge-infered-type.input.js new file mode 100644 index 000000000..1bdd4ba40 --- /dev/null +++ b/test/fixture/merge-infered-type.input.js @@ -0,0 +1,9 @@ +/** + * Add five to `x`. + * + * @param x The number to add five to. + * @returns {number} x plus five. + */ +function addFive(x: number): number { + return x + 5; +} diff --git a/test/fixture/merge-infered-type.output.custom.md b/test/fixture/merge-infered-type.output.custom.md new file mode 100644 index 000000000..e033cf7c6 --- /dev/null +++ b/test/fixture/merge-infered-type.output.custom.md @@ -0,0 +1,9 @@ +# addFive + +Add five to `x`. + +**Parameters** + +- `x` **number** The number to add five to. + +Returns **number** x plus five. diff --git a/test/fixture/merge-infered-type.output.json b/test/fixture/merge-infered-type.output.json new file mode 100644 index 000000000..26a353c14 --- /dev/null +++ b/test/fixture/merge-infered-type.output.json @@ -0,0 +1,82 @@ +[ + { + "description": "Add five to `x`.", + "tags": [ + { + "title": "param", + "description": "The number to add five to.", + "lineNumber": 3, + "type": { + "type": "NameExpression", + "name": "number" + }, + "name": "x" + }, + { + "title": "returns", + "description": "x plus five.", + "lineNumber": 4, + "type": { + "type": "NameExpression", + "name": "number" + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 6, + "column": 3 + } + }, + "context": { + "loc": { + "start": { + "line": 7, + "column": 0 + }, + "end": { + "line": 9, + "column": 1 + } + }, + "code": "/**\n * Add five to `x`.\n *\n * @param x The number to add five to.\n * @returns {number} x plus five.\n */\nfunction addFive(x: number): number {\n return x + 5;\n}\n" + }, + "errors": [], + "params": [ + { + "title": "param", + "description": "The number to add five to.", + "lineNumber": 3, + "type": { + "type": "NameExpression", + "name": "number" + }, + "name": "x" + } + ], + "returns": [ + { + "title": "returns", + "description": "x plus five.", + "lineNumber": 4, + "type": { + "type": "NameExpression", + "name": "number" + } + } + ], + "name": "addFive", + "kind": "function", + "members": { + "instance": [], + "static": [] + }, + "path": [ + "addFive" + ] + } +] \ No newline at end of file diff --git a/test/fixture/merge-infered-type.output.md b/test/fixture/merge-infered-type.output.md new file mode 100644 index 000000000..e033cf7c6 --- /dev/null +++ b/test/fixture/merge-infered-type.output.md @@ -0,0 +1,9 @@ +# addFive + +Add five to `x`. + +**Parameters** + +- `x` **number** The number to add five to. + +Returns **number** x plus five. diff --git a/test/fixture/merge-infered-type.output.md.json b/test/fixture/merge-infered-type.output.md.json new file mode 100644 index 000000000..3dde4a8b3 --- /dev/null +++ b/test/fixture/merge-infered-type.output.md.json @@ -0,0 +1,206 @@ +{ + "type": "root", + "children": [ + { + "depth": 1, + "type": "heading", + "children": [ + { + "type": "text", + "value": "addFive" + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Add five to ", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + }, + "indent": [] + } + }, + { + "type": "inlineCode", + "value": "x", + "position": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 16 + }, + "indent": [] + } + }, + { + "type": "text", + "value": ".", + "position": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 17 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 17 + }, + "indent": [] + } + }, + { + "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": [ + { + "type": "text", + "value": "number" + } + ] + }, + { + "type": "text", + "value": " " + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "The number to add five to.", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 27 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 27 + }, + "indent": [] + } + } + ] + } + ] + } + ] + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "Returns " + }, + { + "type": "strong", + "children": [ + { + "type": "text", + "value": "number" + } + ] + }, + { + "type": "text", + "value": " " + }, + { + "type": "paragraph", + "children": [ + { + "type": "text", + "value": "x plus five.", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + }, + "indent": [] + } + } + ], + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 13 + }, + "indent": [] + } + } + ] + } + ] +} \ No newline at end of file