From cd6c89ec10214ef97b4c8fd2173cf88f76fcb94e Mon Sep 17 00:00:00 2001 From: Jeff Williams Date: Tue, 18 Mar 2014 10:00:16 -0700 Subject: [PATCH] re-enable default values that are object literals (#607) --- lib/jsdoc/src/astnode.js | 18 ++++++++++++++++++ lib/jsdoc/tag/dictionary/definitions.js | 24 +++++++++++++++--------- test/fixtures/defaulttag.js | 7 ++++--- test/specs/tags/defaulttag.js | 9 ++++----- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/lib/jsdoc/src/astnode.js b/lib/jsdoc/src/astnode.js index 2a5b0515..36ecf5ce 100644 --- a/lib/jsdoc/src/astnode.js +++ b/lib/jsdoc/src/astnode.js @@ -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; diff --git a/lib/jsdoc/tag/dictionary/definitions.js b/lib/jsdoc/tag/dictionary/definitions.js index e069c43c..029896a0 100644 --- a/lib/jsdoc/tag/dictionary/definitions.js +++ b/lib/jsdoc/tag/dictionary/definitions.js @@ -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) { - doclet.defaultvalue = String(value); + switch(type) { + case Syntax.Literal: + doclet.defaultvalue = String(value); + break; + + case Syntax.ObjectExpression: + doclet.defaultvalue = astnode.nodeToString(doclet.meta.code.node); + doclet.defaultvaluetype = 'object'; + break; + + default: + // do nothing + break; } - // 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()); - doclet.defaultvaluetype = 'object'; - } - */ } } }) diff --git a/test/fixtures/defaulttag.js b/test/fixtures/defaulttag.js index 996033a7..989bfada 100644 --- a/test/fixtures/defaulttag.js +++ b/test/fixtures/defaulttag.js @@ -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 }; diff --git a/test/specs/tags/defaulttag.js b/test/specs/tags/defaulttag.js index c36e0719..d0f6d47e 100644 --- a/test/specs/tags/defaulttag.js +++ b/test/specs/tags/defaulttag.js @@ -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); });