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
var nodeToString = exports.nodeToString = function(node) {
var tempObject;
var str = '';
switch (node.type) {
@ -151,6 +153,22 @@ var nodeToString = exports.nodeToString = function(node) {
}
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:
str = 'this';
break;

View File

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

View File

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

View File

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