如何覆盖我的 nx 工作区中的 eslint 规则?

问题描述 投票:0回答:3

如何覆盖

plugin:@nrwl/nx/typescript
中的 eslint 规则?我已对
.eslintrc.json
的根进行了此更改。

"rules": {
  "@typescript-eslint/member-ordering": "warn"
},

在引入违反规则的演示后仍然出现错误

D:\me\sample\apps\my-app\src\app\app.component.ts
  16:3  error  Member outOfOrder should be declared before all instance method definitions  @typescript-eslint/member-ordering

✖ 1 problem (1 error, 0 warnings)

Lint errors found in the listed files.

我也尝试将规则更改添加到打字稿的

overrides
部分。

typescript eslint nrwl-nx
3个回答
13
投票

无法在根级别进行设置,因为您的项目的

.eslintrc.json
将始终覆盖这些更改,因为
@nrwl/nx/typescript
在覆盖部分中设置。

不幸的是,您必须在覆盖部分中将其设置为每个项目。

如果您有多个项目和/或需要应用多个更改,您可以将其提取到

eslint-custom-overrides
文件中并将其用作
extends
部分中的最后一个:

{
  "extends": ["../../.eslintrc.json"],
  "ignorePatterns": ["!**/*"],
  "overrides": [
    {
      "files": ["*.ts"],
      "extends": [
        "plugin:@nrwl/nx/typescript",
        "../../eslintrc-custom-overrides.json"
      ],
   },
   ...
  ],
  ...
}

你的

eslintrc-custom-overrides.json
看起来像这样:

{
  "overrides": [
    {
      "files": ["*.ts"],
      "rules": {
         "@typescript-eslint/member-ordering": "warn"
      }
    }
  ]
}

查看有关 NX 问题的更多详细信息。


3
投票

我找到了一个非常适合我的解决方案,可以避免任何复制粘贴(我发现这是不可接受的),并且或多或少违背了已接受答案中的“不可能”断言。对我来说,关键是不使用基于文件的覆盖,而是使用扩展。它工作得很好并且完全符合我的要求(并且没有看到任何缺点)。希望有帮助:

NextJS / React UI 项目 enter image description here

NodeJS GraphQL 项目的配置

enter image description here

所有 NextJS / UI 项目的配置

enter image description here

可以被上述任何内容覆盖的所有内容的配置

enter image description here


0
投票

这现在在全局级别起作用,因此在根 .eslintrc.json 文件中,您可以添加例外。

例如,我想要规范文件中除“no-explicit-any”之外的所有规则。此配置有效:

"overrides": [
   {...},
   {
     "files": ["*.spec.ts", "*.spec.tsx", "*.spec.js", "*.spec.jsx"],
     "env": {
       "jest": true
     },
     "rules": {
       "@typescript-eslint/no-explicit-any": "off"
     }
   }
 ]
© www.soinside.com 2019 - 2024. All rights reserved.