为什么我专门为 .test.ts 文件为 @typescript-eslint/parser 设置了"parserOptions.project"?



在过去的三天里,我一直在试图修复.test.ts文件的这个错误。

我有我的tsconfig如下

{
"compilerOptions": {
"target": "es6",                                     /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["ESNext"],                                   /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"module": "commonjs",                                /* Specify what module code is generated. */
"rootDir": "./src",                                  /* Specify the root folder within your source files. */
"moduleResolution": "node",                          /* Specify how TypeScript looks up a file from a given module specifier. */
"baseUrl": "src",                                    /* Specify the base directory to resolve non-relative module names. */
"types": ["jest", "node"],                                      /* Specify type package names to be included without being referenced in a source file. */
"resolveJsonModule": true,                           /* Enable importing .json files */
"allowJs": true,                                     /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
"checkJs": true,                                     /* Enable error reporting in type-checked JavaScript files. */
"outDir": "./dist",                                  /* Specify an output folder for all emitted files. */
"removeComments": true,                              /* Disable emitting comments. */
"importsNotUsedAsValues": "error",                   /* Specify emit/checking behavior for imports that are only used for types */
"isolatedModules": true,                             /* Ensure that each file can be safely transpiled without relying on other imports. */
"allowSyntheticDefaultImports": true,                /* Allow 'import x from y' when a module doesn't have a default export. */
"esModuleInterop": true,                             /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */
// "preserveSymlinks": true,                         /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
"forceConsistentCasingInFileNames": true,            /* Ensure that casing is correct in imports. */
/* Type Checking */
"strict": true,                                      /* Enable all strict type-checking options. */
"noImplicitAny": true,                               /* Enable error reporting for expressions and declarations with an implied `any` type.. */
"noUnusedLocals": true,                              /* Enable error reporting when a local variables aren't read. */
"noUnusedParameters": true,                          /* Raise an error when a function parameter isn't read */
"exactOptionalPropertyTypes": false,                 /* Interpret optional property types as written, rather than adding 'undefined'. */
"noImplicitReturns": true,                           /* Enable error reporting for codepaths that do not explicitly return in a function. */
"noFallthroughCasesInSwitch": true,                  /* Enable error reporting for fallthrough cases in switch statements. */
"noUncheckedIndexedAccess": true,                    /* Include 'undefined' in index signature results */
"noImplicitOverride": true,                          /* Ensure overriding members in derived classes are marked with an override modifier. */
"noPropertyAccessFromIndexSignature": true,          /* Enforces using indexed accessors for keys declared using an indexed type */
"allowUnusedLabels": false,                          /* Disable error reporting for unused labels. */
"allowUnreachableCode": false,                       /* Disable error reporting for unreachable code. */
"skipLibCheck": true                                 /* Skip type checking all .d.ts files. */
},
"include": ["src"],
"exclude": ["node_modules", "dist", "coverage", "src/__tests__", "**/*.test.ts"]
}

我的eslintrc.js有这些内容

module.exports = {
env: {
es2021: true,
node: true,
jest: true,
},
extends: [
'eslint:recommended',
'airbnb-base',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-requiring-type-checking',
'plugin:prettier/recommended',
'plugin:import/typescript',
'prettier'
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
project: 'tsconfig.json',
tsconfigRootDir: './',
},
plugins: ['@typescript-eslint', 'prettier', 'import'],
rules: {
}
}

src/__tests__/sample.test.ts中,我得到这个错误

Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser.
The file does not match your project config: src/__tests__/sample.test.ts.
The file must be included in at least one of the projects provided.

我注意到它只发生在.test.ts文件中,其余的只有.ts扩展名的typescript文件工作正常

我错过了什么?

所以,由于我在tsconfig,json中排除了测试文件,我不得不在开发期间以某种方式将这些文件添加到eslint中,并在构建时将它们排除在外。

我最终创建了一个新文件tsconfig.eslint.json,内容为

{
"extends": "./tsconfig.json", <-- since the entire src folder is already included here
"exclude": ["node_modules", "dist", "coverage"] <--- override the exclude property to not have test files here
}

在我的eslintrc.js

parserOptions: {
sourceType: 'module',
project: 'tsconfig.eslint.json',
tsconfigRootDir: './',
},

修复错误。

最新更新