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 | function _jsTokens() {
|
11 | const data = _interopRequireWildcard(require("js-tokens"));
|
12 |
|
13 | _jsTokens = function () {
|
14 | return data;
|
15 | };
|
16 |
|
17 | return data;
|
18 | }
|
19 |
|
20 | function _esutils() {
|
21 | const data = _interopRequireDefault(require("esutils"));
|
22 |
|
23 | _esutils = function () {
|
24 | return data;
|
25 | };
|
26 |
|
27 | return data;
|
28 | }
|
29 |
|
30 | function _chalk() {
|
31 | const data = _interopRequireDefault(require("chalk"));
|
32 |
|
33 | _chalk = function () {
|
34 | return data;
|
35 | };
|
36 |
|
37 | return data;
|
38 | }
|
39 |
|
40 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
41 |
|
42 | function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
|
43 |
|
44 | function getDefs(chalk) {
|
45 | return {
|
46 | keyword: chalk.cyan,
|
47 | capitalized: chalk.yellow,
|
48 | jsx_tag: chalk.yellow,
|
49 | punctuator: chalk.yellow,
|
50 | number: chalk.magenta,
|
51 | string: chalk.green,
|
52 | regex: chalk.magenta,
|
53 | comment: chalk.grey,
|
54 | invalid: chalk.white.bgRed.bold
|
55 | };
|
56 | }
|
57 |
|
58 | const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
59 | const JSX_TAG = /^[a-z][\w-]*$/i;
|
60 | const BRACKET = /^[()[\]{}]$/;
|
61 |
|
62 | function getTokenType(match) {
|
63 | const [offset, text] = match.slice(-2);
|
64 | const token = (0, _jsTokens().matchToToken)(match);
|
65 |
|
66 | if (token.type === "name") {
|
67 | if (_esutils().default.keyword.isReservedWordES6(token.value)) {
|
68 | return "keyword";
|
69 | }
|
70 |
|
71 | if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
|
72 | return "jsx_tag";
|
73 | }
|
74 |
|
75 | if (token.value[0] !== token.value[0].toLowerCase()) {
|
76 | return "capitalized";
|
77 | }
|
78 | }
|
79 |
|
80 | if (token.type === "punctuator" && BRACKET.test(token.value)) {
|
81 | return "bracket";
|
82 | }
|
83 |
|
84 | if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
|
85 | return "punctuator";
|
86 | }
|
87 |
|
88 | return token.type;
|
89 | }
|
90 |
|
91 | function highlightTokens(defs, text) {
|
92 | return text.replace(_jsTokens().default, function (...args) {
|
93 | const type = getTokenType(args);
|
94 | const colorize = defs[type];
|
95 |
|
96 | if (colorize) {
|
97 | return args[0].split(NEWLINE).map(str => colorize(str)).join("\n");
|
98 | } else {
|
99 | return args[0];
|
100 | }
|
101 | });
|
102 | }
|
103 |
|
104 | function shouldHighlight(options) {
|
105 | return _chalk().default.supportsColor || options.forceColor;
|
106 | }
|
107 |
|
108 | function getChalk(options) {
|
109 | let chalk = _chalk().default;
|
110 |
|
111 | if (options.forceColor) {
|
112 | chalk = new (_chalk().default.constructor)({
|
113 | enabled: true,
|
114 | level: 1
|
115 | });
|
116 | }
|
117 |
|
118 | return chalk;
|
119 | }
|
120 |
|
121 | function highlight(code, options = {}) {
|
122 | if (shouldHighlight(options)) {
|
123 | const chalk = getChalk(options);
|
124 | const defs = getDefs(chalk);
|
125 | return highlightTokens(defs, code);
|
126 | } else {
|
127 | return code;
|
128 | }
|
129 | } |
\ | No newline at end of file |