Index

词汇表

此页面可作为与 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 中源代码的每个部分都被称为一个 节点。每个节点可以有任意数量的属性,包括存储子节点的属性。

🌐 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,并且在检测到 违规 时,可能在 AST 的部分生成 违规

🌐 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 文件在 error 严重性 下启用 prefer-const 规则

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

import { defineConfig } from "eslint/config";

export default defineConfig([
	{
		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.

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

🌐 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

ES查询

🌐 ESQuery

ESLint 用于解析 selector 语法以查询 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[operator='+']:选择所有 BinaryExpression 节点,其 operator+
  • BinaryExpression > Literal[value=1]:选择所有 Literal 节点,其 value1,且其直接父节点是 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 来操作。

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

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

F

修复

🌐 Fix

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

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

修复通常是“安全的”,可以自动应用:它们不应导致代码行为的变化。 ESLint 在使用 --fix 标志运行时,会尝试在 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.

违规行为还可能包括不安全且不会以建议形式自动应用的文件更改。

🌐 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”。 “平面”配置文件之所以如此命名,是因为所有嵌套都必须在一个配置文件中完成。 相比之下,[“Legacy”配置格式(#legacy-config)允许在项目的子目录中嵌套配置文件。

🌐 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 生成的报告的包。

🌐 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.

更多信息,请参见 Formatters

🌐 For more information, see Formatters.

格式化器(工具)

🌐 Formatter (Tool)

一个静态分析工具,可以快速重新格式化代码而不改变其逻辑或名称。

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

格式化工具通常只会修改代码的“琐碎部分”,例如分号、空格、换行以及一般的空白字符。 琐碎部分的改变通常不会修改代码的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 规则也可以对源代码应用格式化更改。有关格式化规则的更多信息,请参见 Formatting (Rule)

🌐 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)

一个只针对格式问题的规则,例如分号和空白字符。这些规则不会改变应用逻辑,是风格规则的子集。

🌐 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 Stylistic 项目 提供与格式化相关的 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

向 ESLint 描述在运行时应存在的 JavaScript 全局变量

🌐 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 对象。

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

🌐 For information about configuring globals, see Configure Language Options > Specify 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 > Use configuration comments.

L

旧版配置

🌐 Legacy Config

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

🌐 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

一个静态分析工具,可以报告运行一组规则在源代码上的结果。每条规则可能在源代码中报告任意数量的违规情况。

🌐 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格式化工具类型检查器 是分开的。

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

逻辑规则

🌐 Logical Rule

一个规则,用于检查代码的运行方式以发现问题。

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

许多逻辑规则会寻找可能的崩溃(例如 no-undef)、非预期行为(例如 [no-sparse-arrays](…/…/rules/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).

你可以在 Rules > Possible Problems 查看随 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 文件中的 "error"no-unused-expressions 覆盖为 "off"

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

import { defineConfig } from "eslint/config";

export default defineConfig([
	{
		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" */

P

解析器

🌐 Parser

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

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

ESLint 使用解析器将源代码字符串转换为 AST 结构。 默认情况下,ESLint 使用 Espree 解析器,它生成与标准 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/parser 是包含在 typescript-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

一个可以包含一组配置处理器和/或规则的包。

🌐 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/markdown 包含一个处理器,它将 Markdown 文件中 ``` 代码块的文本转换为可以进行代码检查的代码。

🌐 For example, @eslint/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 运行的 违规 集合。

🌐 A collection of violations from a single ESLint run.

当 ESLint 在源文件上运行时,它会将每个源文件的 AST 传递给每个配置的 规则。 每条规则的违规集合将被打包在一起,并传递给 格式化器 以呈现给用户。

🌐 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中预期模式的代码。当规则的预期未被满足时,它会创建一个违规

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

ESLint 提供了大量规则,用于检查常见的 JavaScript 代码问题。更多规则可以通过 插件 加载。

🌐 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):不要运行该规则。
  • "warn" (1):运行规则,但不要根据其违规情况退出非零状态码(不包括 --max-warnings 标志
  • "error" (2):运行规则,如果产生任何违规,则以非零状态码退出

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

🌐 For documentation on configuring rules, see Configure Rules.

可共享配置(配置)

🌐 Shareable Config (Configuration)

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

🌐 A module that provides a predefined config file configurations.

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

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

可共享的配置通常与插件一起提供。许多插件提供名为“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 { defineConfig } from "eslint/config";
import js from "@eslint/js";
import solid from "eslint-plugin-solid/configs/recommended";

export default defineConfig([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 这样的 代码检查工具格式化工具类型检查工具 都是静态分析工具的例子。

静态分析不同于_动态_分析,后者是指在源代码构建并执行后进行评估的过程。单元测试、集成测试和端到端测试是动态分析的常见例子。

🌐 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

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

🌐 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).

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

🌐 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.

类型检查器通常比代码风格检查工具更慢但更全面。而传统上,代码风格检查工具通常只在单个文件或代码片段的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 规则中使用类型检查器。

V

违反

🌐 Violation

来自 规则 的指示,表明一段代码未达到该规则的预期。

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

规则违规表示源代码中的范围以及解释违规的错误消息。违规还可以选择性地包含一个修复和/或建议,以说明如何改进违规代码。

🌐 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.