ESLint v10.0.0-rc.0 发布

我们刚刚发布了 ESLint v10.0.0-rc.0,这是 ESLint 的一次重大版本升级。本次发布增加了一些新功能,并修复了前一版本中发现的若干错误。本次发布还包含一些重大变更,请仔细阅读以下内容。

亮点

🌐 Highlights

此版本的 ESLint 尚未准备好用于生产环境,提供此版本是为了在发布最终版本之前收集社区的反馈。如果你遇到任何问题或有任何反馈,请通过在我们的 GitHub 仓库 创建问题告诉我们。

🌐 This version of ESLint is not ready for production use and is provided to gather feedback from the community before releasing the final version. Please let us know if you have any problems or feedback by creating issues on our GitHub repo.

请注意,这个 ESLint 的预发布版本有一个独立的文档部分

🌐 Note that this prerelease version of ESLint has a separate documentation section.

RuleTester 的增强功能

🌐 Enhancements to RuleTester

自最早期以来,ESLint 就提供了 RuleTester API,帮助插件作者针对自定义测试案例和配置测试他们的规则。本次发布对 RuleTester 进行了若干增强,以执行更强健的测试定义并改进调试。

🌐 Since its earliest days, ESLint has provided the RuleTester API to help plugin authors test their rules against custom test cases and configurations. This release introduces several enhancements to RuleTester to enforce more robust test definitions and improve debugging.

requireData 断言选项

🌐 requireData assertion option

一个新的断言选项 requireData 现已可用。当设置为 true 时,RuleTester 将要求无效测试用例在 messageId 引用包含占位符的消息时包含一个 data 对象。这有助于确保测试与依赖占位符替换的规则消息保持一致。

🌐 A new assertion option, requireData, is now available. When set to true, RuleTester will require invalid test cases to include a data object whenever a messageId references a message with placeholders. This helps ensure that tests remain consistent with rule messages that rely on placeholder substitution.

例如,考虑一个假设规则 no-trivial-sum,它报告诸如 1 + 2 的表达式,并定义一个带有占位符的消息:

🌐 For example, consider a hypothetical rule no-trivial-sum that reports on expressions such as 1 + 2 and defines a message with placeholders:

trivialSum: "Trivial sum found. Replace {{actualExpression}} with {{sum}}."

如果一个无效的测试用例包括 messageId: "trivialSum" 但省略了 data

🌐 If an invalid test case includes messageId: "trivialSum" but omits data:

assertionOptions: { requireData: true },
invalid: [
  {
    code: "const a = 1 + 2;",
    errors: [{ messageId: "trivialSum" }],
  },
],

RuleTester 现在将抛出一个断言错误,表明缺少 data 属性。

为了解决此问题,请在错误对象中包含占位符值:

🌐 To resolve this, include the placeholder values in the error object:

  {
    code: "const a = 1 + 2;",
    errors: [
      {
        messageId: "trivialSum",
        data: { actualExpression: "1 + 2", sum: 3 },
      },
    ],
  },

改进了失败测试的位置信息报告

🌐 Improved location reporting for failing tests

RuleTester 现在会在堆栈跟踪中添加信息,使你更容易在源代码中定位失败的测试用例。例如,如果 no-trivial-sum 规则未能对 1 + 2 报告错误,则上一节中的测试用例将失败,测试输出将包括如下堆栈跟踪行:

roughly at RuleTester.run.invalid[0] (/my-project/test/no-trivial-sum.js:10)
roughly at RuleTester.run.invalid (/my-project/test/no-trivial-sum.js:7)

第一行表示:

🌐 The first line indicates:

  • invalid[0]invalid 数组中失败测试用例的索引
  • /my-project/test/no-trivial-sum.js:10:该测试用例定义的文件和行号。许多IDE终端,包括Visual Studio Code的终端,都能识别这种格式,并允许你直接点击跳转到相关行。

第二行指向整个 invalid 数组的起始位置。

🌐 The second line points to the start of the entire invalid array.

请注意,这些行号可能并不总是包含在内,这取决于你的测试是如何构建的。当行无法精确确定时,仍然可以使用失败的测试索引(例如 0)和打印的代码片段来定位测试用例。

🌐 Note that these line numbers may not always be included, depending on how your tests are structured. When the lines cannot be determined precisely, the failing test index (e.g., 0) and the printed code snippet are still available to locate the test case.

max-params 规则中的 countThis 选项

🌐 countThis option in max-params rule

max-params 规则现在支持新的 countThis 选项,该选项取代了已弃用的 countVoidThis。使用 countThis: "never" 设置时,该规则在计算 TypeScript 函数的参数数量时,将忽略函数参数列表中的任何 this 注解。例如:

🌐 The max-params rule now supports the new countThis option, which supersedes the deprecated countVoidThis. With the setting countThis: "never", the rule will now ignore any this annotation in a function’s argument list when counting the number of parameters in a TypeScript function. For example:

function doSomething(this: SomeType, first: string, second: number) {
 // ...
}

将被视为一个只接受两个参数的函数。

🌐 will be considered a function taking only 2 parameters.

正在安装

🌐 Installing

由于这是预发布版本,npm 不会自动升级。安装时必须指定 next 标签:

🌐 Since this is a pre-release version, you will not automatically be upgraded by npm. You must specify the next tag when installing:

npm i eslint@next --save-dev

你也可以直接指定版本:

🌐 You can also specify the version directly:

npm i eslint@10.0.0-rc.0 --save-dev

迁移指南

🌐 Migration Guide

由于有很多变化,我们创建了一个 迁移指南,详细描述了重大更改以及你应该采取的应对步骤。我们预计大多数用户应该能够在不更改构建的情况下升级,但如果遇到问题,迁移指南应该是一个有用的资源。

🌐 As there are a lot of changes, we’ve created a migration guide describing the breaking changes in great detail along with the steps you should take to address them. We expect that most users should be able to upgrade without any build changes, but the migration guide should be a useful resource if you encounter problems.

重大更改

🌐 Breaking Changes

  • f9e54f4 功能更新!: 估算规则测试失败位置 (#20420) (ST-DDT)

特性

🌐 Features

错误修复

🌐 Bug Fixes

  • d186f8c 修复:更新 eslint (#20427) (renovate[bot])
  • 2332262 修复:错误位置不应修改 RuleTester 中的错误消息 (#20421) (Milos Djermanovic)
  • ab99b21 修复:确保将 filename 作为第三个参数传递给 verifyAndFix() (#20405) (루밀LuMir)
  • 8a60f3b 修复:从 ParserOptions 类型中移除 ecmaVersionsourceType (#20415) (Pixel998)
  • eafd727 修复:移除 TDZ 范围类型 (#20231) (jaymarvelz)
  • 39d1f51 修复:更正 Scope 类型 (#20404) (sethamus)
  • 2bd0f13 修复:更新 verifyverifyAndFix 类型 (#20384) (Francesco Trotta)

文档

🌐 Documentation

  • 65ed0c9 文档:更新自述文件(GitHub Actions 机器人)
  • b0e4717 文档: [no-await-in-loop] 扩展不适用性 (#20363) (Niklas Hambüchen)
  • fca421f 文档:更新自述文件(GitHub Actions 机器人)
  • d925c54 文档:更新 no-lone-blocks 中的配置语法 (#20413) (Pixel998)
  • 7d5c95f 文档:从规则示例中删除多余的 sourceType: "module" (#20412) (Pixel998)
  • 02e7e71 文档:修复带有扩展名示例文件中的 .mts 通配符模式 (#20403) (Ali Essalihi)

杂项

🌐 Chores

  • b4b3127 事务:更新 package.json 以适配 @eslint/js 版本发布(Jenkins)
  • f658419 重构:从 JS 语言中移除 raw 解析器选项 (#20416) (Pixel998)
  • 2c3efb7 任务:从类型测试夹具中移除 category (#20417) (Pixel998)
  • 36193fd 任务:从格式化器测试夹具中移除 category (#20418) (Pixel998)
  • e8d203b 事务: 在 check-rule-examples 中添加 JSX 语言标签验证 (#20414) (Pixel998)
  • bc465a1 任务:固定依赖 (#20397) (renovate[bot])
  • 703f0f5 测试:在 linter 测试中替换已弃用的规则 (#20406) (루밀LuMir)
  • ba71baa 测试:在类型测试中启用 strict 模式 (#20398) (루밀LuMir)
  • f9c4968 重构:移除 lib/linter/rules.js (#20399) (Francesco Trotta)
  • 6f1c48e 事务: v9.39.2 版本更新 (Jenkins)

最新的 ESLint 新闻、案例研究、教程和资源。

ESLint v10.3.0 发布
1 min read

ESLint v10.3.0 发布

我们刚刚发布了 ESLint v10.3.0,这是 ESLint 的一次小版本升级。此版本添加了一些新功能,并修复了上一版本中发现的几个错误。

ESLint v10.2.1 发布
1 min read

ESLint v10.2.1 发布

我们刚刚发布了 ESLint v10.2.1,这是 ESLint 的一个补丁版本升级。本次发布修复了上一版本中发现的几个错误。

ESLint v10.2.0 发布
2 min read

ESLint v10.2.0 发布

我们刚刚发布了 ESLint v10.2.0,这是 ESLint 的一次小版本升级。此版本添加了一些新功能,并修复了上一版本中发现的几个错误。