Skip to content

Commit a62f641

Browse files
authored
util: allow color aliases in styleText
Fixes an issue where `util.styleText()` would throw an error for valid color aliases like 'grey' in Node.js >= 25.7.0. It now uses `ObjectGetOwnPropertyNames` instead of `ObjectKeys` to fetch both keys and aliases. Fixes: #62177 PR-URL: #62180 Reviewed-By: René <[email protected]> Reviewed-By: Zeyu "Alex" Yang <[email protected]>
1 parent f6d02af commit a62f641

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

lib/util.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const {
3232
ObjectDefineProperties,
3333
ObjectDefineProperty,
3434
ObjectGetOwnPropertyDescriptors,
35+
ObjectGetOwnPropertyNames,
3536
ObjectKeys,
3637
ObjectSetPrototypeOf,
3738
ObjectValues,
@@ -115,7 +116,7 @@ function getStyleCache() {
115116
if (styleCache === undefined) {
116117
styleCache = { __proto__: null };
117118
const colors = inspect.colors;
118-
for (const key of ObjectKeys(colors)) {
119+
for (const key of ObjectGetOwnPropertyNames(colors)) {
119120
const codes = colors[key];
120121
if (codes) {
121122
const openNum = codes[0];
@@ -206,7 +207,7 @@ function styleText(format, text, options) {
206207
if (key === 'none') continue;
207208
const style = cache[key];
208209
if (style === undefined) {
209-
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
210+
validateOneOf(key, 'format', ObjectGetOwnPropertyNames(inspect.colors));
210211
}
211212
openCodes += style.openSeq;
212213
closeCodes = style.closeSeq + closeCodes;

test/parallel/test-util-styletext.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ assert.strictEqual(
4141
'\u001b[31mtest\u001b[39m',
4242
);
4343

44+
assert.strictEqual(
45+
util.styleText('gray', 'test', { validateStream: false }),
46+
'\u001b[90mtest\u001b[39m',
47+
);
48+
49+
assert.strictEqual(
50+
util.styleText('grey', 'test', { validateStream: false }),
51+
'\u001b[90mtest\u001b[39m',
52+
);
53+
4454
assert.strictEqual(
4555
util.styleText(['bold', 'red'], 'test', { validateStream: false }),
4656
'\u001b[1m\u001b[31mtest\u001b[39m\u001b[22m',
@@ -144,6 +154,29 @@ assert.throws(() => {
144154
code: 'ERR_INVALID_ARG_TYPE',
145155
});
146156

157+
// Color aliases should be accepted (e.g. 'grey' is an alias for 'gray')
158+
// See https://github.com/nodejs/node/issues/62177
159+
assert.strictEqual(
160+
util.styleText('grey', 'test', { validateStream: false }),
161+
util.styleText('gray', 'test', { validateStream: false }),
162+
);
163+
assert.strictEqual(
164+
util.styleText('bgGrey', 'test', { validateStream: false }),
165+
util.styleText('bgGray', 'test', { validateStream: false }),
166+
);
167+
assert.strictEqual(
168+
util.styleText('blackBright', 'test', { validateStream: false }),
169+
util.styleText('gray', 'test', { validateStream: false }),
170+
);
171+
assert.strictEqual(
172+
util.styleText('faint', 'test', { validateStream: false }),
173+
util.styleText('dim', 'test', { validateStream: false }),
174+
);
175+
assert.strictEqual(
176+
util.styleText(['grey', 'bold'], 'test', { validateStream: false }),
177+
util.styleText(['gray', 'bold'], 'test', { validateStream: false }),
178+
);
179+
147180
// does not throw
148181
util.styleText('red', 'text', { stream: {}, validateStream: false });
149182

0 commit comments

Comments
 (0)