@eslint/plugin-kit

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

Utilities for building ESLint plugins.

原始使用文档
文档目录14 节
  1. Description
  2. Installation
  3. Usage
  4. ConfigCommentParser
  5. VisitNodeStep and CallMethodStep
  6. Directive
  7. TextSourceCodeBase
  8. License
  9. Sponsors
  10. Platinum Sponsors
  11. Gold Sponsors
  12. Silver Sponsors
  13. Bronze Sponsors
  14. Technology Sponsors

ESLint Plugin Kit

Description

A collection of utilities to help build ESLint plugins.

Installation

For Node.js and compatible runtimes:

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

For Deno:

deno add @eslint/plugin-kit

Usage

This package exports the following utilities:

ConfigCommentParser

To use the ConfigCommentParser class, import it from the package and create a new instance, such as:

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

There are different styles of directive values that you'll need to parse separately to get the correct format:

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 and CallMethodStep

The VisitNodeStep and CallMethodStep classes represent steps in the traversal of source code. They implement the correct interfaces to return from the SourceCode#traverse() method.

The VisitNodeStep class is the more common of the two, where you are describing a visit to a particular node during the traversal. The constructor accepts three arguments:

  • target - the node being visited. This is used to determine the method to call inside of a rule. For instance, if the node's type is Literal then ESLint will call a method named Literal() on the rule (if present).
  • phase - either 1 for enter or 2 for exit.
  • args - an array of arguments to pass into the visitor method of a rule.

For example:

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

The CallMethodStep class is less common and is used to tell ESLint to call a specific method on the rule. The constructor accepts two arguments:

  • target - the name of the method to call, frequently beginning with "on" such as "onCodePathStart".
  • args - an array of arguments to pass to the method.

For example:

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

The Directive class represents a disable directive in the source code and implements the Directive interface from @eslint/core. You can tell ESLint about disable directives using the SourceCode#getDisableDirectives() method, where part of the return value is an array of Directive objects. Here's an example:

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

The TextSourceCodeBase class is intended to be a base class that has several of the common members found in SourceCode objects already implemented. Those members are:

  • lines - an array of text lines that is created automatically when the constructor is called.
  • getLoc(nodeOrToken) - gets the location of a node or token. Works for nodes that have the ESLint-style loc property and nodes that have the Unist-style position property. If you're using an AST with a different location format, you'll still need to implement this method yourself.
  • getLocFromIndex(index) - Converts a source text index into a { line: number, column: number } pair. (For this method to work, the root node should always cover the entire source code text, and the getLoc() method needs to be implemented correctly.)
  • getIndexFromLoc(loc) - Converts a { line: number, column: number } pair into a source text index. (For this method to work, the root node should always cover the entire source code text, and the getLoc() method needs to be implemented correctly.)
  • getRange(nodeOrToken) - gets the range of a node or token within the source text. Works for nodes that have the ESLint-style range property and nodes that have the Unist-style position property. If you're using an AST with a different range format, you'll still need to implement this method yourself.
  • getText(node, beforeCount, afterCount) - gets the source text for the given node that has range information attached. If node is omitted or falsy (such as undefined, null, or an empty string), the entire source text is returned. Optionally, can return additional characters before and after the given node. As long as getRange() is properly implemented, this method will just work.
  • getAncestors(node) - returns the ancestry of the node. In order for this to work, you must implement the getParent() method yourself.

Here's an example:

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

In general, it's safe to collect the parent information during the traverse() method as getParent() and getAncestors() will only be called from rules once the AST has been traversed at least once.

License

Apache 2.0

Sponsors

The following companies, organizations, and individuals support ESLint's ongoing maintenance and development. Become a Sponsor to get your logo on our READMEs and website.

Platinum Sponsors

Automattic

Gold Sponsors

Qlty Software Shopify CodeRabbit

Silver Sponsors

Vite Liftoff StackBlitz

Bronze Sponsors

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

Technology Sponsors

Technology sponsors allow us to use their products and services for free as part of a contribution to the open source ecosystem and our work.

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 是什么?

Utilities for building ESLint plugins.

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