为什么我得到“parserOptions.project”已设置为 @typescript-eslint/parser 专门用于 .test.ts 文件?

问题描述 投票:0回答:2

过去三天我一直在尝试修复

.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
扩展名的打字稿文件工作正常

我错过了什么?

node.js typescript eslint
2个回答
8
投票

因此,由于我排除了

tsconfig,json
中的测试文件,我必须在开发过程中以某种方式将 eslint 的这些文件添加到 lint 中,并在构建时排除它们。

我最终创建了一个新文件

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: './',
  },

这修复了错误。


0
投票

嗨,我的朋友 我很高兴了解您有趣的计划,并希望看到它继续推动我们在生活和工作中取得成功

© www.soinside.com 2019 - 2024. All rights reserved.