Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint with TypeScript parser/plugin: how to include .spec.ts, that is excluded from tsconfig.json?

I'm using the awesome typescript-eslint together with ESLint.

Problem description: the TypeScript ESLint parser complains about src/module.spec.ts not being part of the project, and this is correct. I'm excluding all spec.ts files from TypeScript tsconfig.json file because they don't need to be transpiled.

How to make src/module.spec.ts not transplied but still checked against ESLint?

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

My .eslint.json (stripped down):

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

My 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"
  ]
}
like image 334
gremo Avatar asked Jun 02 '20 14:06

gremo


1 Answers

In the project folder containing tsconfig.json, create another JSON file with the following contents:

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

The exact file name is irrelevant here. Note that this is a perfectly valid TypeScript configuration file where all settings are inherited from ./tsconfig.json, execept for extends, where the pattern "**/*.spec.ts" has been removed.

A tool that reads its settings from this new JSON file (as opposed to the default tsconfig.json) will not ignore src/module.spec.ts.

Then in your .eslint.json under parserOptions set the name of the new JSON file as project, e.g. if the file is called test.tsconfig.json:

"project": "test.tsconfig.json"

Then run ESLint and see what else it's complaining about.

This is basically what I'm doing in one of my projects having encountered a similar problem. There must be probably a few other ways to achieve the same effect.

like image 106
GOTO 0 Avatar answered Nov 15 '22 08:11

GOTO 0