ESlint - 此文件与您的项目配置不匹配

Posted

技术标签:

【中文标题】ESlint - 此文件与您的项目配置不匹配【英文标题】:ESlint - This file does not match your project config 【发布时间】:2021-12-07 09:37:00 【问题描述】:

我遇到了以下两个错误

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

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

我的项目应该用作库,包含一个 src 和一个测试文件夹。

src/index.ts 是我的入口文件,src/loaders/index.ts 是通过节点样式模块解析导入的,即import * from './loaders'

这是我的.eslintrc


  "parser": "@typescript-eslint/parser",
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:jest/style",
    "plugin:jest/recommended",
    "prettier"
  ],
  "plugins": ["@typescript-eslint"],
  "parserOptions": 
    "project": "./tsconfig.eslint.json"
  ,
  "root": true,
  "rules": 
    ...
  

这是我的 tsconfigs tsconfig.json


  "compilerOptions": 
    "target": "es2020",
    "module": "commonjs",
    "allowJs": true,
    "checkJs": true,
    "sourceMap": true,
    "outDir": "./dist",
    "strict": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "isolatedModules": true,
    "baseUrl": ".",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true
  ,
  "include": ["src/**/*"]


tsconfig.eslint.json


  "extends": "./tsconfig.json",
  "include": [
    "tests/**/*"
  ]

【问题讨论】:

我目前的临时修复是添加一个包含语句"include": [ "tests/**/*", "**/index.ts" ] 【参考方案1】:

TypeScript-ESLint 文档中有一个方便的常见问题解答

https://typescript-eslint.io/docs/linting/type-linting#i-get-errors-telling-me-the-file-must-be-included-in-at-least-one-of-the-projects-provided

TL;DR - 如果您想使用类型信息对文件进行 lint,那么您必须将其包含在通过 parserOptions.projects 提供的 tsconfig 中。

另外值得注意的是,在扩展另一个 tsconfig 文件的 tsconfig 文件中 - 包含不会与扩展配置合并。 因此,在您的tsconfig.eslint.json 中,您只包含tests/**/*,而src/**/* 不包括在内。因此出现错误。

【讨论】:

这对测试有意义,但对索引导入没有意义。 在扩展另一个 tsconfig 文件的 tsconfig 文件中 - includes 未与扩展配置合并。因此,在您的tsconfig.eslint.json 中,您只有includes tests/**/*,并且不包括src/**/*。因此出现错误。

以上是关于ESlint - 此文件与您的项目配置不匹配的主要内容,如果未能解决你的问题,请参考以下文章

解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。该文件与您的项目配置不匹配:.eslintrc.js

权利文件与您的配置文件中指定的不匹配。(0xE8008016)

权利文件与您的配置文件中指定的不匹配。(0xE8008016)

您的应用程序包签名中的权利与您的配置文件中包含的权利不匹配

我们计算的请求签名与您提供的 aws 签名不匹配

eslint 的配置