为什么@typescript eslint/parser包含在mytsconfig.json中配置的文件之外的文件?

为什么@typescript eslint/parser包含在mytsconfig.json中配置的文件之外的文件?,typescript,visual-studio-code,eslint,vscode-settings,Typescript,Visual Studio Code,Eslint,Vscode Settings,我得到一个错误:- 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.

我得到一个错误:-

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: {}
}
baseslint.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: {}
}
module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}
这也可以通过.eslintignore来实现(参见下面Anton Bessonov的回答)

与此相关的另一件可能对其他人有用的事情是我使用的VScode配置(在发布原始问题后,我从WebStorm切换到VScode)。我非常喜欢在save上运行eslint fix,但我不希望它尝试对所有文件执行此操作。我的项目文件(仅我的项目文件)都是.ts/.tsx。settings.json中的以下选项允许在保存时进行修复,而不包括我的repo中的每个文件:-

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.probe": [
        "typescript",
        "typescriptreact"
    ],
}
如果未指定eslint.probe,则默认为:-

[“javascript”、“javascriptreact”、“typescript”、“typescriptreact”、“html”、“vue”]


可以找到有关VSCode ESLint扩展设置的更多信息。

通过添加
.eslintignore

/*.*
编辑:

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

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

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

为什么会发生这种情况?

我不完全确定,但似乎
@typescript eslint/parser
试图将eslint配置中包含的文件(例如扩展名)与
tsconfig.json
中包含的文件进行比较。由于
.js
文件由eslint配置覆盖并取决于您的配置,因此该文件可能包含在eslint运行中。但由于该文件未包含在tsconfig中,因此会出现此错误

如何解决这个问题?

根据您的情况,您可以选择其中一种:

  • 您可以在
    .eslintignore
    中忽略它。这就是@U4EA所做的
  • 如果可以将其添加到
    tsconfig.json
    中的includes中:
  • 如果您没有“正确”的tsconfig.json,因为您使用的是monorepo,那么您可以创建一个新文件
    tsconfig.eslint.json
  • 并仅将此文件用于eslint:

    parserOptions:{
    项目:[
    “./tsconfig.eslint.json”,
    “./packages/*/tsconfig.json”,
    ],
    },
    
    您也可以忽略.eslintrc.js本身中的.eslintrc.js,为您保存一个附加文件:

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


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

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

    为了将来的参考,你能提供应用解决方案中的代码或代码片段吗?这真的很有帮助。谢谢