-
Notifications
You must be signed in to change notification settings - Fork 29.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
util: display constructor name when inspecting objects #1935
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,22 @@ function arrayToHash(array) { | |
} | ||
|
||
|
||
function getConstructorOf(obj) { | ||
while (obj) { | ||
var descriptor = Object.getOwnPropertyDescriptor(obj, 'constructor'); | ||
if (descriptor !== undefined && | ||
typeof descriptor.value === 'function' && | ||
descriptor.value.name !== '') { | ||
return descriptor.value; | ||
} | ||
|
||
obj = Object.getPrototypeOf(obj); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
function inspectPromise(p) { | ||
Debug = Debug || require('vm').runInDebugContext('Debug'); | ||
var mirror = Debug.MakeMirror(p, true); | ||
|
@@ -260,14 +276,17 @@ function formatValue(ctx, value, recurseTimes) { | |
} | ||
} | ||
|
||
var constructor = getConstructorOf(value); | ||
var base = '', empty = false, braces, formatter; | ||
|
||
if (Array.isArray(value)) { | ||
if (constructor === Array) | ||
constructor = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why it is nulled out here and below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Null constructors aren't displayed. We explicitly null out for |
||
braces = ['[', ']']; | ||
empty = value.length === 0; | ||
formatter = formatArray; | ||
} else if (value instanceof Set) { | ||
braces = ['Set {', '}']; | ||
braces = ['{', '}']; | ||
// With `showHidden`, `length` will display as a hidden property for | ||
// arrays. For consistency's sake, do the same for `size`, even though this | ||
// property isn't selected by Object.getOwnPropertyNames(). | ||
|
@@ -276,7 +295,7 @@ function formatValue(ctx, value, recurseTimes) { | |
empty = value.size === 0; | ||
formatter = formatSet; | ||
} else if (value instanceof Map) { | ||
braces = ['Map {', '}']; | ||
braces = ['{', '}']; | ||
// Ditto. | ||
if (ctx.showHidden) | ||
keys.unshift('size'); | ||
|
@@ -286,9 +305,11 @@ function formatValue(ctx, value, recurseTimes) { | |
// Only create a mirror if the object superficially looks like a Promise. | ||
var promiseInternals = value instanceof Promise && inspectPromise(value); | ||
if (promiseInternals) { | ||
braces = ['Promise {', '}']; | ||
braces = ['{', '}']; | ||
formatter = formatPromise; | ||
} else { | ||
if (constructor === Object) | ||
constructor = null; | ||
braces = ['{', '}']; | ||
empty = true; // No other data than keys. | ||
formatter = formatObject; | ||
|
@@ -336,6 +357,10 @@ function formatValue(ctx, value, recurseTimes) { | |
base = ' ' + '[Boolean: ' + formatted + ']'; | ||
} | ||
|
||
// Add constructor name if available | ||
if (base === '' && constructor) | ||
braces[0] = constructor.name + ' ' + braces[0]; | ||
|
||
if (empty === true) { | ||
return braces[0] + base + braces[1]; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there is no constructor this just returns undefined I guess..?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
I modified this so we explicitly return
null
instead of relying on the implicitundefined
.