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

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

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

问题描述:TypeScript ESLint解析器抱怨src/module.spec.ts不属于项目,这是正确的。我将从TypeScript spec.ts文件中排除所有tsconfig.json文件,因为它们不需要进行转译。

如何根据ESLint使src/module.spec.ts 未被复制但仍处于检查状态

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"
  ]
}

我的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"
  ]
}
typescript eslint tsconfig eslintrc typescript-eslint
1个回答
1
投票

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

{
  "exclude": [
    "./dist"
  ],
  "extends": "./tsconfig.json"
}

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

从这个新的JSON文件中读取其设置的工具(与默认的tsconfig.json相反)将not忽略src/module.spec.ts

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

"project": "test.tsconfig.json"

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

这基本上是我在遇到类似问题的一个项目中正在做的事情。可能还必须有其他几种方法才能达到相同的效果。

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