source: trip-planner-front/node_modules/@babel/code-frame/lib/index.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.6 KB
RevLine 
[6a3a178]1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.codeFrameColumns = codeFrameColumns;
7exports.default = _default;
8
9var _highlight = require("@babel/highlight");
10
11let deprecationWarningShown = false;
12
13function getDefs(chalk) {
14 return {
15 gutter: chalk.grey,
16 marker: chalk.red.bold,
17 message: chalk.red.bold
18 };
19}
20
21const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
22
23function getMarkerLines(loc, source, opts) {
24 const startLoc = Object.assign({
25 column: 0,
26 line: -1
27 }, loc.start);
28 const endLoc = Object.assign({}, startLoc, loc.end);
29 const {
30 linesAbove = 2,
31 linesBelow = 3
32 } = opts || {};
33 const startLine = startLoc.line;
34 const startColumn = startLoc.column;
35 const endLine = endLoc.line;
36 const endColumn = endLoc.column;
37 let start = Math.max(startLine - (linesAbove + 1), 0);
38 let end = Math.min(source.length, endLine + linesBelow);
39
40 if (startLine === -1) {
41 start = 0;
42 }
43
44 if (endLine === -1) {
45 end = source.length;
46 }
47
48 const lineDiff = endLine - startLine;
49 const markerLines = {};
50
51 if (lineDiff) {
52 for (let i = 0; i <= lineDiff; i++) {
53 const lineNumber = i + startLine;
54
55 if (!startColumn) {
56 markerLines[lineNumber] = true;
57 } else if (i === 0) {
58 const sourceLength = source[lineNumber - 1].length;
59 markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
60 } else if (i === lineDiff) {
61 markerLines[lineNumber] = [0, endColumn];
62 } else {
63 const sourceLength = source[lineNumber - i].length;
64 markerLines[lineNumber] = [0, sourceLength];
65 }
66 }
67 } else {
68 if (startColumn === endColumn) {
69 if (startColumn) {
70 markerLines[startLine] = [startColumn, 0];
71 } else {
72 markerLines[startLine] = true;
73 }
74 } else {
75 markerLines[startLine] = [startColumn, endColumn - startColumn];
76 }
77 }
78
79 return {
80 start,
81 end,
82 markerLines
83 };
84}
85
86function codeFrameColumns(rawLines, loc, opts = {}) {
87 const highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight.shouldHighlight)(opts);
88 const chalk = (0, _highlight.getChalk)(opts);
89 const defs = getDefs(chalk);
90
91 const maybeHighlight = (chalkFn, string) => {
92 return highlighted ? chalkFn(string) : string;
93 };
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
111 if (hasMarker) {
112 let markerLine = "";
113
114 if (Array.isArray(hasMarker)) {
115 const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
116 const numberOfMarkers = hasMarker[1] || 1;
117 markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), " ", markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join("");
118
119 if (lastMarkerLine && opts.message) {
120 markerLine += " " + maybeHighlight(defs.message, opts.message);
121 }
122 }
123
124 return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line.length > 0 ? ` ${line}` : "", markerLine].join("");
125 } else {
126 return ` ${maybeHighlight(defs.gutter, gutter)}${line.length > 0 ? ` ${line}` : ""}`;
127 }
128 }).join("\n");
129
130 if (opts.message && !hasColumns) {
131 frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
132 }
133
134 if (highlighted) {
135 return chalk.reset(frame);
136 } else {
137 return frame;
138 }
139}
140
141function _default(rawLines, lineNumber, colNumber, opts = {}) {
142 if (!deprecationWarningShown) {
143 deprecationWarningShown = true;
144 const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
145
146 if (process.emitWarning) {
147 process.emitWarning(message, "DeprecationWarning");
148 } else {
149 const deprecationError = new Error(message);
150 deprecationError.name = "DeprecationWarning";
151 console.warn(new Error(message));
152 }
153 }
154
155 colNumber = Math.max(colNumber, 0);
156 const location = {
157 start: {
158 column: colNumber,
159 line: lineNumber
160 }
161 };
162 return codeFrameColumns(rawLines, location, opts);
163}
Note: See TracBrowser for help on using the repository browser.