@@ -9,14 +9,57 @@ const flowDoctrine = require('../flow_doctrine');
9
9
const util = require ( 'util' ) ;
10
10
const debuglog = util . debuglog ( 'infer' ) ;
11
11
12
- const PATH_SPLIT_CAPTURING = / ( \[ ] ) ? ( \. ) / g;
12
+ /**
13
+ * Infers param tags by reading function parameter names
14
+ *
15
+ * @param {Object } comment parsed comment
16
+ * @returns {Object } comment with parameters
17
+ */
18
+ function inferParams ( comment /*: Comment */ ) {
19
+ var path = findTarget ( comment . context . ast ) ;
20
+
21
+ // In case of `/** */ var x = function () {}` findTarget returns
22
+ // the declarator.
23
+ if ( t . isVariableDeclarator ( path ) ) {
24
+ path = path . get ( 'init' ) ;
25
+ }
26
+
27
+ if ( ! t . isFunction ( path ) ) {
28
+ return comment ;
29
+ }
13
30
31
+ // Then merge the trees. This is the hard part.
32
+ return _ . assign ( comment , {
33
+ params : mergeTrees (
34
+ path . node . params . map ( ( param , i ) => paramToDoc ( param , i , '' ) ) ,
35
+ comment . params
36
+ )
37
+ } ) ;
38
+ }
39
+
40
+ // Utility methods ============================================================
41
+ //
14
42
function addPrefix ( doc /*: CommentTagNamed */ , prefix ) {
15
43
return _ . assign ( doc , {
16
44
name : prefix + doc . name
17
45
} ) ;
18
46
}
47
+ const PATH_SPLIT_CAPTURING = / ( \[ ] ) ? ( \. ) / g;
19
48
49
+ /**
50
+ * Index tags by their `name` property into an ES6 map.
51
+ */
52
+ function mapTags ( tags ) {
53
+ return new Map (
54
+ tags . map ( tag => {
55
+ return [ tag . name , tag ] ;
56
+ } )
57
+ ) ;
58
+ }
59
+
60
+ // ___toDoc methods ============================================================
61
+ //
62
+ // These methods take Babel AST nodes and output equivalent JSDoc parameter tags.
20
63
function destructuringObjectParamToDoc ( param , i , prefix ) /*: CommentTag */ {
21
64
return {
22
65
title : 'param' ,
@@ -174,38 +217,19 @@ function paramToDoc(
174
217
}
175
218
}
176
219
177
- /**
178
- * Infers param tags by reading function parameter names
179
- *
180
- * @param {Object } comment parsed comment
181
- * @returns {Object } comment with parameters
182
- */
183
- function inferParams ( comment /*: Comment */ ) {
184
- var path = findTarget ( comment . context . ast ) ;
185
-
186
- // In case of `/** */ var x = function () {}` findTarget returns
187
- // the declarator.
188
- if ( t . isVariableDeclarator ( path ) ) {
189
- path = path . get ( 'init' ) ;
190
- }
191
-
192
- if ( ! t . isFunction ( path ) ) {
193
- return comment ;
194
- }
195
-
196
- // Then merge the trees. This is the hard part.
197
- return _ . assign ( comment , {
198
- params : mergeTrees (
199
- path . node . params . map ( ( param , i ) => paramToDoc ( param , i , '' ) ) ,
200
- comment . params
201
- )
202
- } ) ;
203
- }
204
-
205
220
/**
206
221
* Recurse through a potentially nested parameter tag,
207
222
* replacing the auto-generated name, like $0, with an explicit
208
- * name provided from a JSDoc comment
223
+ * name provided from a JSDoc comment. For instance, if you have a code
224
+ * block like
225
+ *
226
+ * function f({ x });
227
+ *
228
+ * It would by default be documented with a first param $0, with a member $0.x
229
+ *
230
+ * If you specify the name of the param, then it could be documented with, say,
231
+ * options and options.x. So we need to recursively rename not just $0 but
232
+ * also $0.x and maybe $0.x.y.z all to options.x and options.x.y.z
209
233
*/
210
234
function renameTree ( node , explicitName ) {
211
235
var parts = node . name . split ( PATH_SPLIT_CAPTURING ) ;
@@ -216,8 +240,6 @@ function renameTree(node, explicitName) {
216
240
}
217
241
}
218
242
219
- var mapTags = tags => new Map ( tags . map ( tag => [ tag . name , tag ] ) ) ;
220
-
221
243
function mergeTrees ( inferred , explicit ) {
222
244
// The first order of business is ensuring that the root types are specified
223
245
// in the right order. For the order of arguments, the inferred reality
0 commit comments