templates.js
  1  'use strict';
  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;
  6  
  7  const ESCAPES = new Map([
  8  	['n', '\n'],
  9  	['r', '\r'],
 10  	['t', '\t'],
 11  	['b', '\b'],
 12  	['f', '\f'],
 13  	['v', '\v'],
 14  	['0', '\0'],
 15  	['\\', '\\'],
 16  	['e', '\u001B'],
 17  	['a', '\u0007']
 18  ]);
 19  
 20  function unescape(c) {
 21  	const u = c[0] === 'u';
 22  	const bracket = c[1] === '{';
 23  
 24  	if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
 25  		return String.fromCharCode(parseInt(c.slice(1), 16));
 26  	}
 27  
 28  	if (u && bracket) {
 29  		return String.fromCodePoint(parseInt(c.slice(2, -1), 16));
 30  	}
 31  
 32  	return ESCAPES.get(c) || c;
 33  }
 34  
 35  function parseArguments(name, arguments_) {
 36  	const results = [];
 37  	const chunks = arguments_.trim().split(/\s*,\s*/g);
 38  	let matches;
 39  
 40  	for (const chunk of chunks) {
 41  		const number = Number(chunk);
 42  		if (!Number.isNaN(number)) {
 43  			results.push(number);
 44  		} else if ((matches = chunk.match(STRING_REGEX))) {
 45  			results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
 46  		} else {
 47  			throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
 48  		}
 49  	}
 50  
 51  	return results;
 52  }
 53  
 54  function parseStyle(style) {
 55  	STYLE_REGEX.lastIndex = 0;
 56  
 57  	const results = [];
 58  	let matches;
 59  
 60  	while ((matches = STYLE_REGEX.exec(style)) !== null) {
 61  		const name = matches[1];
 62  
 63  		if (matches[2]) {
 64  			const args = parseArguments(name, matches[2]);
 65  			results.push([name].concat(args));
 66  		} else {
 67  			results.push([name]);
 68  		}
 69  	}
 70  
 71  	return results;
 72  }
 73  
 74  function buildStyle(chalk, styles) {
 75  	const enabled = {};
 76  
 77  	for (const layer of styles) {
 78  		for (const style of layer.styles) {
 79  			enabled[style[0]] = layer.inverse ? null : style.slice(1);
 80  		}
 81  	}
 82  
 83  	let current = chalk;
 84  	for (const [styleName, styles] of Object.entries(enabled)) {
 85  		if (!Array.isArray(styles)) {
 86  			continue;
 87  		}
 88  
 89  		if (!(styleName in current)) {
 90  			throw new Error(`Unknown Chalk style: ${styleName}`);
 91  		}
 92  
 93  		current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
 94  	}
 95  
 96  	return current;
 97  }
 98  
 99  module.exports = (chalk, temporary) => {
100  	const styles = [];
101  	const chunks = [];
102  	let chunk = [];
103  
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));
108  		} else if (style) {
109  			const string = chunk.join('');
110  			chunk = [];
111  			chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
112  			styles.push({inverse, styles: parseStyle(style)});
113  		} else if (close) {
114  			if (styles.length === 0) {
115  				throw new Error('Found extraneous } in Chalk template literal');
116  			}
117  
118  			chunks.push(buildStyle(chalk, styles)(chunk.join('')));
119  			chunk = [];
120  			styles.pop();
121  		} else {
122  			chunk.push(character);
123  		}
124  	});
125  
126  	chunks.push(chunk.join(''));
127  
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);
131  	}
132  
133  	return chunks.join('');
134  };