re-enable default values that are object literals (#607)

This commit is contained in:
Jeff Williams 2014-03-18 10:00:16 -07:00
parent 3ec52a3a35
commit cd6c89ec10
4 changed files with 41 additions and 17 deletions

View File

@ -118,6 +118,8 @@ exports.makeGlobalNode = function() {
// TODO: docs // TODO: docs
var nodeToString = exports.nodeToString = function(node) { var nodeToString = exports.nodeToString = function(node) {
var tempObject;
var str = ''; var str = '';
switch (node.type) { switch (node.type) {
@ -151,6 +153,22 @@ var nodeToString = exports.nodeToString = function(node) {
} }
break; break;
case Syntax.ObjectExpression:
tempObject = {};
node.properties.forEach(function(prop) {
var key = prop.key.name;
// preserve literal values so that the JSON form shows the correct type
if (prop.value.type === Syntax.Literal) {
tempObject[key] = prop.value.value;
}
else {
tempObject[key] = nodeToString(prop);
}
});
str = JSON.stringify(tempObject);
break;
case Syntax.ThisExpression: case Syntax.ThisExpression:
str = 'this'; str = 'this';
break; break;

View File

@ -288,6 +288,8 @@ exports.defineTags = function(dictionary) {
dictionary.defineTag('default', { dictionary.defineTag('default', {
onTagged: function(doclet, tag) { onTagged: function(doclet, tag) {
var astnode = require('jsdoc/src/astnode');
var type; var type;
var value; var value;
@ -298,16 +300,20 @@ exports.defineTags = function(dictionary) {
type = doclet.meta.code.type; type = doclet.meta.code.type;
value = doclet.meta.code.value; value = doclet.meta.code.value;
if (type === Syntax.Literal) { switch(type) {
case Syntax.Literal:
doclet.defaultvalue = String(value); doclet.defaultvalue = String(value);
} break;
// TODO: reenable the changes for https://github.com/jsdoc3/jsdoc/pull/419
/* case Syntax.ObjectExpression:
else if (doclet.meta.code.type === 'OBJECTLIT') { doclet.defaultvalue = astnode.nodeToString(doclet.meta.code.node);
doclet.defaultvalue = String(doclet.meta.code.node.toSource());
doclet.defaultvaluetype = 'object'; doclet.defaultvaluetype = 'object';
break;
default:
// do nothing
break;
} }
*/
} }
} }
}) })

View File

@ -36,12 +36,13 @@ var header = getHeaders(request);
/** /**
@default @default
*/ */
var obj = { value_a : 'a', value_b : 'b' }; var obj = {valueA: 'a', valueB: false, valueC: 7};
/** /**
* @default * @default
*/ */
var multilineObject = { var multilineObject = {
value_a : 'a', valueA : 'a',
value_b : 'b' valueB : false,
valueC : 7
}; };

View File

@ -39,14 +39,13 @@ describe("@default tag", function() {
expect(header.defaultvalue).toBeUndefined(); expect(header.defaultvalue).toBeUndefined();
}); });
// TODO: reenable the changes for https://github.com/jsdoc3/jsdoc/pull/419 it('When symbol has a @default tag with an object, the doclet\'s defaultValue property should contain the stringified object', function() {
xit('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}';
var expected_value = "{value_a: 'a', value_b: 'b'}";
expect(obj.defaultvalue).toEqual(expected_value); expect(obj.defaultvalue).toEqual(expected_value);
}); });
xit('When symbol has a @default tag with a multiline object, the doclet\'s defaultValue property should contain the properly stringified object', function() { 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 = "{value_a: 'a', value_b: 'b'}"; var expected_value = '{"valueA":"a","valueB":false,"valueC":7}';
expect(obj.defaultvalue).toEqual(expected_value); expect(obj.defaultvalue).toEqual(expected_value);
}); });