Skip to content

Commit 33552c6

Browse files
committed
Fix type merging. Fixes #233
Some params cannot be identified with only param.name. This changes the algorithm to transform parameters into doc objects first, and then using the value of doc.name, which is defined even for parameters with default values.
1 parent eed48f7 commit 33552c6

6 files changed

+303
-18
lines changed

lib/infer/params.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -119,25 +119,26 @@ module.exports = function () {
119119
var paramOrder = {};
120120
var i = 0;
121121

122-
path.value.params.forEach(function (param, j) {
123-
if (existingParams[param.name] === undefined) {
124-
// This type is not explicitly documented
125-
if (!comment.params) {
126-
comment.params = [];
122+
path.value.params
123+
.map(paramToDoc)
124+
.forEach(function (doc) {
125+
if (existingParams[doc.name] === undefined) {
126+
// This type is not explicitly documented
127+
if (!comment.params) {
128+
comment.params = [];
129+
}
130+
131+
comment.params = comment.params.concat(doc);
132+
} else if (!existingParams[doc.name].type) {
133+
// This param has a description, but potentially it can
134+
// be have an inferred type. Infer its type without
135+
// dropping the description.
136+
if (doc.type) {
137+
existingParams[doc.name].type = doc.type;
138+
}
127139
}
128-
129-
comment.params = comment.params.concat(paramToDoc(param, j));
130-
} else if (!existingParams[param.name].type) {
131-
// This param has a description, but potentially it can
132-
// be have an inferred type. Infer its type without
133-
// dropping the description.
134-
var doc = paramToDoc(param, j);
135-
if (doc.type) {
136-
existingParams[param.name].type = doc.type;
137-
}
138-
}
139-
paramOrder[param.name] = i++;
140-
});
140+
paramOrder[doc.name] = i++;
141+
});
141142

142143
// Ensure that if params are specified partially or in
143144
// the wrong order, they'll be output in the order

test/fixture/partial-default.input.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* Number
3+
*
4+
* @param {number} x an argument
5+
*
6+
* @returns {number} some
7+
*/
8+
export const myfunc = (x = 123) => x;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# myfunc
2+
3+
Number
4+
5+
**Parameters**
6+
7+
- `x` **number** an argument
8+
9+
Returns **number** some
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
[
2+
{
3+
"description": "Number",
4+
"tags": [
5+
{
6+
"title": "param",
7+
"description": "an argument",
8+
"lineNumber": 3,
9+
"type": {
10+
"type": "NameExpression",
11+
"name": "number"
12+
},
13+
"name": "x"
14+
},
15+
{
16+
"title": "returns",
17+
"description": "some",
18+
"lineNumber": 5,
19+
"type": {
20+
"type": "NameExpression",
21+
"name": "number"
22+
}
23+
}
24+
],
25+
"loc": {
26+
"start": {
27+
"line": 1,
28+
"column": 0
29+
},
30+
"end": {
31+
"line": 7,
32+
"column": 3
33+
}
34+
},
35+
"context": {
36+
"loc": {
37+
"start": {
38+
"line": 8,
39+
"column": 0
40+
},
41+
"end": {
42+
"line": 8,
43+
"column": 37
44+
}
45+
},
46+
"code": "/**\n * Number\n *\n * @param {number} x an argument\n *\n * @returns {number} some\n */\nexport const myfunc = (x = 123) => x;\n"
47+
},
48+
"errors": [],
49+
"params": [
50+
{
51+
"title": "param",
52+
"description": "an argument",
53+
"lineNumber": 3,
54+
"type": {
55+
"type": "NameExpression",
56+
"name": "number"
57+
},
58+
"name": "x"
59+
}
60+
],
61+
"returns": [
62+
{
63+
"title": "returns",
64+
"description": "some",
65+
"lineNumber": 5,
66+
"type": {
67+
"type": "NameExpression",
68+
"name": "number"
69+
}
70+
}
71+
],
72+
"name": "myfunc",
73+
"kind": "constant",
74+
"members": {
75+
"instance": [],
76+
"static": []
77+
},
78+
"path": [
79+
"myfunc"
80+
]
81+
}
82+
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# myfunc
2+
3+
Number
4+
5+
**Parameters**
6+
7+
- `x` **number** an argument
8+
9+
Returns **number** some
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"depth": 1,
6+
"type": "heading",
7+
"children": [
8+
{
9+
"type": "text",
10+
"value": "myfunc"
11+
}
12+
]
13+
},
14+
{
15+
"type": "paragraph",
16+
"children": [
17+
{
18+
"type": "text",
19+
"value": "Number",
20+
"position": {
21+
"start": {
22+
"line": 1,
23+
"column": 1
24+
},
25+
"end": {
26+
"line": 1,
27+
"column": 7
28+
},
29+
"indent": []
30+
}
31+
}
32+
],
33+
"position": {
34+
"start": {
35+
"line": 1,
36+
"column": 1
37+
},
38+
"end": {
39+
"line": 1,
40+
"column": 7
41+
},
42+
"indent": []
43+
}
44+
},
45+
{
46+
"type": "strong",
47+
"children": [
48+
{
49+
"type": "text",
50+
"value": "Parameters"
51+
}
52+
]
53+
},
54+
{
55+
"ordered": false,
56+
"type": "list",
57+
"children": [
58+
{
59+
"type": "listItem",
60+
"children": [
61+
{
62+
"type": "paragraph",
63+
"children": [
64+
{
65+
"type": "inlineCode",
66+
"value": "x"
67+
},
68+
{
69+
"type": "text",
70+
"value": " "
71+
},
72+
{
73+
"type": "strong",
74+
"children": [
75+
{
76+
"type": "text",
77+
"value": "number"
78+
}
79+
]
80+
},
81+
{
82+
"type": "text",
83+
"value": " "
84+
},
85+
{
86+
"type": "paragraph",
87+
"children": [
88+
{
89+
"type": "text",
90+
"value": "an argument",
91+
"position": {
92+
"start": {
93+
"line": 1,
94+
"column": 1
95+
},
96+
"end": {
97+
"line": 1,
98+
"column": 12
99+
},
100+
"indent": []
101+
}
102+
}
103+
],
104+
"position": {
105+
"start": {
106+
"line": 1,
107+
"column": 1
108+
},
109+
"end": {
110+
"line": 1,
111+
"column": 12
112+
},
113+
"indent": []
114+
}
115+
}
116+
]
117+
}
118+
]
119+
}
120+
]
121+
},
122+
{
123+
"type": "paragraph",
124+
"children": [
125+
{
126+
"type": "text",
127+
"value": "Returns "
128+
},
129+
{
130+
"type": "strong",
131+
"children": [
132+
{
133+
"type": "text",
134+
"value": "number"
135+
}
136+
]
137+
},
138+
{
139+
"type": "text",
140+
"value": " "
141+
},
142+
{
143+
"type": "paragraph",
144+
"children": [
145+
{
146+
"type": "text",
147+
"value": "some",
148+
"position": {
149+
"start": {
150+
"line": 1,
151+
"column": 1
152+
},
153+
"end": {
154+
"line": 1,
155+
"column": 5
156+
},
157+
"indent": []
158+
}
159+
}
160+
],
161+
"position": {
162+
"start": {
163+
"line": 1,
164+
"column": 1
165+
},
166+
"end": {
167+
"line": 1,
168+
"column": 5
169+
},
170+
"indent": []
171+
}
172+
}
173+
]
174+
}
175+
]
176+
}

0 commit comments

Comments
 (0)