source: trip-planner-front/node_modules/@babel/highlight/lib/index.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.6 KB
Line 
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.shouldHighlight = shouldHighlight;
7exports.getChalk = getChalk;
8exports.default = highlight;
9
10var _jsTokens = require("js-tokens");
11
12var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
13
14var _chalk = require("chalk");
15
16const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
17
18function 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
32const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
33const BRACKET = /^[()[\]{}]$/;
34let 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
70 yield {
71 type: getTokenType(token, match.index, text),
72 value: token.value
73 };
74 }
75 };
76}
77
78function highlightTokens(defs, text) {
79 let highlighted = "";
80
81 for (const {
82 type,
83 value
84 } of tokenize(text)) {
85 const colorize = defs[type];
86
87 if (colorize) {
88 highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
89 } else {
90 highlighted += value;
91 }
92 }
93
94 return highlighted;
95}
96
97function shouldHighlight(options) {
98 return !!_chalk.supportsColor || options.forceColor;
99}
100
101function getChalk(options) {
102 return options.forceColor ? new _chalk.constructor({
103 enabled: true,
104 level: 1
105 }) : _chalk;
106}
107
108function highlight(code, options = {}) {
109 if (shouldHighlight(options)) {
110 const chalk = getChalk(options);
111 const defs = getDefs(chalk);
112 return highlightTokens(defs, code);
113 } else {
114 return code;
115 }
116}
Note: See TracBrowser for help on using the repository browser.