Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: enforce shouldColorize in styleText array arg #56722

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
validateString(text, 'text');
validateBoolean(validateStream, 'options.validateStream');

let skipColorize;
if (validateStream) {
if (
!isReadableStream(stream) &&
Expand All @@ -127,40 +128,28 @@ function styleText(format, text, { validateStream = true, stream = process.stdou
) {
throw new ERR_INVALID_ARG_TYPE('stream', ['ReadableStream', 'WritableStream', 'Stream'], stream);
}
}

if (ArrayIsArray(format)) {
let left = '';
let right = '';
for (const key of format) {
const formatCodes = inspect.colors[key];
if (formatCodes == null) {
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
}
left += escapeStyleCode(formatCodes[0]);
right = `${escapeStyleCode(formatCodes[1])}${right}`;
}

return `${left}${text}${right}`;
// If the stream is falsy or should not be colorized, set skipColorize to true
skipColorize = !lazyUtilColors().shouldColorize(stream);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about returning text here if lazyUtilColors().shouldColorize(stream) is false?
(If this change is applied, we also need to modify line 122, 147, and 152)

Suggested change
skipColorize = !lazyUtilColors().shouldColorize(stream);
if (!lazyUtilColors().shouldColorize(stream)) {
return text;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would not validate the styles which is what I wanted to avoid

}

const formatCodes = inspect.colors[format];
if (formatCodes == null) {
validateOneOf(format, 'format', ObjectKeys(inspect.colors));
}
// If the format is not an array, convert it to an array
const formatArray = ArrayIsArray(format) ? format : [format];

// Check colorize only after validating arg type and value
if (
validateStream &&
(
!stream ||
!lazyUtilColors().shouldColorize(stream)
)
) {
return text;
let left = '';
let right = '';
for (const key of formatArray) {
const formatCodes = inspect.colors[key];
// If the format is not a valid style, throw an error
if (formatCodes == null) {
validateOneOf(key, 'format', ObjectKeys(inspect.colors));
}
if (skipColorize) continue;
left += escapeStyleCode(formatCodes[0]);
right = `${escapeStyleCode(formatCodes[1])}${right}`;
}

return `${escapeStyleCode(formatCodes[0])}${text}${escapeStyleCode(formatCodes[1])}`;
return skipColorize ? text : `${left}${text}${right}`;
}

/**
Expand Down
11 changes: 9 additions & 2 deletions test/parallel/test-util-styletext.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,15 @@ if (fd !== -1) {
...process.env,
...testCase.env
};
const output = util.styleText('red', 'test', { stream: writeStream });
assert.strictEqual(output, testCase.expected);
{
const output = util.styleText('red', 'test', { stream: writeStream });
assert.strictEqual(output, testCase.expected);
}
{
// Check that when passing an array of styles, the output behaves the same
const output = util.styleText(['red'], 'test', { stream: writeStream });
assert.strictEqual(output, testCase.expected);
}
process.env = originalEnv;
});
} else {
Expand Down
Loading