Disallow template literals on colors and modifiers (#341)
This commit is contained in:
parent
0e6fecc7f7
commit
72063704df
2 changed files with 107 additions and 90 deletions
107
index.d.ts
vendored
107
index.d.ts
vendored
|
|
@ -132,6 +132,15 @@ declare namespace chalk {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ChalkFunction {
|
interface ChalkFunction {
|
||||||
|
/**
|
||||||
|
Use a string.
|
||||||
|
|
||||||
|
@remarks Template literals are unsupported for nested calls (see [issue #341](https://github.com/chalk/chalk/issues/341))
|
||||||
|
*/
|
||||||
|
(text: string): string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ChalkTemplateFunction {
|
||||||
/**
|
/**
|
||||||
Use a template string.
|
Use a template string.
|
||||||
|
|
||||||
|
|
@ -153,7 +162,7 @@ declare namespace chalk {
|
||||||
(...text: unknown[]): string;
|
(...text: unknown[]): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Chalk extends ChalkFunction {
|
interface ChalkObject {
|
||||||
/**
|
/**
|
||||||
Return a new Chalk instance.
|
Return a new Chalk instance.
|
||||||
*/
|
*/
|
||||||
|
|
@ -291,105 +300,109 @@ declare namespace chalk {
|
||||||
/**
|
/**
|
||||||
Modifier: Resets the current color chain.
|
Modifier: Resets the current color chain.
|
||||||
*/
|
*/
|
||||||
readonly reset: Chalk;
|
readonly reset: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Make text bold.
|
Modifier: Make text bold.
|
||||||
*/
|
*/
|
||||||
readonly bold: Chalk;
|
readonly bold: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Emitting only a small amount of light.
|
Modifier: Emitting only a small amount of light.
|
||||||
*/
|
*/
|
||||||
readonly dim: Chalk;
|
readonly dim: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Make text italic. (Not widely supported)
|
Modifier: Make text italic. (Not widely supported)
|
||||||
*/
|
*/
|
||||||
readonly italic: Chalk;
|
readonly italic: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Make text underline. (Not widely supported)
|
Modifier: Make text underline. (Not widely supported)
|
||||||
*/
|
*/
|
||||||
readonly underline: Chalk;
|
readonly underline: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Inverse background and foreground colors.
|
Modifier: Inverse background and foreground colors.
|
||||||
*/
|
*/
|
||||||
readonly inverse: Chalk;
|
readonly inverse: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Prints the text, but makes it invisible.
|
Modifier: Prints the text, but makes it invisible.
|
||||||
*/
|
*/
|
||||||
readonly hidden: Chalk;
|
readonly hidden: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
|
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
|
||||||
*/
|
*/
|
||||||
readonly strikethrough: Chalk;
|
readonly strikethrough: ChalkProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Modifier: Prints the text only when Chalk has a color support level > 0.
|
Modifier: Prints the text only when Chalk has a color support level > 0.
|
||||||
Can be useful for things that are purely cosmetic.
|
Can be useful for things that are purely cosmetic.
|
||||||
*/
|
*/
|
||||||
readonly visible: Chalk;
|
readonly visible: ChalkProperty;
|
||||||
|
|
||||||
readonly black: Chalk;
|
readonly black: ChalkProperty;
|
||||||
readonly red: Chalk;
|
readonly red: ChalkProperty;
|
||||||
readonly green: Chalk;
|
readonly green: ChalkProperty;
|
||||||
readonly yellow: Chalk;
|
readonly yellow: ChalkProperty;
|
||||||
readonly blue: Chalk;
|
readonly blue: ChalkProperty;
|
||||||
readonly magenta: Chalk;
|
readonly magenta: ChalkProperty;
|
||||||
readonly cyan: Chalk;
|
readonly cyan: ChalkProperty;
|
||||||
readonly white: Chalk;
|
readonly white: ChalkProperty;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Alias for `blackBright`.
|
Alias for `blackBright`.
|
||||||
*/
|
*/
|
||||||
readonly gray: Chalk;
|
readonly gray: ChalkProperty;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Alias for `blackBright`.
|
Alias for `blackBright`.
|
||||||
*/
|
*/
|
||||||
readonly grey: Chalk;
|
readonly grey: ChalkProperty;
|
||||||
|
|
||||||
readonly blackBright: Chalk;
|
readonly blackBright: ChalkProperty;
|
||||||
readonly redBright: Chalk;
|
readonly redBright: ChalkProperty;
|
||||||
readonly greenBright: Chalk;
|
readonly greenBright: ChalkProperty;
|
||||||
readonly yellowBright: Chalk;
|
readonly yellowBright: ChalkProperty;
|
||||||
readonly blueBright: Chalk;
|
readonly blueBright: ChalkProperty;
|
||||||
readonly magentaBright: Chalk;
|
readonly magentaBright: ChalkProperty;
|
||||||
readonly cyanBright: Chalk;
|
readonly cyanBright: ChalkProperty;
|
||||||
readonly whiteBright: Chalk;
|
readonly whiteBright: ChalkProperty;
|
||||||
|
|
||||||
readonly bgBlack: Chalk;
|
readonly bgBlack: ChalkProperty;
|
||||||
readonly bgRed: Chalk;
|
readonly bgRed: ChalkProperty;
|
||||||
readonly bgGreen: Chalk;
|
readonly bgGreen: ChalkProperty;
|
||||||
readonly bgYellow: Chalk;
|
readonly bgYellow: ChalkProperty;
|
||||||
readonly bgBlue: Chalk;
|
readonly bgBlue: ChalkProperty;
|
||||||
readonly bgMagenta: Chalk;
|
readonly bgMagenta: ChalkProperty;
|
||||||
readonly bgCyan: Chalk;
|
readonly bgCyan: ChalkProperty;
|
||||||
readonly bgWhite: Chalk;
|
readonly bgWhite: ChalkProperty;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Alias for `bgBlackBright`.
|
Alias for `bgBlackBright`.
|
||||||
*/
|
*/
|
||||||
readonly bgGray: Chalk;
|
readonly bgGray: ChalkProperty;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Alias for `bgBlackBright`.
|
Alias for `bgBlackBright`.
|
||||||
*/
|
*/
|
||||||
readonly bgGrey: Chalk;
|
readonly bgGrey: ChalkProperty;
|
||||||
|
|
||||||
readonly bgBlackBright: Chalk;
|
readonly bgBlackBright: ChalkProperty;
|
||||||
readonly bgRedBright: Chalk;
|
readonly bgRedBright: ChalkProperty;
|
||||||
readonly bgGreenBright: Chalk;
|
readonly bgGreenBright: ChalkProperty;
|
||||||
readonly bgYellowBright: Chalk;
|
readonly bgYellowBright: ChalkProperty;
|
||||||
readonly bgBlueBright: Chalk;
|
readonly bgBlueBright: ChalkProperty;
|
||||||
readonly bgMagentaBright: Chalk;
|
readonly bgMagentaBright: ChalkProperty;
|
||||||
readonly bgCyanBright: Chalk;
|
readonly bgCyanBright: ChalkProperty;
|
||||||
readonly bgWhiteBright: Chalk;
|
readonly bgWhiteBright: ChalkProperty;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ChalkProperty extends ChalkObject, ChalkFunction {}
|
||||||
|
|
||||||
|
interface Chalk extends ChalkObject, ChalkFunction, ChalkTemplateFunction {}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -398,7 +411,7 @@ Call the last one as a method with a string argument.
|
||||||
Order doesn't matter, and later styles take precedent in case of a conflict.
|
Order doesn't matter, and later styles take precedent in case of a conflict.
|
||||||
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
||||||
*/
|
*/
|
||||||
declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
|
declare const chalk: chalk.Chalk & chalk.ChalkFunction & chalk.ChalkTemplateFunction & {
|
||||||
supportsColor: chalk.ColorSupport | false;
|
supportsColor: chalk.ColorSupport | false;
|
||||||
Level: typeof LevelEnum;
|
Level: typeof LevelEnum;
|
||||||
Color: Color;
|
Color: Color;
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,10 @@ const name = 'John';
|
||||||
expectType<string>(chalk`Hello {bold.red ${name}}`);
|
expectType<string>(chalk`Hello {bold.red ${name}}`);
|
||||||
expectType<string>(chalk`Works with numbers {bold.red ${1}}`);
|
expectType<string>(chalk`Works with numbers {bold.red ${1}}`);
|
||||||
|
|
||||||
|
// -- Members of the Chalk interface do not support template literals (#341) --
|
||||||
|
expectError(chalk.bold`Hello {bold.red ${name}}`);
|
||||||
|
expectError(chalk.bold`Works with numbers {bold.red ${1}}`);
|
||||||
|
|
||||||
// -- Color methods --
|
// -- Color methods --
|
||||||
expectType<colorReturn>(chalk.hex('#DEADED'));
|
expectType<colorReturn>(chalk.hex('#DEADED'));
|
||||||
expectType<colorReturn>(chalk.keyword('orange'));
|
expectType<colorReturn>(chalk.keyword('orange'));
|
||||||
|
|
@ -70,15 +74,15 @@ expectType<string>(chalk.inverse('foo'));
|
||||||
expectType<string>(chalk.hidden('foo'));
|
expectType<string>(chalk.hidden('foo'));
|
||||||
expectType<string>(chalk.strikethrough('foo'));
|
expectType<string>(chalk.strikethrough('foo'));
|
||||||
expectType<string>(chalk.visible('foo'));
|
expectType<string>(chalk.visible('foo'));
|
||||||
expectType<string>(chalk.reset`foo`);
|
expectError(chalk.reset`foo`);
|
||||||
expectType<string>(chalk.bold`foo`);
|
expectError(chalk.bold`foo`);
|
||||||
expectType<string>(chalk.dim`foo`);
|
expectError(chalk.dim`foo`);
|
||||||
expectType<string>(chalk.italic`foo`);
|
expectError(chalk.italic`foo`);
|
||||||
expectType<string>(chalk.underline`foo`);
|
expectError(chalk.underline`foo`);
|
||||||
expectType<string>(chalk.inverse`foo`);
|
expectError(chalk.inverse`foo`);
|
||||||
expectType<string>(chalk.hidden`foo`);
|
expectError(chalk.hidden`foo`);
|
||||||
expectType<string>(chalk.strikethrough`foo`);
|
expectError(chalk.strikethrough`foo`);
|
||||||
expectType<string>(chalk.visible`foo`);
|
expectError(chalk.visible`foo`);
|
||||||
|
|
||||||
// -- Colors --
|
// -- Colors --
|
||||||
expectType<string>(chalk.black('foo'));
|
expectType<string>(chalk.black('foo'));
|
||||||
|
|
@ -115,40 +119,40 @@ expectType<string>(chalk.bgBlueBright('foo'));
|
||||||
expectType<string>(chalk.bgMagentaBright('foo'));
|
expectType<string>(chalk.bgMagentaBright('foo'));
|
||||||
expectType<string>(chalk.bgCyanBright('foo'));
|
expectType<string>(chalk.bgCyanBright('foo'));
|
||||||
expectType<string>(chalk.bgWhiteBright('foo'));
|
expectType<string>(chalk.bgWhiteBright('foo'));
|
||||||
expectType<string>(chalk.black`foo`);
|
expectError(chalk.black`foo`);
|
||||||
expectType<string>(chalk.red`foo`);
|
expectError(chalk.red`foo`);
|
||||||
expectType<string>(chalk.green`foo`);
|
expectError(chalk.green`foo`);
|
||||||
expectType<string>(chalk.yellow`foo`);
|
expectError(chalk.yellow`foo`);
|
||||||
expectType<string>(chalk.blue`foo`);
|
expectError(chalk.blue`foo`);
|
||||||
expectType<string>(chalk.magenta`foo`);
|
expectError(chalk.magenta`foo`);
|
||||||
expectType<string>(chalk.cyan`foo`);
|
expectError(chalk.cyan`foo`);
|
||||||
expectType<string>(chalk.white`foo`);
|
expectError(chalk.white`foo`);
|
||||||
expectType<string>(chalk.gray`foo`);
|
expectError(chalk.gray`foo`);
|
||||||
expectType<string>(chalk.grey`foo`);
|
expectError(chalk.grey`foo`);
|
||||||
expectType<string>(chalk.blackBright`foo`);
|
expectError(chalk.blackBright`foo`);
|
||||||
expectType<string>(chalk.redBright`foo`);
|
expectError(chalk.redBright`foo`);
|
||||||
expectType<string>(chalk.greenBright`foo`);
|
expectError(chalk.greenBright`foo`);
|
||||||
expectType<string>(chalk.yellowBright`foo`);
|
expectError(chalk.yellowBright`foo`);
|
||||||
expectType<string>(chalk.blueBright`foo`);
|
expectError(chalk.blueBright`foo`);
|
||||||
expectType<string>(chalk.magentaBright`foo`);
|
expectError(chalk.magentaBright`foo`);
|
||||||
expectType<string>(chalk.cyanBright`foo`);
|
expectError(chalk.cyanBright`foo`);
|
||||||
expectType<string>(chalk.whiteBright`foo`);
|
expectError(chalk.whiteBright`foo`);
|
||||||
expectType<string>(chalk.bgBlack`foo`);
|
expectError(chalk.bgBlack`foo`);
|
||||||
expectType<string>(chalk.bgRed`foo`);
|
expectError(chalk.bgRed`foo`);
|
||||||
expectType<string>(chalk.bgGreen`foo`);
|
expectError(chalk.bgGreen`foo`);
|
||||||
expectType<string>(chalk.bgYellow`foo`);
|
expectError(chalk.bgYellow`foo`);
|
||||||
expectType<string>(chalk.bgBlue`foo`);
|
expectError(chalk.bgBlue`foo`);
|
||||||
expectType<string>(chalk.bgMagenta`foo`);
|
expectError(chalk.bgMagenta`foo`);
|
||||||
expectType<string>(chalk.bgCyan`foo`);
|
expectError(chalk.bgCyan`foo`);
|
||||||
expectType<string>(chalk.bgWhite`foo`);
|
expectError(chalk.bgWhite`foo`);
|
||||||
expectType<string>(chalk.bgBlackBright`foo`);
|
expectError(chalk.bgBlackBright`foo`);
|
||||||
expectType<string>(chalk.bgRedBright`foo`);
|
expectError(chalk.bgRedBright`foo`);
|
||||||
expectType<string>(chalk.bgGreenBright`foo`);
|
expectError(chalk.bgGreenBright`foo`);
|
||||||
expectType<string>(chalk.bgYellowBright`foo`);
|
expectError(chalk.bgYellowBright`foo`);
|
||||||
expectType<string>(chalk.bgBlueBright`foo`);
|
expectError(chalk.bgBlueBright`foo`);
|
||||||
expectType<string>(chalk.bgMagentaBright`foo`);
|
expectError(chalk.bgMagentaBright`foo`);
|
||||||
expectType<string>(chalk.bgCyanBright`foo`);
|
expectError(chalk.bgCyanBright`foo`);
|
||||||
expectType<string>(chalk.bgWhiteBright`foo`);
|
expectError(chalk.bgWhiteBright`foo`);
|
||||||
|
|
||||||
// -- Complex --
|
// -- Complex --
|
||||||
expectType<string>(chalk.red.bgGreen.underline('foo'));
|
expectType<string>(chalk.red.bgGreen.underline('foo'));
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue