Skip to content

Commit 42b9e19

Browse files
marco-ippolitoaduh95
authored andcommitted
util: enforce shouldColorize in styleText array arg
PR-URL: #56722 Fixes: #56717 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Deokjin Kim <deokjin81.kim@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent 0573c19 commit 42b9e19

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

lib/util.js

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
236236
validateString(text, 'text');
237237
validateBoolean(validateStream, 'options.validateStream');
238238

239+
let skipColorize;
239240
if (validateStream) {
240241
if (
241242
!isReadableStream(stream) &&
@@ -244,40 +245,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
244245
) {
245246
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
246247
}
247-
}
248-
249-
if (ArrayIsArray(format)) {
250-
let left = '';
251-
let right = '';
252-
for (const key of format) {
253-
const formatCodes = inspect.colors[key];
254-
if (formatCodes == null) {
255-
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
256-
}
257-
left += escapeStyleCode(formatCodes[0]);
258-
right = `${escapeStyleCode(formatCodes[1])}${right}`;
259-
}
260248

261-
return `${left}${text}${right}`;
249+
// If the stream is falsy or should not be colorized, set skipColorize to true
250+
skipColorize = !lazyUtilColors().shouldColorize(stream);
262251
}
263252

264-
const formatCodes = inspect.colors[format];
265-
if (formatCodes == null) {
266-
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
267-
}
253+
// If the format is not an array, convert it to an array
254+
const formatArray = ArrayIsArray(format) ? format : [format];
268255

269-
// Check colorize only after validating arg type and value
270-
if (
271-
validateStream &&
272-
(
273-
!stream ||
274-
!lazyUtilColors().shouldColorize(stream)
275-
)
276-
) {
277-
return text;
256+
let left = '';
257+
let right = '';
258+
for (const key of formatArray) {
259+
const formatCodes = inspect.colors[key];
260+
// If the format is not a valid style, throw an error
261+
if (formatCodes == null) {
262+
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
263+
}
264+
if (skipColorize) continue;
265+
left += escapeStyleCode(formatCodes[0]);
266+
right = `${escapeStyleCode(formatCodes[1])}${right}`;
278267
}
279268

280-
return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
269+
return skipColorize ? text : `${left}${text}${right}`;
281270
}
282271

283272
const months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',

test/parallel/test-util-styletext.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,15 @@ if (fd !== -1) {
9595
...process.env,
9696
...testCase.env
9797
};
98-
const output = util.styleText('red', 'test', { stream: writeStream });
99-
assert.strictEqual(output, testCase.expected);
98+
{
99+
const output = util.styleText('red', 'test', { stream: writeStream });
100+
assert.strictEqual(output, testCase.expected);
101+
}
102+
{
103+
// Check that when passing an array of styles, the output behaves the same
104+
const output = util.styleText(['red'], 'test', { stream: writeStream });
105+
assert.strictEqual(output, testCase.expected);
106+
}
100107
process.env = originalEnv;
101108
});
102109
} else {

0 commit comments

Comments
 (0)