[英]Why am I getting "parserOptions.project" has been set for @typescript-eslint/parser specifically for .test.ts files?
[英]Parsing error "parserOptions.project" has been set for @typescript-eslint/parser
我创建了一个新的NestJS 项目,它是一个非常流行的 NodeJS 框架。 但是我的 IDE (PhpStorm 2020.2-Beta) 上有这个错误(见标题),ESLint 根本不起作用。
我使用了 NestJS CLI:
nest new nestjs-micro
我似乎不是唯一遇到这个问题的人,所以找到这个问题的原因并一劳永逸地解决它会很好。
我已经有一个未解决的问题,但我还没有得到答案,这真的很成问题。
如果有人知道如何解决问题并保持 ESLint / Prettier 与 PhpStorm 的集成,谢谢。
复现
// .eslintrc.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
root: true,
env: {
node: true,
jest: true,
},
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
// tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true
}
}
版本
Typescript: 3.7.4
Node: 14.3.0
ESLint: 7.1.0
@typescript-eslint/parser: 3.0.2
Yarn: 1.22.4
FWIW 对我有用的这个错误是将ignorePatterns: ['.eslintrc.js'],
添加到.eslintrc.js
文件中。 这一行告诉eslint
忽略.eslintrc.js
文件(因为它不包含在tsconfig
声明的项目rootDir
)。
我想到了。
当 Typescript 没有要包含用于编译的文件时,会发生该错误。
最简单的解决方案是创建一个tsconfig.build.json文件,并在其中指定以下参数:
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "dist/**/*spec.ts"],
"include": ["src/**/*", ".eslintrc.js"]
}
上面的示例适用于 NestJS,但也适用于其他项目。
最令人惊讶的是,它只是出现在 PHPStorm 上的一个错误,构建,它工作正常。
将其添加到 tsconfig.json 中的包含:
"include": [
".eslintrc.js",
]
在我的例子中,这是由于typings.d.ts
文件并将其添加到 tsconfig.json 中的包含:
"include": [
...
"typings.d.ts",
...
]
解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.