Skip to content

Commit 55f79eb

Browse files
committed
Infer rest parameters. Fixes #223
1 parent c53f577 commit 55f79eb

12 files changed

+725
-22
lines changed

lib/html_helpers.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ function formatType(type, paths) {
8989
return type.elements.map(recurse).join(' or ');
9090
case 'AllLiteral':
9191
return 'Any';
92+
case 'RestType':
93+
return '...' + formatType(type.expression);
9294
case 'OptionalType':
9395
return '<code>[' + formatType(type.expression, paths) + ']</code>';
9496
case 'TypeApplication':

lib/infer/params.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,21 @@ module.exports = function () {
5858
}].concat(param.properties.map(destructuringPropertyToDoc.bind(null, i)));
5959
}
6060

61+
function restParamToDoc(param) {
62+
var newParam = {
63+
title: 'param',
64+
name: param.argument.name,
65+
lineNumber: param.loc.start.name,
66+
type: {
67+
type: 'RestType'
68+
}
69+
};
70+
if (param.typeAnnotation) {
71+
newParam.type.expression = flowDoctrine(param.typeAnnotation.typeAnnotation);
72+
}
73+
return newParam;
74+
}
75+
6176
function paramToDoc(param, i) {
6277
// ES6 default
6378
if (param.type === 'AssignmentPattern') {
@@ -68,6 +83,10 @@ module.exports = function () {
6883
return destructuringParamToDoc(param, i);
6984
}
7085

86+
if (param.type === 'RestElement') {
87+
return restParamToDoc(param);
88+
}
89+
7190
var newParam = {
7291
title: 'param',
7392
name: param.name,

lib/markdown_format_type.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function formatType(type) {
1313
return 'Any';
1414
case 'OptionalType':
1515
return '[' + formatType(type.expression) + ']';
16+
case 'RestType':
17+
return '...' + formatType(type.expression);
1618
case 'TypeApplication':
1719
return formatType(type.expression) + '&lt;' +
1820
type.applications.map(function (application) {

test/fixture/es6-import.output.custom.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ This method says hello
2323

2424
This is an async method
2525

26+
# functionWithRest
27+
28+
This function takes rest params
29+
30+
**Parameters**
31+
32+
- `someParams` **...**
33+
34+
35+
# functionWithRestAndType
36+
37+
So does this one, with types
38+
39+
**Parameters**
40+
41+
- `someParams` **...number**
42+
43+
2644
# multiply
2745

2846
This function returns the number one.

test/fixture/es6-import.output.json

Lines changed: 107 additions & 11 deletions
Large diffs are not rendered by default.

test/fixture/es6-import.output.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ This method says hello
2323

2424
This is an async method
2525

26+
# functionWithRest
27+
28+
This function takes rest params
29+
30+
**Parameters**
31+
32+
- `someParams` **...**
33+
34+
35+
# functionWithRestAndType
36+
37+
So does this one, with types
38+
39+
**Parameters**
40+
41+
- `someParams` **...number**
42+
43+
2644
# multiply
2745

2846
This function returns the number one.

test/fixture/es6-import.output.md.json

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,208 @@
344344
"indent": []
345345
}
346346
},
347+
{
348+
"depth": 1,
349+
"type": "heading",
350+
"children": [
351+
{
352+
"type": "text",
353+
"value": "functionWithRest"
354+
}
355+
]
356+
},
357+
{
358+
"type": "paragraph",
359+
"children": [
360+
{
361+
"type": "text",
362+
"value": "This function takes rest params",
363+
"position": {
364+
"start": {
365+
"line": 1,
366+
"column": 1
367+
},
368+
"end": {
369+
"line": 1,
370+
"column": 32
371+
},
372+
"indent": []
373+
}
374+
}
375+
],
376+
"position": {
377+
"start": {
378+
"line": 1,
379+
"column": 1
380+
},
381+
"end": {
382+
"line": 1,
383+
"column": 32
384+
},
385+
"indent": []
386+
}
387+
},
388+
{
389+
"type": "strong",
390+
"children": [
391+
{
392+
"type": "text",
393+
"value": "Parameters"
394+
}
395+
]
396+
},
397+
{
398+
"ordered": false,
399+
"type": "list",
400+
"children": [
401+
{
402+
"type": "listItem",
403+
"children": [
404+
{
405+
"type": "paragraph",
406+
"children": [
407+
{
408+
"type": "inlineCode",
409+
"value": "someParams"
410+
},
411+
{
412+
"type": "text",
413+
"value": " "
414+
},
415+
{
416+
"type": "strong",
417+
"children": [
418+
{
419+
"type": "text",
420+
"value": "..."
421+
}
422+
]
423+
},
424+
{
425+
"type": "text",
426+
"value": " "
427+
},
428+
{
429+
"type": "root",
430+
"children": [],
431+
"position": {
432+
"start": {
433+
"line": 1,
434+
"column": 1
435+
},
436+
"end": {
437+
"line": 1,
438+
"column": 1
439+
}
440+
}
441+
}
442+
]
443+
}
444+
]
445+
}
446+
]
447+
},
448+
{
449+
"depth": 1,
450+
"type": "heading",
451+
"children": [
452+
{
453+
"type": "text",
454+
"value": "functionWithRestAndType"
455+
}
456+
]
457+
},
458+
{
459+
"type": "paragraph",
460+
"children": [
461+
{
462+
"type": "text",
463+
"value": "So does this one, with types",
464+
"position": {
465+
"start": {
466+
"line": 1,
467+
"column": 1
468+
},
469+
"end": {
470+
"line": 1,
471+
"column": 29
472+
},
473+
"indent": []
474+
}
475+
}
476+
],
477+
"position": {
478+
"start": {
479+
"line": 1,
480+
"column": 1
481+
},
482+
"end": {
483+
"line": 1,
484+
"column": 29
485+
},
486+
"indent": []
487+
}
488+
},
489+
{
490+
"type": "strong",
491+
"children": [
492+
{
493+
"type": "text",
494+
"value": "Parameters"
495+
}
496+
]
497+
},
498+
{
499+
"ordered": false,
500+
"type": "list",
501+
"children": [
502+
{
503+
"type": "listItem",
504+
"children": [
505+
{
506+
"type": "paragraph",
507+
"children": [
508+
{
509+
"type": "inlineCode",
510+
"value": "someParams"
511+
},
512+
{
513+
"type": "text",
514+
"value": " "
515+
},
516+
{
517+
"type": "strong",
518+
"children": [
519+
{
520+
"type": "text",
521+
"value": "...number"
522+
}
523+
]
524+
},
525+
{
526+
"type": "text",
527+
"value": " "
528+
},
529+
{
530+
"type": "root",
531+
"children": [],
532+
"position": {
533+
"start": {
534+
"line": 1,
535+
"column": 1
536+
},
537+
"end": {
538+
"line": 1,
539+
"column": 1
540+
}
541+
}
542+
}
543+
]
544+
}
545+
]
546+
}
547+
]
548+
},
347549
{
348550
"depth": 1,
349551
"type": "heading",

test/fixture/es6.input.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ class Sink {
3737
}
3838
}
3939

40+
/**
41+
* This function takes rest params
42+
*/
43+
function functionWithRest(...someParams) {
44+
}
45+
46+
/**
47+
* So does this one, with types
48+
*/
49+
function functionWithRestAndType(...someParams: number) {
50+
}
51+
4052
/**
4153
* This is an async method
4254
*/

test/fixture/es6.output.custom.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ This method says hello
2323

2424
This is an async method
2525

26+
# functionWithRest
27+
28+
This function takes rest params
29+
30+
**Parameters**
31+
32+
- `someParams` **...**
33+
34+
35+
# functionWithRestAndType
36+
37+
So does this one, with types
38+
39+
**Parameters**
40+
41+
- `someParams` **...number**
42+
43+
2644
# multiply
2745

2846
This function returns the number one.

0 commit comments

Comments
 (0)