index.js 5.0 KB

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