1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = highlight;
|
---|
7 | exports.shouldHighlight = shouldHighlight;
|
---|
8 | var _jsTokens = require("js-tokens");
|
---|
9 | var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
|
---|
10 | var _picocolors = _interopRequireWildcard(require("picocolors"), true);
|
---|
11 | function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
---|
12 | function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
---|
13 | const colors = typeof process === "object" && (process.env.FORCE_COLOR === "0" || process.env.FORCE_COLOR === "false") ? (0, _picocolors.createColors)(false) : _picocolors.default;
|
---|
14 | const compose = (f, g) => v => f(g(v));
|
---|
15 | const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
|
---|
16 | function getDefs(colors) {
|
---|
17 | return {
|
---|
18 | keyword: colors.cyan,
|
---|
19 | capitalized: colors.yellow,
|
---|
20 | jsxIdentifier: colors.yellow,
|
---|
21 | punctuator: colors.yellow,
|
---|
22 | number: colors.magenta,
|
---|
23 | string: colors.green,
|
---|
24 | regex: colors.magenta,
|
---|
25 | comment: colors.gray,
|
---|
26 | invalid: compose(compose(colors.white, colors.bgRed), colors.bold)
|
---|
27 | };
|
---|
28 | }
|
---|
29 | const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
---|
30 | const BRACKET = /^[()[\]{}]$/;
|
---|
31 | let tokenize;
|
---|
32 | {
|
---|
33 | const JSX_TAG = /^[a-z][\w-]*$/i;
|
---|
34 | const getTokenType = function (token, offset, text) {
|
---|
35 | if (token.type === "name") {
|
---|
36 | if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isStrictReservedWord)(token.value, true) || sometimesKeywords.has(token.value)) {
|
---|
37 | return "keyword";
|
---|
38 | }
|
---|
39 | if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) === "</")) {
|
---|
40 | return "jsxIdentifier";
|
---|
41 | }
|
---|
42 | if (token.value[0] !== token.value[0].toLowerCase()) {
|
---|
43 | return "capitalized";
|
---|
44 | }
|
---|
45 | }
|
---|
46 | if (token.type === "punctuator" && BRACKET.test(token.value)) {
|
---|
47 | return "bracket";
|
---|
48 | }
|
---|
49 | if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
|
---|
50 | return "punctuator";
|
---|
51 | }
|
---|
52 | return token.type;
|
---|
53 | };
|
---|
54 | tokenize = function* (text) {
|
---|
55 | let match;
|
---|
56 | while (match = _jsTokens.default.exec(text)) {
|
---|
57 | const token = _jsTokens.matchToToken(match);
|
---|
58 | yield {
|
---|
59 | type: getTokenType(token, match.index, text),
|
---|
60 | value: token.value
|
---|
61 | };
|
---|
62 | }
|
---|
63 | };
|
---|
64 | }
|
---|
65 | function highlightTokens(defs, text) {
|
---|
66 | let highlighted = "";
|
---|
67 | for (const {
|
---|
68 | type,
|
---|
69 | value
|
---|
70 | } of tokenize(text)) {
|
---|
71 | const colorize = defs[type];
|
---|
72 | if (colorize) {
|
---|
73 | highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
|
---|
74 | } else {
|
---|
75 | highlighted += value;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return highlighted;
|
---|
79 | }
|
---|
80 | function shouldHighlight(options) {
|
---|
81 | return colors.isColorSupported || options.forceColor;
|
---|
82 | }
|
---|
83 | let pcWithForcedColor = undefined;
|
---|
84 | function getColors(forceColor) {
|
---|
85 | if (forceColor) {
|
---|
86 | var _pcWithForcedColor;
|
---|
87 | (_pcWithForcedColor = pcWithForcedColor) != null ? _pcWithForcedColor : pcWithForcedColor = (0, _picocolors.createColors)(true);
|
---|
88 | return pcWithForcedColor;
|
---|
89 | }
|
---|
90 | return colors;
|
---|
91 | }
|
---|
92 | function highlight(code, options = {}) {
|
---|
93 | if (code !== "" && shouldHighlight(options)) {
|
---|
94 | const defs = getDefs(getColors(options.forceColor));
|
---|
95 | return highlightTokens(defs, code);
|
---|
96 | } else {
|
---|
97 | return code;
|
---|
98 | }
|
---|
99 | }
|
---|
100 | {
|
---|
101 | let chalk, chalkWithForcedColor;
|
---|
102 | exports.getChalk = ({
|
---|
103 | forceColor
|
---|
104 | }) => {
|
---|
105 | var _chalk;
|
---|
106 | (_chalk = chalk) != null ? _chalk : chalk = require("chalk");
|
---|
107 | if (forceColor) {
|
---|
108 | var _chalkWithForcedColor;
|
---|
109 | (_chalkWithForcedColor = chalkWithForcedColor) != null ? _chalkWithForcedColor : chalkWithForcedColor = new chalk.constructor({
|
---|
110 | enabled: true,
|
---|
111 | level: 1
|
---|
112 | });
|
---|
113 | return chalkWithForcedColor;
|
---|
114 | }
|
---|
115 | return chalk;
|
---|
116 | };
|
---|
117 | }
|
---|
118 |
|
---|
119 | //# sourceMappingURL=index.js.map
|
---|