Skip to content

Commit 77294d8

Browse files
marco-ippolitotargos
authored andcommitted
util: enforce shouldColorize in styleText array arg
PR-URL: #56722 Fixes: #56717 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Deokjin Kim <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 4606a5f commit 77294d8

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

lib/util.js

+17-28
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
119119
validateString(text, 'text');
120120
validateBoolean(validateStream, 'options.validateStream');
121121

122+
let skipColorize;
122123
if (validateStream) {
123124
if (
124125
!isReadableStream(stream) &&
@@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
127128
) {
128129
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
129130
}
130-
}
131-
132-
if (ArrayIsArray(format)) {
133-
let left = '';
134-
let right = '';
135-
for (const key of format) {
136-
const formatCodes = inspect.colors[key];
137-
if (formatCodes == null) {
138-
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
139-
}
140-
left += escapeStyleCode(formatCodes[0]);
141-
right = `${escapeStyleCode(formatCodes[1])}${right}`;
142-
}
143131

144-
return `${left}${text}${right}`;
132+
// If the stream is falsy or should not be colorized, set skipColorize to true
133+
skipColorize = !lazyUtilColors().shouldColorize(stream);
145134
}
146135

147-
const formatCodes = inspect.colors[format];
148-
if (formatCodes == null) {
149-
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
150-
}
136+
// If the format is not an array, convert it to an array
137+
const formatArray = ArrayIsArray(format) ? format : [format];
151138

152-
// Check colorize only after validating arg type and value
153-
if (
154-
validateStream &&
155-
(
156-
!stream ||
157-
!lazyUtilColors().shouldColorize(stream)
158-
)
159-
) {
160-
return text;
139+
let left = '';
140+
let right = '';
141+
for (const key of formatArray) {
142+
const formatCodes = inspect.colors[key];
143+
// If the format is not a valid style, throw an error
144+
if (formatCodes == null) {
145+
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
146+
}
147+
if (skipColorize) continue;
148+
left += escapeStyleCode(formatCodes[0]);
149+
right = `${escapeStyleCode(formatCodes[1])}${right}`;
161150
}
162151

163-
return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
152+
return skipColorize ? text : `${left}${text}${right}`;
164153
}
165154

166155
/**

test/parallel/test-util-styletext.js

+9-2
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)