Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c987c61

Browse files
authored
Remove support for tagged template literals (#524)
1 parent f478655 commit c987c61

File tree

6 files changed

+8
-442
lines changed

6 files changed

+8
-442
lines changed

readme.md

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ RAM: ${chalk.green('40%')}
127127
DISK: ${chalk.yellow('70%')}
128128
`);
129129

130-
// ES2015 tagged template literal
131-
log(chalk`
132-
CPU: {red ${cpu.totalPercent}%}
133-
RAM: {green ${ram.used / ram.total * 100}%}
134-
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
135-
`);
136-
137130
// Use RGB colors in terminal emulators that support it.
138131
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
139132
log(chalk.hex('#DEADED').bold('Bold gray!'));
@@ -257,38 +250,6 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
257250
- `bgCyanBright`
258251
- `bgWhiteBright`
259252

260-
## Tagged template literal
261-
262-
Chalk can be used as a [tagged template literal](https://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
263-
264-
```js
265-
import chalk from 'chalk';
266-
267-
const miles = 18;
268-
const calculateFeet = miles => miles * 5280;
269-
270-
console.log(chalk`
271-
There are {bold 5280 feet} in a mile.
272-
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
273-
`);
274-
```
275-
276-
Blocks are delimited by an opening curly brace (`{`), a style, some content, and a closing curly brace (`}`).
277-
278-
Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:
279-
280-
```js
281-
import chalk from 'chalk';
282-
283-
console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
284-
console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
285-
console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
286-
```
287-
288-
Note that function styles (`rgb()`, `hex()`, etc.) may not contain spaces between parameters.
289-
290-
All interpolated values (`` chalk`${foo}` ``) are converted to strings via the `.toString()` method. All curly braces (`{` and `}`) in interpolated value strings are escaped.
291-
292253
## 256 and Truecolor color support
293254

294255
Chalk supports 256 colors and [Truecolor](https://gist.github.com/XVilka/8346728) (16 million colors) on supported terminal apps.
@@ -331,6 +292,7 @@ The maintainers of chalk and thousands of other packages are working with Tideli
331292

332293
## Related
333294

295+
- [chalk-template](https://github.com/chalk/chalk-template) - [Tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) support for this module
334296
- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
335297
- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
336298
- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color

source/index.d.ts

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -121,36 +121,9 @@ export interface ColorSupport {
121121
has16m: boolean;
122122
}
123123

124-
interface ChalkFunction {
125-
/**
126-
Use a template string.
127-
128-
@remarks Template literals are unsupported for nested calls (see [issue #341](https://github.com/chalk/chalk/issues/341))
129-
130-
@example
131-
```
132-
import chalk from 'chalk';
133-
134-
log(chalk`
135-
CPU: {red ${cpu.totalPercent}%}
136-
RAM: {green ${ram.used / ram.total * 100}%}
137-
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
138-
`);
139-
```
140-
141-
@example
142-
```
143-
import chalk from 'chalk';
144-
145-
log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`)
146-
```
147-
*/
148-
(text: TemplateStringsArray, ...placeholders: unknown[]): string;
149-
124+
export interface ChalkInstance {
150125
(...text: unknown[]): string;
151-
}
152126

153-
export interface ChalkInstance extends ChalkFunction {
154127
/**
155128
The color support for Chalk.
156129
@@ -358,7 +331,7 @@ Order doesn't matter, and later styles take precedent in case of a conflict.
358331
359332
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
360333
*/
361-
declare const chalk: ChalkInstance & ChalkFunction;
334+
declare const chalk: ChalkInstance;
362335

363336
export const supportsColor: ColorSupport | false;
364337

source/index.js

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import {
44
stringReplaceAll,
55
stringEncaseCRLFWithFirstIndex,
66
} from './util.js';
7-
import template from './templates.js';
87

98
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
10-
const {isArray} = Array;
119

1210
const GENERATOR = Symbol('GENERATOR');
1311
const STYLER = Symbol('STYLER');
@@ -41,17 +39,12 @@ export class Chalk {
4139
}
4240

4341
const chalkFactory = options => {
44-
const chalk = {};
42+
const chalk = (...strings) => strings.join(' ');
4543
applyOptions(chalk, options);
4644

47-
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
48-
4945
Object.setPrototypeOf(chalk, createChalk.prototype);
50-
Object.setPrototypeOf(chalk.template, chalk);
51-
52-
chalk.template.Chalk = Chalk;
5346

54-
return chalk.template;
47+
return chalk;
5548
};
5649

5750
function createChalk(options) {
@@ -157,16 +150,9 @@ const createStyler = (open, close, parent) => {
157150
};
158151

159152
const createBuilder = (self, _styler, _isEmpty) => {
160-
const builder = (...arguments_) => {
161-
if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) {
162-
// Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}`
163-
return applyStyle(builder, chalkTag(builder, ...arguments_));
164-
}
165-
166-
// Single argument is hot path, implicit coercion is faster than anything
167-
// eslint-disable-next-line no-implicit-coercion
168-
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
169-
};
153+
// Single argument is hot path, implicit coercion is faster than anything
154+
// eslint-disable-next-line no-implicit-coercion
155+
const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
170156

171157
// We alter the prototype because we must return a function, but there is
172158
// no way to create a function with a different prototype
@@ -213,28 +199,6 @@ const applyStyle = (self, string) => {
213199
return openAll + string + closeAll;
214200
};
215201

216-
const chalkTag = (chalk, ...strings) => {
217-
const [firstString] = strings;
218-
219-
if (!isArray(firstString) || !isArray(firstString.raw)) {
220-
// If chalk() was called by itself or with a string,
221-
// return the string itself as a string.
222-
return strings.join(' ');
223-
}
224-
225-
const arguments_ = strings.slice(1);
226-
const parts = [firstString.raw[0]];
227-
228-
for (let i = 1; i < firstString.length; i++) {
229-
parts.push(
230-
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
231-
String(firstString.raw[i]),
232-
);
233-
}
234-
235-
return template(chalk, parts.join(''));
236-
};
237-
238202
Object.defineProperties(createChalk.prototype, styles);
239203

240204
const chalk = createChalk();

source/index.test-d.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ expectType<ChalkInstance>(new Chalk({level: 1}));
3434
// -- Properties --
3535
expectType<ColorSupportLevel>(chalk.level);
3636

37-
// -- Template literal --
38-
expectType<string>(chalk``);
39-
const name = 'John';
40-
expectType<string>(chalk`Hello {bold.red ${name}}`);
41-
expectType<string>(chalk`Works with numbers {bold.red ${1}}`);
42-
4337
// -- Color methods --
4438
expectAssignable<colorReturn>(chalk.rgb(0, 0, 0));
4539
expectAssignable<colorReturn>(chalk.hex('#DEADED'));

source/templates.js

Lines changed: 0 additions & 133 deletions
This file was deleted.

0 commit comments

Comments
 (0)