index.js 5.1 KB

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