Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 77294d8

Browse filesBrowse files
marco-ippolitotargos
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 4606a5f commit 77294d8
Copy full SHA for 77294d8

File tree

Expand file treeCollapse file tree

2 files changed

+26
-30
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+26
-30
lines changed
Open diff view settings
Collapse file

‎lib/util.js‎

Copy file name to clipboardExpand all lines: lib/util.js
+17-28Lines 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+
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
/**
Collapse file

‎test/parallel/test-util-styletext.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-util-styletext.js
+9-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.