簡體   English   中英

如何將 typescript-eslint 規則添加到 eslint

[英]How to add typescript-eslint rules to eslint

我正在使用 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"
  }
}

根據我對 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中指定的規則。

您的插件引用數組中缺少插件。因此只有“@nrwl/nx”規則和其他沒有命名空間的規則將適用。 試試這個:“插件”:[“@nrwl/nx”,“@typescript-eslint”]。 哦,如果您只想將其應用於某種文件(例如 .ts 或 .tsx 文件),或者/和更改處理解析器,您也可以將其添加到“覆蓋”部分中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM