aboutsummaryrefslogtreecommitdiff
path: root/node_modules/@babel/code-frame
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/@babel/code-frame')
-rw-r--r--node_modules/@babel/code-frame/README.md142
-rw-r--r--node_modules/@babel/code-frame/lib/index.js177
-rw-r--r--node_modules/@babel/code-frame/package.json17
3 files changed, 336 insertions, 0 deletions
diff --git a/node_modules/@babel/code-frame/README.md b/node_modules/@babel/code-frame/README.md
new file mode 100644
index 00000000..818b78a0
--- /dev/null
+++ b/node_modules/@babel/code-frame/README.md
@@ -0,0 +1,142 @@
+# @babel/code-frame
+
+> Generate errors that contain a code frame that point to source locations.
+
+## Install
+
+```sh
+npm install --save-dev @babel/code-frame
+```
+
+## Usage
+
+```js
+import { codeFrameColumns } from '@babel/code-frame';
+
+const rawLines = `class Foo {
+ constructor()
+}`;
+const location = { start: { line: 2, column: 16 } };
+
+const result = codeFrameColumns(rawLines, location, { /* options */ });
+
+console.log(result);
+```
+
+```
+ 1 | class Foo {
+> 2 | constructor()
+ | ^
+ 3 | }
+```
+
+If the column number is not known, you may omit it.
+
+You can also pass an `end` hash in `location`.
+
+```js
+import { codeFrameColumns } from '@babel/code-frame';
+
+const rawLines = `class Foo {
+ constructor() {
+ console.log("hello");
+ }
+}`;
+const location = { start: { line: 2, column: 17 }, end: { line: 4, column: 3 } };
+
+const result = codeFrameColumns(rawLines, location, { /* options */ });
+
+console.log(result);
+```
+
+```
+ 1 | class Foo {
+> 2 | constructor() {
+ | ^
+> 3 | console.log("hello");
+ | ^^^^^^^^^^^^^^^^^^^^^^^^^
+> 4 | }
+ | ^^^
+ 5 | };
+```
+
+## Options
+
+### `highlightCode`
+
+`boolean`, defaults to `false`.
+
+Toggles syntax highlighting the code as JavaScript for terminals.
+
+
+### `linesAbove`
+
+`number`, defaults to `2`.
+
+Adjust the number of lines to show above the error.
+
+### `linesBelow`
+
+`number`, defaults to `3`.
+
+Adjust the number of lines to show below the error.
+
+### `forceColor`
+
+`boolean`, defaults to `false`.
+
+Enable this to forcibly syntax highlight the code as JavaScript (for non-terminals); overrides `highlightCode`.
+
+### `message`
+
+`string`, otherwise nothing
+
+Pass in a string to be displayed inline (if possible) next to the highlighted
+location in the code. If it can't be positioned inline, it will be placed above
+the code frame.
+
+```
+1 | class Foo {
+> 2 | constructor()
+ | ^ Missing {
+3 | };
+```
+
+## Upgrading from prior versions
+
+Prior to version 7, the only API exposed by this module was for a single line and optional column pointer. The old API will now log a deprecation warning.
+
+The new API takes a `location` object, similar to what is available in an AST.
+
+This is an example of the deprecated (but still available) API:
+
+```js
+import codeFrame from '@babel/code-frame';
+
+const rawLines = `class Foo {
+ constructor()
+}`;
+const lineNumber = 2;
+const colNumber = 16;
+
+const result = codeFrame(rawLines, lineNumber, colNumber, { /* options */ });
+
+console.log(result);
+```
+
+To get the same highlighting using the new API:
+
+```js
+import { codeFrameColumns } from '@babel/code-frame';
+
+const rawLines = `class Foo {
+ constructor() {
+ console.log("hello");
+ }
+}`;
+const location = { start: { line: 2, column: 16 } };
+
+const result = codeFrameColumns(rawLines, location, { /* options */ });
+
+console.log(result);
+```
diff --git a/node_modules/@babel/code-frame/lib/index.js b/node_modules/@babel/code-frame/lib/index.js
new file mode 100644
index 00000000..18b2ae89
--- /dev/null
+++ b/node_modules/@babel/code-frame/lib/index.js
@@ -0,0 +1,177 @@
+"use strict";
+
+Object.defineProperty(exports, "__esModule", {
+ value: true
+});
+exports.codeFrameColumns = codeFrameColumns;
+exports.default = _default;
+
+function _highlight() {
+ const data = _interopRequireWildcard(require("@babel/highlight"));
+
+ _highlight = function _highlight() {
+ return data;
+ };
+
+ return data;
+}
+
+function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
+
+let deprecationWarningShown = false;
+
+function getDefs(chalk) {
+ return {
+ gutter: chalk.grey,
+ marker: chalk.red.bold,
+ message: chalk.red.bold
+ };
+}
+
+const NEWLINE = /\r\n|[\n\r\u2028\u2029]/;
+
+function getMarkerLines(loc, source, opts) {
+ const startLoc = Object.assign({
+ column: 0,
+ line: -1
+ }, loc.start);
+ const endLoc = Object.assign({}, startLoc, loc.end);
+
+ const _ref = opts || {},
+ _ref$linesAbove = _ref.linesAbove,
+ linesAbove = _ref$linesAbove === void 0 ? 2 : _ref$linesAbove,
+ _ref$linesBelow = _ref.linesBelow,
+ linesBelow = _ref$linesBelow === void 0 ? 3 : _ref$linesBelow;
+
+ const startLine = startLoc.line;
+ const startColumn = startLoc.column;
+ const endLine = endLoc.line;
+ const endColumn = endLoc.column;
+ let start = Math.max(startLine - (linesAbove + 1), 0);
+ let end = Math.min(source.length, endLine + linesBelow);
+
+ if (startLine === -1) {
+ start = 0;
+ }
+
+ if (endLine === -1) {
+ end = source.length;
+ }
+
+ const lineDiff = endLine - startLine;
+ const markerLines = {};
+
+ if (lineDiff) {
+ for (let i = 0; i <= lineDiff; i++) {
+ const lineNumber = i + startLine;
+
+ if (!startColumn) {
+ markerLines[lineNumber] = true;
+ } else if (i === 0) {
+ const sourceLength = source[lineNumber - 1].length;
+ markerLines[lineNumber] = [startColumn, sourceLength - startColumn];
+ } else if (i === lineDiff) {
+ markerLines[lineNumber] = [0, endColumn];
+ } else {
+ const sourceLength = source[lineNumber - i].length;
+ markerLines[lineNumber] = [0, sourceLength];
+ }
+ }
+ } else {
+ if (startColumn === endColumn) {
+ if (startColumn) {
+ markerLines[startLine] = [startColumn, 0];
+ } else {
+ markerLines[startLine] = true;
+ }
+ } else {
+ markerLines[startLine] = [startColumn, endColumn - startColumn];
+ }
+ }
+
+ return {
+ start,
+ end,
+ markerLines
+ };
+}
+
+function codeFrameColumns(rawLines, loc, opts = {}) {
+ const highlighted = (opts.highlightCode || opts.forceColor) && (0, _highlight().shouldHighlight)(opts);
+ const chalk = (0, _highlight().getChalk)(opts);
+ const defs = getDefs(chalk);
+
+ const maybeHighlight = (chalkFn, string) => {
+ return highlighted ? chalkFn(string) : string;
+ };
+
+ if (highlighted) rawLines = (0, _highlight().default)(rawLines, opts);
+ const lines = rawLines.split(NEWLINE);
+
+ const _getMarkerLines = getMarkerLines(loc, lines, opts),
+ start = _getMarkerLines.start,
+ end = _getMarkerLines.end,
+ markerLines = _getMarkerLines.markerLines;
+
+ const hasColumns = loc.start && typeof loc.start.column === "number";
+ const numberMaxWidth = String(end).length;
+ let frame = lines.slice(start, end).map((line, index) => {
+ const number = start + 1 + index;
+ const paddedNumber = ` ${number}`.slice(-numberMaxWidth);
+ const gutter = ` ${paddedNumber} | `;
+ const hasMarker = markerLines[number];
+ const lastMarkerLine = !markerLines[number + 1];
+
+ if (hasMarker) {
+ let markerLine = "";
+
+ if (Array.isArray(hasMarker)) {
+ const markerSpacing = line.slice(0, Math.max(hasMarker[0] - 1, 0)).replace(/[^\t]/g, " ");
+ const numberOfMarkers = hasMarker[1] || 1;
+ markerLine = ["\n ", maybeHighlight(defs.gutter, gutter.replace(/\d/g, " ")), markerSpacing, maybeHighlight(defs.marker, "^").repeat(numberOfMarkers)].join("");
+
+ if (lastMarkerLine && opts.message) {
+ markerLine += " " + maybeHighlight(defs.message, opts.message);
+ }
+ }
+
+ return [maybeHighlight(defs.marker, ">"), maybeHighlight(defs.gutter, gutter), line, markerLine].join("");
+ } else {
+ return ` ${maybeHighlight(defs.gutter, gutter)}${line}`;
+ }
+ }).join("\n");
+
+ if (opts.message && !hasColumns) {
+ frame = `${" ".repeat(numberMaxWidth + 1)}${opts.message}\n${frame}`;
+ }
+
+ if (highlighted) {
+ return chalk.reset(frame);
+ } else {
+ return frame;
+ }
+}
+
+function _default(rawLines, lineNumber, colNumber, opts = {}) {
+ if (!deprecationWarningShown) {
+ deprecationWarningShown = true;
+ const message = "Passing lineNumber and colNumber is deprecated to @babel/code-frame. Please use `codeFrameColumns`.";
+
+ if (process.emitWarning) {
+ process.emitWarning(message, "DeprecationWarning");
+ } else {
+ const deprecationError = new Error(message);
+ deprecationError.name = "DeprecationWarning";
+ console.warn(new Error(message));
+ }
+ }
+
+ colNumber = Math.max(colNumber, 0);
+ const location = {
+ start: {
+ column: colNumber,
+ line: lineNumber
+ }
+ };
+ return codeFrameColumns(rawLines, location, opts);
+} \ No newline at end of file
diff --git a/node_modules/@babel/code-frame/package.json b/node_modules/@babel/code-frame/package.json
new file mode 100644
index 00000000..9dd743a1
--- /dev/null
+++ b/node_modules/@babel/code-frame/package.json
@@ -0,0 +1,17 @@
+{
+ "name": "@babel/code-frame",
+ "version": "7.0.0-beta.47",
+ "description": "Generate errors that contain a code frame that point to source locations.",
+ "author": "Sebastian McKenzie <sebmck@gmail.com>",
+ "homepage": "https://babeljs.io/",
+ "license": "MIT",
+ "repository": "https://github.com/babel/babel/tree/master/packages/babel-code-frame",
+ "main": "lib/index.js",
+ "dependencies": {
+ "@babel/highlight": "7.0.0-beta.47"
+ },
+ "devDependencies": {
+ "chalk": "^2.0.0",
+ "strip-ansi": "^4.0.0"
+ }
+}