index.js 6.1 KB

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