带有 TypeScript 解析器/插件的 ESLint:如何包含从 tsconfig.json 中排除的 .spec.ts?

gre*_*emo 5 typescript eslint tsconfig eslintrc typescript-eslint

我正在使用很棒的 typescript-eslint和 ESLint。

问题描述:TypeScript ESLint 解析器抱怨src/module.spec.ts不是项目的一部分,这是正确的。我spec.ts从 TypeScripttsconfig.json文件中排除了所有文件,因为它们不需要被转译。

如何使src/module.spec.ts 未转译但仍针对 ESLint进行检查

my-project\src\module.spec.ts 0:0 错误解析错误:已为@typescript-eslint/parser 设置了“parserOptions.project”。该文件与您的项目配置不匹配:src\module.spec.ts。该文件必须至少包含在提供的项目之一中

我的.eslint.json(精简):

{
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended"
  ],
  "env": {
    "node": true
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "ecmaVersion": 6,
    "sourceType": "module",
    "project": "tsconfig.json"
  },
  "plugins": [
    "@typescript-eslint"
  ]
}
Run Code Online (Sandbox Code Playgroud)

我的tsconfig.json

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "declaration": true,
    "sourceMap": false,
    "outDir": "./dist",
    "rootDir": "./src",
    "removeComments": true,
    "strict": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true
  },
  "exclude": [
    "./dist",
    "**/*.spec.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

GOT*_*O 0 4

在包含 的项目文件夹中tsconfig.json,创建另一个包含以下内容的 JSON 文件:

{
  "exclude": [
    "./dist"
  ],
  "extends": "./tsconfig.json"
}
Run Code Online (Sandbox Code Playgroud)

确切的文件名在这里无关紧要。请注意,这是一个完全有效的 TypeScript 配置文件,其中所有设置均继承自./tsconfig.jsonextends但模式"**/*.spec.ts"已被删除。

tsconfig.json从这个新的 JSON 文件(而不是默认文件)读取其设置的工具将不会忽略src/module.spec.ts

然后在您的.eslint.json下方parserOptions将新 JSON 文件的名称设置为project,例如,如果该文件名为test.tsconfig.json

"project": "test.tsconfig.json"
Run Code Online (Sandbox Code Playgroud)

然后运行 ​​ESLint 看看它还抱怨什么。

这基本上就是我在遇到类似问题的项目之一中所做的事情。可能还有其他一些方法可以达到相同的效果。