Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rest parameters syntax for multiple params #158

Merged
merged 1 commit into from
Oct 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/syntax/index.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,8 @@ ATTRS[RENDERED] = {
<ul>
<li>`[name]` &mdash; optional parameter</li>
<li>`[name=foo]` &mdash; default value is foo</li>
<li>`name*` &mdash; placeholder for 1..n args</li>
<li>`[name]*` &mdash; placeholder for 0..n args</li>
<li>`...name` &mdash; placeholder for 1..n args</li>
<li>`[...name]` &mdash; placeholder for 0..n args</li>
</ul>

<p>As shown in the example, you can also nest `@param` tags.
Expand Down
10 changes: 9 additions & 1 deletion lib/docparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ YUI.add('docparser', function (Y) {
// "type": "string",
// "optional": true, // [surroundedbybrackets]
// "optdefault": "if specified, this is always string to avoid syntax errors @TODO",
// "multiple": true // endswith*
// "multiple": true // endswith* or ...startswith
// }
// ],
// @param {type} name description -or-
Expand Down Expand Up @@ -312,6 +312,14 @@ YUI.add('docparser', function (Y) {
}
}

// This should run after the check for optional parameters
// and before the check for child parameters
// because the signature for 0..n params is [...args]
if (name.substr(0, 3) === '...') {
multiple = true;
name = name.substr(3);
}

// parse object.prop, indicating a child property for object
if (name.indexOf('.') > -1) {
match = name.split('.');
Expand Down
16 changes: 16 additions & 0 deletions tests/input/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@
* @returns something without a type
*/

/**
test alternative 1..n param with ...args

@method testrestparam1n
@param {String} ...multiple my desc
@returns something without a type
**/

/**
test alternative 0..n param with ...args

@method testrestparam0n
@param {String} [...multiple] my desc
@returns something without a type
**/

/**
Test newlines before descriptions.

Expand Down
20 changes: 17 additions & 3 deletions tests/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ suite.add(new YUITest.TestCase({
Assert.areSame('String', item2["return"].type, 'Type should not be missing');
},
'test: parameter parsing': function () {
var item, item2;
var item, item2, item3, item4;
item = this.findByName('testoptional', 'myclass');
Assert.isArray(item.params, 'Params should be an array');
Assert.areSame(5, item.params.length, 'Failed to parse all 5 parameters');
Expand Down Expand Up @@ -294,6 +294,20 @@ suite.add(new YUITest.TestCase({
Assert.isTrue(item2.params[0].multiple, 'Multiple not set');
Assert.isUndefined(item2["return"].type, 'Type should be missing');

item3 = this.findByName('testrestparam0n', 'myclass');
Assert.isArray(item3.params, 'Params should be an array');
Assert.areSame(1, item3.params.length, 'Failed to parse all 5 parameters');
Assert.isTrue(item3.params[0].optional, 'Optional not set');
Assert.isTrue(item3.params[0].multiple, 'Multiple not set');
Assert.isUndefined(item3['return'].type, 'Type should be missing');

item4 = this.findByName('testrestparam1n', 'myclass');
Assert.isArray(item4.params, 'Params should be an array');
Assert.areSame(1, item4.params.length, 'Failed to parse all 5 parameters');
Assert.isUndefined(item4.params[0].optional, 'Optional should not be set');
Assert.isTrue(item4.params[0].multiple, 'Multiple not set');
Assert.isUndefined(item4['return'].type, 'Type should be missing');

item = this.findByName('testNewlineBeforeDescription', 'myclass');
Assert.isArray(item.params, 'Params should be an array.');
Assert.areSame(2, item.params.length, 'Should parse two params.');
Expand All @@ -315,10 +329,10 @@ suite.add(new YUITest.TestCase({
'test: indented return description': function () {
var item = this.findByName('testNewlineBeforeDescription', 'myclass');

Assert.areSame('Boolean', item.return.type, 'Type should be correct.');
Assert.areSame('Boolean', item['return'].type, 'Type should be correct.');
Assert.areSame(
'Sometimes true, sometimes false.\nNobody knows!',
item.return.description,
item['return'].description,
'Description indentation should be normalized to the first line.'
);
},
Expand Down