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 special entries as such #22287

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
util: mark special entries as such
This adds the color code to special entries if colors are active.
  • Loading branch information
BridgeAR committed Aug 19, 2018
commit 2988330fd506b76b148cf7e823fb2360b9d6dd99
12 changes: 8 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -758,14 +758,14 @@ function formatValue(ctx, value, recurseTimes) {
if (ctx.showHidden) {
formatter = formatWeakSet;
} else {
extra = '<items unknown>';
extra = ctx.stylize('<items unknown>', 'special');
}
} else if (isWeakMap(value)) {
braces[0] = `${getPrefix(constructor, tag, 'WeakMap')}{`;
if (ctx.showHidden) {
formatter = formatWeakMap;
} else {
extra = '<items unknown>';
extra = ctx.stylize('<items unknown>', 'special');
}
} else if (types.isModuleNamespaceObject(value)) {
braces[0] = `[${tag}] {`;
Expand Down Expand Up @@ -1210,14 +1210,18 @@ function formatPromise(ctx, value, recurseTimes, keys) {
let output;
const [state, result] = getPromiseDetails(value);
if (state === kPending) {
output = ['<pending>'];
output = [ctx.stylize('<pending>', 'special')];
} else {
// Using `formatValue` is correct here without the need to fix the
// indentation level.
ctx.indentationLvl += 2;
const str = formatValue(ctx, result, recurseTimes);
ctx.indentationLvl -= 2;
output = [state === kRejected ? `<rejected> ${str}` : str];
output = [
state === kRejected ?
`${ctx.stylize('<rejected>', 'special')} ${str}` :
str
];
}
for (var n = 0; n < keys.length; n++) {
output.push(formatProperty(ctx, value, recurseTimes, keys[n], 0));
Expand Down
27 changes: 27 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1647,3 +1647,30 @@ assert.strictEqual(inspect(new BigUint64Array([0n])), 'BigUint64Array [ 0n ]');
'{ [Non\\nenumerable\\tkey]: true }'
);
}

// Check for special colors.
{
const special = inspect.colors[inspect.styles.special];
const string = inspect.colors[inspect.styles.string];

assert.strictEqual(
inspect(new WeakSet(), { colors: true }),
`WeakSet { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }`
);
assert.strictEqual(
inspect(new WeakMap(), { colors: true }),
`WeakMap { \u001b[${special[0]}m<items unknown>\u001b[${special[1]}m }`
);
assert.strictEqual(
inspect(new Promise(() => {}), { colors: true }),
`Promise { \u001b[${special[0]}m<pending>\u001b[${special[1]}m }`
);

const rejection = Promise.reject('Oh no!');
assert.strictEqual(
inspect(rejection, { colors: true }),
`Promise { \u001b[${special[0]}m<rejected>\u001b[${special[1]}m ` +
`\u001b[${string[0]}m'Oh no!'\u001b[${string[1]}m }`
);
rejection.catch(() => {});
}