3 Object.defineProperty(exports, "__esModule", {
6 exports.default = highlight;
7 exports.getChalk = getChalk;
8 exports.shouldHighlight = shouldHighlight;
10 var _jsTokens = require("js-tokens");
12 var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
14 var _chalk = require("chalk");
16 const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
18 function getDefs(chalk) {
21 capitalized: chalk.yellow,
22 jsxIdentifier: chalk.yellow,
23 punctuator: chalk.yellow,
24 number: chalk.magenta,
28 invalid: chalk.white.bgRed.bold
32 const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
33 const BRACKET = /^[()[\]{}]$/;
36 const JSX_TAG = /^[a-z][\w-]*$/i;
38 const getTokenType = function (token, offset, text) {
39 if (token.type === "name") {
40 if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isStrictReservedWord)(token.value, true) || sometimesKeywords.has(token.value)) {
44 if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
45 return "jsxIdentifier";
48 if (token.value[0] !== token.value[0].toLowerCase()) {
53 if (token.type === "punctuator" && BRACKET.test(token.value)) {
57 if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
64 tokenize = function* (text) {
67 while (match = _jsTokens.default.exec(text)) {
68 const token = _jsTokens.matchToToken(match);
71 type: getTokenType(token, match.index, text),
78 function highlightTokens(defs, text) {
84 } of tokenize(text)) {
85 const colorize = defs[type];
88 highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
97 function shouldHighlight(options) {
98 return !!_chalk.supportsColor || options.forceColor;
101 function getChalk(options) {
102 return options.forceColor ? new _chalk.constructor({
108 function highlight(code, options = {}) {
109 if (shouldHighlight(options)) {
110 const chalk = getChalk(options);
111 const defs = getDefs(chalk);
112 return highlightTokens(defs, code);