Skip to content

Commit

Permalink
Merge pull request #157 from documentationjs/infer-params
Browse files Browse the repository at this point in the history
Infer parameters. Fixes #51
  • Loading branch information
tmcw committed Oct 5, 2015
2 parents c058a0d + 5753da1 commit 472e7ff
Show file tree
Hide file tree
Showing 21 changed files with 780 additions and 10 deletions.
8 changes: 7 additions & 1 deletion index.js
Expand Up @@ -13,6 +13,7 @@ var sort = require('./lib/sort'),
hierarchy = require('./lib/hierarchy'),
inferName = require('./lib/infer/name'),
inferKind = require('./lib/infer/kind'),
inferParams = require('./lib/infer/params'),
inferMembership = require('./lib/infer/membership'),
lint = require('./lib/lint');

Expand Down Expand Up @@ -59,7 +60,12 @@ module.exports = function (indexes, options, callback) {
}, [])
.map(function (comment) {
// compose nesting & membership to avoid intermediate arrays
comment = nestParams(inferMembership(inferKind(inferName(lint(comment)))));
comment = nestParams(
inferMembership(
inferParams(
inferKind(
inferName(
lint(comment))))));
if (options.github) {
comment = github(comment);
}
Expand Down
3 changes: 3 additions & 0 deletions lib/html_helpers.js
Expand Up @@ -88,6 +88,9 @@ function autolink(paths, text) {
* // generates String
*/
function formatType(type, paths) {
if (!type) {
return '';
}
function recurse(element) {
return formatType(element, paths);
}
Expand Down
54 changes: 54 additions & 0 deletions lib/infer/params.js
@@ -0,0 +1,54 @@
'use strict';

var types = require('ast-types');

/**
* Infers param tags by reading function parameter names
*
* @name inferParams
* @param {Object} comment parsed comment
* @returns {Object} comment with parameters
*/
module.exports = function inferParams(comment) {

types.visit(comment.context.ast, {
visitFunction: function (path) {

// Ensure that explicitly specified parameters are not overridden
// by inferred parameters
var existingParams = (comment.params || []).reduce(function (memo, param) {
memo[param.name] = param;
return memo;
}, {});

var paramOrder = [];

path.value.params.forEach(function (param) {
if (existingParams[param.name] === undefined) {
if (!comment.params) {
comment.params = [];
}
comment.params.push({
title: 'param',
name: param.name,
lineNumber: param.loc.start.line
});
}
paramOrder.push(param.name);
});

// Ensure that if params are specified partially or in
// the wrong order, they'll be output in the order
// they actually appear in code
if (comment.params) {
comment.params.sort(function (a, b) {
return paramOrder.indexOf(a.name) - paramOrder.indexOf(b.name);
});
}

this.abort();
}
});

return comment;
};
8 changes: 8 additions & 0 deletions test/fixture/es6.output.custom.md
Expand Up @@ -3,6 +3,14 @@
This function returns the number one.


**Parameters**

- `a`

- `b`



Returns **Number** numberone


Expand Down
12 changes: 12 additions & 0 deletions test/fixture/es6.output.json
Expand Up @@ -49,6 +49,18 @@
],
"name": "multiply",
"kind": "function",
"params": [
{
"title": "param",
"name": "a",
"lineNumber": 5
},
{
"title": "param",
"name": "b",
"lineNumber": 5
}
],
"members": {
"instance": [],
"static": []
Expand Down
8 changes: 8 additions & 0 deletions test/fixture/es6.output.md
Expand Up @@ -3,6 +3,14 @@
This function returns the number one.


**Parameters**

- `a`

- `b`



Returns **Number** numberone


Expand Down
92 changes: 92 additions & 0 deletions test/fixture/es6.output.md.json
Expand Up @@ -60,6 +60,98 @@
}
}
},
{
"type": "root",
"children": [
{
"type": "strong",
"children": [
{
"type": "text",
"value": "Parameters"
}
]
},
{
"ordered": false,
"type": "list",
"children": [
{
"type": "listItem",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "inlineCode",
"value": "a"
},
{
"type": "text",
"value": " "
},
{
"type": "text",
"value": " "
},
{
"type": "root",
"children": [],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
}
}
]
}
]
},
{
"type": "listItem",
"children": [
{
"type": "paragraph",
"children": [
{
"type": "inlineCode",
"value": "b"
},
{
"type": "text",
"value": " "
},
{
"type": "text",
"value": " "
},
{
"type": "root",
"children": [],
"position": {
"start": {
"line": 1,
"column": 1
},
"end": {
"line": 1,
"column": 1
}
}
}
]
}
]
}
]
}
]
},
{
"type": "root",
"children": [
Expand Down
12 changes: 12 additions & 0 deletions test/fixture/factory.output.custom.md
Expand Up @@ -13,9 +13,21 @@ Returns **area** chart



**Parameters**

- `selection`




# data

Sets the chart data.


**Parameters**

- `_`



14 changes: 14 additions & 0 deletions test/fixture/factory.output.json
Expand Up @@ -98,6 +98,13 @@
},
"name": "area",
"kind": "class",
"params": [
{
"title": "param",
"name": "selection",
"lineNumber": 10
}
],
"members": {
"instance": [],
"static": []
Expand Down Expand Up @@ -146,6 +153,13 @@
"function": null,
"name": "data",
"kind": "function",
"params": [
{
"title": "param",
"name": "_",
"lineNumber": 17
}
],
"memberof": "chart",
"scope": "static",
"members": {
Expand Down
12 changes: 12 additions & 0 deletions test/fixture/factory.output.md
Expand Up @@ -13,9 +13,21 @@ Returns **area** chart



**Parameters**

- `selection`




# data

Sets the chart data.


**Parameters**

- `_`



0 comments on commit 472e7ff

Please sign in to comment.