@eslint/plugin-kit

中文文档
0.7.3发布于 2026/09/04Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 5.3亿

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

中文使用文档
文档目录14 节
  1. 说明
  2. 安装
  3. 使用量
  4. ConfigCommentParser
  5. VisitNodeStep 财务报告和审定财务报表 CallMethodStep
  6. Directive
  7. TextSourceCodeBase
  8. 许可证
  9. 发起人
  10. 白金赞助商
  11. 黄金赞助商
  12. 白银赞助商
  13. 青铜赞助商
  14. 技术赞助商

ESLint 插件包

说明

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

安装

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

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

对于德诺:

deno add @eslint/plugin-kit

使用量

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

ConfigCommentParser

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

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."
}

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

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 - 将各种论据传入规则的访客方法.

例如:

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 - 要传递到方法上的一系列论据.

例如:

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 对象。 举个例子:

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

举个例子:

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的持续维护与发展. 成为发起人 让你的标志在我们的读取器和 网站。 。 。 .

白金赞助商

Automattic

黄金赞助商

Qlty Software Shopify CodeRabbit

白银赞助商

Vite Liftoff StackBlitz

青铜赞助商

Cybozu SAP Icons8 Discord GitBook Citadel AI TestMu AI Open Source Office (Formerly LambdaTest)

技术赞助商

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

Netlify Algolia 1Password

版本与发布记录

@eslint/plugin-kit 共发布 25 个版本,首次发布于 2024/08/27;过去 12 个月发布了 8 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.7.3latest2026/09/042 个月前
0.7.22026/05/305 个月前
0.7.12026/04/086 个月前
0.7.02026/04/047 个月前
0.6.12026/03/078 个月前
0.6.02026/01/319 个月前

关于 @eslint/plugin-kit 的常见问题

@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 和提交记录判断。