Skip to main content

@babel/code-frame

安装

¥Install

npm install --save-dev @babel/code-frame

用法

¥Usage

JavaScript
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.

你还可以在 location 中传递 end 哈希。

¥You can also pass an end hash in location.

JavaScript
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,默认为 false

¥boolean, defaults to false.

切换语法,将代码高亮为终端的 JavaScript。

¥Toggles syntax highlighting the code as JavaScript for terminals.

linesAbove

number,默认为 2

¥number, defaults to 2.

调整行数以显示在错误上方。

¥Adjust the number of lines to show above the error.

linesBelow

number,默认为 3

¥number, defaults to 3.

调整行数以显示在错误下方。

¥Adjust the number of lines to show below the error.

forceColor

boolean,默认为 false

¥boolean, defaults to false.

启用它以强制将代码语法高亮显示为 JavaScript(对于非终端);覆盖 highlightCode

¥Enable this to forcibly syntax highlight the code as JavaScript (for non-terminals); overrides highlightCode.

message

string,否则什么都没有

¥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

在版本 7 之前,此模块公开的唯一 API 是用于单行和可选列指针。旧 API 现在将记录弃用警告。

¥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.

新 API 采用 location 对象,类似于 AST 中可用的对象。

¥The new API takes a location object, similar to what is available in an AST.

这是已弃用(但仍可用)的 API 的示例:

¥This is an example of the deprecated (but still available) API:

JavaScript
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);

要使用新 API 获得相同的高亮:

¥To get the same highlighting using the new API:

JavaScript
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);