Skip to content

Commit 1828017

Browse files
BridgeARtargos
authored andcommitted
util: mark special entries as such
This adds the color code to special entries if colors are active. PR-URL: #22287 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent f763ac7 commit 1828017

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
@@ -717,14 +717,14 @@ function formatValue(ctx, value, recurseTimes) {
717717
if (ctx.showHidden) {
718718
formatter = formatWeakSet;
719719
} else {
720-
extra = '[items unknown]';
720+
extra = ctx.stylize('[items unknown]', 'special');
721721
}
722722
} else if (isWeakMap(value)) {
723723
braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`;
724724
if (ctx.showHidden) {
725725
formatter = formatWeakMap;
726726
} else {
727-
extra = '[items unknown]';
727+
extra = ctx.stylize('[items unknown]', 'special');
728728
}
729729
} else if (types.isModuleNamespaceObject(value)) {
730730
braces[0] = `[${tag}] {`;
@@ -1162,14 +1162,18 @@ function formatPromise(ctx, value, recurseTimes, keys) {
11621162
let output;
11631163
const [state, result] = getPromiseDetails(value);
11641164
if (state === kPending) {
1165-
output = ['<pending>'];
1165+
output = [ctx.stylize('<pending>', 'special')];
11661166
} else {
11671167
// Using `formatValue` is correct here without the need to fix the
11681168
// indentation level.
11691169
ctx.indentationLvl += 2;
11701170
const str = formatValue(ctx, result, recurseTimes);
11711171
ctx.indentationLvl -= 2;
1172-
output = [state === kRejected ? `<rejected> ${str}` : str];
1172+
output = [
1173+
state === kRejected ?
1174+
`${ctx.stylize('<rejected>', 'special')} ${str}` :
1175+
str
1176+
];
11731177
}
11741178
for (var n = 0; n < keys.length; n++) {
11751179
output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0));

test/parallel/test-util-inspect.js

+27
Original file line numberDiff line numberDiff line change
@@ -1688,3 +1688,30 @@ assert.strictEqual(inspect(Object(13n)), '[BigInt: 13n]');
16881688
'{ [Non\\nenumerable\\tkey]: true }'
16891689
);
16901690
}
1691+
1692+
// Check for special colors.
1693+
{
1694+
const special = inspect.colors[inspect.styles.special];
1695+
const string = inspect.colors[inspect.styles.string];
1696+
1697+
assert.strictEqual(
1698+
inspect(new WeakSet(), { colors: true }),
1699+
`WeakSet { \u001b[${special[0]}m[items unknown]\u001b[${special[1]}m }`
1700+
);
1701+
assert.strictEqual(
1702+
inspect(new WeakMap(), { colors: true }),
1703+
`WeakMap { \u001b[${special[0]}m[items unknown]\u001b[${special[1]}m }`
1704+
);
1705+
assert.strictEqual(
1706+
inspect(new Promise(() => {}), { colors: true }),
1707+
`Promise { \u001b[${special[0]}m<pending>\u001b[${special[1]}m }`
1708+
);
1709+
1710+
const rejection = Promise.reject('Oh no!');
1711+
assert.strictEqual(
1712+
inspect(rejection, { colors: true }),
1713+
`Promise { \u001b[${special[0]}m<rejected>\u001b[${special[1]}m ` +
1714+
`\u001b[${string[0]}m'Oh no!'\u001b[${string[1]}m }`
1715+
);
1716+
rejection.catch(() => {});
1717+
}

0 commit comments

Comments
 (0)