Skip to content

Commit 77294d8

Browse files
marco-ippolitotargos
authored andcommitted
util: enforce shouldColorize in styleText array arg
PR-URL: #56722Fixes: #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 4606a5f commit 77294d8

2 files changed

Lines changed: 26 additions & 30 deletions

File tree

‎lib/util.js‎

Lines changed: 17 additions & 28 deletions
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+
letskipColorize;
122123
if(validateStream){
123124
if(
124125
!isReadableStream(stream)&&
@@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
127128
){
128129
thrownewERR_INVALID_ARG_TYPE('stream',['ReadableStream','WritableStream','Stream'],stream);
129130
}
130-
}
131-
132-
if(ArrayIsArray(format)){
133-
letleft='';
134-
letright='';
135-
for(constkeyofformat){
136-
constformatCodes=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-
constformatCodes=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+
constformatArray=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-
returntext;
139+
letleft='';
140+
letright='';
141+
for(constkeyofformatArray){
142+
constformatCodes=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+
returnskipColorize ? text : `${left}${text}${right}`;
164153
}
165154

166155
/**

‎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-
constoutput=util.styleText('red','test',{stream: writeStream});
99-
assert.strictEqual(output,testCase.expected);
98+
{
99+
constoutput=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+
constoutput=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)