Skip to content

Commit a65801a

Browse files
committed
Add and test default parameter parsing.
cc @killercup this implements default parameter parsing for both flow-annotated and unannotated methods
1 parent 0aa42d2 commit a65801a

18 files changed

+638
-21
lines changed

lib/infer/params.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var types = require('ast-types'),
44
flowDoctrine = require('../flow_doctrine');
55

6+
67
/**
78
* Infers param tags by reading function parameter names
89
*
@@ -12,6 +13,41 @@ var types = require('ast-types'),
1213
*/
1314
module.exports = function inferParams(comment) {
1415

16+
function paramToDoc(param) {
17+
// ES6 default
18+
if (param.type === 'AssignmentPattern') {
19+
var newParam = paramToDoc(param.left);
20+
var optionalParam = {
21+
title: 'param',
22+
name: newParam.name,
23+
'default': comment.context.code.substring(
24+
param.right.start, param.right.end)
25+
};
26+
27+
if (newParam.type) {
28+
optionalParam.type = {
29+
type: 'OptionalType',
30+
expression: newParam.type
31+
};
32+
}
33+
34+
return optionalParam;
35+
}
36+
37+
var newParam = {
38+
title: 'param',
39+
name: param.name,
40+
lineNumber: param.loc.start.line
41+
};
42+
43+
// Flow/TS annotations
44+
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation) {
45+
newParam.type = flowDoctrine(param.typeAnnotation.typeAnnotation);
46+
}
47+
48+
return newParam;
49+
}
50+
1551
types.visit(comment.context.ast, {
1652
visitFunction: function (path) {
1753

@@ -30,15 +66,8 @@ module.exports = function inferParams(comment) {
3066
if (!comment.params) {
3167
comment.params = [];
3268
}
33-
var newParam = {
34-
title: 'param',
35-
name: param.name,
36-
lineNumber: param.loc.start.line
37-
};
38-
if (param.typeAnnotation && param.typeAnnotation.typeAnnotation) {
39-
newParam.type = flowDoctrine(param.typeAnnotation.typeAnnotation);
40-
}
41-
comment.params.push(newParam);
69+
70+
comment.params.push(paramToDoc(param));
4271
}
4372
paramOrder[param.name] = i++;
4473
});

lib/output/markdown_ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function commentsToAST(comments, opts, callback) {
2121
mdast.parse(formatInlineTags(param.description)),
2222
!!param.default && u('root', [
2323
u('text', ' (optional, default '),
24-
u('text', param.default, 'inlineCode'),
24+
u('inlineCode', param.default),
2525
u('text', ')')
2626
]),
2727
param.properties && paramList(param.properties)

test/fixture/default-param.input.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Very Important Transform
3+
*/
4+
function veryImportantTransform(foo = 'bar') {
5+
return "42";
6+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# veryImportantTransform
2+
3+
Very Important Transform
4+
5+
6+
**Parameters**
7+
8+
- `foo`
9+
(optional, default
10+
11+
`'bar'`
12+
13+
)
14+
15+
16+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
[
2+
{
3+
"description": "Very Important Transform",
4+
"tags": [],
5+
"loc": {
6+
"start": {
7+
"line": 1,
8+
"column": 0
9+
},
10+
"end": {
11+
"line": 3,
12+
"column": 3
13+
}
14+
},
15+
"context": {
16+
"loc": {
17+
"start": {
18+
"line": 4,
19+
"column": 0
20+
},
21+
"end": {
22+
"line": 6,
23+
"column": 1
24+
}
25+
},
26+
"code": "/**\n * Very Important Transform\n */\nfunction veryImportantTransform(foo = 'bar') {\n return \"42\";\n}\n"
27+
},
28+
"errors": [],
29+
"name": "veryImportantTransform",
30+
"kind": "function",
31+
"params": [
32+
{
33+
"title": "param",
34+
"name": "foo",
35+
"default": "'bar'"
36+
}
37+
],
38+
"members": {
39+
"instance": [],
40+
"static": []
41+
},
42+
"events": [],
43+
"path": [
44+
"veryImportantTransform"
45+
]
46+
}
47+
]

test/fixture/default-param.output.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# veryImportantTransform
2+
3+
Very Important Transform
4+
5+
6+
**Parameters**
7+
8+
- `foo`
9+
(optional, default
10+
11+
`'bar'`
12+
13+
)
14+
15+
16+
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"type": "root",
3+
"children": [
4+
{
5+
"type": "root",
6+
"children": [
7+
{
8+
"depth": 1,
9+
"type": "heading",
10+
"children": [
11+
{
12+
"type": "text",
13+
"value": "veryImportantTransform"
14+
}
15+
]
16+
},
17+
{
18+
"type": "root",
19+
"children": [
20+
{
21+
"type": "paragraph",
22+
"children": [
23+
{
24+
"type": "text",
25+
"value": "Very Important Transform",
26+
"position": {
27+
"start": {
28+
"line": 1,
29+
"column": 1
30+
},
31+
"end": {
32+
"line": 1,
33+
"column": 25
34+
},
35+
"indent": []
36+
}
37+
}
38+
],
39+
"position": {
40+
"start": {
41+
"line": 1,
42+
"column": 1
43+
},
44+
"end": {
45+
"line": 1,
46+
"column": 25
47+
},
48+
"indent": []
49+
}
50+
}
51+
],
52+
"position": {
53+
"start": {
54+
"line": 1,
55+
"column": 1
56+
},
57+
"end": {
58+
"line": 1,
59+
"column": 25
60+
}
61+
}
62+
},
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": "foo"
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+
"type": "root",
113+
"children": [
114+
{
115+
"type": "text",
116+
"value": " (optional, default "
117+
},
118+
{
119+
"type": "inlineCode",
120+
"value": "'bar'"
121+
},
122+
{
123+
"type": "text",
124+
"value": ")"
125+
}
126+
]
127+
}
128+
]
129+
}
130+
]
131+
}
132+
]
133+
}
134+
]
135+
}
136+
]
137+
}
138+
]
139+
}

test/fixture/flow-default.input.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* Very Important Transform
3+
*/
4+
function veryImportantTransform(
5+
input: Array<string>,
6+
options: Object = {}
7+
): string {
8+
return "42";
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# veryImportantTransform
2+
3+
Very Important Transform
4+
5+
6+
**Parameters**
7+
8+
- `input` **Array<string>**
9+
10+
- `options` **[Object]**
11+
(optional, default
12+
13+
`{}`
14+
15+
)
16+
17+
18+
19+
Returns **string**
20+
21+
22+

0 commit comments

Comments
 (0)