Skip to content

Commit cefe689

Browse files
committed
Infer destructured params. Fixes #170
Waiting on an answer in babel/babel#2543 because tests will currently fail.
1 parent cabef43 commit cefe689

8 files changed

+231
-12
lines changed

Diff for: lib/infer/params.js

+33-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
var types = require('ast-types'),
4+
extend = require('extend'),
45
flowDoctrine = require('../flow_doctrine');
56

67

@@ -13,6 +14,17 @@ var types = require('ast-types'),
1314
*/
1415
module.exports = function () {
1516
return function inferParams(comment) {
17+
18+
/**
19+
* Given a parameter like
20+
*
21+
* function a(b = 1)
22+
*
23+
* Format it as an optional parameter in JSDoc land
24+
*
25+
* @param {Object} param ESTree node
26+
* @returns {Object} JSDoc param
27+
*/
1628
function paramWithDefaultToDoc(param) {
1729
var newParam = paramToDoc(param.left);
1830
var optionalParam = {
@@ -32,12 +44,30 @@ module.exports = function () {
3244
return optionalParam;
3345
}
3446

35-
function paramToDoc(param) {
47+
function destructuringPropertyToDoc(i, property) {
48+
return paramToDoc(extend({}, property, {
49+
name: '$' + i + '.' + property.key.name
50+
}));
51+
}
52+
53+
function destructuringParamToDoc(param, i) {
54+
return [{
55+
title: 'param',
56+
name: '$' + i,
57+
type: flowDoctrine(param)
58+
}].concat(param.properties.map(destructuringPropertyToDoc.bind(null, i)));
59+
}
60+
61+
function paramToDoc(param, i) {
3662
// ES6 default
3763
if (param.type === 'AssignmentPattern') {
3864
return paramWithDefaultToDoc(param);
3965
}
4066

67+
if (param.type === 'ObjectPattern') {
68+
return destructuringParamToDoc(param, i);
69+
}
70+
4171
var newParam = {
4272
title: 'param',
4373
name: param.name,
@@ -65,13 +95,13 @@ module.exports = function () {
6595
var paramOrder = {};
6696
var i = 0;
6797

68-
path.value.params.forEach(function (param) {
98+
path.value.params.forEach(function (param, j) {
6999
if (existingParams[param.name] === undefined) {
70100
if (!comment.params) {
71101
comment.params = [];
72102
}
73103

74-
comment.params.push(paramToDoc(param));
104+
comment.params = comment.params.concat(paramToDoc(param, j));
75105
}
76106
paramOrder[param.name] = i++;
77107
});

Diff for: test/fixture/bad/syntax.output.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
},
77
"_babel": true,
88
"codeFrame": "> 1 | *\n | ^\n 2 | "
9-
}
9+
}

Diff for: test/fixture/infer-params.input.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
* This function returns the number one.
33
* @param {number} b the second param
44
*/
5-
function addThem(a, b, c) {
6-
return a + b + c;
5+
function addThem(a, b, c, { d, e, f }) {
6+
return a + b + c + d + e + f;
77
}

Diff for: test/fixture/infer-params.output.custom.md

+7
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ This function returns the number one.
1111

1212
- `c`
1313

14+
- `$3`
15+
- `$3.d`
16+
17+
- `$3.e`
18+
19+
- `$3.f`
20+
1421

1522

Diff for: test/fixture/infer-params.output.json

+22-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"column": 1
3535
}
3636
},
37-
"code": "/**\n * This function returns the number one.\n * @param {number} b the second param\n */\nfunction addThem(a, b, c) {\n return a + b + c;\n}\n"
37+
"code": "/**\n * This function returns the number one.\n * @param {number} b the second param\n */\nfunction addThem(a, b, c, { d, e, f }) {\n return a + b + c + d + e + f;\n}\n"
3838
},
3939
"errors": [],
4040
"params": [
@@ -57,6 +57,27 @@
5757
"title": "param",
5858
"name": "c",
5959
"lineNumber": 5
60+
},
61+
{
62+
"title": "param",
63+
"name": "$3",
64+
"properties": [
65+
{
66+
"title": "param",
67+
"name": "$3.d",
68+
"lineNumber": 5
69+
},
70+
{
71+
"title": "param",
72+
"name": "$3.e",
73+
"lineNumber": 5
74+
},
75+
{
76+
"title": "param",
77+
"name": "$3.f",
78+
"lineNumber": 5
79+
}
80+
]
6081
}
6182
],
6283
"name": "addThem",

Diff for: test/fixture/infer-params.output.md

+7
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,12 @@ This function returns the number one.
1111

1212
- `c`
1313

14+
- `$3`
15+
- `$3.d`
16+
17+
- `$3.e`
18+
19+
- `$3.f`
20+
1421

1522

Diff for: test/fixture/infer-params.output.md.json

+150
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,156 @@
224224
]
225225
}
226226
]
227+
},
228+
{
229+
"type": "listItem",
230+
"children": [
231+
{
232+
"type": "paragraph",
233+
"children": [
234+
{
235+
"type": "inlineCode",
236+
"value": "$3"
237+
},
238+
{
239+
"type": "text",
240+
"value": " "
241+
},
242+
{
243+
"type": "text",
244+
"value": " "
245+
},
246+
{
247+
"type": "root",
248+
"children": [],
249+
"position": {
250+
"start": {
251+
"line": 1,
252+
"column": 1
253+
},
254+
"end": {
255+
"line": 1,
256+
"column": 1
257+
}
258+
}
259+
},
260+
{
261+
"ordered": false,
262+
"type": "list",
263+
"children": [
264+
{
265+
"type": "listItem",
266+
"children": [
267+
{
268+
"type": "paragraph",
269+
"children": [
270+
{
271+
"type": "inlineCode",
272+
"value": "$3.d"
273+
},
274+
{
275+
"type": "text",
276+
"value": " "
277+
},
278+
{
279+
"type": "text",
280+
"value": " "
281+
},
282+
{
283+
"type": "root",
284+
"children": [],
285+
"position": {
286+
"start": {
287+
"line": 1,
288+
"column": 1
289+
},
290+
"end": {
291+
"line": 1,
292+
"column": 1
293+
}
294+
}
295+
}
296+
]
297+
}
298+
]
299+
},
300+
{
301+
"type": "listItem",
302+
"children": [
303+
{
304+
"type": "paragraph",
305+
"children": [
306+
{
307+
"type": "inlineCode",
308+
"value": "$3.e"
309+
},
310+
{
311+
"type": "text",
312+
"value": " "
313+
},
314+
{
315+
"type": "text",
316+
"value": " "
317+
},
318+
{
319+
"type": "root",
320+
"children": [],
321+
"position": {
322+
"start": {
323+
"line": 1,
324+
"column": 1
325+
},
326+
"end": {
327+
"line": 1,
328+
"column": 1
329+
}
330+
}
331+
}
332+
]
333+
}
334+
]
335+
},
336+
{
337+
"type": "listItem",
338+
"children": [
339+
{
340+
"type": "paragraph",
341+
"children": [
342+
{
343+
"type": "inlineCode",
344+
"value": "$3.f"
345+
},
346+
{
347+
"type": "text",
348+
"value": " "
349+
},
350+
{
351+
"type": "text",
352+
"value": " "
353+
},
354+
{
355+
"type": "root",
356+
"children": [],
357+
"position": {
358+
"start": {
359+
"line": 1,
360+
"column": 1
361+
},
362+
"end": {
363+
"line": 1,
364+
"column": 1
365+
}
366+
}
367+
}
368+
]
369+
}
370+
]
371+
}
372+
]
373+
}
374+
]
375+
}
376+
]
227377
}
228378
]
229379
}

Diff for: test/test.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ var test = require('tap').test,
1313

1414
var UPDATE = !!process.env.UPDATE;
1515

16+
function makePOJO(ast) {
17+
return JSON.parse(JSON.stringify(ast));
18+
}
19+
1620
if (fs.existsSync(path.join(__dirname, '../.git'))) {
1721
test('git option', function (t) {
1822
var file = path.join(__dirname, './fixture/simple.input.js');
@@ -50,7 +54,7 @@ test('external modules option', function (t) {
5054

5155
test('parse', function (tt) {
5256
glob.sync(path.join(__dirname, 'fixture', '*.input.js')).forEach(function (file) {
53-
tt.test(path.basename(file), function (t) {
57+
tt.test(file, function (t) {
5458
documentation([file], null, function (err, result) {
5559
t.ifError(err);
5660
normalize(result);
@@ -59,23 +63,23 @@ test('parse', function (tt) {
5963
fs.writeFileSync(outputfile, JSON.stringify(result, null, 2));
6064
}
6165
var expect = require(outputfile);
62-
t.deepEqual(result, expect);
66+
t.deepEqual(makePOJO(result), expect);
6367
t.end();
6468
});
6569
});
6670
});
6771
tt.end();
6872
});
6973

70-
test('formats', function (tt) {
74+
test('json', function (tt) {
7175
glob.sync(path.join(__dirname, 'fixture', '*.input.js')).forEach(function (file) {
72-
tt.test('json', function (ttt) {
76+
tt.test(file, function (ttt) {
7377
documentation([file], null, function (err, result) {
7478
ttt.ifError(err);
7579
normalize(result);
7680
var outputfile = file.replace('.input.js', '.output.json');
7781
var expect = require(outputfile);
78-
ttt.deepEqual(result, expect);
82+
ttt.deepEqual(makePOJO(result), expect);
7983
ttt.end();
8084
});
8185
});

0 commit comments

Comments
 (0)