简体   繁体   English

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

[英]Why am I getting "parserOptions.project" has been set for @typescript-eslint/parser specifically for .test.ts files?

I have been trying to fix this error for .test.ts files for the past three days.在过去的三天里,我一直在尝试修复.test.ts文件的这个错误。

I have my tsconfig as follows我的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"]
}

And my eslintrc.js has these contents而我的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: {
 }
}

In src/__tests__/sample.test.ts I get this errorsrc/__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.

I have noticed that it only occurs for .test.ts files rest of the typescript files with only .ts extension are working fine我注意到它只发生在.test.ts文件中,其余只有.ts扩展名的打字稿文件工作正常

What am I missing?我错过了什么?

So, since I was excluding the test files in tsconfig,json I had to somehow add those files for eslint to lint during development and also exclude them on build time.因此,由于我在tsconfig,json中排除了测试文件,因此我不得不在开发过程中以某种方式将这些文件添加到 eslint 到 lint 中,并在构建时排除它们。

I ended up creating a new file tsconfig.eslint.json with the contents我最终创建了一个包含内容的新文件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
}

And in my eslintrc.js在我的eslintrc.js

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

This fixes the error.这修复了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 已为@typescript-eslint/parser 设置解析错误“parserOptions.project” - Parsing error "parserOptions.project" has been set for @typescript-eslint/parser eslint 缩进和@typescript-eslint/indent 冲突 - eslint indent and @typescript-eslint/indent conflict npm 错误。 notarget 找不到与@typescript-eslint/parser@5.4.0 匹配的版本 - npm ERR! notarget No matching version found for @typescript-eslint/parser@5.4.0 为什么会出现错误“标识符...已经被声明”? - Why am I getting the error “Identifier … has already been declared”? 类型定义在.test.ts 文件中不可用 - Types definitions are not available in .test.ts file 无法运行我的 nodejs 和 typescript 项目,并且我得到“.ts 作为未知文件扩展名” - Cant run my nodejs and typescript project, and I am getting “.ts as an unknown file extension” ESLint 与 eslint-plugin-import 和 typescript-eslint 冲突 - ESLint conflicts with eslint-plugin-import and typescript-eslint 为什么我在每个 d.ts 文件中都收到 typescript SyntaxError? - Why i am getting typescript SyntaxError in every d.ts file? @typescript-eslint/no-unsafe-assignment: `any` 值的不安全赋值 - @typescript-eslint/no-unsafe-assignment: Unsafe assignment of an `any` value 找不到与 @typescript-eslint/scope-manager@4.22.1 匹配的版本 - No matching version found for @typescript-eslint/scope-manager@4.22.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM