为什么@typescript-eslint/parser 包括在我的 tsconfig.json 中配置的文件之外的文件?

Why is @typescript-eslint/parser including files outside of those configured in my tsconfig.json?

我收到错误:-

Parsing error: "parserOptions.project" has been set for u/typescript-eslint/parser.

The file does not match your project config: .eslintrc.js.

The file must be included in at least one of the projects provided.

IDE - WebStorm 20.1.1

项目结构:-

node_modules

src

--- testfile.js

.eslintrc.js

.prettierrc.js

baseeslint.js

package.json

tsconfig.json

.eslintrc.js: -

module.exports = {
extends: ['./baseeslint'],
parserOptions: {
project: './tsconfig.json'
},
rules: {}
}

baseeslint.js:-

module.exports = {

parser: '@typescript-eslint/parser'

}

tsconfig.json:-

{

"include": [

"/src/**/*.ts",

"/src/**/*.tsx"

],

"exclude": ["node_modules"],

"compilerOptions": {

"outDir": "./lib",

"types": ["styled-components", "react", "@types/react-router-dom", "reactstrap"]

}

}

我完全不知道为什么会这样?我假设它会忽略根目录中的文件,尤其是在我的 tsconfg.json?

中指定的 include

如有任何帮助,我们将不胜感激。

编辑:-

我的解决方案,对别人有帮助吗...

我将 ignorePatterns 配置添加到我的 .eslintrc.js 文件中:-

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

这也可以通过 .eslintignore 完成(请参阅下面 Anton Bessonov 的回复)。

另一件可能对其他人有用的与此相关的事情是我使用的 VScode 配置(我在发布原始问题后从 WebStorm 切换到 VSCode)。我是 运行 eslint fix on save 的忠实粉丝,但我不希望它尝试对所有文件执行此操作。我的项目文件(并且只有我的项目文件)都是 .ts/.tsx。 settings.json 中的以下选项允许在不包含我的存储库中的每个文件的情况下修复保存:-

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.probe": [
        "typescript",
        "typescriptreact"
    ],
}

如果不指定eslint.probe,则默认为:-

["javascript", "javascriptreact", "typescript", "typescriptreact", "html", "vue"]

有关 VSCode ESLint 扩展设置的更多信息,请参见 here

添加.eslintignore

解决
/*.*

编辑:

我最终的决定是将 ignorePatterns 配置添加到我的 .eslintrc.js 文件中:-

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

这样做的原因是为了摆脱 .eslintignore 文件(减少工作区混乱)。

为什么会这样?

我不完全确定,但似乎 @typescript-eslint/parser 试图将 eslint 配置中涵盖的文件(例如通过扩展名)与 tsconfig.json 中包含的文件进行比较。由于 .js 文件被 @typescript-eslint/parser 覆盖,并且根据您的 eslint 配置,该文件可能包含在 eslint 运行 中。但是因为文件没有包含在 tsconfig 中,所以你会得到这个错误。

如何解决?

根据您的情况,您可以select其中之一:

  1. 你可以在.eslintignore中忽略它。这就是@U4EA 所做的。
  2. 如果可以将其添加到tsconfig.json中的包含:
    "include": [
        ".eslintrc.js",
        // another includes
    ]
  1. 如果你没有“权利”tsconfig.json,因为你使用的是 monorepo,那么你可以创建一个新文件 tsconfig.eslint.json:
{
    "include": [
        ".eslintrc.js"
    ]
}

并将此文件仅用于 eslint:

    parserOptions: {
        project: [
            './tsconfig.eslint.json',
            './packages/*/tsconfig.json',
        ],
    },

您也可以忽略 .eslintrc.js 本身中的 .eslintrc.js,为您节省一个额外的文件:

"ignorePatterns": [
    ".eslintrc.js"
],

Extension

我有一个类似的问题,我禁用了 vs code 上的 eslint 扩展,这就是我的问题。我假设全局 eslint 和我的项目之间存在某种冲突。

对 VS Code 用户有帮助,卸载或禁用 ESLint 扩展可能会解决这个问题,它对我有用:)

只是为了建立 , something I found helpful for me was that, by default, hidden files aren't captured by globs

要解决此问题,您可以结合使用(取决于您的 shell environment supports):

"include": [
    "./**/*", // All non-hidden files at any directory level
    "./**/.*", // Hidden dotfiles like .eslintrc.js at any level
    "./**/.*.[tj]s?(x)" // All hidden TS/JS dotfiles
]