mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
handle sparse arrays correctly in nodeToString (#749)
This commit is contained in:
parent
6328336d1d
commit
e093716ebe
@ -138,8 +138,13 @@ var nodeToString = exports.nodeToString = function(node) {
|
||||
case Syntax.ArrayExpression:
|
||||
tempObject = [];
|
||||
node.elements.forEach(function(el, i) {
|
||||
// handle sparse arrays. use `null` to represent missing values, consistent with
|
||||
// JSON.stringify([,]).
|
||||
if (!el) {
|
||||
tempObject[i] = null;
|
||||
}
|
||||
// preserve literal values so that the JSON form shows the correct type
|
||||
if (el.type === Syntax.Literal) {
|
||||
else if (el.type === Syntax.Literal) {
|
||||
tempObject[i] = el.value;
|
||||
}
|
||||
else {
|
||||
|
||||
@ -13,6 +13,7 @@ describe('jsdoc/src/astnode', function() {
|
||||
};
|
||||
|
||||
// create the AST nodes we'll be testing
|
||||
var arrayExpression = esprima.parse('[,]').body[0].expression;
|
||||
var assignmentExpression = esprima.parse('foo = 1;').body[0].expression;
|
||||
var binaryExpression = esprima.parse('foo & foo;').body[0].expression;
|
||||
var functionDeclaration1 = esprima.parse('function foo() {}').body[0];
|
||||
@ -793,6 +794,10 @@ describe('jsdoc/src/astnode', function() {
|
||||
});
|
||||
|
||||
describe('nodeToString', function() {
|
||||
it('should return `[null]` for the sparse array `[,]`', function() {
|
||||
expect( astnode.nodeToString(arrayExpression) ).toBe('[null]');
|
||||
});
|
||||
|
||||
it('should return the variable name for assignment expressions', function() {
|
||||
expect( astnode.nodeToString(assignmentExpression) ).toBe('foo');
|
||||
});
|
||||
@ -848,7 +853,7 @@ describe('jsdoc/src/astnode', function() {
|
||||
});
|
||||
|
||||
it('should return the variable name for variable declarators', function() {
|
||||
expect ( astnode.nodeToString(variableDeclarator1) ).toBe('foo');
|
||||
expect( astnode.nodeToString(variableDeclarator1) ).toBe('foo');
|
||||
});
|
||||
|
||||
it('should return an empty string for all other nodes', function() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user