3 Object.defineProperty(exports, "__esModule", {
6 exports.shouldHighlight = shouldHighlight;
7 exports.getChalk = getChalk;
8 exports.default = highlight;
10 var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
12 const jsTokens = require("js-tokens");
14 const 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);
70 type: getTokenType(token, match.index, text),
77 function highlightTokens(defs, text) {
83 } of tokenize(text)) {
84 const colorize = defs[type];
87 highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
96 function shouldHighlight(options) {
97 return !!Chalk.supportsColor || options.forceColor;
100 function getChalk(options) {
101 return options.forceColor ? new Chalk.constructor({
107 function highlight(code, options = {}) {
108 if (shouldHighlight(options)) {
109 const chalk = getChalk(options);
110 const defs = getDefs(chalk);
111 return highlightTokens(defs, code);