3 Object.defineProperty(exports, "__esModule", {
6 exports.codeFrameColumns = codeFrameColumns;
7 exports.default = _default;
9 var _highlight = _interopRequireWildcard(require("@babel/highlight"));
11 function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
13 function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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; }
15 let deprecationWarningShown = false;
17 function getDefs(chalk) {
20 marker: chalk.red.bold,
21 message: chalk.red.bold
25 const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
27 function getMarkerLines(loc, source, opts) {
28 const startLoc = Object.assign({
32 const endLoc = Object.assign({}, startLoc, loc.end);
37 const startLine = startLoc.line;
38 const startColumn = startLoc.column;
39 const endLine = endLoc.line;
40 const endColumn = endLoc.column;
41 let start = Math.max(startLine - (linesAbove + 1), 0);
42 let end = Math.min(source.length, endLine + linesBelow);
44 if (startLine === -1) {
52 const lineDiff = endLine - startLine;
53 const markerLines = {};
56 for (let i = 0; i <= lineDiff; i++) {
57 const lineNumber = i + startLine;
60 markerLines[lineNumber] = true;
62 const sourceLength = source[lineNumber - 1].length;
63 markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
64 } else if (i === lineDiff) {
65 markerLines[lineNumber] = [0, endColumn];
67 const sourceLength = source[lineNumber - i].length;
68 markerLines[lineNumber] = [0, sourceLength];
72 if (startColumn === endColumn) {
74 markerLines[startLine] = [startColumn, 0];
76 markerLines[startLine] = true;
79 markerLines[startLine] = [startColumn, endColumn - startColumn];
90 function codeFrameColumns(rawLines, loc, opts = {}) {
91 const highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight.shouldHighlight)(opts);
92 const chalk = (0, _highlight.getChalk)(opts);
93 const defs = getDefs(chalk);
95 const maybeHighlight = (chalkFn, string) => {
96 return highlighted ? chalkFn(string) : string;
99 const lines = rawLines.split(NEWLINE);
104 } = getMarkerLines(loc, lines, opts);
105 const hasColumns = loc.start && typeof loc.start.column === "number";
106 const numberMaxWidth = String(end).length;
107 const highlightedLines = highlighted ? (0, _highlight.default)(rawLines, opts) : rawLines;
108 let frame = highlightedLines.split(NEWLINE).slice(start, end).map((line, index) => {
109 const number = start + 1 + index;
110 const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
111 const gutter = ` ${paddedNumber} | `;
112 const hasMarker = markerLines[number];
113 const lastMarkerLine = !markerLines[number + 1];
118 if (Array.isArray(hasMarker)) {
119 const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
120 const numberOfMarkers = hasMarker[1] || 1;
121 markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join("");
123 if (lastMarkerLine && opts.message) {
124 markerLine += " " + maybeHighlight(defs.message, opts.message);
128 return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
130 return ` ${maybeHighlight(defs.gutter, gutter)}${line}`;
134 if (opts.message && !hasColumns) {
135 frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
139 return chalk.reset(frame);
145 function _default(rawLines, lineNumber, colNumber, opts = {}) {
146 if (!deprecationWarningShown) {
147 deprecationWarningShown = true;
148 const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
150 if (process.emitWarning) {
151 process.emitWarning(message, "DeprecationWarning");
153 const deprecationError = new Error(message);
154 deprecationError.name = "DeprecationWarning";
155 console.warn(new Error(message));
159 colNumber = Math.max(colNumber, 0);
166 return codeFrameColumns(rawLines, location, opts);