.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @babel / code-frame / lib / index.js
1 "use strict";
2
3 Object.defineProperty(exports, "__esModule", {
4   value: true
5 });
6 exports.codeFrameColumns = codeFrameColumns;
7 exports.default = _default;
8
9 var _highlight = _interopRequireWildcard(require("@babel/highlight"));
10
11 function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
12
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; }
14
15 let deprecationWarningShown = false;
16
17 function getDefs(chalk) {
18   return {
19     gutter: chalk.grey,
20     marker: chalk.red.bold,
21     message: chalk.red.bold
22   };
23 }
24
25 const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
26
27 function getMarkerLines(loc, source, opts) {
28   const startLoc = Object.assign({
29     column: 0,
30     line: -1
31   }, loc.start);
32   const endLoc = Object.assign({}, startLoc, loc.end);
33   const {
34     linesAbove = 2,
35     linesBelow = 3
36   } = opts || {};
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);
43
44   if (startLine === -1) {
45     start = 0;
46   }
47
48   if (endLine === -1) {
49     end = source.length;
50   }
51
52   const lineDiff = endLine - startLine;
53   const markerLines = {};
54
55   if (lineDiff) {
56     for (let i = 0; i <= lineDiff; i++) {
57       const lineNumber = i + startLine;
58
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
83   return {
84     start,
85     end,
86     markerLines
87   };
88 }
89
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);
94
95   const maybeHighlight = (chalkFn, string) => {
96     return highlighted ? chalkFn(string) : string;
97   };
98
99   const lines = rawLines.split(NEWLINE);
100   const {
101     start,
102     end,
103     markerLines
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];
114
115     if (hasMarker) {
116       let markerLine = "";
117
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("");
122
123         if (lastMarkerLine && opts.message) {
124           markerLine += " " + maybeHighlight(defs.message, opts.message);
125         }
126       }
127
128       return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
129     } else {
130       return ` ${maybeHighlight(defs.gutter, gutter)}${line}`;
131     }
132   }).join("\n");
133
134   if (opts.message && !hasColumns) {
135     frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
136   }
137
138   if (highlighted) {
139     return chalk.reset(frame);
140   } else {
141     return frame;
142   }
143 }
144
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`.";
149
150     if (process.emitWarning) {
151       process.emitWarning(message, "DeprecationWarning");
152     } else {
153       const deprecationError = new Error(message);
154       deprecationError.name = "DeprecationWarning";
155       console.warn(new Error(message));
156     }
157   }
158
159   colNumber = Math.max(colNumber, 0);
160   const location = {
161     start: {
162       column: colNumber,
163       line: lineNumber
164     }
165   };
166   return codeFrameColumns(rawLines, location, opts);
167 }