Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,20 @@ function formatValue(ctx, value, recurseTimes, typedArray) {
return formatRaw(ctx, value, recurseTimes, typedArray);
}

function setIteratorBraces(type, tag) {
if (tag !== `${type} Iterator`) {
if (tag !== '')
tag += '] [';
tag += `${type} Iterator`;
}
return [`[${tag}] {`, '}'];
}

function formatRaw(ctx, value, recurseTimes, typedArray) {
let keys;

const constructor = getConstructorName(value, ctx);
let tag = value[Symbol.toStringTag];
if (typeof tag !== 'string')
// Only list the tag in case it's non-enumerable / not an own property.
// Otherwise we'd print this twice.
if (typeof tag !== 'string' ||
tag !== '' &&
(ctx.showHidden ? hasOwnProperty : propertyIsEnumerable)(
value, Symbol.toStringTag
)) {
tag = '';
}
let base = '';
let formatter = getEmptyFormatArray;
let braces;
Expand Down Expand Up @@ -623,11 +621,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
extrasType = kArrayExtrasType;
} else if (isMapIterator(value)) {
keys = getKeys(value, ctx.showHidden);
braces = setIteratorBraces('Map', tag);
braces = getIteratorBraces('Map', tag);
formatter = formatIterator;
} else if (isSetIterator(value)) {
keys = getKeys(value, ctx.showHidden);
braces = setIteratorBraces('Set', tag);
braces = getIteratorBraces('Set', tag);
formatter = formatIterator;
} else {
noIterator = true;
Expand Down Expand Up @@ -747,10 +745,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
}
}
if (isMapIterator(value)) {
braces = setIteratorBraces('Map', tag);
braces = getIteratorBraces('Map', tag);
formatter = formatIterator;
} else if (isSetIterator(value)) {
braces = setIteratorBraces('Set', tag);
braces = getIteratorBraces('Set', tag);
formatter = formatIterator;
// Handle other regular objects again.
} else if (keys.length === 0) {
Expand Down Expand Up @@ -811,6 +809,15 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
return res;
}

function getIteratorBraces(type, tag) {
if (tag !== `${type} Iterator`) {
if (tag !== '')
tag += '] [';
tag += `${type} Iterator`;
}
return [`[${tag}] {`, '}'];
}

function formatError(err, constructor, tag, ctx) {
// TODO(BridgeAR): Always show the error code if present.
let stack = err.stack || ErrorPrototype.toString(err);
Expand Down
16 changes: 14 additions & 2 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1264,8 +1264,20 @@ util.inspect(process);

{
// @@toStringTag
assert.strictEqual(util.inspect({ [Symbol.toStringTag]: 'a' }),
"Object [a] { [Symbol(Symbol.toStringTag)]: 'a' }");
const obj = { [Symbol.toStringTag]: 'a' };
assert.strictEqual(
util.inspect(obj),
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
);
Object.defineProperty(obj, Symbol.toStringTag, {
value: 'a',
enumerable: false
});
assert.strictEqual(util.inspect(obj), 'Object [a] {}');
assert.strictEqual(
util.inspect(obj, { showHidden: true }),
"{ [Symbol(Symbol.toStringTag)]: 'a' }"
);

class Foo {
constructor() {
Expand Down