【问题标题】:ESLint with TypeScript parser/plugin: how to include .spec.ts, that is excluded from tsconfig.json?带有 TypeScript 解析器/插件的 ESLint:如何包含从 tsconfig.json 中排除的 .spec.ts?
【发布时间】:2020-09-21 01:17:37
【问题描述】:

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

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

如何使src/module.spec.ts 不转译但仍检查与 ESLint?

我的项目\src\module.spec.ts 0:0 错误解析错误:“parserOptions.project”已为@typescript-eslint/parser 设置。 该文件与您的项目配置不匹配: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】:

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

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

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

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

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

"project": "test.tsconfig.json"

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

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

【讨论】:

  • 但是 test.tsconfig.json 然后会覆盖项目 tsconfig.json 对吗?或者我们可以两者兼得?
  • 没错。 TypeScript 默认使用 tsconfig.json,而 typescript-eslint 使用任何设置为 project 的文件是 ESLint 配置。
  • 哦,我明白了。聪明的。顺便说一句,为什么要重复排除键?将其命名为 tsconfig.eslint.conf 不是更好吗?有什么命名约定吗?
  • 查看我的更新答案。我找不到任何有关命名约定的信息,VSCode 和其他 IDE 仅将 tsconfig.json 识别为 TS 配置文件。无论如何,docs 提供了两个替代名称示例:configs/base.jsontsconfig.nostrictnull.json(虽然都是非常规的)。
  • 所以在 monorepo 中,我必须为每个项目创建 2 个 tsconfig,这样我才能对测试进行 linted?
猜你喜欢
  • 2018-07-05
  • 2017-09-23
  • 2018-03-27
  • 2020-09-09
  • 2019-05-09
  • 2021-03-04
  • 2022-11-11
  • 2020-12-02
  • 2012-04-14
相关资源
最近更新 更多