Skip to content
This repository has been archived by the owner on Dec 4, 2018. It is now read-only.

Commit

Permalink
Fix: Allow whitespace in optional param with default value (fixes #141)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswong committed Jan 13, 2016
1 parent 165fc18 commit ae07aa8
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/doctrine.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,25 +340,37 @@
}

if (useBrackets) {


skipWhiteSpace(last);
// do we have a default value for this?
if (source.charCodeAt(index) === 0x3D /* '=' */) {
// consume the '='' symbol
name += advance();
skipWhiteSpace(last);

var ch;
var bracketDepth = 1;
// scan in the default value
while (index < last) {
if (source.charCodeAt(index) === 0x5B /* '[' */) {
ch = source.charCodeAt(index);

if (esutils.code.isWhiteSpace(ch)) {
skipWhiteSpace(last);
ch = source.charCodeAt(index);
}

if (ch === 0x5B /* '[' */) {
bracketDepth++;
} else if (source.charCodeAt(index) === 0x5D /* ']' */ &&
} else if (ch === 0x5D /* ']' */ &&
--bracketDepth === 0) {
break;
}

name += advance();
}
}

skipWhiteSpace(last);

if (index >= last || source.charCodeAt(index) !== 0x5D /* ']' */) {
// we never found a closing ']'
return null;
Expand Down
45 changes: 45 additions & 0 deletions test/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -1947,6 +1947,28 @@ describe('optional params', function() {
});
});

it('success 4', function() {
doctrine.parse(
["/**", " * @param {String=} [val = abc] some description", " */"].join('\n'),
{ unwrap: true, sloppy: true}
).should.eql({
"description": "",
"tags": [{
"title": "param",
"description": "some description",
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "String"
}
},
"name": "val",
"default": "abc"
}]
});
});

it('default array', function() {
doctrine.parse(
["/**", " * @param {String} [val=['foo']] some description", " */"].join('\n'),
Expand All @@ -1969,6 +1991,29 @@ describe('optional params', function() {
});
});


it('default array within white spaces', function() {
doctrine.parse(
["/**", " * @param {String} [val = [ 'foo' ]] some description", " */"].join('\n'),
{ unwrap: true, sloppy: true}
).should.eql({
"description": "",
"tags": [{
"title": "param",
"description": "some description",
"type": {
"type": "OptionalType",
"expression": {
"type": "NameExpression",
"name": "String"
}
},
"name": "val",
"default": "['foo']"
}]
});
});

it('line numbers', function() {
var res = doctrine.parse(
[
Expand Down

0 comments on commit ae07aa8

Please sign in to comment.