index.js 4.3 KB

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