Skip to content

Commit

Permalink
handle default values that are array literals (#604)
Browse files Browse the repository at this point in the history
plus some test cleanup
  • Loading branch information
hegemonic committed Mar 18, 2014
1 parent cd6c89e commit 8128dc5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/jsdoc/schema.js
Expand Up @@ -269,7 +269,7 @@ var DOCLET_SCHEMA = exports.DOCLET_SCHEMA = {
defaultvaluetype: {
type: STRING,
optional: true,
enum: [OBJECT]
enum: [OBJECT, ARRAY]
},
// is usage of this symbol deprecated?
deprecated: {
Expand Down
15 changes: 15 additions & 0 deletions lib/jsdoc/src/astnode.js
Expand Up @@ -123,6 +123,21 @@ var nodeToString = exports.nodeToString = function(node) {
var str = '';

switch (node.type) {
case Syntax.ArrayExpression:
tempObject = [];
node.elements.forEach(function(el, i) {
// preserve literal values so that the JSON form shows the correct type
if (el.type === Syntax.Literal) {
tempObject[i] = el.value;
}
else {
tempObject[i] = nodeToString(el);
}
});

str = JSON.stringify(tempObject);
break;

case Syntax.AssignmentExpression:
str = nodeToString(node.left);
break;
Expand Down
5 changes: 5 additions & 0 deletions lib/jsdoc/tag/dictionary/definitions.js
Expand Up @@ -301,6 +301,11 @@ exports.defineTags = function(dictionary) {
value = doclet.meta.code.value;

switch(type) {
case Syntax.ArrayExpression:
doclet.defaultvalue = astnode.nodeToString(doclet.meta.code.node);
doclet.defaultvaluetype = 'array';
break;

case Syntax.Literal:
doclet.defaultvalue = String(value);
break;
Expand Down
4 changes: 2 additions & 2 deletions templates/default/tmpl/details.tmpl
Expand Up @@ -3,8 +3,8 @@ var data = obj;
var self = this;
var defaultObjectClass = '';

// Check if the default value is an object, if so, apply code highlighting
if (data.defaultvalue && data.defaultvaluetype === 'object') {
// Check if the default value is an object or array; if so, apply code highlighting
if (data.defaultvalue && (data.defaultvaluetype === 'object' || data.defaultvaluetype === 'array')) {
data.defaultvalue = "<pre class=\"prettyprint\"><code>" + data.defaultvalue + "</code></pre>";
defaultObjectClass = ' class="object-value"';
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/defaulttag.js
Expand Up @@ -46,3 +46,6 @@ var multilineObject = {
valueB : false,
valueC : 7
};

/** @default */
var arr = ['foo', true, 19];
24 changes: 18 additions & 6 deletions test/specs/tags/defaulttag.js
Expand Up @@ -10,6 +10,7 @@ describe("@default tag", function() {
var header = (docSet.getByLongname('header') || [])[0];
var obj = docSet.getByLongname('obj')[0];
var multilineObject = docSet.getByLongname('multilineObject')[0];
var arr = docSet.getByLongname('arr')[0];

it('When symbol set to null has a @default tag with no text, the doclet\'s defaultValue property should be: null', function() {
expect(request.defaultvalue).toBe('null');
Expand Down Expand Up @@ -39,14 +40,25 @@ describe("@default tag", function() {
expect(header.defaultvalue).toBeUndefined();
});

it('When symbol has a @default tag with an object, the doclet\'s defaultValue property should contain the stringified object', function() {
var expected_value = '{"valueA":"a","valueB":false,"valueC":7}';
expect(obj.defaultvalue).toEqual(expected_value);
it('When symbol has a @default tag with an object, the doclet should contain the stringified object', function() {
var testObj = { valueA: 'a', valueB: false, valueC: 7};
expect(obj.defaultvalue).toBe( JSON.stringify(testObj) );
expect(obj.defaultvaluetype).toBe('object');
});

it('When symbol has a @default tag with a multiline object, the doclet\'s defaultValue property should contain the properly stringified object', function() {
var expected_value = '{"valueA":"a","valueB":false,"valueC":7}';
expect(obj.defaultvalue).toEqual(expected_value);
it('When symbol has a @default tag with a multiline object, the doclet should contain the stringified object', function() {
var testObj = {
valueA: 'a',
valueB: false,
valueC: 7
};
expect(obj.defaultvalue).toBe( JSON.stringify(testObj) );
expect(obj.defaultvaluetype).toBe('object');
});

it('When symbol has a @default tag with an array, the doclet should contain the stringified array', function() {
var testArray = ['foo', true, 19];
expect(arr.defaultvalue).toBe( JSON.stringify(testArray) );
expect(arr.defaultvaluetype).toBe('array');
});
});

0 comments on commit 8128dc5

Please sign in to comment.