Skip to content
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: mark iterator entries as such #26222

Closed
wants to merge 1 commit into from
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
23 changes: 17 additions & 6 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,15 @@ 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;

Expand Down Expand Up @@ -594,11 +603,11 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
extrasType = kArrayExtrasType;
} else if (isMapIterator(value)) {
keys = getKeys(value, ctx.showHidden);
braces = [`[${tag}] {`, '}'];
braces = setIteratorBraces('Map', tag);
formatter = formatIterator;
} else if (isSetIterator(value)) {
keys = getKeys(value, ctx.showHidden);
braces = [`[${tag}] {`, '}'];
braces = setIteratorBraces('Set', tag);
formatter = formatIterator;
} else {
noIterator = true;
Expand Down Expand Up @@ -730,10 +739,10 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
}
}
if (isMapIterator(value)) {
braces = [`[${tag || 'Map Iterator'}] {`, '}'];
braces = setIteratorBraces('Map', tag);
formatter = formatIterator;
} else if (isSetIterator(value)) {
braces = [`[${tag || 'Set Iterator'}] {`, '}'];
braces = setIteratorBraces('Set', tag);
formatter = formatIterator;
// Handle other regular objects again.
} else if (keys.length === 0) {
Expand All @@ -755,7 +764,7 @@ function formatRaw(ctx, value, recurseTimes, typedArray) {
let output;
const indentationLvl = ctx.indentationLvl;
try {
output = formatter(ctx, value, recurseTimes, keys);
output = formatter(ctx, value, recurseTimes, keys, braces);
for (i = 0; i < keys.length; i++) {
output.push(
formatProperty(ctx, value, recurseTimes, keys[i], extrasType));
Expand Down Expand Up @@ -1091,9 +1100,11 @@ function formatWeakMap(ctx, value, recurseTimes) {
return formatMapIterInner(ctx, recurseTimes, entries, kWeak);
}

function formatIterator(ctx, value, recurseTimes) {
function formatIterator(ctx, value, recurseTimes, keys, braces) {
const [entries, isKeyValue] = previewEntries(value, true);
if (isKeyValue) {
// Mark entry iterators as such.
braces[0] = braces[0].replace(/ Iterator] {$/, ' Entries] {');
return formatMapIterInner(ctx, recurseTimes, entries, kMapEntries);
}

Expand Down
18 changes: 13 additions & 5 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,15 @@ if (typeof Symbol !== 'undefined') {
{
const map = new Map([['foo', 'bar']]);
assert.strictEqual(util.inspect(map.keys()), '[Map Iterator] { \'foo\' }');
assert.strictEqual(util.inspect(map.values()), '[Map Iterator] { \'bar\' }');
const mapValues = map.values();
Object.defineProperty(mapValues, Symbol.toStringTag, { value: 'Foo' });
assert.strictEqual(
util.inspect(mapValues),
'[Foo] [Map Iterator] { \'bar\' }'
);
map.set('A', 'B!');
assert.strictEqual(util.inspect(map.entries(), { maxArrayLength: 1 }),
"[Map Iterator] { [ 'foo', 'bar' ], ... 1 more item }");
"[Map Entries] { [ 'foo', 'bar' ], ... 1 more item }");
// Make sure the iterator doesn't get consumed.
const keys = map.keys();
assert.strictEqual(util.inspect(keys), "[Map Iterator] { 'foo', 'A' }");
Expand All @@ -995,10 +1000,13 @@ if (typeof Symbol !== 'undefined') {
const aSet = new Set([1, 3]);
assert.strictEqual(util.inspect(aSet.keys()), '[Set Iterator] { 1, 3 }');
assert.strictEqual(util.inspect(aSet.values()), '[Set Iterator] { 1, 3 }');
assert.strictEqual(util.inspect(aSet.entries()),
'[Set Iterator] { [ 1, 1 ], [ 3, 3 ] }');
const setEntries = aSet.entries();
Object.defineProperty(setEntries, Symbol.toStringTag, { value: 'Foo' });
assert.strictEqual(util.inspect(setEntries),
'[Foo] [Set Entries] { [ 1, 1 ], [ 3, 3 ] }');
// Make sure the iterator doesn't get consumed.
const keys = aSet.keys();
Object.defineProperty(keys, Symbol.toStringTag, { value: null });
assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }');
assert.strictEqual(util.inspect(keys), '[Set Iterator] { 1, 3 }');
keys.extra = true;
Expand Down Expand Up @@ -1610,7 +1618,7 @@ assert.strictEqual(util.inspect('"\'${a}'), "'\"\\'${a}'");
[{ a: 5 }, '{ a: 5 }'],
[new Set([1, 2]), 'Set { 1, 2 }'],
[new Map([[1, 2]]), 'Map { 1 => 2 }'],
[new Set([1, 2]).entries(), '[Set Iterator] { [ 1, 1 ], [ 2, 2 ] }'],
[new Set([1, 2]).entries(), '[Set Entries] { [ 1, 1 ], [ 2, 2 ] }'],
[new Map([[1, 2]]).keys(), '[Map Iterator] { 1 }'],
[new Date(2000), '1970-01-01T00:00:02.000Z'],
[new Uint8Array(2), 'Uint8Array [ 0, 0 ]'],
Expand Down