index.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = highlight;
  6. exports.getChalk = getChalk;
  7. exports.shouldHighlight = shouldHighlight;
  8. var _jsTokens = require("js-tokens");
  9. var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
  10. var _chalk = require("chalk");
  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. function getChalk(options) {
  80. return options.forceColor ? new _chalk.constructor({
  81. enabled: true,
  82. level: 1
  83. }) : _chalk;
  84. }
  85. function highlight(code, options = {}) {
  86. if (code !== "" && shouldHighlight(options)) {
  87. const chalk = getChalk(options);
  88. const defs = getDefs(chalk);
  89. return highlightTokens(defs, code);
  90. } else {
  91. return code;
  92. }
  93. }