| [9af201e] | 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 | const JSX_TAG = /^[a-z][\w-]*$/i;
|
|---|
| 42 | const getTokenType = function (token, offset, text) {
|
|---|
| 43 | if (token.type === "name") {
|
|---|
| 44 | const tokenValue = token.value;
|
|---|
| 45 | if (helperValidatorIdentifier.isKeyword(tokenValue) || helperValidatorIdentifier.isStrictReservedWord(tokenValue, true) || sometimesKeywords.has(tokenValue)) {
|
|---|
| 46 | return "keyword";
|
|---|
| 47 | }
|
|---|
| 48 | if (JSX_TAG.test(tokenValue) && (text[offset - 1] === "<" || text.slice(offset - 2, offset) === "</")) {
|
|---|
| 49 | return "jsxIdentifier";
|
|---|
| 50 | }
|
|---|
| 51 | const firstChar = String.fromCodePoint(tokenValue.codePointAt(0));
|
|---|
| 52 | if (firstChar !== firstChar.toLowerCase()) {
|
|---|
| 53 | return "capitalized";
|
|---|
| 54 | }
|
|---|
| 55 | }
|
|---|
| 56 | if (token.type === "punctuator" && BRACKET.test(token.value)) {
|
|---|
| 57 | return "bracket";
|
|---|
| 58 | }
|
|---|
| 59 | if (token.type === "invalid" && (token.value === "@" || token.value === "#")) {
|
|---|
| 60 | return "punctuator";
|
|---|
| 61 | }
|
|---|
| 62 | return token.type;
|
|---|
| 63 | };
|
|---|
| 64 | tokenize = function* (text) {
|
|---|
| 65 | let match;
|
|---|
| 66 | while (match = jsTokens.default.exec(text)) {
|
|---|
| 67 | const token = jsTokens.matchToToken(match);
|
|---|
| 68 | yield {
|
|---|
| 69 | type: getTokenType(token, match.index, text),
|
|---|
| 70 | value: token.value
|
|---|
| 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, startLineBaseZero) {
|
|---|
| 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 - startLineBaseZero;
|
|---|
| 104 | const startColumn = startLoc.column;
|
|---|
| 105 | const endLine = endLoc.line - startLineBaseZero;
|
|---|
| 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 startLineBaseZero = (opts.startLine || 1) - 1;
|
|---|
| 152 | const defs = getDefs(shouldHighlight);
|
|---|
| 153 | const lines = rawLines.split(NEWLINE);
|
|---|
| 154 | const {
|
|---|
| 155 | start,
|
|---|
| 156 | end,
|
|---|
| 157 | markerLines
|
|---|
| 158 | } = getMarkerLines(loc, lines, opts, startLineBaseZero);
|
|---|
| 159 | const hasColumns = loc.start && typeof loc.start.column === "number";
|
|---|
| 160 | const numberMaxWidth = String(end + startLineBaseZero).length;
|
|---|
| 161 | const highlightedLines = shouldHighlight ? highlight(rawLines) : rawLines;
|
|---|
| 162 | let frame = highlightedLines.split(NEWLINE, end).slice(start, end).map((line, index) => {
|
|---|
| 163 | const number = start + 1 + index;
|
|---|
| 164 | const paddedNumber = ` ${number + startLineBaseZero}`.slice(-numberMaxWidth);
|
|---|
| 165 | const gutter = ` ${paddedNumber} |`;
|
|---|
| 166 | const hasMarker = markerLines[number];
|
|---|
| 167 | const lastMarkerLine = !markerLines[number + 1];
|
|---|
| 168 | if (hasMarker) {
|
|---|
| 169 | let markerLine = "";
|
|---|
| 170 | if (Array.isArray(hasMarker)) {
|
|---|
| 171 | const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
|
|---|
| 172 | const numberOfMarkers = hasMarker[1] || 1;
|
|---|
| 173 | markerLine = ["\n ", defs.gutter(gutter.replace(/\d/g, " ")), " ", markerSpacing, defs.marker("^").repeat(numberOfMarkers)].join("");
|
|---|
| 174 | if (lastMarkerLine && opts.message) {
|
|---|
| 175 | markerLine += " " + defs.message(opts.message);
|
|---|
| 176 | }
|
|---|
| 177 | }
|
|---|
| 178 | return [defs.marker(">"), defs.gutter(gutter), line.length > 0 ? ` ${line}` : "", markerLine].join("");
|
|---|
| 179 | } else {
|
|---|
| 180 | return ` ${defs.gutter(gutter)}${line.length > 0 ? ` ${line}` : ""}`;
|
|---|
| 181 | }
|
|---|
| 182 | }).join("\n");
|
|---|
| 183 | if (opts.message && !hasColumns) {
|
|---|
| 184 | frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
|
|---|
| 185 | }
|
|---|
| 186 | if (shouldHighlight) {
|
|---|
| 187 | return defs.reset(frame);
|
|---|
| 188 | } else {
|
|---|
| 189 | return frame;
|
|---|
| 190 | }
|
|---|
| 191 | }
|
|---|
| 192 | function index (rawLines, lineNumber, colNumber, opts = {}) {
|
|---|
| 193 | if (!deprecationWarningShown) {
|
|---|
| 194 | deprecationWarningShown = true;
|
|---|
| 195 | const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
|
|---|
| 196 | if (process.emitWarning) {
|
|---|
| 197 | process.emitWarning(message, "DeprecationWarning");
|
|---|
| 198 | } else {
|
|---|
| 199 | const deprecationError = new Error(message);
|
|---|
| 200 | deprecationError.name = "DeprecationWarning";
|
|---|
| 201 | console.warn(new Error(message));
|
|---|
| 202 | }
|
|---|
| 203 | }
|
|---|
| 204 | colNumber = Math.max(colNumber, 0);
|
|---|
| 205 | const location = {
|
|---|
| 206 | start: {
|
|---|
| 207 | column: colNumber,
|
|---|
| 208 | line: lineNumber
|
|---|
| 209 | }
|
|---|
| 210 | };
|
|---|
| 211 | return codeFrameColumns(rawLines, location, opts);
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 | exports.codeFrameColumns = codeFrameColumns;
|
|---|
| 215 | exports.default = index;
|
|---|
| 216 | exports.highlight = highlight;
|
|---|
| 217 | //# sourceMappingURL=index.js.map
|
|---|