diff options
author | Gus Caplan <me@gus.host> | 2017-11-11 10:35:01 -0600 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2017-12-01 21:00:33 +0100 |
commit | 31e0dbc0c700e7bb8fa453258ba0233975ece575 (patch) | |
tree | 8caaad41ef6275264c4d44722de64746be89c17a /lib/util.js | |
parent | f6926d5d002273100a24a33a05ceb000eaae9fbe (diff) | |
download | android-node-v8-31e0dbc0c700e7bb8fa453258ba0233975ece575.tar.gz android-node-v8-31e0dbc0c700e7bb8fa453258ba0233975ece575.tar.bz2 android-node-v8-31e0dbc0c700e7bb8fa453258ba0233975ece575.zip |
util: use @@toStringTag
uses @@toStringTag when creating the "tag" for an inspected value
PR-URL: https://github.com/nodejs/node/pull/16956
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Timothy Gu <timothygu99@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/util.js')
-rw-r--r-- | lib/util.js | 52 |
1 files changed, 29 insertions, 23 deletions
diff --git a/lib/util.js b/lib/util.js index 058b5ed6bd..da8d1f0f9b 100644 --- a/lib/util.js +++ b/lib/util.js @@ -56,7 +56,7 @@ const { const { customInspectSymbol, deprecate, - getConstructorOf, + getIdentificationOf, isError, promisify, join @@ -429,9 +429,15 @@ function formatValue(ctx, value, recurseTimes, ln) { } const keyLength = keys.length + symbols.length; - const constructor = getConstructorOf(value); - const ctorName = constructor && constructor.name ? - `${constructor.name} ` : ''; + + const { constructor, tag } = getIdentificationOf(value); + var prefix = ''; + if (constructor && tag && constructor !== tag) + prefix = `${constructor} [${tag}] `; + else if (constructor) + prefix = `${constructor} `; + else if (tag) + prefix = `[${tag}] `; var base = ''; var formatter = formatObject; @@ -444,28 +450,28 @@ function formatValue(ctx, value, recurseTimes, ln) { noIterator = false; if (Array.isArray(value)) { // Only set the constructor for non ordinary ("Array [...]") arrays. - braces = [`${ctorName === 'Array ' ? '' : ctorName}[`, ']']; + braces = [`${prefix === 'Array ' ? '' : prefix}[`, ']']; if (value.length === 0 && keyLength === 0) return `${braces[0]}]`; formatter = formatArray; } else if (isSet(value)) { if (value.size === 0 && keyLength === 0) - return `${ctorName}{}`; - braces = [`${ctorName}{`, '}']; + return `${prefix}{}`; + braces = [`${prefix}{`, '}']; formatter = formatSet; } else if (isMap(value)) { if (value.size === 0 && keyLength === 0) - return `${ctorName}{}`; - braces = [`${ctorName}{`, '}']; + return `${prefix}{}`; + braces = [`${prefix}{`, '}']; formatter = formatMap; } else if (isTypedArray(value)) { - braces = [`${ctorName}[`, ']']; + braces = [`${prefix}[`, ']']; formatter = formatTypedArray; } else if (isMapIterator(value)) { - braces = ['MapIterator {', '}']; + braces = [`[${tag}] {`, '}']; formatter = formatMapIterator; } else if (isSetIterator(value)) { - braces = ['SetIterator {', '}']; + braces = [`[${tag}] {`, '}']; formatter = formatSetIterator; } else { // Check for boxed strings with valueOf() @@ -491,12 +497,13 @@ function formatValue(ctx, value, recurseTimes, ln) { } if (noIterator) { braces = ['{', '}']; - if (ctorName === 'Object ') { + if (prefix === 'Object ') { // Object fast path if (keyLength === 0) return '{}'; } else if (typeof value === 'function') { - const name = `${constructor.name}${value.name ? `: ${value.name}` : ''}`; + const name = + `${constructor || tag}${value.name ? `: ${value.name}` : ''}`; if (keyLength === 0) return ctx.stylize(`[${name}]`, 'special'); base = ` [${name}]`; @@ -523,16 +530,16 @@ function formatValue(ctx, value, recurseTimes, ln) { // Can't do the same for DataView because it has a non-primitive // .buffer property that we need to recurse for. if (keyLength === 0) - return ctorName + + return prefix + `{ byteLength: ${formatNumber(ctx.stylize, value.byteLength)} }`; - braces[0] = `${ctorName}{`; + braces[0] = `${prefix}{`; keys.unshift('byteLength'); } else if (isDataView(value)) { - braces[0] = `${ctorName}{`; + braces[0] = `${prefix}{`; // .buffer goes last, it's not a primitive like the others. keys.unshift('byteLength', 'byteOffset', 'buffer'); } else if (isPromise(value)) { - braces[0] = `${ctorName}{`; + braces[0] = `${prefix}{`; formatter = formatPromise; } else { // Check boxed primitives other than string with valueOf() @@ -560,22 +567,21 @@ function formatValue(ctx, value, recurseTimes, ln) { } else if (keyLength === 0) { if (isExternal(value)) return ctx.stylize('[External]', 'special'); - return `${ctorName}{}`; + return `${prefix}{}`; } else { - braces[0] = `${ctorName}{`; + braces[0] = `${prefix}{`; } } } // Using an array here is actually better for the average case than using - // a Set. `seen` will only check for the depth and will never grow to large. + // a Set. `seen` will only check for the depth and will never grow too large. if (ctx.seen.indexOf(value) !== -1) return ctx.stylize('[Circular]', 'special'); if (recurseTimes != null) { if (recurseTimes < 0) - return ctx.stylize(`[${constructor ? constructor.name : 'Object'}]`, - 'special'); + return ctx.stylize(`[${constructor || tag || 'Object'}]`, 'special'); recurseTimes -= 1; } |