Security:
- S2: hexToRgb — explicit typeof branch + padStart(6) for numeric hex inputs;
makes the numeric-input path intentional and preserves leading zeros
- S3: FORCE_COLOR parsing — guard against NaN propagation when env value is
non-numeric (e.g. FORCE_COLOR=yes now correctly falls back to level 1)
- S4: _supportsColor — remove side-effecting mutation of module-level
flagForceColor; effective value is now computed locally, eliminating
cross-call state corruption in test environments
- S5: applyOptions — change `options.level &&` to `options.level !== undefined`
so null and NaN are properly rejected instead of silently stored as the level
- S6: browser.js — explicit Number(brand.version) > 93 instead of implicit
string-to-number coercion for Chromium UA version check
Performance / correctness:
- P1: rainbow.js — replace stateful global-regex test() in loop (which
misclassified every other non-printable character due to lastIndex advancing)
with a direct code-point comparison: character < '!' || character > '~'
- P4: stringEncaseCRLFWithFirstIndex — switch from += string concatenation in
loop to array-of-parts + single join(), reducing intermediate allocations for
multi-line strings
- P6: builder — detect tagged template literal calls via .raw property and route
through String.raw(), so chalk.red`hello ${name}` now produces correct output
Tests:
- instance.js: new Chalk({level: null/NaN}) now throws (S5 regression test)
- chalk.js: numeric hex with leading zeros (S2), template literal interpolations
(P6) covered by new tests; all 35 tests pass
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
145 lines
5.4 KiB
JavaScript
145 lines
5.4 KiB
JavaScript
import process from 'node:process';
|
|
import test from 'ava';
|
|
import chalk, {Chalk, chalkStderr} from '../source/index.js';
|
|
|
|
chalk.level = 3;
|
|
chalkStderr.level = 3;
|
|
|
|
console.log('TERM:', process.env.TERM || '[none]');
|
|
console.log('platform:', process.platform || '[unknown]');
|
|
|
|
test('don\'t add any styling when called as the base function', t => {
|
|
t.is(chalk('foo'), 'foo');
|
|
});
|
|
|
|
test('support multiple arguments in base function', t => {
|
|
t.is(chalk('hello', 'there'), 'hello there');
|
|
});
|
|
|
|
test('support automatic casting to string', t => {
|
|
t.is(chalk(['hello', 'there']), 'hello,there');
|
|
t.is(chalk(123), '123');
|
|
|
|
t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m');
|
|
t.is(chalk.green(98_765), '\u001B[32m98765\u001B[39m');
|
|
});
|
|
|
|
test('style string', t => {
|
|
t.is(chalk.underline('foo'), '\u001B[4mfoo\u001B[24m');
|
|
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
|
t.is(chalk.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
|
|
});
|
|
|
|
test('support applying multiple styles at once', t => {
|
|
t.is(chalk.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
|
|
t.is(chalk.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
|
|
});
|
|
|
|
test('support nesting styles', t => {
|
|
t.is(
|
|
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
|
|
'\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m',
|
|
);
|
|
});
|
|
|
|
test('support nesting styles of the same type (color, underline, bg)', t => {
|
|
t.is(
|
|
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
|
|
'\u001B[31ma\u001B[33mb\u001B[32mc\u001B[39m\u001B[31m\u001B[33mb\u001B[39m\u001B[31mc\u001B[39m',
|
|
);
|
|
});
|
|
|
|
test('reset all styles with `.reset()`', t => {
|
|
t.is(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
|
|
});
|
|
|
|
test('support caching multiple styles', t => {
|
|
const {red, green} = chalk.red;
|
|
const redBold = red.bold;
|
|
const greenBold = green.bold;
|
|
|
|
t.not(red('foo'), green('foo'));
|
|
t.not(redBold('bar'), greenBold('bar'));
|
|
t.not(green('baz'), greenBold('baz'));
|
|
});
|
|
|
|
test('alias gray to grey', t => {
|
|
t.is(chalk.grey('foo'), '\u001B[90mfoo\u001B[39m');
|
|
});
|
|
|
|
test('support variable number of arguments', t => {
|
|
t.is(chalk.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
|
|
});
|
|
|
|
test('support falsy values', t => {
|
|
t.is(chalk.red(0), '\u001B[31m0\u001B[39m');
|
|
});
|
|
|
|
test('don\'t output escape codes if the input is empty', t => {
|
|
t.is(chalk.red(), '');
|
|
t.is(chalk.red.blue.black(), '');
|
|
});
|
|
|
|
test('keep Function.prototype methods', t => {
|
|
t.is(Reflect.apply(chalk.grey, null, ['foo']), '\u001B[90mfoo\u001B[39m');
|
|
t.is(chalk.reset(chalk.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
|
|
t.is(chalk.red.blue.black.call(null), '');
|
|
});
|
|
|
|
test('line breaks should open and close colors', t => {
|
|
t.is(chalk.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
|
|
});
|
|
|
|
test('line breaks should open and close colors with CRLF', t => {
|
|
t.is(chalk.grey('hello\r\nworld'), '\u001B[90mhello\u001B[39m\r\n\u001B[90mworld\u001B[39m');
|
|
});
|
|
|
|
test('properly convert RGB to 16 colors on basic color terminals', t => {
|
|
t.is(new Chalk({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
|
|
t.is(new Chalk({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
|
|
});
|
|
|
|
test('properly convert RGB to 256 colors on basic color terminals', t => {
|
|
t.is(new Chalk({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
|
t.is(new Chalk({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
|
t.is(new Chalk({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
|
|
});
|
|
|
|
test('don\'t emit RGB codes if level is 0', t => {
|
|
t.is(new Chalk({level: 0}).hex('#FF0000')('hello'), 'hello');
|
|
t.is(new Chalk({level: 0}).bgHex('#FF0000')('hello'), 'hello');
|
|
});
|
|
|
|
test('supports blackBright color', t => {
|
|
t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m');
|
|
});
|
|
|
|
test('sets correct level for chalkStderr and respects it', t => {
|
|
t.is(chalkStderr.level, 3);
|
|
t.is(chalkStderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m');
|
|
});
|
|
|
|
test('keeps function prototype methods', t => {
|
|
t.is(chalk.apply(chalk, ['foo']), 'foo');
|
|
t.is(chalk.bind(chalk, 'foo')(), 'foo');
|
|
t.is(chalk.call(chalk, 'foo'), 'foo');
|
|
});
|
|
|
|
test('accept numeric hex values in .hex() (S2)', t => {
|
|
// Numeric values should behave identically to their string equivalents
|
|
t.is(new Chalk({level: 3}).hex(0xFF_00_00)('hello'), new Chalk({level: 3}).hex('#FF0000')('hello'));
|
|
t.is(new Chalk({level: 3}).bgHex(0xFF_00_FF)('hello'), new Chalk({level: 3}).bgHex('#FF00FF')('hello'));
|
|
// Leading zeros must be preserved: 0x0000FF is blue, not a garbled 3-char match
|
|
t.is(new Chalk({level: 3}).hex(0x00_00_FF)('hello'), new Chalk({level: 3}).hex('#0000FF')('hello'));
|
|
t.is(new Chalk({level: 3}).hex(0x00_FF_00)('hello'), new Chalk({level: 3}).hex('#00FF00')('hello'));
|
|
});
|
|
|
|
test('support tagged template literals with interpolations (P6)', t => {
|
|
const name = 'World';
|
|
t.is(chalk.red`hello ${name}`, '\u001B[31mhello World\u001B[39m');
|
|
t.is(chalk.bold`count: ${42}`, '\u001B[1mcount: 42\u001B[22m');
|
|
// Without interpolations should still work
|
|
t.is(chalk.red`hello`, '\u001B[31mhello\u001B[39m');
|
|
// Array arguments (not template literals) must be unaffected
|
|
t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m');
|
|
});
|