Remove support for tagged template literals (#524)

This commit is contained in:
Richie Bendall 2021-11-10 22:12:33 +13:00 committed by GitHub
parent f478655c3c
commit c987c61486
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 8 additions and 442 deletions

View file

@ -4,10 +4,8 @@ import {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex,
} from './util.js';
import template from './templates.js';
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
const {isArray} = Array;
const GENERATOR = Symbol('GENERATOR');
const STYLER = Symbol('STYLER');
@ -41,17 +39,12 @@ export class Chalk {
}
const chalkFactory = options => {
const chalk = {};
const chalk = (...strings) => strings.join(' ');
applyOptions(chalk, options);
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
Object.setPrototypeOf(chalk, createChalk.prototype);
Object.setPrototypeOf(chalk.template, chalk);
chalk.template.Chalk = Chalk;
return chalk.template;
return chalk;
};
function createChalk(options) {
@ -157,16 +150,9 @@ const createStyler = (open, close, parent) => {
};
const createBuilder = (self, _styler, _isEmpty) => {
const builder = (...arguments_) => {
if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) {
// Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}`
return applyStyle(builder, chalkTag(builder, ...arguments_));
}
// Single argument is hot path, implicit coercion is faster than anything
// eslint-disable-next-line no-implicit-coercion
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
};
// Single argument is hot path, implicit coercion is faster than anything
// eslint-disable-next-line no-implicit-coercion
const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
// We alter the prototype because we must return a function, but there is
// no way to create a function with a different prototype
@ -213,28 +199,6 @@ const applyStyle = (self, string) => {
return openAll + string + closeAll;
};
const chalkTag = (chalk, ...strings) => {
const [firstString] = strings;
if (!isArray(firstString) || !isArray(firstString.raw)) {
// If chalk() was called by itself or with a string,
// return the string itself as a string.
return strings.join(' ');
}
const arguments_ = strings.slice(1);
const parts = [firstString.raw[0]];
for (let i = 1; i < firstString.length; i++) {
parts.push(
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
String(firstString.raw[i]),
);
}
return template(chalk, parts.join(''));
};
Object.defineProperties(createChalk.prototype, styles);
const chalk = createChalk();