【问题标题】:How to add typescript-eslint rules to eslint如何将 typescript-eslint 规则添加到 eslint
【发布时间】:2021-04-14 12:03:31
【问题描述】:

我正在使用 NX 构建 Angular 应用程序,并且我已将其配置为使用 ESLint。我们可以配置像no-console 这样的内置规则,但@typescript-eslint/no-inferrable-types 不接受我们的覆盖。我们甚至尝试将规则移动到 overrides 部分的变量条目中,但没有任何效果。

我们在哪里指定typescript-eslint 规则?

我们的根级.eslintrc.json

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nrwl/nx"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              { "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
            ]
          }
        ]
      }
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "parserOptions": { "project": "./tsconfig.*?.json" },
      "rules": {}
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    }
  ],
  "rules": {
    "no-console":"warn",
    "@typescript-eslint/no-inferrable-types": "off"
  }
}

【问题讨论】:

  • 在您的.eslintrc 文件中?见this

标签: angular typescript eslint nrwl-nx


【解决方案1】:

根据我对 NX linting 工作原理的理解,我认为您可以将规则添加到覆盖部分。

在下面的 JSON 中,您可以看到 no-console 已添加到具有 *.ts 和 *.tsx 文件匹配的覆盖数组元素的“规则”中。

{
  "root": true,
  "ignorePatterns": ["**/*"],
  "plugins": ["@nrwl/nx"],
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nrwl/nx/enforce-module-boundaries": [
          "error",
          {
            "enforceBuildableLibDependency": true,
            "allow": [],
            "depConstraints": [
              { "sourceTag": "*", "onlyDependOnLibsWithTags": ["*"] }
            ]
          }
        ]
      }
    },
    {
      "files": ["*.ts", "*.tsx"],
      "extends": ["plugin:@nrwl/nx/typescript"],
      "parserOptions": { "project": "./tsconfig.*?.json" },
      "rules": {
        "no-console":"warn"
      }
    },
    {
      "files": ["*.js", "*.jsx"],
      "extends": ["plugin:@nrwl/nx/javascript"],
      "rules": {}
    }
  ],
  "rules": {
    "@typescript-eslint/no-inferrable-types": "off"
  }
}

重要的是覆盖您放入规则的数组元素。我遇到了类似的问题,您可以在https://stackoverflow.com/a/71230627/990642 的答案中看到我尝试的不同方法的完整说明。

值得注意的是,似乎人们对pluginsextends 中已经指定的覆盖规则喜忧参半。

【讨论】:

    【解决方案2】:

    您的插件引用数组中缺少插件。因此只有“@nrwl/nx”规则和其他没有命名空间的规则将适用。 尝试这个 : “插件”:[ "@nrwl/nx", “@typescript-eslint”]。 哦,如果您只想将它​​应用于某种文件(例如 .ts 或 .tsx 文件),或者/并更改处理解析器,您也可以将其添加到“覆盖”部分中

    【讨论】:

      猜你喜欢
      • 2020-11-04
      • 2019-11-10
      • 2023-03-14
      • 2020-03-23
      • 2019-09-04
      • 2021-06-08
      • 1970-01-01
      • 2020-10-01
      • 2020-11-10
      相关资源
      最近更新 更多