1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
4 |
|
---|
5 | var picocolors = require('picocolors');
|
---|
6 | var jsTokens = require('js-tokens');
|
---|
7 | var helperValidatorIdentifier = require('@babel/helper-validator-identifier');
|
---|
8 |
|
---|
9 | function isColorSupported() {
|
---|
10 | return (typeof process === "object" && (process.env.FORCE_COLOR === "0" || process.env.FORCE_COLOR === "false") ? false : picocolors.isColorSupported
|
---|
11 | );
|
---|
12 | }
|
---|
13 | const compose = (f, g) => v => f(g(v));
|
---|
14 | function buildDefs(colors) {
|
---|
15 | return {
|
---|
16 | keyword: colors.cyan,
|
---|
17 | capitalized: colors.yellow,
|
---|
18 | jsxIdentifier: colors.yellow,
|
---|
19 | punctuator: colors.yellow,
|
---|
20 | number: colors.magenta,
|
---|
21 | string: colors.green,
|
---|
22 | regex: colors.magenta,
|
---|
23 | comment: colors.gray,
|
---|
24 | invalid: compose(compose(colors.white, colors.bgRed), colors.bold),
|
---|
25 | gutter: colors.gray,
|
---|
26 | marker: compose(colors.red, colors.bold),
|
---|
27 | message: compose(colors.red, colors.bold),
|
---|
28 | reset: colors.reset
|
---|
29 | };
|
---|
30 | }
|
---|
31 | const defsOn = buildDefs(picocolors.createColors(true));
|
---|
32 | const defsOff = buildDefs(picocolors.createColors(false));
|
---|
33 | function getDefs(enabled) {
|
---|
34 | return enabled ? defsOn : defsOff;
|
---|
35 | }
|
---|
36 |
|
---|
37 | const sometimesKeywords = new Set(["as", "async", "from", "get", "of", "set"]);
|
---|
38 | const NEWLINE$1 = /\r\n|[\n\r\u2028\u2029]/;
|
---|
39 | const BRACKET = /^[()[\]{}]$/;
|
---|
40 | let tokenize;
|
---|
41 | {
|
---|
42 | const JSX_TAG = /^[a-z][\w-]*$/i;
|
---|
43 | const getTokenType = function (token, offset, text) {
|
---|
44 | if (token.type === "name") {
|
---|
45 | if (helperValidatorIdentifier.isKeyword(token.value) || helperValidatorIdentifier.isStrictReservedWord(token.value, true) || sometimesKeywords.has(token.value)) {
|
---|
46 | return "keyword";
|
---|
47 | }
|
---|
48 | if (JSX_TAG.test(token.value) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) === "</")) {
|
---|
49 | return "jsxIdentifier";
|
---|
50 | }
|
---|
51 | if (token.value[0] !== token.value[0].toLowerCase()) {
|
---|
52 | return "capitalized";
|
---|
53 | }
|
---|
54 | }
|
---|
55 | if (token.type === "punctuator" && BRACKET.test(token.value)) {
|
---|
56 | return "bracket";
|
---|
57 | }
|
---|
58 | if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
|
---|
59 | return "punctuator";
|
---|
60 | }
|
---|
61 | return token.type;
|
---|
62 | };
|
---|
63 | tokenize = function* (text) {
|
---|
64 | let match;
|
---|
65 | while (match = jsTokens.default.exec(text)) {
|
---|
66 | const token = jsTokens.matchToToken(match);
|
---|
67 | yield {
|
---|
68 | type: getTokenType(token, match.index, text),
|
---|
69 | value: token.value
|
---|
70 | };
|
---|
71 | }
|
---|
72 | };
|
---|
73 | }
|
---|
74 | function highlight(text) {
|
---|
75 | if (text === "") return "";
|
---|
76 | const defs = getDefs(true);
|
---|
77 | let highlighted = "";
|
---|
78 | for (const {
|
---|
79 | type,
|
---|
80 | value
|
---|
81 | } of tokenize(text)) {
|
---|
82 | if (type in defs) {
|
---|
83 | highlighted += value.split(NEWLINE$1).map(str => defs[type](str)).join("\n");
|
---|
84 | } else {
|
---|
85 | highlighted += value;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | return highlighted;
|
---|
89 | }
|
---|
90 |
|
---|
91 | let deprecationWarningShown = false;
|
---|
92 | const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
|
---|
93 | function getMarkerLines(loc, source, opts) {
|
---|
94 | const startLoc = Object.assign({
|
---|
95 | column: 0,
|
---|
96 | line: -1
|
---|
97 | }, loc.start);
|
---|
98 | const endLoc = Object.assign({}, startLoc, loc.end);
|
---|
99 | const {
|
---|
100 | linesAbove = 2,
|
---|
101 | linesBelow = 3
|
---|
102 | } = opts || {};
|
---|
103 | const startLine = startLoc.line;
|
---|
104 | const startColumn = startLoc.column;
|
---|
105 | const endLine = endLoc.line;
|
---|
106 | const endColumn = endLoc.column;
|
---|
107 | let start = Math.max(startLine - (linesAbove + 1), 0);
|
---|
108 | let end = Math.min(source.length, endLine + linesBelow);
|
---|
109 | if (startLine === -1) {
|
---|
110 | start = 0;
|
---|
111 | }
|
---|
112 | if (endLine === -1) {
|
---|
113 | end = source.length;
|
---|
114 | }
|
---|
115 | const lineDiff = endLine - startLine;
|
---|
116 | const markerLines = {};
|
---|
117 | if (lineDiff) {
|
---|
118 | for (let i = 0; i <= lineDiff; i++) {
|
---|
119 | const lineNumber = i + startLine;
|
---|
120 | if (!startColumn) {
|
---|
121 | markerLines[lineNumber] = true;
|
---|
122 | } else if (i === 0) {
|
---|
123 | const sourceLength = source[lineNumber - 1].length;
|
---|
124 | markerLines[lineNumber] = [startColumn, sourceLength - startColumn + 1];
|
---|
125 | } else if (i === lineDiff) {
|
---|
126 | markerLines[lineNumber] = [0, endColumn];
|
---|
127 | } else {
|
---|
128 | const sourceLength = source[lineNumber - i].length;
|
---|
129 | markerLines[lineNumber] = [0, sourceLength];
|
---|
130 | }
|
---|
131 | }
|
---|
132 | } else {
|
---|
133 | if (startColumn === endColumn) {
|
---|
134 | if (startColumn) {
|
---|
135 | markerLines[startLine] = [startColumn, 0];
|
---|
136 | } else {
|
---|
137 | markerLines[startLine] = true;
|
---|
138 | }
|
---|
139 | } else {
|
---|
140 | markerLines[startLine] = [startColumn, endColumn - startColumn];
|
---|
141 | }
|
---|
142 | }
|
---|
143 | return {
|
---|
144 | start,
|
---|
145 | end,
|
---|
146 | markerLines
|
---|
147 | };
|
---|
148 | }
|
---|
149 | function codeFrameColumns(rawLines, loc, opts = {}) {
|
---|
150 | const shouldHighlight = opts.forceColor || isColorSupported() && opts.highlightCode;
|
---|
151 | const defs = getDefs(shouldHighlight);
|
---|
152 | const lines = rawLines.split(NEWLINE);
|
---|
153 | const {
|
---|
154 | start,
|
---|
155 | end,
|
---|
156 | markerLines
|
---|
157 | } = getMarkerLines(loc, lines, opts);
|
---|
158 | const hasColumns = loc.start && typeof loc.start.column === "number";
|
---|
159 | const numberMaxWidth = String(end).length;
|
---|
160 | const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines;
|
---|
161 | let frame = highlightedLines.split(NEWLINE, end).slice(start, end).map((line, index) => {
|
---|
162 | const number = start + 1 + index;
|
---|
163 | const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
|
---|
164 | const gutter = ` ${paddedNumber} |`;
|
---|
165 | const hasMarker = markerLines[number];
|
---|
166 | const lastMarkerLine = !markerLines[number + 1];
|
---|
167 | if (hasMarker) {
|
---|
168 | let markerLine = "";
|
---|
169 | if (Array.isArray(hasMarker)) {
|
---|
170 | const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
---|
171 | const numberOfMarkers = hasMarker[1] || 1;
|
---|
172 | markerLine = ["\n ", defs.gutter(gutter.replace(/\d/g, " ")), " ", markerSpacing, defs.marker("^").repeat(numberOfMarkers)].join("");
|
---|
173 | if (lastMarkerLine && opts.message) {
|
---|
174 | markerLine += " " + defs.message(opts.message);
|
---|
175 | }
|
---|
176 | }
|
---|
177 | return [defs.marker(">"), defs.gutter(gutter), line.length > 0 ? ` ${line}` : "", markerLine].join("");
|
---|
178 | } else {
|
---|
179 | return ` ${defs.gutter(gutter)}${line.length > 0 ? ` ${line}` : ""}`;
|
---|
180 | }
|
---|
181 | }).join("\n");
|
---|
182 | if (opts.message && !hasColumns) {
|
---|
183 | frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
|
---|
184 | }
|
---|
185 | if (shouldHighlight) {
|
---|
186 | return defs.reset(frame);
|
---|
187 | } else {
|
---|
188 | return frame;
|
---|
189 | }
|
---|
190 | }
|
---|
191 | function index (rawLines, lineNumber, colNumber, opts = {}) {
|
---|
192 | if (!deprecationWarningShown) {
|
---|
193 | deprecationWarningShown = true;
|
---|
194 | const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
|
---|
195 | if (process.emitWarning) {
|
---|
196 | process.emitWarning(message, "DeprecationWarning");
|
---|
197 | } else {
|
---|
198 | const deprecationError = new Error(message);
|
---|
199 | deprecationError.name = "DeprecationWarning";
|
---|
200 | console.warn(new Error(message));
|
---|
201 | }
|
---|
202 | }
|
---|
203 | colNumber = Math.max(colNumber, 0);
|
---|
204 | const location = {
|
---|
205 | start: {
|
---|
206 | column: colNumber,
|
---|
207 | line: lineNumber
|
---|
208 | }
|
---|
209 | };
|
---|
210 | return codeFrameColumns(rawLines, location, opts);
|
---|
211 | }
|
---|
212 |
|
---|
213 | exports.codeFrameColumns = codeFrameColumns;
|
---|
214 | exports.default = index;
|
---|
215 | exports.highlight = highlight;
|
---|
216 | //# sourceMappingURL=index.js.map
|
---|