index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.codeFrameColumns = codeFrameColumns;
  6. exports.default = _default;
  7. var _highlight = require("@babel/highlight");
  8. let deprecationWarningShown = false;
  9. function getDefs(chalk) {
  10. return {
  11. gutter: chalk.grey,
  12. marker: chalk.red.bold,
  13. message: chalk.red.bold
  14. };
  15. }
  16. const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
  17. function getMarkerLines(loc, source, opts) {
  18. const startLoc = Object.assign({
  19. column: 0,
  20. line: -1
  21. }, loc.start);
  22. const endLoc = Object.assign({}, startLoc, loc.end);
  23. const {
  24. linesAbove = 2,
  25. linesBelow = 3
  26. } = opts || {};
  27. const startLine = startLoc.line;
  28. const startColumn = startLoc.column;
  29. const endLine = endLoc.line;
  30. const endColumn = endLoc.column;
  31. let start = Math.max(startLine - (linesAbove + 1), 0);
  32. let end = Math.min(source.length, endLine + linesBelow);
  33. if (startLine === -1) {
  34. start = 0;
  35. }
  36. if (endLine === -1) {
  37. end = source.length;
  38. }
  39. const lineDiff = endLine - startLine;
  40. const markerLines = {};
  41. if (lineDiff) {
  42. for (let i = 0; i <= lineDiff; i++) {
  43. const lineNumber = i + startLine;
  44. if (!startColumn) {
  45. markerLines[lineNumber] = true;
  46. } else if (i === 0) {
  47. const sourceLength = source[lineNumber - 1].length;
  48. markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
  49. } else if (i === lineDiff) {
  50. markerLines[lineNumber] = [0, endColumn];
  51. } else {
  52. const sourceLength = source[lineNumber - i].length;
  53. markerLines[lineNumber] = [0, sourceLength];
  54. }
  55. }
  56. } else {
  57. if (startColumn === endColumn) {
  58. if (startColumn) {
  59. markerLines[startLine] = [startColumn, 0];
  60. } else {
  61. markerLines[startLine] = true;
  62. }
  63. } else {
  64. markerLines[startLine] = [startColumn, endColumn - startColumn];
  65. }
  66. }
  67. return {
  68. start,
  69. end,
  70. markerLines
  71. };
  72. }
  73. function codeFrameColumns(rawLines, loc, opts = {}) {
  74. const highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight.shouldHighlight)(opts);
  75. const chalk = (0, _highlight.getChalk)(opts);
  76. const defs = getDefs(chalk);
  77. const maybeHighlight = (chalkFn, string) => {
  78. return highlighted ? chalkFn(string) : string;
  79. };
  80. const lines = rawLines.split(NEWLINE);
  81. const {
  82. start,
  83. end,
  84. markerLines
  85. } = getMarkerLines(loc, lines, opts);
  86. const hasColumns = loc.start && typeof loc.start.column === "number";
  87. const numberMaxWidth = String(end).length;
  88. const highlightedLines = highlighted ? (0, _highlight.default)(rawLines, opts) : rawLines;
  89. let frame = highlightedLines.split(NEWLINE, end).slice(start, end).map((line, index) => {
  90. const number = start + 1 + index;
  91. const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
  92. const gutter = ` ${paddedNumber} |`;
  93. const hasMarker = markerLines[number];
  94. const lastMarkerLine = !markerLines[number + 1];
  95. if (hasMarker) {
  96. let markerLine = "";
  97. if (Array.isArray(hasMarker)) {
  98. const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
  99. const numberOfMarkers = hasMarker[1] || 1;
  100. markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), " ", markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join("");
  101. if (lastMarkerLine && opts.message) {
  102. markerLine += " " + maybeHighlight(defs.message, opts.message);
  103. }
  104. }
  105. return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line.length > 0 ? ` ${line}` : "", markerLine].join("");
  106. } else {
  107. return ` ${maybeHighlight(defs.gutter, gutter)}${line.length > 0 ? ` ${line}` : ""}`;
  108. }
  109. }).join("\n");
  110. if (opts.message && !hasColumns) {
  111. frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
  112. }
  113. if (highlighted) {
  114. return chalk.reset(frame);
  115. } else {
  116. return frame;
  117. }
  118. }
  119. function _default(rawLines, lineNumber, colNumber, opts = {}) {
  120. if (!deprecationWarningShown) {
  121. deprecationWarningShown = true;
  122. const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
  123. if (process.emitWarning) {
  124. process.emitWarning(message, "DeprecationWarning");
  125. } else {
  126. const deprecationError = new Error(message);
  127. deprecationError.name = "DeprecationWarning";
  128. console.warn(new Error(message));
  129. }
  130. }
  131. colNumber = Math.max(colNumber, 0);
  132. const location = {
  133. start: {
  134. column: colNumber,
  135. line: lineNumber
  136. }
  137. };
  138. return codeFrameColumns(rawLines, location, opts);
  139. }