174 lines
5.2 KiB
JavaScript
174 lines
5.2 KiB
JavaScript
"use strict";
|
|
|
|
exports.__esModule = true;
|
|
exports.codeFrameColumns = codeFrameColumns;
|
|
exports.default = _default;
|
|
|
|
var _highlight = _interopRequireWildcard(require("@babel/highlight"));
|
|
|
|
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; } }
|
|
|
|
var deprecationWarningShown = false;
|
|
|
|
function getDefs(chalk) {
|
|
return {
|
|
gutter: chalk.grey,
|
|
marker: chalk.red.bold,
|
|
message: chalk.red.bold
|
|
};
|
|
}
|
|
|
|
var NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
|
|
|
function getMarkerLines(loc, source, opts) {
|
|
var startLoc = Object.assign({}, {
|
|
column: 0,
|
|
line: -1
|
|
}, loc.start);
|
|
var endLoc = Object.assign({}, startLoc, loc.end);
|
|
|
|
var _ref = opts || {},
|
|
_ref$linesAbove = _ref.linesAbove,
|
|
linesAbove = _ref$linesAbove === void 0 ? 2 : _ref$linesAbove,
|
|
_ref$linesBelow = _ref.linesBelow,
|
|
linesBelow = _ref$linesBelow === void 0 ? 3 : _ref$linesBelow;
|
|
|
|
var startLine = startLoc.line;
|
|
var startColumn = startLoc.column;
|
|
var endLine = endLoc.line;
|
|
var endColumn = endLoc.column;
|
|
var start = Math.max(startLine - (linesAbove + 1), 0);
|
|
var end = Math.min(source.length, endLine + linesBelow);
|
|
|
|
if (startLine === -1) {
|
|
start = 0;
|
|
}
|
|
|
|
if (endLine === -1) {
|
|
end = source.length;
|
|
}
|
|
|
|
var lineDiff = endLine - startLine;
|
|
var markerLines = {};
|
|
|
|
if (lineDiff) {
|
|
for (var i = 0; i <= lineDiff; i++) {
|
|
var lineNumber = i + startLine;
|
|
|
|
if (!startColumn) {
|
|
markerLines[lineNumber] = true;
|
|
} else if (i === 0) {
|
|
var sourceLength = source[lineNumber - 1].length;
|
|
markerLines[lineNumber] = [startColumn, sourceLength - startColumn];
|
|
} else if (i === lineDiff) {
|
|
markerLines[lineNumber] = [0, endColumn];
|
|
} else {
|
|
var _sourceLength = source[lineNumber - i].length;
|
|
markerLines[lineNumber] = [0, _sourceLength];
|
|
}
|
|
}
|
|
} else {
|
|
if (startColumn === endColumn) {
|
|
if (startColumn) {
|
|
markerLines[startLine] = [startColumn, 0];
|
|
} else {
|
|
markerLines[startLine] = true;
|
|
}
|
|
} else {
|
|
markerLines[startLine] = [startColumn, endColumn - startColumn];
|
|
}
|
|
}
|
|
|
|
return {
|
|
start: start,
|
|
end: end,
|
|
markerLines: markerLines
|
|
};
|
|
}
|
|
|
|
function codeFrameColumns(rawLines, loc, opts) {
|
|
if (opts === void 0) {
|
|
opts = {};
|
|
}
|
|
|
|
var highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight.shouldHighlight)(opts);
|
|
var chalk = (0, _highlight.getChalk)(opts);
|
|
var defs = getDefs(chalk);
|
|
|
|
var maybeHighlight = function maybeHighlight(chalkFn, string) {
|
|
return highlighted ? chalkFn(string) : string;
|
|
};
|
|
|
|
if (highlighted) rawLines = (0, _highlight.default)(rawLines, opts);
|
|
var lines = rawLines.split(NEWLINE);
|
|
|
|
var _getMarkerLines = getMarkerLines(loc, lines, opts),
|
|
start = _getMarkerLines.start,
|
|
end = _getMarkerLines.end,
|
|
markerLines = _getMarkerLines.markerLines;
|
|
|
|
var hasColumns = loc.start && typeof loc.start.column === "number";
|
|
var numberMaxWidth = String(end).length;
|
|
var frame = lines.slice(start, end).map(function (line, index) {
|
|
var number = start + 1 + index;
|
|
var paddedNumber = (" " + number).slice(-numberMaxWidth);
|
|
var gutter = " " + paddedNumber + " | ";
|
|
var hasMarker = markerLines[number];
|
|
var lastMarkerLine = !markerLines[number + 1];
|
|
|
|
if (hasMarker) {
|
|
var markerLine = "";
|
|
|
|
if (Array.isArray(hasMarker)) {
|
|
var markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
|
var numberOfMarkers = hasMarker[1] || 1;
|
|
markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join("");
|
|
|
|
if (lastMarkerLine && opts.message) {
|
|
markerLine += " " + maybeHighlight(defs.message, opts.message);
|
|
}
|
|
}
|
|
|
|
return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
|
|
} else {
|
|
return " " + maybeHighlight(defs.gutter, gutter) + line;
|
|
}
|
|
}).join("\n");
|
|
|
|
if (opts.message && !hasColumns) {
|
|
frame = "" + " ".repeat(numberMaxWidth + 1) + opts.message + "\n" + frame;
|
|
}
|
|
|
|
if (highlighted) {
|
|
return chalk.reset(frame);
|
|
} else {
|
|
return frame;
|
|
}
|
|
}
|
|
|
|
function _default(rawLines, lineNumber, colNumber, opts) {
|
|
if (opts === void 0) {
|
|
opts = {};
|
|
}
|
|
|
|
if (!deprecationWarningShown) {
|
|
deprecationWarningShown = true;
|
|
var deprecationError = new Error("Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.");
|
|
deprecationError.name = "DeprecationWarning";
|
|
|
|
if (process.emitWarning) {
|
|
process.emitWarning(deprecationError);
|
|
} else {
|
|
console.warn(deprecationError);
|
|
}
|
|
}
|
|
|
|
colNumber = Math.max(colNumber, 0);
|
|
var location = {
|
|
start: {
|
|
column: colNumber,
|
|
line: lineNumber
|
|
}
|
|
};
|
|
return codeFrameColumns(rawLines, location, opts);
|
|
} |