You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
3.4 KiB

4 years ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.shouldHighlight = shouldHighlight;
  6. exports.getChalk = getChalk;
  7. exports.default = highlight;
  8. var _jsTokens = _interopRequireWildcard(require("js-tokens"));
  9. var _helperValidatorIdentifier = require("@babel/helper-validator-identifier");
  10. var _chalk = _interopRequireDefault(require("chalk"));
  11. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12. function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
  13. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
  14. function getDefs(chalk) {
  15. return {
  16. keyword: chalk.cyan,
  17. capitalized: chalk.yellow,
  18. jsx_tag: chalk.yellow,
  19. punctuator: chalk.yellow,
  20. number: chalk.magenta,
  21. string: chalk.green,
  22. regex: chalk.magenta,
  23. comment: chalk.grey,
  24. invalid: chalk.white.bgRed.bold
  25. };
  26. }
  27. const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
  28. const JSX_TAG = /^[a-z][\w-]*$/i;
  29. const BRACKET = /^[()[\]{}]$/;
  30. function getTokenType(match) {
  31. const [offset, text] = match.slice(-2);
  32. const token = (0, _jsTokens.matchToToken)(match);
  33. if (token.type === "name") {
  34. if ((0, _helperValidatorIdentifier.isKeyword)(token.value) || (0, _helperValidatorIdentifier.isReservedWord)(token.value)) {
  35. return "keyword";
  36. }
  37. if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
  38. return "jsx_tag";
  39. }
  40. if (token.value[0] !== token.value[0].toLowerCase()) {
  41. return "capitalized";
  42. }
  43. }
  44. if (token.type === "punctuator" && BRACKET.test(token.value)) {
  45. return "bracket";
  46. }
  47. if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
  48. return "punctuator";
  49. }
  50. return token.type;
  51. }
  52. function highlightTokens(defs, text) {
  53. return text.replace(_jsTokens.default, function (...args) {
  54. const type = getTokenType(args);
  55. const colorize = defs[type];
  56. if (colorize) {
  57. return args[0].split(NEWLINE).map(str => colorize(str)).join("\n");
  58. } else {
  59. return args[0];
  60. }
  61. });
  62. }
  63. function shouldHighlight(options) {
  64. return _chalk.default.supportsColor || options.forceColor;
  65. }
  66. function getChalk(options) {
  67. let chalk = _chalk.default;
  68. if (options.forceColor) {
  69. chalk = new _chalk.default.constructor({
  70. enabled: true,
  71. level: 1
  72. });
  73. }
  74. return chalk;
  75. }
  76. function highlight(code, options = {}) {
  77. if (shouldHighlight(options)) {
  78. const chalk = getChalk(options);
  79. const defs = getDefs(chalk);
  80. return highlightTokens(defs, code);
  81. } else {
  82. return code;
  83. }
  84. }