Skip to main content

@babel/code-frame

安装

¥Install

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

用法

¥Usage

codeFrameColumns

codeFrameColumns 函数允许你使用行号和指向特定位置的标记来装饰代码片段。

¥The codeFrameColumns function allows you to decorate a code snipped with line numbers and with a marker pointing to a specific location.

它还将选择性地高亮你的代码,默认为输出终端支持的内容。

¥It will also optionally highlight your code, defaulting to what is supported by the output terminal.

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 | };

highlight

highlight 函数为代码片段添加语法高亮,以显示在终端中。

¥The highlight function adds syntax highlighting to a code snipped, to be displayed in a terminal.

JavaScript
import { highlight } from "@babel/code-frame";

const code = `class Foo {
constructor()
}`;

const result = highlight(code);

console.log(result);
JavaScript
class Foo {
constructor()
}

@babel/highlight 迁移

¥Migrating from @babel/highlight

highlight 功能最初在其自己的包 @babel/highlight 中拆分。

¥The highlight functionality was originally split in its own package, @babel/highlight.

你可以按如下方式迁移:

¥You can migrate as follows:

使用 @babel/highlight使用 @babel/code-frame
JavaScript
import highlight from "@babel/highlight";

highlight(text, { forceColor: true });
JavaScript
import { highlight } from "@babel/code-frame";

highlight(text);
JavaScript
import highlight from "@babel/highlight";

highlight(text);
JavaScript
import { highlight } from "@babel/code-frame";

process.stdout.hasColors() ? highlight(text) : text;

从以前的版本升级

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