Skip to content

Commit fbef1cf

Browse files
authored
util: fix stylize of special properties in inspect
Previously, formatExtraProperties applied ctx.stylize to the entire '[key]: value' string. This caused the colon and space to be styled, making the output inconsistent with normal object properties. Now, only the key itself is stylized, and the colon and space remain unstyled, aligning with the formatting of regular properties. Refs: #60131 PR-URL: #60479 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Jordan Harband <ljharb@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 0981426 commit fbef1cf

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/internal/util/inspect.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2520,7 +2520,8 @@ function formatExtraProperties(ctx, value, recurseTimes, key, typedArray) {
25202520
ctx.indentationLvl -= 2;
25212521

25222522
// These entries are mainly getters. Should they be formatted like getters?
2523-
return ctx.stylize(`[${key}]: ${str}`, 'string');
2523+
const name = ctx.stylize(`[${key}]`, 'string');
2524+
return `${name}: ${str}`;
25242525
}
25252526

25262527
function formatProperty(ctx, value, recurseTimes, key, type, desc,

test/parallel/test-util-inspect.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2391,6 +2391,20 @@ assert.strictEqual(
23912391
inspect.styles.string = stringStyle;
23922392
}
23932393

2394+
// Special (extra) properties follow normal coloring:
2395+
// only the name is colored, ":" and space are unstyled.
2396+
{
2397+
const [open, close] = inspect.colors[inspect.styles.string];
2398+
const keyPattern = (k) => new RegExp(
2399+
`\\u001b\\[${open}m\\[${k}\\]\\u001b\\[${close}m: `
2400+
);
2401+
const colored = util.inspect(new Uint8Array(0), { showHidden: true, colors: true });
2402+
assert.match(colored, keyPattern('BYTES_PER_ELEMENT'));
2403+
assert.match(colored, keyPattern('length'));
2404+
assert.match(colored, keyPattern('byteLength'));
2405+
assert.match(colored, keyPattern('byteOffset'));
2406+
}
2407+
23942408
assert.strictEqual(
23952409
inspect([1, 3, 2], { sorted: true }),
23962410
inspect([1, 3, 2])

0 commit comments

Comments
 (0)