Skip to content

Commit 2988330

Browse files
committed
util: mark special entries as such
This adds the color code to special entries if colors are active.
1 parent a04f2f7 commit 2988330

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

lib/util.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,14 @@ function formatValue(ctx, value, recurseTimes) {
758758
if (ctx.showHidden) {
759759
formatter = formatWeakSet;
760760
} else {
761-
extra = '<items unknown>';
761+
extra = ctx.stylize('<items unknown>', 'special');
762762
}
763763
} else if (isWeakMap(value)) {
764764
braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`;
765765
if (ctx.showHidden) {
766766
formatter = formatWeakMap;
767767
} else {
768-
extra = '<items unknown>';
768+
extra = ctx.stylize('<items unknown>', 'special');
769769
}
770770
} else if (types.isModuleNamespaceObject(value)) {
771771
braces[0] = `[${tag}] {`;
@@ -1210,14 +1210,18 @@ function formatPromise(ctx, value, recurseTimes, keys) {
12101210
let output;
12111211
const [state, result] = getPromiseDetails(value);
12121212
if (state === kPending) {
1213-
output = ['<pending>'];
1213+
output = [ctx.stylize('<pending>', 'special')];
12141214
} else {
12151215
// Using `formatValue` is correct here without the need to fix the
12161216
// indentation level.
12171217
ctx.indentationLvl += 2;
12181218
const str = formatValue(ctx, result, recurseTimes);
12191219
ctx.indentationLvl -= 2;
1220-
output = [state === kRejected ? `<rejected> ${str}` : str];
1220+
output = [
1221+
state === kRejected ?
1222+
`${ctx.stylize('<rejected>', 'special')} ${str}` :
1223+
str
1224+
];
12211225
}
12221226
for (var n = 0; n < keys.length; n++) {
12231227
output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0));

test/parallel/test-util-inspect.js

+27
Original file line numberDiff line numberDiff line change
@@ -1647,3 +1647,30 @@ assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');
16471647
'{ [Non\\nenumerable\\tkey]: true }'
16481648
);
16491649
}
1650+
1651+
// Check for special colors.
1652+
{
1653+
const special = inspect.colors[inspect.styles.special];
1654+
const string = inspect.colors[inspect.styles.string];
1655+
1656+
assert.strictEqual(
1657+
inspect(new WeakSet(), { colors: true }),
1658+
`WeakSet { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }`
1659+
);
1660+
assert.strictEqual(
1661+
inspect(new WeakMap(), { colors: true }),
1662+
`WeakMap { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }`
1663+
);
1664+
assert.strictEqual(
1665+
inspect(new Promise(() => {}), { colors: true }),
1666+
`Promise { \u001b[${special[0]}m<pending>\u001b[${special[1]}m }`
1667+
);
1668+
1669+
const rejection = Promise.reject('Oh no!');
1670+
assert.strictEqual(
1671+
inspect(rejection, { colors: true }),
1672+
`Promise { \u001b[${special[0]}m<rejected>\u001b[${special[1]}m ` +
1673+
`\u001b[${string[0]}m'Oh no!'\u001b[${string[1]}m }`
1674+
);
1675+
rejection.catch(() => {});
1676+
}

0 commit comments

Comments
 (0)