Skip to content

Commit 93e84a7

Browse files
committed
util: fix styleText to accept format aliases
The optimization in #61792 introduced a style cache using ObjectKeys(inspect.colors), which only returns enumerable properties. Color aliases (e.g. 'grey', 'blackBright', 'faint') are defined as non-enumerable via defineColorAlias(), so they were excluded from the cache, causing ERR_INVALID_ARG_VALUE when used with styleText(). Replace ObjectKeys with ObjectGetOwnPropertyNames to include non-enumerable alias properties in both the style cache and the validation error message. Fixes: #62177
1 parent c96c3da commit 93e84a7

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ assert.throws(() => {
138138
code: 'ERR_INVALID_ARG_VALUE',
139139
});
140140

141+
// Color aliases should be accepted (e.g. 'grey' is an alias for 'gray')
142+
// See https://github.com/nodejs/node/issues/62177
143+
assert.strictEqual(
144+
util.styleText('grey', 'test', { validateStream: false }),
145+
util.styleText('gray', 'test', { validateStream: false }),
146+
);
147+
assert.strictEqual(
148+
util.styleText('bgGrey', 'test', { validateStream: false }),
149+
util.styleText('bgGray', 'test', { validateStream: false }),
150+
);
151+
assert.strictEqual(
152+
util.styleText('blackBright', 'test', { validateStream: false }),
153+
util.styleText('gray', 'test', { validateStream: false }),
154+
);
155+
assert.strictEqual(
156+
util.styleText('faint', 'test', { validateStream: false }),
157+
util.styleText('dim', 'test', { validateStream: false }),
158+
);
159+
assert.strictEqual(
160+
util.styleText(['grey', 'bold'], 'test', { validateStream: false }),
161+
util.styleText(['gray', 'bold'], 'test', { validateStream: false }),
162+
);
163+
141164
assert.throws(() => {
142165
util.styleText('red', 'text', { stream: {} });
143166
}, {

0 commit comments

Comments
 (0)