mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
handle default values that are array literals (#604)
plus some test cleanup
This commit is contained in:
parent
cd6c89ec10
commit
8128dc5335
@ -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: {
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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"';
|
||||
}
|
||||
|
||||
3
test/fixtures/defaulttag.js
vendored
3
test/fixtures/defaulttag.js
vendored
@ -46,3 +46,6 @@ var multilineObject = {
|
||||
valueB : false,
|
||||
valueC : 7
|
||||
};
|
||||
|
||||
/** @default */
|
||||
var arr = ['foo', true, 19];
|
||||
|
||||
@ -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');
|
||||
@ -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');
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user