index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 _chalk = _interopRequireWildcard(require("chalk"), true);
  10. function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
  11. function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
  12. const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
  13. function getDefs(chalk) {
  14. return {
  15. keyword: chalk.cyan,
  16. capitalized: chalk.yellow,
  17. jsxIdentifier: chalk.yellow,
  18. punctuator: chalk.yellow,
  19. number: chalk.magenta,
  20. string: chalk.green,
  21. regex: chalk.magenta,
  22. comment: chalk.grey,
  23. invalid: chalk.white.bgRed.bold
  24. };
  25. }
  26. const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
  27. const BRACKET = /^[()[\]{}]$/;
  28. let tokenize;
  29. {
  30. const JSX_TAG = /^[a-z][\w-]*$/i;
  31. const getTokenType = function (token, offset, text) {
  32. if (token.type === "name") {
  33. if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isStrictReservedWord)(token.value, true) || sometimesKeywords.has(token.value)) {
  34. return "keyword";
  35. }
  36. if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) == "</")) {
  37. return "jsxIdentifier";
  38. }
  39. if (token.value[0] !== token.value[0].toLowerCase()) {
  40. return "capitalized";
  41. }
  42. }
  43. if (token.type === "punctuator" && BRACKET.test(token.value)) {
  44. return "bracket";
  45. }
  46. if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
  47. return "punctuator";
  48. }
  49. return token.type;
  50. };
  51. tokenize = function* (text) {
  52. let match;
  53. while (match = _jsTokens.default.exec(text)) {
  54. const token = _jsTokens.matchToToken(match);
  55. yield {
  56. type: getTokenType(token, match.index, text),
  57. value: token.value
  58. };
  59. }
  60. };
  61. }
  62. function highlightTokens(defs, text) {
  63. let highlighted = "";
  64. for (const {
  65. type,
  66. value
  67. } of tokenize(text)) {
  68. const colorize = defs[type];
  69. if (colorize) {
  70. highlighted += value.split(NEWLINE).map(str => colorize(str)).join("\n");
  71. } else {
  72. highlighted += value;
  73. }
  74. }
  75. return highlighted;
  76. }
  77. function shouldHighlight(options) {
  78. return _chalk.default.level > 0 || options.forceColor;
  79. }
  80. let chalkWithForcedColor = undefined;
  81. function getChalk(forceColor) {
  82. if (forceColor) {
  83. var _chalkWithForcedColor;
  84. (_chalkWithForcedColor = chalkWithForcedColor) != null ? _chalkWithForcedColor : chalkWithForcedColor = new _chalk.default.constructor({
  85. enabled: true,
  86. level: 1
  87. });
  88. return chalkWithForcedColor;
  89. }
  90. return _chalk.default;
  91. }
  92. {
  93. exports.getChalk = options => getChalk(options.forceColor);
  94. }
  95. function highlight(code, options = {}) {
  96. if (code !== "" && shouldHighlight(options)) {
  97. const defs = getDefs(getChalk(options.forceColor));
  98. return highlightTokens(defs, code);
  99. } else {
  100. return code;
  101. }
  102. }
  103. //# sourceMappingURL=index.js.map