index.js 2.9 KB

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