2 const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
3 const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
4 const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
5 const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi;
7 const ESCAPES = new Map([
20 function unescape(c) {
21 const u = c[0] === 'u';
22 const bracket = c[1] === '{';
24 if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
25 return String.fromCharCode(parseInt(c.slice(1), 16));
29 return String.fromCodePoint(parseInt(c.slice(2, -1), 16));
32 return ESCAPES.get(c) || c;
35 function parseArguments(name, arguments_) {
37 const chunks = arguments_.trim().split(/\s*,\s*/g);
40 for (const chunk of chunks) {
41 const number = Number(chunk);
42 if (!Number.isNaN(number)) {
44 } else if ((matches = chunk.match(STRING_REGEX))) {
45 results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
47 throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
54 function parseStyle(style) {
55 STYLE_REGEX.lastIndex = 0;
60 while ((matches = STYLE_REGEX.exec(style)) !== null) {
61 const name = matches[1];
64 const args = parseArguments(name, matches[2]);
65 results.push([name].concat(args));
74 function buildStyle(chalk, styles) {
77 for (const layer of styles) {
78 for (const style of layer.styles) {
79 enabled[style[0]] = layer.inverse ? null : style.slice(1);
84 for (const [styleName, styles] of Object.entries(enabled)) {
85 if (!Array.isArray(styles)) {
89 if (!(styleName in current)) {
90 throw new Error(`Unknown Chalk style: ${styleName}`);
93 current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
99 module.exports = (chalk, temporary) => {
104 // eslint-disable-next-line max-params
105 temporary.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
106 if (escapeCharacter) {
107 chunk.push(unescape(escapeCharacter));
109 const string = chunk.join('');
111 chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
112 styles.push({inverse, styles: parseStyle(style)});
114 if (styles.length === 0) {
115 throw new Error('Found extraneous } in Chalk template literal');
118 chunks.push(buildStyle(chalk, styles)(chunk.join('')));
122 chunk.push(character);
126 chunks.push(chunk.join(''));
128 if (styles.length > 0) {
129 const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
130 throw new Error(errMessage);
133 return chunks.join('');