Skip to content

Commit 472e7ff

Browse files
committed
Merge pull request #157 from documentationjs/infer-params
Infer parameters. Fixes #51
2 parents c058a0d + 5753da1 commit 472e7ff

21 files changed

+780
-10
lines changed

index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ var sort = require('./lib/sort'),
1313
hierarchy = require('./lib/hierarchy'),
1414
inferName = require('./lib/infer/name'),
1515
inferKind = require('./lib/infer/kind'),
16+
inferParams = require('./lib/infer/params'),
1617
inferMembership = require('./lib/infer/membership'),
1718
lint = require('./lib/lint');
1819

@@ -59,7 +60,12 @@ module.exports = function (indexes, options, callback) {
5960
}, [])
6061
.map(function (comment) {
6162
// compose nesting & membership to avoid intermediate arrays
62-
comment = nestParams(inferMembership(inferKind(inferName(lint(comment)))));
63+
comment = nestParams(
64+
inferMembership(
65+
inferParams(
66+
inferKind(
67+
inferName(
68+
lint(comment))))));
6369
if (options.github) {
6470
comment = github(comment);
6571
}

lib/html_helpers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ function autolink(paths, text) {
8888
* // generates String
8989
*/
9090
function formatType(type, paths) {
91+
if (!type) {
92+
return '';
93+
}
9194
function recurse(element) {
9295
return formatType(element, paths);
9396
}

lib/infer/params.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict';
2+
3+
var types = require('ast-types');
4+
5+
/**
6+
* Infers param tags by reading function parameter names
7+
*
8+
* @name inferParams
9+
* @param {Object} comment parsed comment
10+
* @returns {Object} comment with parameters
11+
*/
12+
module.exports = function inferParams(comment) {
13+
14+
types.visit(comment.context.ast, {
15+
visitFunction: function (path) {
16+
17+
// Ensure that explicitly specified parameters are not overridden
18+
// by inferred parameters
19+
var existingParams = (comment.params || []).reduce(function (memo, param) {
20+
memo[param.name] = param;
21+
return memo;
22+
}, {});
23+
24+
var paramOrder = [];
25+
26+
path.value.params.forEach(function (param) {
27+
if (existingParams[param.name] === undefined) {
28+
if (!comment.params) {
29+
comment.params = [];
30+
}
31+
comment.params.push({
32+
title: 'param',
33+
name: param.name,
34+
lineNumber: param.loc.start.line
35+
});
36+
}
37+
paramOrder.push(param.name);
38+
});
39+
40+
// Ensure that if params are specified partially or in
41+
// the wrong order, they'll be output in the order
42+
// they actually appear in code
43+
if (comment.params) {
44+
comment.params.sort(function (a, b) {
45+
return paramOrder.indexOf(a.name) - paramOrder.indexOf(b.name);
46+
});
47+
}
48+
49+
this.abort();
50+
}
51+
});
52+
53+
return comment;
54+
};

test/fixture/es6.output.custom.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
This function returns the number one.
44

55

6+
**Parameters**
7+
8+
- `a`
9+
10+
- `b`
11+
12+
13+
614
Returns **Number** numberone
715

816

test/fixture/es6.output.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@
4949
],
5050
"name": "multiply",
5151
"kind": "function",
52+
"params": [
53+
{
54+
"title": "param",
55+
"name": "a",
56+
"lineNumber": 5
57+
},
58+
{
59+
"title": "param",
60+
"name": "b",
61+
"lineNumber": 5
62+
}
63+
],
5264
"members": {
5365
"instance": [],
5466
"static": []

test/fixture/es6.output.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
This function returns the number one.
44

55

6+
**Parameters**
7+
8+
- `a`
9+
10+
- `b`
11+
12+
13+
614
Returns **Number** numberone
715

816

test/fixture/es6.output.md.json

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,98 @@
6060
}
6161
}
6262
},
63+
{
64+
"type": "root",
65+
"children": [
66+
{
67+
"type": "strong",
68+
"children": [
69+
{
70+
"type": "text",
71+
"value": "Parameters"
72+
}
73+
]
74+
},
75+
{
76+
"ordered": false,
77+
"type": "list",
78+
"children": [
79+
{
80+
"type": "listItem",
81+
"children": [
82+
{
83+
"type": "paragraph",
84+
"children": [
85+
{
86+
"type": "inlineCode",
87+
"value": "a"
88+
},
89+
{
90+
"type": "text",
91+
"value": " "
92+
},
93+
{
94+
"type": "text",
95+
"value": " "
96+
},
97+
{
98+
"type": "root",
99+
"children": [],
100+
"position": {
101+
"start": {
102+
"line": 1,
103+
"column": 1
104+
},
105+
"end": {
106+
"line": 1,
107+
"column": 1
108+
}
109+
}
110+
}
111+
]
112+
}
113+
]
114+
},
115+
{
116+
"type": "listItem",
117+
"children": [
118+
{
119+
"type": "paragraph",
120+
"children": [
121+
{
122+
"type": "inlineCode",
123+
"value": "b"
124+
},
125+
{
126+
"type": "text",
127+
"value": " "
128+
},
129+
{
130+
"type": "text",
131+
"value": " "
132+
},
133+
{
134+
"type": "root",
135+
"children": [],
136+
"position": {
137+
"start": {
138+
"line": 1,
139+
"column": 1
140+
},
141+
"end": {
142+
"line": 1,
143+
"column": 1
144+
}
145+
}
146+
}
147+
]
148+
}
149+
]
150+
}
151+
]
152+
}
153+
]
154+
},
63155
{
64156
"type": "root",
65157
"children": [

test/fixture/factory.output.custom.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ Returns **area** chart
1313

1414

1515

16+
**Parameters**
17+
18+
- `selection`
19+
20+
21+
1622

1723
# data
1824

1925
Sets the chart data.
2026

2127

28+
**Parameters**
29+
30+
- `_`
31+
32+
33+

test/fixture/factory.output.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,13 @@
9898
},
9999
"name": "area",
100100
"kind": "class",
101+
"params": [
102+
{
103+
"title": "param",
104+
"name": "selection",
105+
"lineNumber": 10
106+
}
107+
],
101108
"members": {
102109
"instance": [],
103110
"static": []
@@ -146,6 +153,13 @@
146153
"function": null,
147154
"name": "data",
148155
"kind": "function",
156+
"params": [
157+
{
158+
"title": "param",
159+
"name": "_",
160+
"lineNumber": 17
161+
}
162+
],
149163
"memberof": "chart",
150164
"scope": "static",
151165
"members": {

test/fixture/factory.output.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,21 @@ Returns **area** chart
1313

1414

1515

16+
**Parameters**
17+
18+
- `selection`
19+
20+
21+
1622

1723
# data
1824

1925
Sets the chart data.
2026

2127

28+
**Parameters**
29+
30+
- `_`
31+
32+
33+

0 commit comments

Comments
 (0)