简体   繁体   English

如何将 typescript-eslint 规则添加到 eslint

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

I'm using NX to build out Angular apps and I've configured it to use ESLint.我正在使用 NX 构建 Angular 个应用程序,并且我已将其配置为使用 ESLint。 We are able to configure built in rules like no-console but @typescript-eslint/no-inferrable-types is not accepting our overrides.我们能够配置内置规则,例如no-console@typescript-eslint/no-inferrable-types不接受我们的覆盖。 We even tried moving the rule into variables entries of the overrides section, but nothing worked.我们甚至尝试将规则移动到overrides部分的变量条目中,但没有任何效果。

Where do we specify the typescript-eslint rules?我们在哪里指定typescript-eslint规则?

显示相关问题的示例代码

Our root level .eslintrc.json :我们的根级别.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"
  }
}

Based on my understanding of how NX linting works, I think you can add the rule to the overrides section.根据我对 NX linting 工作原理的理解,我认为您可以将规则添加到覆盖部分。

In the JSON below you can see the no-console is added to the "rules" of the overrides array element that has file matches for *.ts and *.tsx.在下面的 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"
  }
}

It matters which overrides array element you put the rule in. I encountered a similar issue, and you can see a full explanation of different approaches I tried in the answer on https://stackoverflow.com/a/71230627/990642 .重要的是哪个覆盖了你放入规则的数组元素。我遇到了类似的问题,你可以在https://stackoverflow.com/a/71230627/990642的答案中看到我尝试过的不同方法的完整解释。

It is worth noting that it seems people have mixed luck overriding rules already specified in a plugins or extends .值得注意的是,似乎人们运气好坏参半,覆盖已经在pluginsextends中指定的规则。

You are missing the plugin in your plugs reference array.So only "@nrwl/nx" rules, and other rules with no namespace will apply.您的插件引用数组中缺少插件。因此只有“@nrwl/nx”规则和其他没有命名空间的规则将适用。 Try this: "plugins": [ "@nrwl/nx", "@typescript-eslint"].试试这个:“插件”:[“@nrwl/nx”,“@typescript-eslint”]。 Oh, and you could as well add this inside the "overrides" section if you just want it to be applied only to some kind of file(eg .ts or.tsx files), or/and change the processing parser哦,如果您只想将其应用于某种文件(例如 .ts 或 .tsx 文件),或者/和更改处理解析器,您也可以将其添加到“覆盖”部分中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM