为什么@ 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
IDE - WebStorm 20.1.1
项目结构:-
node_modules
src
--- testfile.js
.eslintrc.js
.prettierrc.js
baseeslint.js
package.json
tsconfig.json
.eslintrc.js:-
.eslintrc.js: -
module.exports = {
extends: ['./baseeslint'],
parserOptions: {
project: './tsconfig.json'
},
rules: {}
}
baseeslint.js:-
baseeslint.js: -
module.exports = {
parser: '@typescript-eslint/parser'
}
tsconfig.json:-
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吗?
I have absolutely no idea why this is happening? I would assume it would ignore files in the root, especially with include specified in my tsconfg.json?
任何帮助将不胜感激。
编辑:-
我的解决方案,是否应该帮助其他人...
My solution, should it help others...
我在我的.eslintrc.js文件中添加了ignorePatterns配置:-
I added the ignorePatterns configuration to my .eslintrc.js file: -
module.exports = {
extends: ["mycustom/eslint-config"],
parserOptions: {
project: './tsconfig.json'
},
ignorePatterns: ["/*.*"],
rules: {}
}
这也可以通过.eslintignore完成(请参见下面的Anton Bessonov的回复)。
This can also be done via .eslintignore (see Anton Bessonov's response below).
另一个与此相关的事情可能是我在这里使用的VScode配置(发布原始问题后,我从WebStorm切换到VSCode)。我非常喜欢在保存时运行eslint修复程序,但我不希望它尝试对所有文件执行此操作。我的项目文件(只有我的项目文件)都是.ts / .tsx。 settings.json中的以下选项允许保存时进行修复,但不包括我的存储库中的每个文件:-
Another thing relating to this which maybe of use to others here is the VScode config I used (I switched from WebStorm to VSCode after posting the original question). I am a big fan of running eslint fix on save but I do not want it attempting to do this with all files. My project files (and only my project files) are all .ts/.tsx. The options below in settings.json allow fix on save without it including every file in my repo: -
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"eslint.probe": [
"typescript",
"typescriptreact"
],
}
如果未指定eslint.probe,则默认为:-
If eslint.probe is not specified, it defaults to: -
[ javascript, javascriptreact, typescript, typescriptreact, html, vue]
有关VSCode ESLint扩展设置的更多信息,请参见这里。
More about the VSCode ESLint extension settings can be found here.
推荐答案
为什么会这样?
我不确定,但是 @ typescript-eslint / parser
试图比较eslint配置中涵盖的文件(对于(以扩展名为例),文件包含在 tsconfig.json
中。由于.js 文件> @ typescript-eslint / parser ,根据您的eslint配置,该文件可能包含在eslint运行中。但是由于该文件未包含在tsconfig中,因此会出现此错误。
I'm not entirely sure, but it seems like @typescript-eslint/parser
tries to compare files covered in the eslint configuration (for example by extension) with files included in tsconfig.json
. Since the .js
files are covered by @typescript-eslint/parser and depending on your eslint configuration, the file is probably included in the eslint run. But because the file isn't included in tsconfig, you get this error.
如何解决此问题?
根据您的情况,可以选择以下选项之一:
Depending on your case, you can select one of them:
- 您可以在
.eslintignore
中忽略它。 。这就是@ U4EA所做的。 - 如果可以将其添加到
tsconfig.json
中的包含物中:
- You can ignore it in
.eslintignore
. This is what @U4EA did. - If can add it to the includes in
tsconfig.json
:
"include": [
".eslintrc.js",
// another includes
]
- 如果您没有正确的答案, tsconfig.json,因为您使用的是monorepo,则可以创建一个新文件
tsconfig.eslint.json
:
- If you don't have a "right" tsconfig.json, because you use a monorepo, then you can create a new file
tsconfig.eslint.json
:
{
"include": [
".eslintrc.js"
]
}
并将此文件仅用于附加费:
and use this file for eslint only:
parserOptions: {
project: [
'./tsconfig.eslint.json',
'./packages/*/tsconfig.json',
],
},
这篇关于为什么@ typescript-eslint / parser包含tsconfig.json中配置的文件之外的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!