[d565449] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.codeFrameColumns = codeFrameColumns;
|
---|
| 7 | exports.default = _default;
|
---|
| 8 | var _highlight = require("@babel/highlight");
|
---|
| 9 | var _picocolors = _interopRequireWildcard(require("picocolors"), true);
|
---|
| 10 | function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
---|
| 11 | function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
---|
| 12 | const colors = typeof process === "object" && (process.env.FORCE_COLOR === "0" || process.env.FORCE_COLOR === "false") ? (0, _picocolors.createColors)(false) : _picocolors.default;
|
---|
| 13 | const compose = (f, g) => v => f(g(v));
|
---|
| 14 | let pcWithForcedColor = undefined;
|
---|
| 15 | function getColors(forceColor) {
|
---|
| 16 | if (forceColor) {
|
---|
| 17 | var _pcWithForcedColor;
|
---|
| 18 | (_pcWithForcedColor = pcWithForcedColor) != null ? _pcWithForcedColor : pcWithForcedColor = (0, _picocolors.createColors)(true);
|
---|
| 19 | return pcWithForcedColor;
|
---|
| 20 | }
|
---|
| 21 | return colors;
|
---|
| 22 | }
|
---|
| 23 | let deprecationWarningShown = false;
|
---|
| 24 | function getDefs(colors) {
|
---|
| 25 | return {
|
---|
| 26 | gutter: colors.gray,
|
---|
| 27 | marker: compose(colors.red, colors.bold),
|
---|
| 28 | message: compose(colors.red, colors.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 colors = getColors(opts.forceColor);
|
---|
| 91 | const defs = getDefs(colors);
|
---|
| 92 | const maybeHighlight = (fmt, string) => {
|
---|
| 93 | return highlighted ? fmt(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 colors.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 |
|
---|
| 156 | //# sourceMappingURL=index.js.map
|
---|