[6a3a178] | 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 _jsTokens = require("js-tokens");
|
---|
| 11 |
|
---|
| 12 | var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
|
---|
| 13 |
|
---|
| 14 | var _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 |
|
---|
| 70 | yield {
|
---|
| 71 | type: getTokenType(token, match.index, text),
|
---|
| 72 | value: token.value
|
---|
| 73 | };
|
---|
| 74 | }
|
---|
| 75 | };
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | function 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 |
|
---|
| 97 | function shouldHighlight(options) {
|
---|
| 98 | return !!_chalk.supportsColor || options.forceColor;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | function getChalk(options) {
|
---|
| 102 | return options.forceColor ? new _chalk.constructor({
|
---|
| 103 | enabled: true,
|
---|
| 104 | level: 1
|
---|
| 105 | }) : _chalk;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | function 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 | } |
---|