Skip to content

Commit e0fa855

Browse files
committed
Rearrange and document params
1 parent 1f99f9c commit e0fa855

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

Diff for: lib/infer/params.js

+54-32
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,57 @@ const flowDoctrine = require('../flow_doctrine');
99
const util = require('util');
1010
const debuglog = util.debuglog('infer');
1111

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+
}
1330

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+
//
1442
function addPrefix(doc /*: CommentTagNamed */, prefix) {
1543
return _.assign(doc, {
1644
name: prefix + doc.name
1745
});
1846
}
47+
const PATH_SPLIT_CAPTURING = /(\[])?(\.)/g;
1948

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.
2063
function destructuringObjectParamToDoc(param, i, prefix) /*: CommentTag */ {
2164
return {
2265
title: 'param',
@@ -174,38 +217,19 @@ function paramToDoc(
174217
}
175218
}
176219

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-
205220
/**
206221
* Recurse through a potentially nested parameter tag,
207222
* 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
209233
*/
210234
function renameTree(node, explicitName) {
211235
var parts = node.name.split(PATH_SPLIT_CAPTURING);
@@ -216,8 +240,6 @@ function renameTree(node, explicitName) {
216240
}
217241
}
218242

219-
var mapTags = tags => new Map(tags.map(tag => [tag.name, tag]));
220-
221243
function mergeTrees(inferred, explicit) {
222244
// The first order of business is ensuring that the root types are specified
223245
// in the right order. For the order of arguments, the inferred reality

0 commit comments

Comments
 (0)