# @elsikora/eslint-plugin-kit 中文文档

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

版本：0\.2\.7
npm 元数据更新时间：2025-03-04T19:41:19.276Z
中文译文更新时间：2026-11-13T14:12:44.146Z
软件包许可：Apache-2\.0

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

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

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

- 包体积（解压后）：76 kB，7 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- Node.js 要求：^18\.18\.0 \|\| ^20\.9\.0 \|\| \>=21\.1\.0
- 运行依赖：2 个
- 首次发布：2025-03-04T19:41:18.593Z
- 最新版本发布：2025-03-04T19:41:18.890Z
- 近 30 天下载量：11195（2026-10-14 至 2026-11-12，npm 官方下载统计）
- 已发布版本数：1（近 12 个月 0 个）

## 中文简介

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

## 安装

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

也可以使用：`pnpm add @elsikora/eslint-plugin-kit`、`yarn add @elsikora/eslint-plugin-kit`、`bun add @elsikora/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 install @eslint/plugin-kit
```

对于德诺:

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

## 使用量

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

-   `ConfigCommentParser` - 用于解析ESLint配置注释(即, `/* eslint-disable rule */`(中文(简体) )
-   `VisitNodeStep` 财务报告和审定财务报表 `CallMethodStep` - 用于帮助执行 `SourceCode#traverse()`
-   `Directive` - 用于帮助执行 `SourceCode#getDisableDirectives()`
-   `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, semi -- 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, semi"
    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, semi");
console.log(Object.entries(list)); // [["prefer-const", true], ["semi", 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(
    "semi:[error, never], prefer-const: warn",
);
console.log(Object.entries(jsonLike.config)); // [["semi", ["error", "never"]], ["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(node)` - 得到一个节点的位置。 为具有ESLint风格的节点工作 `loc` 具有Unist 风格的属性和节点 [`position` 属性](https://github.com/syntax-tree/unist?tab=readme-ov-file#position)。如果使用不同位置格式的AST,你还需要自己执行这种方法.
-   `getRange(node)` - 获得源文本中节点的范围。 为具有ESLint风格的节点工作 `range` 具有Unist 风格的属性和节点 [`position` 属性](https://github.com/syntax-tree/unist?tab=readme-ov-file#position)。如果使用不同范围格式的AST,则还需要自己执行这种方法.
-   `getText(nodeOrToken, charsBefore, charsAfter)` - 获得附有范围信息的给定节点或信使的源文本。 可选的,可以在给定的节点或令牌前后返回附加字符. 璶 `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.#parent.set(node, parent);

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

        return steps;
    }
}
```

一般来说,收集父母信息是安全的 `traverse()` 方法为 `getParent()` 财务报告和审定财务报表 `getAncestor()` 只能从规则中调用 一旦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/) [![Airbnb](https://images.opencollective.com/airbnb/d327d66/logo.png)](https://www.airbnb.com/)

### 黄金赞助商

[![Qlty Software](https://images.opencollective.com/qltysh/33d157d/logo.png)](https://qlty.sh/) [![trunk.io](https://images.opencollective.com/trunkio/fb92d60/avatar.png)](https://trunk.io/)

### 白银赞助商

[![Vite](https://images.opencollective.com/vite/e6d15e1/logo.png)](https://vite.dev/) [![JetBrains](https://images.opencollective.com/jetbrains/fe76f99/logo.png)](https://www.jetbrains.com/) [![Liftoff](https://images.opencollective.com/liftoff/5c4fa84/logo.png)](https://liftoff.io/) [![American Express](https://avatars.githubusercontent.com/u/3853301)](https://americanexpress.io/) [![StackBlitz](https://avatars.githubusercontent.com/u/28635252)](https://stackblitz.com/)

### 青铜赞助商

[![Cybozu](https://images.opencollective.com/cybozu/933e46d/logo.png)](https://cybozu.co.jp/) [![Anagram Solver](https://images.opencollective.com/anagram-solver/2666271/logo.png)](https://www.crosswordsolver.org/anagram-solver/) [![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/) [![Neko](https://avatars.githubusercontent.com/u/11081491)](https://nolebase.ayaka.io/) [![Nx](https://avatars.githubusercontent.com/u/23692104)](https://nx.dev/) [![Mercedes-Benz Group](https://avatars.githubusercontent.com/u/34240465)](https://opensource.mercedes-benz.com/) [![HeroCoders](https://avatars.githubusercontent.com/u/37549774)](https://herocoders.com/) [![LambdaTest](https://avatars.githubusercontent.com/u/171592363)](https://www.lambdatest.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/)

## 常见问题

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

