【问题标题】:ESlint - This file does not match your project configESlint - 此文件与您的项目配置不匹配
【发布时间】: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" ]

标签: typescript eslint typescript-eslint


【解决方案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/**/*。因此出现错误。
猜你喜欢
  • 2021-07-27
  • 2020-11-16
  • 2021-08-24
  • 2014-05-02
  • 2018-09-04
  • 1970-01-01
  • 2016-06-15
相关资源
最近更新 更多