.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @babel / highlight / lib / index.js
1 "use strict";
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6 exports.shouldHighlight = shouldHighlight;
7 exports.getChalk = getChalk;
8 exports.default = highlight;
9
10 var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
11
12 const jsTokens = require("js-tokens");
13
14 const Chalk = require("chalk");
15
16 const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
17
18 function getDefs(chalk) {
19   return {
20     keyword: chalk.cyan,
21     capitalized: chalk.yellow,
22     jsxIdentifier: chalk.yellow,
23     punctuator: chalk.yellow,
24     number: chalk.magenta,
25     string: chalk.green,
26     regex: chalk.magenta,
27     comment: chalk.grey,
28     invalid: chalk.white.bgRed.bold
29   };
30 }
31
32 const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
33 const BRACKET = /^[()[\]{}]$/;
34 let tokenize;
35 {
36   const JSX_TAG = /^[a-z][\w-]*$/i;
37
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)) {
41         return "keyword";
42       }
43
44       if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
45         return "jsxIdentifier";
46       }
47
48       if (token.value[0] !== token.value[0].toLowerCase()) {
49         return "capitalized";
50       }
51     }
52
53     if (token.type === "punctuator" && BRACKET.test(token.value)) {
54       return "bracket";
55     }
56
57     if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
58       return "punctuator";
59     }
60
61     return token.type;
62   };
63
64   tokenize = function* (text) {
65     let match;
66
67     while (match = jsTokens.default.exec(text)) {
68       const token = jsTokens.matchToToken(match);
69       yield {
70         type: getTokenType(token, match.index, text),
71         value: token.value
72       };
73     }
74   };
75 }
76
77 function highlightTokens(defs, text) {
78   let highlighted = "";
79
80   for (const {
81     type,
82     value
83   } of tokenize(text)) {
84     const colorize = defs[type];
85
86     if (colorize) {
87       highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
88     } else {
89       highlighted += value;
90     }
91   }
92
93   return highlighted;
94 }
95
96 function shouldHighlight(options) {
97   return !!Chalk.supportsColor || options.forceColor;
98 }
99
100 function getChalk(options) {
101   return options.forceColor ? new Chalk.constructor({
102     enabled: true,
103     level: 1
104   }) : Chalk;
105 }
106
107 function highlight(code, options = {}) {
108   if (shouldHighlight(options)) {
109     const chalk = getChalk(options);
110     const defs = getDefs(chalk);
111     return highlightTokens(defs, code);
112   } else {
113     return code;
114   }
115 }