迁移至 v10.x

ESLint v10.0.0 是 ESLint 的一个主要版本,因此包含一些需要注意的重大更改。本指南旨在引导你完成重大更改。

¥ESLint v10.0.0 is a major release of ESLint, and as such, has several breaking changes that you need to be aware of. This guide is intended to walk you through the breaking changes.

下面的列表大致按每个更改预计会影响的用户数量排序,其中第一个项目预计会影响最多的用户。

¥The lists below are ordered roughly by the number of users each change is expected to affect, where the first items are expected to affect the most users.

目录

¥Table of Contents

对用户的重大更改

¥Breaking changes for users

对插件开发者的重大更改

¥Breaking changes for plugin developers

对集成开发者的重大更改

¥Breaking changes for integration developers


不再支持 Node.js v20.19、v21、v23 以下版本

¥ Node.js < v20.19, v21, v23 are no longer supported

ESLint 从 v10.0.0 开始正式停止支持以下 Node.js 版本。ESLint 现在支持以下版本的 Node.js:

¥ESLint is officially dropping support for these versions of Node.js starting with ESLint v10.0.0. ESLint now supports the following versions of Node.js:

  • Node.js v20.19.0 及以上版本

    ¥Node.js v20.19.0 and above

  • Node.js v22.13.0 及以上版本

    ¥Node.js v22.13.0 and above

  • Node.js v24 及以上版本

    ¥Node.js v24 and above

要解决:使用 ESLint v10.0.0 时,请确保至少升级到 Node.js v20.19.0。需要仔细检查的一件重要事情是通过编辑器集成使用 ESLint 时编辑器支持的 Node.js 版本。如果你无法升级,我们建议你继续使用 ESLint v9,直到你可以升级 Node.js 为止。

¥To address: Make sure you upgrade to at least Node.js v20.19.0 when using ESLint v10.0.0. One important thing to double check is the Node.js version supported by your editor when using ESLint via editor integrations. If you are unable to upgrade, we recommend continuing to use ESLint v9 until you are able to upgrade Node.js.

相关问题):#19969

¥Related issue(s): #19969

eslint:recommended 已更新

¥ eslint:recommended has been updated

eslint:recommended 中启用了三条新规则:

¥Three new rules have been enabled in eslint:recommended:

要解决:修复错误或禁用这些规则。

¥To address: Fix errors or disable these rules.

相关问题):#19966

¥Related issue(s): #19966

新的配置文件查找算法

¥ New configuration file lookup algorithm

在 ESLint v9 中,可以使用 v10_config_lookup_from_file 特性标志启用备用配置查找行为。此行为使 ESLint 从每个被检查文件的目录开始,向上搜索到文件系统根目录,从而找到 eslint.config.*。在 ESLint v10.0.0 中,此行为现已成为默认行为,并且已移除 v10_config_lookup_from_file 标志。现在尝试使用此标志将导致错误。

¥In ESLint v9, the alternate config lookup behavior could be enabled with the v10_config_lookup_from_file feature flag. This behavior made ESLint locate eslint.config.* by starting from the directory of each linted file and searching up towards the filesystem root. In ESLint v10.0.0, this behavior is now the default and the v10_config_lookup_from_file flag has been removed. Attempting to use this flag will now result in an error.

要解决:

¥To address:

  • 移除设置中所有对该标志的使用:

    ¥Remove any usage of the flag in your setup:

    • CLI:移除 --flag v10_config_lookup_from_file

      ¥CLI: remove --flag v10_config_lookup_from_file.

    • 环境:从 ESLINT_FLAGS 中移除 v10_config_lookup_from_file

      ¥Environment: remove v10_config_lookup_from_file from ESLINT_FLAGS.

    • API:从传递给 new ESLint()new Linter()flags 数组中移除 "v10_config_lookup_from_file"

      ¥API: remove "v10_config_lookup_from_file" from the flags array passed to new ESLint() or new Linter().

  • 如果你依赖于之前的(基于当前工作目录的)查找行为,请提供显式的配置路径。使用 --config path/to/eslint.config.js

    ¥If you relied on the previous (cwd-based) lookup behavior, provide an explicit config path with --config path/to/eslint.config.js.

相关问题):RFC120, #19967

¥Related issue(s): RFC120, #19967

不再支持旧的配置格式

¥ Old config format no longer supported

ESLint v9 引入了基于 eslint.config.js 文件的 新的默认配置格式。在 v9 版本中,仍可通过将 ESLINT_USE_FLAT_CONFIG 环境变量设置为 false 来启用使用 .eslintrc.eslintrc.json旧格式

¥ESLint v9 introduced a new default configuration format based on the eslint.config.js file. The old format, which used .eslintrc or .eslintrc.json, could still be enabled in v9 by setting the ESLINT_USE_FLAT_CONFIG environment variable to false.

从 ESLint v10.0.0 开始,不再支持旧的配置格式。

¥Starting with ESLint v10.0.0, the old configuration format is no longer supported.

要解决:

¥To address:

  • 请按照 配置迁移指南 中的说明进行操作。

    ¥Follow the instructions in the configuration migration guide.

  • 请注意,已弃用的 API FlatESLintLegacyESLint 已被移除。请始终使用 ESLint

    ¥Be aware that the deprecated APIs FlatESLint and LegacyESLint have been removed. Always use ESLint instead.

  • Linter 类的 configType 选项不再可以设置为 "eslintrc"。移除使用新配置格式的选项。

    ¥The configType option of the Linter class can no longer be set to "eslintrc". Remove the option to use the new configuration format.

相关问题):#13481

¥Related issue(s): #13481

eslint-env 注释被报告为错误

¥ eslint-env comments are reported as errors

在现已过时的 ESLint v8 配置系统中,可以使用 /* eslint-env */ 注释来定义文件的全局变量。当前的配置系统不支持此类注释,从 ESLint v10.0.0 开始,它们在代码检查期间会被报告为错误。

¥In the now obsolete ESLint v8 configuration system, /* eslint-env */ comments could be used to define globals for a file. The current configuration system does not support such comments, and starting with ESLint v10.0.0, they are reported as errors during linting.

error: /* eslint-env */ comments are no longer supported at file.js:1:1:
> 1 | /* eslint-env node -- Used in Node.js */
    | ^

要解决:移除代码中所有 eslint-env 注释。如果你如果你仍在使用旧的配置系统并且需要迁移方面的帮助,请检查 迁移指南

¥To address: Remove any eslint-env comments from your code. If you are still using the old configuration system and need help migrating, check the migration guide.

相关问题):#13481

¥Related issue(s): #13481

不再支持 Jiti v2.2.0 以下版本

¥ Jiti < v2.2.0 are no longer supported

ESLint 正式停止支持低于 v2.2.0 的 jiti 版本。

¥ESLint is officially dropping support for versions of jiti that are less than v2.2.0.

要解决:如果你使用 TypeScript 编写了配置文件,并且安装了 jiti v2.1.2 或更早版本,请确保在使用 ESLint 时将其更新到至少 2.2.0。 v10.0.0

¥To address: If you’ve authored your config file in TypeScript and have jiti v2.1.2 or earlier installed, be sure to update it to at least 2.2.0 when using ESLint v10.0.0.

相关问题):#19765

¥Related issue(s): #19765

通配符模式中的 POSIX 字符类

¥ POSIX character classes in glob patterns

ESLint v10.0.0 使用最新版本的 minimatch,该版本支持在方括号表达式中使用 POSIX 字符类。ESLint 依赖 minimatch 将文件名与 通配符模式进行匹配,例如在配置中定义的 filesignoresglobalIgnores() 模式,或通过 CLI 传递的模式。

¥ESLint v10.0.0 uses the latest version of minimatch, which supports POSIX character classes in bracket expressions. ESLint relies on minimatch to match file names against glob patterns, such as those defined in files, ignores, and globalIgnores() within a config, or passed via the CLI.

例如,要检查所有名称以大写字母开头的 .js 文件,你现在可以运行:

¥For example, to lint all .js files whose names begin with an uppercase letter, you can now run:

npx eslint "**/[[:upper:]]*.js"

其中,[[:upper:]] 是一个 POSIX 字符类,用于匹配不同字母表中的大写字母。

¥Here, [[:upper:]] is a POSIX character class that matches uppercase letters in different alphabets.

要解决:如果你的配置、CLI 参数或 Node.js API 调用中的任何 通配符模式看起来像是包含 POSIX 字符类,请验证它们是否按预期匹配文件。

¥To address: If any of the glob patterns in your configuration, CLI arguments, or Node.js API calls look like containing a POSIX character class, verify that they match files as intended.

相关问题):eslint/rewrite#66

¥Related issue(s): eslint/rewrite#66

已弃用的 radix 规则选项

¥ Deprecated options of the radix rule

从 ESLint v10.0.0 开始,radix 规则的字符串选项 "always""as-needed" 已被弃用。设置这两个选项中的任何一个都不会改变此规则的行为,该规则现在始终强制提供基数,就像指定 "always" 选项(默认值)时一样。由于默认基数取决于 parseInt() 的第一个参数,因此此规则假定始终需要第二个参数(基数)。

¥As of ESLint v10.0.0, string options "always" and "as-needed" of the radix rule are deprecated. Setting either of these options doesn’t change the behavior of this rule, which now always enforces providing a radix, as it was the case when the "always" option (default) was specified. Since the default radix depends on the first argument of parseInt(), this rule assumes that the second argument (the radix) is always needed.

此规则的默认行为未更改。

¥The default behavior of this rule has not been changed.

要解决:

¥To address:

  • 如果你使用此规则时未指定任何选项,则无需执行任何操作。

    ¥If you are using this rule without any options specified, there is no action required.

  • 如果你使用此规则时显式指定了 "always" 选项,请移除该选项。此规则的行为将保持不变。

    ¥If you are using this rule with the "always" option explicitly specified, remove the option. The behavior of this rule will remain the same.

  • 如果你使用此规则时指定了 "as-needed" 选项,请移除该选项并更新你的代码,使其始终向 parseInt() 函数提供第二个参数。或者,你可以禁用此规则。

    ¥If you are using this rule with the "as-needed" option, remove the option and update your code to always provide the second argument to the parseInt() function. Alternatively, you can disable this rule.

相关问题):#19916

¥Related issue(s): #19916

no-shadow-restricted-names 现在默认报告 globalThis

¥ no-shadow-restricted-names now reports globalThis by default

在 ESLint v10.0.0 中,no-shadow-restricted-names 规则现在默认将 globalThis 视为受限名称。因此,reportGlobalThis 选项现在默认为 true(以前为 false)。因此,现在默认情况下会报告 const globalThis = "foo";function globalThis() {} 之类的声明。

¥In ESLint v10.0.0, the no-shadow-restricted-names rule now treats globalThis as a restricted name by default. Consequently, the reportGlobalThis option now defaults to true (previously false). As a result, declarations such as const globalThis = "foo"; or function globalThis() {} will now be reported by default.

要解决:

¥To address:

  • 重命名名为 globalThis 的本地标识符,以避免覆盖全局标识符。

    ¥Rename local identifiers named globalThis to avoid shadowing the global.

  • 或者,通过显式配置规则来恢复之前的行为:

    ¥Or restore the previous behavior by configuring the rule explicitly:

{
	"rules": {
		"no-shadow-restricted-names": ["error", { "reportGlobalThis": false }]
	}
}

相关问题):#19673

¥Related issue(s): #19673

func-names 模式更加严格

¥ func-names schema is stricter

在 ESLint v10.0.0 中,func-names 规则架构现在不允许选项数组中包含额外的项。以前,包含超出允许选项范围的额外数组元素的配置会被接受,但会被忽略。此类配置现在被视为无效。

¥In ESLint v10.0.0, the func-names rule schema now disallows extra items in the options array. Previously, configurations that included additional array elements beyond the allowed options were accepted but ignored. Such configurations are now considered invalid.

例如,由于额外的元素 "foo",此配置现在无效:

¥For example, this configuration is now invalid due to the extra element "foo":

/*eslint func-names: ["error", "always", { "generators": "never" }, "foo"]*/

要解决:

¥To address:

  • 移除 func-names 配置中所有多余的数组元素,使其仅包含:

    ¥Remove any extra array elements from your func-names configuration so that it contains only:

    • 一个基本字符串选项:"always" | "as-needed" | "never",以及

      ¥a base string option: "always" | "as-needed" | "never", and

    • 可选,一个对象选项:{ "generators": "always" | "as-needed" | "never" }

      ¥optionally, an object option: { "generators": "always" | "as-needed" | "never" }.

相关问题):#20134

¥Related issue(s): #20134

no-invalid-regexpallowConstructorFlags 选项现在只接受唯一项

¥ allowConstructorFlags option of no-invalid-regexp now accepts only unique items

在 ESLint v10.0.0 中,no-invalid-regexpallowConstructorFlags 选项不再接受重复的标志作为输入。 ​​以前,数组中包含重复标志的配置会被接受,但会被视为具有唯一标志。此类配置现在被视为无效,并将导致配置错误。

¥In ESLint v10.0.0, the allowConstructorFlags option of no-invalid-regexp no longer accepts duplicate flags as input. Previously, configurations with duplicate flags in the array were accepted but treated the same as having unique flags. Such configurations are now considered invalid and will result in a configuration error.

例如,由于重复的 "u" 标志,此配置现在无效:

¥For example, this configuration is now invalid due to the duplicate "u" flag:

/*eslint no-invalid-regexp: ["error", { "allowConstructorFlags": ["u", "y", "u"] }]*/

要解决:移除 no-invalid-regexp 规则的 allowConstructorFlags 数组配置中所有重复的标志。每个标志在数组中只能出现一次。

¥To address: Remove any duplicate flags from your allowConstructorFlags array configuration of no-invalid-regexp rule. Each flag should appear only once in the array.

相关问题):#18755

¥Related issue(s): #18755

name 属性已添加到 ESLint 核心配置

¥ name property added to ESLint core configs

在 ESLint v10.0.0 中,从 @eslint/js 导出的 ESLint 核心配置中已恢复 name 属性。此属性之前由于与旧版 eslintrc 配置系统不兼容而被移除。由于 eslintrc 已不再受支持,name 属性已重新添加。

¥In ESLint v10.0.0, the name property has been restored to the ESLint core configs exported from @eslint/js. This property was previously removed due to incompatibility with the legacy eslintrc configuration system. Now that eslintrc is no longer supported, the name property has been added back.

此更改对大多数用户来说无需任何操作。但是,如果你将 @eslint/js v10.x 与 @eslint/eslintrc 中的 FlatCompat 工具一起使用,则应将 @eslint/eslintrc 升级到最新版本以确保兼容性。

¥This change should not require any action for most users. However, if you are using @eslint/js v10.x with the FlatCompat utility from @eslint/eslintrc, you should upgrade @eslint/eslintrc to the latest version to ensure compatibility.

要解决:如果你正在使用 FlatCompat,请将 @eslint/eslintrc 升级到最新版本。

¥To address: Upgrade @eslint/eslintrc to the latest version if you are using FlatCompat.

相关问题):#19864

¥Related issue(s): #19864

移除无效 RuleTester 错误中的 type 属性案例

¥ Removal of type property in errors of invalid RuleTester cases

在 ESLint v10.0.0 中,规则无效测试用例错误中已弃用的 type 属性已被移除。现在在测试用例中使用 type 属性会引发错误。

¥In ESLint v10.0.0, the deprecated type property in errors of invalid test cases for rules has been removed. Using the type property in test cases now throws an error.

要解决:从无效测试用例的错误对象中移除 type 属性。

¥To address: Remove the type property from error objects in invalid test cases.

相关问题):#19029

¥Related issue(s): #19029

Program AST 节点范围跨越整个源文本

¥ Program AST node range spans entire source text

ESLint v10.0.0 更改了 Program AST 节点范围的计算方式:现在,它会覆盖整个源代码,包括任何前导和尾随注释以及空格。

¥ESLint v10.0.0 changes how the Program AST node’s range is calculated: it now spans the entire source text, including any leading and trailing comments and whitespace.

之前,Program 节点的范围排除了前导和尾随的注释/空格,这可能不太直观。例如:

¥Previously, the Program node’s range excluded leading and trailing comments/whitespace, which could be unintuitive. For example:

// Leading comment
const x = 1;
// Trailing comment

在 ESLint v9 及更早版本中,Program.range 仅涵盖 const x = 1;(不包括周围的注释/空格)。

¥In ESLint v9 and earlier, Program.range covers only const x = 1; (excludes surrounding comments/whitespace).

从 ESLint v10.0.0 开始,Program.range 覆盖整个源文本,包括开头和结尾的注释/空格。

¥Starting with ESLint v10.0.0, Program.range covers the entire source text, including the leading and trailing comments/whitespace.

要解决:

¥To address:

  • 对于规则和插件作者:如果你的代码依赖于之前的 Program.range 行为,或者依赖于假定其行为的 SourceCode 方法(例如用于检索所有前导注释的 sourceCode.getCommentsBefore(programNode)),请更新你的逻辑。

    ¥For rule and plugin authors: If your code depends on the previous Program.range behavior, or on SourceCode methods that assume it (such as sourceCode.getCommentsBefore(programNode) to retrieve all leading comments), update your logic.

  • 自定义解析器:将 Program.range 设置为覆盖整个源文本(通常为 [0, code.length])。

    ¥For custom parsers: Set Program.range to cover the full source text (typically [0, code.length]).

相关问题):eslint/js#648

¥Related issue(s): eslint/js#648

修复方法现在需要字符串 text 参数

¥ Fixer methods now require string text arguments

在 ESLint v10.0.0 中,所有接受 text 参数的规则修复方法现在都要求该参数为字符串。提供非字符串值会抛出 TypeError 异常。

¥In ESLint v10.0.0, all rule fixer methods that accept a text argument now require that it be a string. Providing a non-string value will throw a TypeError.

受影响的方法:

¥Affected methods:

  • insertTextBefore(nodeOrToken, text)

  • insertTextBeforeRange(range, text)

  • insertTextAfter(nodeOrToken, text)

  • insertTextAfterRange(range, text)

  • replaceText(nodeOrToken, text)

  • replaceTextRange(range, text)

要解决:确保传递给修复方法的 text 值是字符串。

¥To address: Ensure the text value you pass to fixer methods is a string.

相关问题):#18807

¥Related issue(s): #18807

ScopeManager 实现的新要求

¥ New requirements for ScopeManager implementations

在 ESLint v10.0.0 中,自定义 ScopeManager 实现必须自动解析代码中声明的全局变量的引用,包括 varfunction 声明,并提供一个实例方法 addGlobals(names: string[]),该方法在全局作用域中创建具有给定名称的变量并解析对它们的引用。

¥In ESLint v10.0.0, custom ScopeManager implementations must automatically resolve references to global variables declared in the code, including var and function declarations, and provide an instance method addGlobals(names: string[]) that creates variables with the given names in the global scope and resolves references to them.

默认的 ScopeManager 实现 eslint-scope 已更新。

¥The default ScopeManager implementation eslint-scope has already been updated.

此更改不影响自定义规则。

¥This change does not affect custom rules.

要解决:如果你维护了一个提供自定义 ScopeManager 实现的自定义解析器,请更新你的自定义 ScopeManager 实现。

¥To address: If you maintain a custom parser that provides a custom ScopeManager implementation, update your custom ScopeManager implementation.

相关问题):eslint/js#665

¥Related issue(s): eslint/js#665

移除已弃用的 context 成员

¥ Removal of deprecated context members

在 ESLint v9.x 中,我们弃用了以下 methodsproperties

¥In ESLint v9.x, we deprecated the following methods and properties:

  • context.getCwd()

  • context.getFilename()

  • context.getPhysicalFilename()

  • context.getSourceCode()

  • context.parserOptions

  • context.parserPath

在 ESLint v10.0.0 中,所有这些成员均已被移除。

¥In ESLint v10.0.0, all of these members have been removed.

要解决:在你的自定义规则中,进行以下更改:

¥To address: In your custom rules, make the following changes:

context 上删除 context 的替换
context.getCwd() context.cwd
context.getFilename() context.filename
context.getPhysicalFilename() context.physicalFilename
context.getSourceCode() context.sourceCode
context.parserOptions context.languageOptionscontext.languageOptions.parserOptions
context.parserPath 无替换。

你可以使用 eslint-transforms 工具修改已移除的 context 方法。要使用此实用程序,请先安装它,然后运行 ​​v9-rule-migration 转换,如下所示:

¥You can make changes for the removed context methods using the eslint-transforms utility. To use the utility, first install it and then run the v9-rule-migration transform, like this:

# install the utility
npm install eslint-transforms -g

# apply the transform to one file
eslint-transforms v9-rule-migration rule.js

# apply the transform to all files in a directory
eslint-transforms v9-rule-migration rules/

移除的 context 属性必须手动配置,因为可能没有直接的一对一替代方案。

¥The removed context properties must be done manually as there may not be a direct one-to-one replacement.

相关问题):eslint/eslint#16999

¥Related issue(s): eslint/eslint#16999

移除已弃用的 SourceCode 方法

¥ Removal of deprecated SourceCode methods

以下已弃用的 SourceCode 方法已在 ESLint v10.0.0 中移除:

¥The following deprecated SourceCode methods have been removed in ESLint v10.0.0:

  • getTokenOrCommentBefore()

  • getTokenOrCommentAfter()

  • isSpaceBetweenTokens()

  • getJSDocComment()

这些方法已在多个主要版本中弃用,主要用于已弃用的格式化规则和 ESLint 内部工具。使用这些方法的自定义规则必须更新为使用其现代替代方案。

¥These methods have been deprecated for multiple major versions and were primarily used by deprecated formatting rules and internal ESLint utilities. Custom rules using these methods must be updated to use their modern replacements.

要解决:在你的自定义规则中,进行以下更改:

¥To address: In your custom rules, make the following changes:

SourceCode 上删除 替换
getTokenOrCommentBefore(nodeOrToken, skip) getTokenBefore(nodeOrToken, { includeComments: true, skip })
getTokenOrCommentAfter(nodeOrToken, skip) getTokenAfter(nodeOrToken, { includeComments: true, skip })
isSpaceBetweenTokens(first, second) isSpaceBetween(first, second)
getJSDocComment(node) 无替换

@eslint/compat 包中提供了兼容性补丁,以帮助你完成过渡。

¥Compatibility patches are available in the @eslint/compat package to help with the transition.

相关问题):#20113

¥Related issue(s): #20113

禁止在有效的 RuleTester 测试用例中使用 errorsoutput 规则

¥ Prohibiting errors or output of valid RuleTester test cases

在 ESLint v10.0.0 中,规则测试器变得更加严格。关于测试用例结构。有效的测试用例(不应产生任何代码检查错误的测试用例)不再允许包含 errorsoutput 属性。

¥In ESLint v10.0.0, the RuleTester has become more strict about test case structure. Valid test cases (those that should not produce any linting errors) are no longer allowed to have errors or output properties.

更改内容:

¥​What changed:​​

  • 之前,有效的测试用例可以包含 errorsoutput 属性,但这些属性会被忽略。

    ¥Previously, valid test cases could include errors or output properties, which were ignored.

  • 现在,在有效的测试用例中包含这些属性将导致测试失败。

    ¥Now, including these properties in valid test cases will cause the test to fail.

无效用法示例:

¥Example of invalid usage:​

// This will now throw an error in ESLint v10.0.0
const validTestCases = [
	{
		code: "const foo = 'bar';",
		errors: 0, // ❌ Not allowed in valid test cases
		output: "const foo = 'bar';", // ❌ Not allowed in valid test cases
	},
];

ruleTester.run("rule-id", rule, { valid: validTestCases, invalid: [] });

要解决:移除有效测试用例中的所有 errors/output 属性。

¥To address: Remove any errors/output properties from valid test cases.

相关问题):#18960

¥Related issue(s): #18960

移除 LintMessage 对象中的 nodeType 属性

¥ Removal of nodeType property in LintMessage objects

在 ESLint v10.0.0 中,已移除 LintMessage 对象上已弃用的 nodeType 属性。这会影响之前依赖 message.nodeType 的 Node.js API 使用者(例如,自定义格式化程序和编辑器/工具集成)。

¥In ESLint v10.0.0, the deprecated nodeType property on LintMessage objects has been removed. This affects consumers of the Node.js API (for example, custom formatters and editor/tool integrations) that previously relied on message.nodeType.

要解决:移除集成和格式化程序中所有对 message.nodeType 的使用。

¥To address: Remove all usages of message.nodeType in your integrations and formatters.

相关问题):#19029

¥Related issue(s): #19029