index.js 3.1 KB

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