ESLint v9.23.0 发布

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

亮点

🌐 Highlights

核心规则中的 TypeScript 语法支持

🌐 TypeScript Syntax Support in Core Rules

ESLint v9.23.0 为三条核心规则引入了完整的 TypeScript 语法支持。这些规则是:

🌐 ESLint v9.23.0 introduces full TypeScript syntax support for three core rules. These rules are:

这些规则现在可以用来检查 TypeScript 文件以及常规的 JavaScript。 要检查 TypeScript 代码,请确保使用 @typescript-eslint/parser 或其他兼容的解析器。 你可以在配置文件中这样定义解析器和规则:

🌐 These rules can now be used to lint TypeScript files as well as regular JavaScript. To lint TypeScript code, be sure to use @typescript-eslint/parser, or another compatible parser. You can define the parser and the rules in your config file like this:

import { defineConfig } from "eslint/config";
import tsParser from "@typescript-eslint/parser";

export default defineConfig([
    {
        files: ["**/*.ts"],
        languageOptions: {
            parser: tsParser,
        },
        rules: {
            "class-methods-use-this": "error",
            "default-param-last": "error",
            "no-useless-constructor": "error",
        },
    },
]);

ESLint 正在积极努力将 TypeScript 语法支持添加到其他核心规则中。请期待更多更新。

🌐 ESLint is actively working to add TypeScript syntax support to other core rules. Expect more updates soon.

冲突的自动修复检测

🌐 Conflicting Autofix Detection

在使用 —fix 选项运行 ESLint 时,某些问题可以自动修复。然而,当两个规则对某个代码部分的修复方式有不同意见时,会发生什么?

🌐 When running ESLint with the —fix option, some problems can be fixed automatically. However, what happens when two rules have differing opinions on how a specific part of code should be fixed?

结果是一个固定的代码,它无法满足任一规则,并且根据你的配置仍然会包含问题。

🌐 The result is a fixed code that fails to satisfy either rule, and that will still contain problems based on your configuration.

例如,考虑这个配置:

🌐 For instance, consider this configuration:

import { defineConfig } from "eslint/config";
import stylistic from "@stylistic/eslint-plugin";
import stylisticJs from "@stylistic/eslint-plugin-js";

export default defineConfig([
    {
        plugins: {
            "@stylistic": stylistic,
            "@stylistic/js": stylisticJs,
        },
        rules: {
            "@stylistic/semi": ["error", "never"],
            "@stylistic/js/semi": ["error", "always"],
        },
    },
]);

在上述配置中,规则 @stylistic/semi 设置为不允许在语句末尾使用分号,而规则 @stylistic/js/semi 总是要求使用分号。显然,不可能以一种方式修复代码,使得一个语句同时符合这两个约束。

🌐 In the above configuration, the rule @stylistic/semi is set to never allow semicolons at the end of a statement, but the rule @stylistic/js/semi always requires them. Obviously, it isn’t possible fix the code in a way that a statement fits both constraints.

在以前的 ESLint 版本中,文件中存在冲突修复的唯一迹象就是修复后的代码仍然包含可修复的错误。这让许多用户感到惊讶,因为这种行为的原因通常不清楚。

🌐 In previous ESLint versions, the only indications for conflicting fixes in a file was the fact that the fixed code would still contain fixable errors. This was surprising to many users, as the cause for the behavior was often unclear.

ESLint v9.23.0 会检测到当两个规则产生冲突的修复时,并发出警告:

🌐 ESLint v9.23.0 detects when two rules produce conflicting fixes and emits a warning:

ESLint循环修复警告:在修复 path/to/file 时检测到循环修复。很可能你的配置中存在冲突的规则。

我们还提供说明来修复配置。

🌐 We also provide instructions to fix the config.

自动查找带有 “flat/” 前缀的配置

🌐 Automatic Lookup for Configs with “flat/” Prefix

对于插件开发者,一个有趣的新功能是 ESLint 能够检测通过 extends 属性指定的旧版配置名称,并回退使用带有 “flat/” 前缀的类似名称的配置。 这简化了需要为 ESLint v9 和之前版本维护类似配置但又不能更改命名约定以保持向后兼容的插件的操作。 详情请参见 文档

🌐 An interesting new feature for plugin developers is ESLint’s ability to detect a legacy config specified by its name with the extends property, and fall back to use a config with a similar name with the “flat/” prefix. This simplifies things for plugins that need to maintain similar configs for ESLint v9 and for previous versions, and that cannot change their naming conventions for backwards compatibility. See the documentation for further information.

特性

🌐 Features

错误修复

🌐 Bug Fixes

文档

🌐 Documentation

  • 5405939 文档:在规则文档的 TypeScript 示例中显示红色下划线 (#19547) (Milos Djermanovic)
  • 48b53d6 文档:在示例中将 var 替换为 const (#19539) (Nitin Kumar)
  • c39d7db 文档:更新自述文件(GitHub Actions 机器人)
  • a4f8760 文档:恢复意外更改 (#19542) (Francesco Trotta)
  • 280128f 文档:添加复制按钮 (#19512) (xbinaryx)
  • cd83eaa 文档:在示例中将 var 替换为 const (#19530) (Nitin Kumar)
  • 7ff0cde 文档:更新自述文件(GitHub Actions 机器人)
  • 996cfb9 文档:将 sass 迁移到模块系统 (#19518) (xbinaryx)
  • 17cb958 文档:在规则示例中将 var 替换为 letconst (#19515) (Tanuj Kanti)
  • 83e24f5 文档:将 var 替换为 let 或 const (#19511) (Jenna Toff)
  • a59d0c0 文档:更新 defineConfig 文档 (#19505) (Nicholas C. Zakas)
  • fe92927 文档: require-unicode-regexpi 标志和 \w 添加说明 (#19510) (Chaemin-Lim)

构建相关

🌐 Build Related

  • 2357edd 构建:从 Prettier 格式化中排除自动生成的文件 (#19548) (Francesco Trotta)

杂项

🌐 Chores

  • 0ac8ea4 杂务:更新依赖以发布 v9.23.0 版本 (#19554) (Francesco Trotta)
  • 20591c4 事务:更新 package.json 以适配 @eslint/js 版本发布(Jenkins)
  • 901344f 杂项:将依赖 @eslint/json 更新到 ^0.11.0 (#19552) (renovate[bot])
  • 5228383 杂项:修复 update-readme 格式 (#19544) (Milos Djermanovic)
  • 5439525 事务: 在 Trunk 中格式化 JSON 文件 (#19541) (Francesco Trotta)
  • 75adc99 事务: 在 Trunk 中启用 Prettier (#19354) (Josh Goldberg ✨)
  • 2395168 杂务:通过 trunk fmt 为 Prettier 添加了 .git-blame-ignore-revs (#19538) (Josh Goldberg ✨)
  • 129882d 事务:通过 trunk fmt 使用 Prettier 格式化文件 (#19355) (Josh Goldberg ✨)
  • 1738dbc 任务:在主干中临时禁用 prettier (#19537) (Josh Goldberg ✨)
  • dc854fd 杂务: 更新依赖 shelljs 至 ^0.9.0 (#19524) (renovate[bot])
  • 5d57496 杂务:修复一些评论 (#19525) (jimmycathy)
  • 9c5c6ee 测试:修复断言失败 (#19500) (fisker Cheung)
  • 7a699a6 杂务:内部移除与格式相关的 lint 规则 (#19473) (Josh Goldberg ✨)
  • c99db89 测试:用 Cypress 替换 WebdriverIO (#19465) (Pixel998)

最新的 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 的一次小版本升级。此版本添加了一些新功能,并修复了上一版本中发现的几个错误。