mirror of
https://github.com/jsdoc/jsdoc.git
synced 2025-12-08 19:46:11 +00:00
dumper should use a new circular-refs cache for every call
This commit is contained in:
parent
522a377845
commit
d3cff64ec0
@ -8,53 +8,58 @@
|
||||
|
||||
var util = require('util');
|
||||
|
||||
var seenItems = [];
|
||||
function seen(object) {
|
||||
if (seenItems.indexOf(object) !== -1) {
|
||||
function ObjectWalker() {
|
||||
this.seenItems = [];
|
||||
}
|
||||
|
||||
ObjectWalker.prototype.seen = function(object) {
|
||||
if (this.seenItems.indexOf(object) !== -1) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
// some objects are unwalkable, like Java native objects
|
||||
function isUnwalkable(o) {
|
||||
ObjectWalker.prototype.isUnwalkable = function(o) {
|
||||
return (o && typeof o === 'object' && typeof o.constructor === 'undefined');
|
||||
}
|
||||
};
|
||||
|
||||
function isFunction(o) {
|
||||
ObjectWalker.prototype.isFunction = function(o) {
|
||||
return (o && typeof o === 'function' || o instanceof Function);
|
||||
}
|
||||
};
|
||||
|
||||
function isObject(o) {
|
||||
ObjectWalker.prototype.isObject = function(o) {
|
||||
return o && o instanceof Object ||
|
||||
(o && typeof o.constructor !== 'undefined' && o.constructor.name === 'Object');
|
||||
}
|
||||
};
|
||||
|
||||
function checkCircularRefs(o, func) {
|
||||
if ( seen(o) ) {
|
||||
ObjectWalker.prototype.checkCircularRefs = function(o, func) {
|
||||
if ( this.seen(o) ) {
|
||||
return '<CircularRef>';
|
||||
}
|
||||
else {
|
||||
seenItems.push(o);
|
||||
this.seenItems.push(o);
|
||||
return func(o);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function walk(o) {
|
||||
ObjectWalker.prototype.walk = function(o) {
|
||||
var result;
|
||||
|
||||
if ( isUnwalkable(o) ) {
|
||||
var self = this;
|
||||
|
||||
if ( this.isUnwalkable(o) ) {
|
||||
result = '<Object>';
|
||||
}
|
||||
else if ( o === undefined ) {
|
||||
result = 'undefined';
|
||||
}
|
||||
else if ( Array.isArray(o) ) {
|
||||
result = checkCircularRefs(o, function(arr) {
|
||||
result = this.checkCircularRefs(o, function(arr) {
|
||||
var newArray = [];
|
||||
arr.forEach(function(item) {
|
||||
newArray.push( walk(item) );
|
||||
newArray.push( self.walk(item) );
|
||||
});
|
||||
|
||||
return newArray;
|
||||
@ -69,14 +74,14 @@ function walk(o) {
|
||||
else if ( util.isError(o) ) {
|
||||
result = { message: o.message };
|
||||
}
|
||||
else if ( isFunction(o) ) {
|
||||
else if ( this.isFunction(o) ) {
|
||||
result = '<Function' + (o.name ? ' ' + o.name : '') + '>';
|
||||
}
|
||||
else if ( isObject(o) && o !== null ) {
|
||||
result = checkCircularRefs(o, function(obj) {
|
||||
else if ( this.isObject(o) && o !== null ) {
|
||||
result = this.checkCircularRefs(o, function(obj) {
|
||||
var newObj = {};
|
||||
Object.keys(obj).forEach(function(key) {
|
||||
newObj[key] = walk(obj[key]);
|
||||
newObj[key] = self.walk(obj[key]);
|
||||
});
|
||||
|
||||
return newObj;
|
||||
@ -88,11 +93,11 @@ function walk(o) {
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {*} object
|
||||
*/
|
||||
exports.dump = function(object) {
|
||||
return JSON.stringify(walk(object), null, 4);
|
||||
return JSON.stringify(new ObjectWalker().walk(object), null, 4);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user