词汇表

此页面可作为与 ESLint 相关的常用术语的参考。

¥This page serves as a reference for common terms associated with ESLint.

A

抽象语法树 (AST)

¥Abstract Syntax Tree (AST)

代码语法的结构化表示。

¥A structured representation of code syntax.

AST 中源代码的每个部分都称为 node。每个节点可以具有任意数量的属性,包括存储子节点的属性。

¥Each section of source code in an AST is referred to as a node. Each node may have any number of properties, including properties that store child nodes.

ESLint 使用的 AST 格式是 ESTree 格式。

¥The AST format used by ESLint is the ESTree format.

ESLint 规则 被赋予一个 AST,当它们检测到 violation 时,可能会在 AST 的一部分上生成 violations

¥ESLint rules are given an AST and may produce violations on parts of the AST when they detect a violation.

C

配置文件(配置文件)

¥Config File (Configuration File)

包含 ESLint 如何解析文件和运行 规则 的首选项的文件。

¥A file containing preferences for how ESLint should parse files and run rules.

ESLint 配置文件的命名类似于 eslint.config.(c|m)js。每个配置文件导出一个包含 配置对象配置数组

¥ESLint config files are named like eslint.config.(c|m)js. Each config file exports a config array containing config objects.

例如,此 eslint.config.js 文件在错误 severity 处启用 prefer-const rule

¥For example, this eslint.config.js file enables the prefer-const rule at the error severity:

export default [
    {
        rules: {
            "prefer-const": "error",
        },
    },
];

详细信息请参见 配置文件

¥See Configuration Files for more details.

配置数组

¥Config Array

配置文件 内有 配置对象 的数组。

¥An array of config objects within a config file.

每个配置文件导出一个配置对象数组。数组中的对象按顺序求值:后面的对象可能会覆盖前面对象中指定的设置。

¥Each config file exports an array of config objects. The objects in the array are evaluated in order: later objects may override settings specified in earlier objects.

详细信息请参见 配置文件

¥See Configuration Files for more details.

配置对象

¥Config Object

配置文件 条目指定 ESLint 在一组文件上执行所需的所有信息。

¥A config file entry specifying all of the information ESLint needs to execute on a set of files.

每个配置对象可以包括描述要运行哪些文件、如何处理不同文件类型、要包括哪些 plugins 以及如何运行 规则 的属性。

¥Each configuration object may include properties describing which files to run on, how to handle different file types, which plugins to include, and how to run rules.

详细信息请参见 配置文件 > 配置对象

¥See Configuration Files > Configuration Objects for more details.

E

ESQuery

ESLint 用于解析 选择器 语法以在 AST 中查询 nodes 的库。

¥The library used by ESLint to parse selector syntax for querying nodes in an AST.

ESQuery 解释 AST 节点属性的 CSS 语法。ESQuery 选择器的示例包括:

¥ESQuery interprets CSS syntax for AST node properties. Examples of ESQuery selectors include:

  • BinaryExpression:选择 BinaryExpression 类型的所有节点

    ¥BinaryExpression: selects all nodes of type BinaryExpression

  • BinaryExpression[operator='+']:选择所有运算符为 + 的 BinaryExpression 节点

    ¥BinaryExpression[operator='+']: selects all BinaryExpression nodes whose operator is +

  • BinaryExpression > Literal[value=1]:选择其直接父级为 BinaryExpression 且值为 1 的所有 Literal 节点

    ¥BinaryExpression > Literal[value=1]: selects all Literal nodes with value 1 whose direct parent is a BinaryExpression

有关 ESQuery 格式的更多信息,请参阅 github.com/estools/esquery

¥See github.com/estools/esquery for more information on the ESQuery format.

ESTree

ESLint 用于如何将 JavaScript 语法表示为 AST 的格式。

¥The format used by ESLint for how to represent JavaScript syntax as an AST.

例如,代码 1 + 2; 的 ESTree 表示将是一个大致如下的对象:

¥For example, the ESTree representation of the code 1 + 2; would be an object roughly like:

{
    "type": "ExpressionStatement",
    "expression": {
        "type": "BinaryExpression",
        "left": {
            "type": "Literal",
            "value": 1,
            "raw": "1"
        },
        "operator": "+",
        "right": {
            "type": "Literal",
            "value": 2,
            "raw": "2"
        }
    }
}

ESLint 等 静态分析 工具通常通过将语法转换为 ESTree 格式的 AST 来进行操作。

¥Static analysis tools such as ESLint typically operate by converting syntax into an AST in the ESTree format.

有关 ESTree 规范的更多信息,请参阅 github.com/estree/estree

¥See github.com/estree/estree for more information on the ESTree specification.

F

修复

¥Fix

rule violation 的可选增强,描述如何自动纠正违规行为。

¥An optional augmentation to a rule violation that describes how to automatically correct the violation.

修复通常是 “safe” 才能自动应用:它们不应导致代码行为发生变化。当使用 --fix 标志运行时,ESLint 尝试在 report 中应用尽可能多的修复,但不能保证所有修复都会被应用。修复也可以通过常见的编辑器扩展来应用。

¥Fixes are generally “safe” to apply automatically: they shouldn’t cause code behavior changes. ESLint attempts to apply as many fixes as possible in a report when run with the --fix flag, though there is no guarantee that all fixes will be applied. Fixes may also be applied by common editor extensions.

违反规则还可能包括不安全且不会以 suggestions 的形式自动应用的文件更改。

¥Rule violations may also include file changes that are unsafe and not automatically applied in the form of suggestions.

扁平配置

¥Flat Config

ESLint 当前的配置文件格式。

¥The current configuration file format for ESLint.

扁平配置文件以 eslint.config.(c|m)?js 格式命名。“扁平” 配置文件之所以如此命名,是因为所有嵌套都必须在一个配置文件中完成。相比之下,“旧版” 配置格式 允许在项目内的子目录中嵌套配置文件。

¥Flat config files are named in the format eslint.config.(c|m)?js. “Flat” config files are named as such because all nesting must be done in one configuration file. In contrast, the “Legacy” config format allowed nesting configuration files in sub-directories within a project.

你可以在 ESLint 的新配置系统,第 2 部分:扁平配置简介 中阅读有关扁平配置背后动机的更多信息。

¥You can read more about the motivations behind flat configurations in ESLint’s new config system, Part 2: Introduction to flat config.

格式化器(代码检查)

¥Formatter (Linting)

一个包含 ESLint 生成的 report 的包。

¥A package that presents the report generated by ESLint.

ESLint 附带了多个内置报告器,包括 stylish(默认)、jsonhtml

¥ESLint ships with several built-in reporters, including stylish (default), json, and html.

欲了解更多信息,请参阅 格式化器

¥For more information, see Formatters.

格式化器(工具)

¥Formatter (Tool)

一款 静态分析 工具,可快速重新格式化代码,而无需更改其逻辑或名称。

¥A static analysis tool that quickly reformats code without changing its logic or names.

格式化程序一般只修改代码的 “trivia”,例如分号、空格、换行符和一般的空格。琐碎的更改通常不会修改代码的 AST

¥Formatters generally only modify the “trivia” of code, such as semicolons, spacing, newlines, and whitespace in general. Trivia changes generally don’t modify the AST of code.

生态系统中常见的格式化程序包括 Prettierdprint

¥Common formatters in the ecosystem include Prettier and dprint.

请注意,虽然 ESLint 是 linter 而不是格式化程序,但 ESLint 规则也可以将格式更改应用于源代码。有关格式化规则的更多信息,请参阅 格式(规则)

¥Note that although ESLint is a linter rather than a formatter, ESLint rules can also apply formatting changes to source code. See Formatting (Rule) for more information on formatting rules.

格式(规则)

¥Formatting (Rule)

仅针对 formatting 问题的规则,例如分号和空格。这些规则不会更改应用逻辑,并且是 文体规则 的子集。

¥A rule that solely targets formatting concerns, such as semicolons and whitespace. These rules don’t change application logic and are a subset of Stylistic rules.

ESLint 不再推荐格式化规则,并且之前已弃用其内置格式化规则。ESLint 建议使用专用格式化程序,例如 Prettierdprint。或者,ESLint 风格项目 提供与格式相关的 lint 规则。

¥ESLint no longer recommends formatting rules and previously deprecated its built-in formatting rules. ESLint recommends instead using a dedicated formatter such as Prettier or dprint. Alternately, the ESLint Stylistic project provides formatting-related lint rules.

欲了解更多信息,请参阅 弃用格式规则

¥For more information, see Deprecation of formatting rules.

G

全局声明

¥Global Declaration

对应该在运行时存在的 JavaScript 全局变量 的 ESLint 的描述。

¥A description to ESLint of a JavaScript global variable that should exist at runtime.

全局声明告知检查全局变量是否正确使用的 lint 规则。例如,no-undef 规则 将创建对未在配置的全局变量列表中定义的全局变量的引用的违规。

¥Global declarations inform lint rules that check for proper uses of global variables. For example, the no-undef rule will create a violation for references to global variables not defined in the configured list of globals.

配置文件 将全局变量定义为 JavaScript 对象。

¥Config files have globals defined as JavaScript objects.

有关配置全局变量的信息,请参阅 配置语言选项 > 指定全局变量

¥For information about configuring globals, see Configure Language Options > Specifying Globals.

全局变量

¥Global Variable

存在于全局作用域内的运行时变量,这意味着所有模块和脚本都可以访问它。

¥A runtime variable that exists in the global scope, meaning all modules and scripts have access to it.

JavaScript 中的全局变量在 globalThis 对象上声明(通常在 Node.js 中别名为 global,在浏览器中别名为 window)。

¥Global variables in JavaScript are declared on the globalThis object (generally aliased as global in Node.js and window in browsers).

你可以让 ESLint 了解你的代码使用哪些全局变量与 全局声明

¥You can let ESLint know which global variables your code uses with global declarations.

I

内联配置(配置注释)

¥Inline Config (Configuration Comment)

将规则配置为不同严重性和/或选项集的源代码注释。

¥A source code comment that configures a rule to a different severity and/or set of options.

内联配置使用与 配置文件 类似的语法来按名称、新严重性以及可选的规则新选项指定任意数量的规则。例如,以下内联配置注释同时禁用 eqeqeq 规则并将 curly 规则设置为 "error"

¥Inline configs use similar syntax as config files to specify any number of rules by name, their new severity, and optionally new options for the rules. For example, the following inline config comment simultaneously disables the eqeqeq rule and sets the curly rule to "error":

/* eslint eqeqeq: "off", curly: "error" */

有关内联配置注释的文档,请参阅 规则 > 使用配置注释

¥For documentation on inline config comments, see Rules > Using configuration comments.

L

旧版配置

¥Legacy Config

ESLint 之前的配置文件格式,现已被 “扁平” 配置 取代。

¥The previous configuration file format for ESLint, now superseded by “Flat” config.

旧版 ESLint 配置以 .eslintrc.* 格式命名,并允许跨文件嵌套在项目的子目录中。

¥Legacy ESLint configurations are named in the format .eslintrc.* and allowed to be nested across files within sub-directories in a project.

你可以在 ESLint 的新配置系统,第 1 部分:背景 中阅读有关旧配置生命周期的更多信息。

¥You can read more about the lifetime of legacy configurations in ESLint’s new config system, Part 1: Background.

Linter

一个 静态分析 工具,可以对源代码上运行一组 规则 的结果进行 report 处理。每个规则可以报告源代码中任意数量的 violations

¥A static analysis tool that can report the results from running a set of rules on source code. Each rule may report any number of violations in the source code.

ESLint 是 JavaScript 和其他 Web 技术常用的 linter。

¥ESLint is a commonly used linter for JavaScript and other web technologies.

请注意,linter 与 formatters类型检查器 是分开的。

¥Note that a linter is separate from formatters and type checkers.

逻辑规则

¥Logical Rule

rule 检查代码如何运行以发现问题。

¥A rule that inspects how code operates to find problems.

许多逻辑规则寻找可能的崩溃(例如 no-undef)、意外行为(例如 no-sparse-arrays)和未使用的代码(例如 no-unused-vars),

¥Many logical rules look for likely crashes (e.g. no-undef), unintended behavior (e.g. no-sparse-arrays), and unused code (e.g no-unused-vars),

你可以在 规则 > 可能出现的问题 下查看 ESLint 附带的逻辑规则的完整列表

¥You can see the full list of logical rules that ship with ESLint under Rules > Possible Problems

N

Node

AST 中的一段代码。

¥A section of code within an AST.

每个节点代表源代码中的一种语法。例如,1 + 2; 的 AST 中的 1 + 2 是一个 BinaryExpression。

¥Each node represents a type of syntax found in source code. For example, the 1 + 2 in the AST for 1 + 2; is a BinaryExpression.

请参阅 #esquery 了解 ESLint 用于解析 选择器 的库,该库允许 规则 搜索节点。

¥See #esquery for the library ESLint uses to parse selectors that allow rules to search for nodes.

O

覆盖

¥Override

配置对象内联配置 设置新的严重性和/或规则选项来取代先前设置的严重性和/或选项时。

¥When a config object or inline config sets a new severity and/or rule options that supersede previously set severity and/or options.

以下 配置文件*.test.js 文件中将 no-unused-expressions"error" 覆盖到 "off"

¥The following config file overrides no-unused-expressions from "error" to "off" in *.test.js files:

export default [
  {
    rules: {
      "no-unused-expressions": "error"
    }
  },
  {
    files: ["*.test.js"],
    rules: {
        "no-unused-expressions": "off"
    }
  }
];

以下 内联配置no-unused-expressions 设置为 "error"

¥The following inline config sets no-unused-expressions to "error":

/* eslint no-unused-expressions: "error" */

有关旧配置中覆盖的更多信息,请参阅 配置文件(已弃用)> 覆盖如何工作?

¥For more information on overrides in legacy configs, see Configuration Files (Deprecated) > How do overrides work?.

P

解析器

¥Parser

包含读取字符串并将其转换为标准化格式的方法的对象。

¥An object containing a method that reads in a string and converts it to a standardized format.

ESLint 使用解析器将源代码字符串转换为 AST 形状。默认情况下,ESLint 使用 埃斯普雷 解析器,它生成与标准 JavaScript 运行时和版本兼容的 AST。

¥ESLint uses parsers to convert source code strings into an AST shape. By default, ESLint uses the Espree parser, which generates an AST compatible with standard JavaScript runtimes and versions.

自定义解析器让 ESLint 解析非标准 JavaScript 语法。通常,自定义解析器作为可共享配置或插件的一部分包含在内,因此你不必直接使用它们。例如,@typescript-eslint/parsertypescript-eslint 项目中包含的自定义解析器,可让 ESLint 解析 TypeScript 代码。

¥Custom parsers let ESLint parse non-standard JavaScript syntax. Often custom parsers are included as part of shareable configurations or plugins, so you don’t have to use them directly. For example, @typescript-eslint/parser is a custom parser included in the typescript-eslint project that lets ESLint parse TypeScript code.

有关使用带有 ESLint 的解析器的更多信息,请参阅 配置解析器

¥For more information on using parsers with ESLint, see Configure a Parser.

插件

¥Plugin

可以包含一组 configurationsprocessors 和/或 规则 的包。

¥A package that can contain a set of configurations, processors, and/or rules.

插件的一个流行用例是强制框架的最佳实践。例如,@angular-eslint/eslint-plugin 包含使用 Angular 框架的最佳实践。

¥A popular use case for plugins is to enforce best practices for a framework. For example, @angular-eslint/eslint-plugin contains best practices for using the Angular framework.

有关详细信息,请参阅 配置插件

¥For more information, refer to Configure Plugins.

处理器

¥Processor

插件的一部分,从其他类型的文件中提取 JavaScript 代码,然后让 ESLint lint JavaScript 代码。

¥A part of a plugin that extracts JavaScript code from other kinds of files, then lets ESLint lint the JavaScript code.

例如,eslint-plugin-markdown 包含一个处理器,可将 Markdown 文件中的 ``` 代码块的文本转换为可检查的代码。

¥For example, eslint-plugin-markdown includes a processor that converts the text of ``` code blocks in Markdown files into code that can be linted.

有关配置处理器的更多信息,请参阅 插件 > 指定处理器

¥For more information on configuring processor, see Plugins > Specify a Processor.

R

报告

¥Report

来自单个 ESLint 运行的 violations 集合。

¥A collection of violations from a single ESLint run.

当 ESLint 在源文件上运行时,它将每个源文件的 AST 传递给每个配置的 rule。每个规则的违规行为集合将被打包在一起并传递到 formatter 以展示给用户。

¥When ESLint runs on source files, it will pass an AST for each source file to each configured rule. The collection of violations from each of the rules will be packaged together and passed to a formatter to be presented to the user.

规则

¥Rule

检查 AST 是否存在预期模式的代码。当规则的期望未得到满足时,它会创建 violation

¥Code that checks an AST for expected patterns. When a rule’s expectation is not met, it creates a violation.

ESLint 提供了大量规则来检查常见的 JavaScript 代码问题。plugins 可能会加载更多规则。

¥ESLint provides a large collection of rules that check for common JavaScript code issues. Many more rules may be loaded in by plugins.

有关所提供规则的概述,请参阅 核心概念 > 规则

¥For an overview of rules provided, see Core Concepts > Rules.

S

选择器

¥Selector

描述如何在 AST 中搜索 nodes 的语法。

¥Syntax describing how to search for nodes within an AST.

ESLint 规则 使用 ESQuery 选择器来查找应检查的节点。

¥ESLint rules use ESQuery selectors to find nodes that should be checked.

严重性

¥Severity

如果有的话,规则配置为运行什么级别的报告。

¥What level of reporting a rule is configured to run, if at all.

ESLint 支持三个严重级别:

¥ESLint supports three levels of severity:

  • "off" (0):不要运行该规则。

    ¥"off" (0): Do not run the rule.

  • "warn" (1):运行规则,但不要因其违规行为而以非零状态代码退出(不包括 --max-warnings

    ¥"warn" (1): Run the rule, but don’t exit with a non-zero status code based on its violations (excluding the --max-warnings flag)

  • "error" (2):运行规则,如果产生任何违规,则以非零状态代码退出

    ¥"error" (2): Run the rule, and exit with a non-zero status code if it produces any violations

有关配置规则的文档,请参阅 配置规则

¥For documentation on configuring rules, see Configure Rules.

可共享配置(配置)

¥Shareable Config (Configuration)

提供预定义 配置文件 配置的模块。

¥A module that provides a predefined config file configurations.

可共享配置可以配置配置文件中的所有相同信息,包括 plugins规则

¥Shareable configs can configure all the same information from config files, including plugins and rules.

可共享的配置通常与 plugins 一起提供。许多插件提供名称如 “recommended” 的配置,以启用其建议的起始规则集。例如,eslint-plugin-solid 提供了一个可共享的推荐配置:

¥Shareable configs are often provided alongside plugins. Many plugins provide configs with names like “recommended” that enable their suggested starting set of rules. For example, eslint-plugin-solid provides a shareable recommended config:

import js from "@eslint/js";
import solid from "eslint-plugin-solid/configs/recommended";

export default [js.configs.recommended, solid];

有关可共享配置的信息,请参阅 共享配置

¥For information on shareable configs, see Share Configurations.

静态分析

¥Static Analysis

在不构建或运行源代码的情况下分析源代码的过程。

¥The process of analyzing source code without building or running it.

诸如 ESLint、formatters类型检查器 之类的 代码检查器 是静态分析工具的示例。

¥Linters such as ESLint, formatters, and type checkers are examples of static analysis tools.

静态分析与动态分析不同,动态分析是在构建和执行源代码后对其进行评估的过程。单元、集成和端到端测试是动态分析的常见示例。

¥Static analysis is different from dynamic analysis, which is the process of evaluating source code after it is built and executed. Unit, integration, and end-to-end tests are common examples of dynamic analysis.

文体(规则)

¥Stylistic (Rule)

强制执行偏好而不是逻辑问题的规则。风格字段包括 格式规则、命名约定以及等效语法之间的一致选择。

¥A rule that enforces a preference rather than a logical issue. Stylistic areas include Formatting rules, naming conventions, and consistent choices between equivalent syntaxes.

ESLint 的内置风格规则是功能冻结的:除了支持新的 ECMAScript 版本之外,它们不会收到新功能。

¥ESLint’s built-in stylistic rules are feature frozen: except for supporting new ECMAScript versions, they won’t receive new features.

有关详细信息,请参阅 我们的规则政策的变更弃用格式规则

¥For more information, see Changes to our rules policies and Deprecation of formatting rules.

建议

¥Suggestion

rule violation 的可选增强,描述如何手动调整代码以解决违规问题。

¥An optional augmentation to a rule violation that describes how one may manually adjust the code to address the violation.

自动应用建议通常并不安全,因为它们会导致代码行为发生变化。ESLint 不直接应用建议,但确实为可能选择应用建议的集成提供建议(例如编辑器扩展)。

¥Suggestions are not generally safe to apply automatically because they cause code behavior changes. ESLint does not apply suggestions directly but does provide suggestion to integrations that may choose to apply suggestions (such as an editor extension).

规则违规还可能包括安全的文件更改,并且可以以 fixes 的形式自动应用。

¥Rule violations may also include file changes that are safe and may be automatically applied in the form of fixes.

T

类型检查器

¥Type Checker

一款 静态分析 工具,可帮助你全面了解项目的代码结构和数据形状。

¥A static analysis tool that builds a full understanding of a project’s code constructs and data shapes.

类型检查器通常比 linter 更慢且更全面。传统上,linter 一次仅对单个文件或片段的 AST 进行操作,而类型检查器则了解跨文件依赖和类型。

¥Type checkers are generally slower and more comprehensive than linters. Whereas linters traditionally operate only on a single file’s or snippet’s AST at a time, type checkers understand cross-file dependencies and types.

TypeScript 是最常见的 JavaScript 类型检查器。typescript-eslint 项目提供了允许在 lint 规则中使用类型检查器的集成。

¥TypeScript is the most common type checker for JavaScript. The typescript-eslint project provides integrations that allow using type checker in lint rules.

V

违反

¥Violation

来自 rule 的指示,表明代码区域不符合规则的期望。

¥An indication from a rule that an area of code doesn’t meet the expectation of the rule.

规则违规指示源代码中的范围以及解释违规的错误消息。违规还可以选择性地包括 fix 和/或 suggestions,指示如何改进违规代码。

¥Rule violations indicate a range in source code and error message explaining the violation. Violations may also optionally include a fix and/or suggestions that indicate how to improve the violating code.