# @eslint/plugin-kit 中文文档

规范地址：https://npmcn.com/package/%40eslint/plugin-kit/

版本：0\.7\.3
npm 元数据更新时间：2026-09-03T18:10:04.045Z
中文译文更新时间：2026-11-05T14:41:38.665Z
软件包许可：Apache-2\.0

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40eslint/plugin-kit
原文对照：https://npmcn.com/package/%40eslint/plugin-kit/?lang=original
项目主页：https://github.com/eslint/rewrite/tree/main/packages/plugin-kit#readme
源码仓库：https://github.com/eslint/rewrite

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：111 kB，11 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：^20\.19\.0 \|\| ^22\.13\.0 \|\| \>=24
- 运行依赖：2 个
- 首次发布：2024-08-27T13:59:03.959Z
- 最新版本发布：2026-09-03T18:10:03.592Z
- 近 30 天下载量：527599510（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：25（近 12 个月 8 个）

## 中文简介

用于建设ESLint插件的公用事业.

## 安装

```sh
npm install @eslint/plugin-kit
```

也可以使用：`pnpm add @eslint/plugin-kit`、`yarn add @eslint/plugin-kit`、`bun add @eslint/plugin-kit`

## README

# ESLint 插件包

## 说明

用于帮助建设ESLint插件的公用设施集.

## 安装

对于节点和相容运行时间:

```shell
npm install @eslint/plugin-kit
# or
yarn add @eslint/plugin-kit
# or
pnpm install @eslint/plugin-kit
# or
bun add @eslint/plugin-kit
```

对于德诺:

```shell
deno add @eslint/plugin-kit
```

## 使用量

该套件出口以下公用事业:

-   [`ConfigCommentParser`](https://npmcn.com/package/%40eslint/plugin-kit/#readme-configcommentparser) - 用于解析ESLint配置注释(即, `/* eslint-disable rule */`(中文(简体) )
-   [`VisitNodeStep` 财务报告和审定财务报表 `CallMethodStep`](https://npmcn.com/package/%40eslint/plugin-kit/#readme-visitnodestep-and-callmethodstep) - 用于帮助执行 `SourceCode#traverse()`
-   [`Directive`](https://npmcn.com/package/%40eslint/plugin-kit/#readme-directive) - 用于帮助执行 `SourceCode#getDisableDirectives()`
-   [`TextSourceCodeBase`](https://npmcn.com/package/%40eslint/plugin-kit/#readme-textsourcecodebase) - 基础课,帮助执行 `SourceCode` 接口

### `ConfigCommentParser`

用来使用 `ConfigCommentParser` 类,从软件包中导入并创建新实例,例如:

```js
import { ConfigCommentParser } from "@eslint/plugin-kit";

// create a new instance
const commentParser = new ConfigCommentParser();

// pass in a comment string without the comment delimiters
const directive = commentParser.parseDirective(
    "eslint-disable prefer-const, no-var -- I don't want to use these.",
);

// will be undefined when a directive can't be parsed
if (directive) {
    console.log(directive.label); // "eslint-disable"
    console.log(directive.value); // "prefer-const, no-var"
    console.log(directive.justification); // "I don't want to use these."
}
```

指令值有不同的样式,您需要分别分析才能得到正确的格式:

```js
import { ConfigCommentParser } from "@eslint/plugin-kit";

// create a new instance
const commentParser = new ConfigCommentParser();

// list format
const list = commentParser.parseListConfig("prefer-const, no-var");
console.log(Object.entries(list)); // [["prefer-const", true], ["no-var", true]]

// string format
const strings = commentParser.parseStringConfig("foo:off, bar");
console.log(Object.entries(strings)); // [["foo", "off"], ["bar", null]]

// JSON-like config format
const jsonLike = commentParser.parseJSONLikeConfig(
    "radix:[error, always], prefer-const: warn",
);
console.log(Object.entries(jsonLike.config)); // [["radix", ["error", "always"]], ["prefer-const", "warn"]]
```

### `VisitNodeStep` 财务报告和审定财务报表 `CallMethodStep`

这个 `VisitNodeStep` 财务报告和审定财务报表 `CallMethodStep` 类代表源代码的倒转中的步骤。 他们执行正确的接口从 `SourceCode#traverse()` 方法.

这个 `VisitNodeStep` 类是两者中比较常见的,其中你描述在转弯时访问某个特定节点. 构造者接受三个论点:

-   `target` - 被访问的节点。 这用于确定规则内调用的方法. 例如,如果节点的类型是 `Literal` 然后ESLint将调用一个命名的方法 `Literal()` (如果有的话).
-   `phase` - 进站1个,出站2个.
-   `args` - 将各种论据传入规则的访客方法.

例如:

```js
import { VisitNodeStep } from "@eslint/plugin-kit";

class MySourceCode {
    traverse() {
        const steps = [];

        for (const { node, parent, phase } of iterator(this.ast)) {
            steps.push(
                new VisitNodeStep({
                    target: node,
                    phase: phase === "enter" ? 1 : 2,
                    args: [node, parent],
                }),
            );
        }

        return steps;
    }
}
```

这个 `CallMethodStep` 类不太常见,用来告诉ESLint在规则上调用特定的方法. 构造者接受两个论点:

-   `target` - 调用方法的名称,经常从 `"on"` 例如 `"onCodePathStart"`。 。 。 .
-   `args` - 要传递到方法上的一系列论据.

例如:

```js
import { VisitNodeStep, CallMethodStep } from "@eslint/plugin-kit";

class MySourceCode {
    traverse() {
        const steps = [];

        for (const { node, parent, phase } of iterator(this.ast)) {
            steps.push(
                new VisitNodeStep({
                    target: node,
                    phase: phase === "enter" ? 1 : 2,
                    args: [node, parent],
                }),
            );

            // call a method indicating how many times we've been through the loop
            steps.push(
                new CallMethodStep({
                    target: "onIteration",
                    args: [steps.length]
                });
            )
        }

        return steps;
    }
}
```

### `Directive`

这个 `Directive` 类代表源代码中的禁用指令并执行 `Directive` 界面从 `@eslint/core`您可以告诉 ESLint 关于使用 `SourceCode#getDisableDirectives()` 方法,其中部分返回值为 `Directive` 对象。 举个例子:

```js
import { Directive, ConfigCommentParser } from "@eslint/plugin-kit";

class MySourceCode {
    getDisableDirectives() {
        const directives = [];
        const problems = [];
        const commentParser = new ConfigCommentParser();

        // read in the inline config nodes to check each one
        this.getInlineConfigNodes().forEach(comment => {
            // Step 1: Parse the directive
            const { label, value, justification } =
                commentParser.parseDirective(comment.value);

            // Step 2: Extract the directive value and create the `Directive` object
            switch (label) {
                case "eslint-disable":
                case "eslint-enable":
                case "eslint-disable-next-line":
                case "eslint-disable-line": {
                    const directiveType = label.slice("eslint-".length);

                    directives.push(
                        new Directive({
                            type: directiveType,
                            node: comment,
                            value,
                            justification,
                        }),
                    );
                }

                // ignore any comments that don't begin with known labels
            }
        });

        return {
            directives,
            problems,
        };
    }
}
```

### `TextSourceCodeBase`

这个 `TextSourceCodeBase` class 意在是一个基础类,其若干共同成员存在于 `SourceCode` 已执行对象。 这些成员是:

-   `lines` - 构造器调用时自动创建的一行文字.
-   `getLoc(nodeOrToken)` - 得到一个节点或标志的位置。 为具有ESLint风格的节点工作 `loc` 具有Unist 风格的属性和节点 [`position` 属性](https://github.com/syntax-tree/unist?tab=readme-ov-file#position)。如果使用不同位置格式的AST,你还需要自己执行这种方法.
-   `getLocFromIndex(index)` - 将源文本索引转换为 `{ line: number, column: number }` 双人 (要使这种方法发挥作用,根节点应始终涵盖整个源代码文本,以及 `getLoc()` 方法需要正确执行。 )
-   `getIndexFromLoc(loc)` - 转换一个 `{ line: number, column: number }` 组合成源文本索引。 (要使这种方法发挥作用,根节点应始终涵盖整个源代码文本,以及 `getLoc()` 方法需要正确执行。 )
-   `getRange(nodeOrToken)` - 获得源文本中节点或信使的范围。 为具有 ESLint 风格的节点工作 `range` 具有Unist 风格的属性和节点 [`position` 属性](https://github.com/syntax-tree/unist?tab=readme-ov-file#position)。如果使用不同范围格式的AST,则还需要自己执行这种方法.
-   `getText(node, beforeCount, afterCount)` - 获取带有范围信息的给定节点的源文本。 若为 `node` 省略或变质(例如: `undefined`, (中文(简体) ), `null`,或空字符串),返回整个源文本。 可选的,可以在给定节点前后还原附加字符. 璶 `getRange()` 正确实施,这种方法将只是工作.
-   `getAncestors(node)` - 返回节点的祖先。 要做到这一点,你必须执行 `getParent()` 自己来.

举个例子:

```js
import { TextSourceCodeBase } from "@eslint/plugin-kit";

export class MySourceCode extends TextSourceCodeBase {
    #parents = new Map();

    constructor({ ast, text }) {
        super({ ast, text });
    }

    getParent(node) {
        return this.#parents.get(node);
    }

    traverse() {
        const steps = [];

        for (const { node, parent, phase } of iterator(this.ast)) {
            //save the parent information
            this.#parents.set(node, parent);

            steps.push(
                new VisitNodeStep({
                    target: node,
                    phase: phase === "enter" ? 1 : 2,
                    args: [node, parent],
                }),
            );
        }

        return steps;
    }
}
```

一般来说,收集父母信息是安全的 `traverse()` 方法为 `getParent()` 财务报告和审定财务报表 `getAncestors()` 只能从规则中调用 一旦AST至少经过一次.

## 许可证

Apache 2.0 (英语)

## 发起人

以下公司,组织,个人支持ESLint的持续维护与发展. [成为发起人](https://eslint.org/donate) 让你的标志在我们的读取器和 [网站](https://eslint.org/sponsors)。 。 。 .

### 白金赞助商

[![Automattic](https://images.opencollective.com/automattic/d0ef3e1/logo.png)](https://automattic.com/)

### 黄金赞助商

[![Qlty Software](https://images.opencollective.com/qltysh/33d157d/logo.png)](https://qlty.sh/) [![Shopify](https://avatars.githubusercontent.com/u/8085)](https://shopify.engineering/) [![CodeRabbit](https://avatars.githubusercontent.com/u/132028505)](https://www.coderabbit.ai/?utm_source=cr_org&utm_medium=github)

### 白银赞助商

[![Vite](https://images.opencollective.com/vite/d472863/logo.png)](https://vite.dev/) [![Liftoff](https://images.opencollective.com/liftoff/2d6c3b6/logo.png)](https://liftoff.io/) [![StackBlitz](https://avatars.githubusercontent.com/u/28635252)](https://stackblitz.com/)

### 青铜赞助商

[![Cybozu](https://images.opencollective.com/cybozu/933e46d/logo.png)](https://cybozu.co.jp/) [![SAP](https://avatars.githubusercontent.com/u/2531208)](https://opensource.sap.com/) [![Icons8](https://images.opencollective.com/icons8/7fa1641/logo.png)](https://icons8.com/) [![Discord](https://images.opencollective.com/discordapp/f9645d9/logo.png)](https://discord.com/) [![GitBook](https://avatars.githubusercontent.com/u/7111340)](https://www.gitbook.com/) [![Citadel AI](https://avatars.githubusercontent.com/u/75781367)](https://citadel-ai.com/) [![TestMu AI Open Source Office (Formerly LambdaTest)](https://avatars.githubusercontent.com/u/171592363)](https://www.testmuai.com/)

### 技术赞助商

技术赞助者允许我们免费利用其产品和服务,作为对开放源生态系统和我们工作的贡献的一部分.

[![Netlify](https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/netlify-icon.svg)](https://netlify.com/) [![Algolia](https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/algolia-icon.svg)](https://algolia.com/) [![1Password](https://raw.githubusercontent.com/eslint/eslint.org/main/src/assets/images/techsponsors/1password-icon.svg)](https://1password.com/)

## 常见问题

### @eslint/plugin-kit 是什么？

用于建设ESLint插件的公用事业\.

### 如何安装 @eslint/plugin-kit？

在项目目录运行 npm install @eslint/plugin-kit，或使用 pnpm add / yarn add / bun add。

### @eslint/plugin-kit 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @eslint/plugin-kit 支持 ESM 还是 CommonJS？

同时提供 ES Module 与 CommonJS 入口，import 和 require\(\) 都可以使用，打包工具会自动选择合适的版本。

### @eslint/plugin-kit 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js ^20\.19\.0 \|\| ^22\.13\.0 \|\| \>=24。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @eslint/plugin-kit 可以商用吗？

@eslint/plugin-kit 使用 Apache-2\.0 许可。宽松许可：允许商业使用、修改和再分发，并附带明确的专利授权；分发时需保留许可与 NOTICE 文件，修改过的文件要注明改动。以上为概括，不构成法律意见，请以许可原文为准。

### @eslint/plugin-kit 的下载量有多少？

根据 npm 官方下载统计，@eslint/plugin-kit 在 2026/10/07 – 2026/11/05 这 30 天内被下载 527,599,510 次，日均约 17,586,650 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @eslint/plugin-kit 还在维护吗？

最新版本 0\.7\.3 发布于 2026/09/04（2 个月前）；至今共发布 25 个版本，其中过去 12 个月发布了 8 个；首次发布于 2024/08/27。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

