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.

140 lines
3.7 KiB

4 years ago
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (rawLines, lineNumber, colNumber) {
  4. var opts = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  5. colNumber = Math.max(colNumber, 0);
  6. var highlighted = opts.highlightCode && _chalk2.default.supportsColor || opts.forceColor;
  7. var chalk = _chalk2.default;
  8. if (opts.forceColor) {
  9. chalk = new _chalk2.default.constructor({ enabled: true });
  10. }
  11. var maybeHighlight = function maybeHighlight(chalkFn, string) {
  12. return highlighted ? chalkFn(string) : string;
  13. };
  14. var defs = getDefs(chalk);
  15. if (highlighted) rawLines = highlight(defs, rawLines);
  16. var linesAbove = opts.linesAbove || 2;
  17. var linesBelow = opts.linesBelow || 3;
  18. var lines = rawLines.split(NEWLINE);
  19. var start = Math.max(lineNumber - (linesAbove + 1), 0);
  20. var end = Math.min(lines.length, lineNumber + linesBelow);
  21. if (!lineNumber && !colNumber) {
  22. start = 0;
  23. end = lines.length;
  24. }
  25. var numberMaxWidth = String(end).length;
  26. var frame = lines.slice(start, end).map(function (line, index) {
  27. var number = start + 1 + index;
  28. var paddedNumber = (" " + number).slice(-numberMaxWidth);
  29. var gutter = " " + paddedNumber + " | ";
  30. if (number === lineNumber) {
  31. var markerLine = "";
  32. if (colNumber) {
  33. var markerSpacing = line.slice(0, colNumber - 1).replace(/[^\t]/g, " ");
  34. markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), markerSpacing, maybeHighlight(defs.marker, "^")].join("");
  35. }
  36. return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
  37. } else {
  38. return " " + maybeHighlight(defs.gutter, gutter) + line;
  39. }
  40. }).join("\n");
  41. if (highlighted) {
  42. return chalk.reset(frame);
  43. } else {
  44. return frame;
  45. }
  46. };
  47. var _jsTokens = require("js-tokens");
  48. var _jsTokens2 = _interopRequireDefault(_jsTokens);
  49. var _esutils = require("esutils");
  50. var _esutils2 = _interopRequireDefault(_esutils);
  51. var _chalk = require("chalk");
  52. var _chalk2 = _interopRequireDefault(_chalk);
  53. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  54. function getDefs(chalk) {
  55. return {
  56. keyword: chalk.cyan,
  57. capitalized: chalk.yellow,
  58. jsx_tag: chalk.yellow,
  59. punctuator: chalk.yellow,
  60. number: chalk.magenta,
  61. string: chalk.green,
  62. regex: chalk.magenta,
  63. comment: chalk.grey,
  64. invalid: chalk.white.bgRed.bold,
  65. gutter: chalk.grey,
  66. marker: chalk.red.bold
  67. };
  68. }
  69. var NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
  70. var JSX_TAG = /^[a-z][\w-]*$/i;
  71. var BRACKET = /^[()\[\]{}]$/;
  72. function getTokenType(match) {
  73. var _match$slice = match.slice(-2),
  74. offset = _match$slice[0],
  75. text = _match$slice[1];
  76. var token = (0, _jsTokens.matchToToken)(match);
  77. if (token.type === "name") {
  78. if (_esutils2.default.keyword.isReservedWordES6(token.value)) {
  79. return "keyword";
  80. }
  81. if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.substr(offset - 2, 2) == "</")) {
  82. return "jsx_tag";
  83. }
  84. if (token.value[0] !== token.value[0].toLowerCase()) {
  85. return "capitalized";
  86. }
  87. }
  88. if (token.type === "punctuator" && BRACKET.test(token.value)) {
  89. return "bracket";
  90. }
  91. return token.type;
  92. }
  93. function highlight(defs, text) {
  94. return text.replace(_jsTokens2.default, function () {
  95. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  96. args[_key] = arguments[_key];
  97. }
  98. var type = getTokenType(args);
  99. var colorize = defs[type];
  100. if (colorize) {
  101. return args[0].split(NEWLINE).map(function (str) {
  102. return colorize(str);
  103. }).join("\n");
  104. } else {
  105. return args[0];
  106. }
  107. });
  108. }
  109. module.exports = exports["default"];