TrueWill
TrueWill

Reputation: 25523

Catch-22 with parserOptions and exclude

I'm wanting to add @typescript-eslint/no-floating-promises to my ESLint rules and am having difficulties.

That requires parserOptions.

Here's my .eslintrc.js:

module.exports = {
  root: true,
  parser: '@typescript-eslint/parser',
  parserOptions: {
    ecmaVersion: 2017,
    project: './tsconfig.json'
  },
  plugins: [
    '@typescript-eslint',
  ],
  extends: [
    'eslint:recommended',
    'plugin:@typescript-eslint/eslint-recommended',
    'plugin:@typescript-eslint/recommended',
    'prettier'
  ],
  rules: {
    '@typescript-eslint/no-floating-promises': ['error'],
    '@typescript-eslint/no-explicit-any': 'off'
  }
};

and my tsconfig.json:

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["ES2017"],
    "module": "commonjs",
    "declaration": true,
    "outDir": "lib",
    "removeComments": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"],
  "exclude": ["src/**/*.spec.ts"]
}

The catch is the exclude. I don't want my tests (specs) to be compiled to the outDir. But this causes the following error when I run ESLint:

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

(Script is "lint": "eslint \"src/**/*.ts\"" )

I do want to lint my tests.

Everything worked fine until I added parserOptions - and I need that for the no-floating-promises rule.

The typescript-eslint FAQ says:

To fix this, simply make sure the include option in your tsconfig includes every single file you want to lint.

The catch is that I'm creating a library, and I don't want to publish the test files.

How can I build only the files I want to publish to one directory while still linting all the files? (I would like to keep the build as simple as possible.)

Upvotes: 2

Views: 667

Answers (1)

Lauren Yim
Lauren Yim

Reputation: 14108

I use two tsconfig.jsons in my projects.

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["ES2017"],
    "module": "commonjs",
    "declaration": true,
    "outDir": "lib",
    "removeComments": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": ["src/**/*"]
}

tsconfig.build.json

{
    "extends": "./tsconfig.json",
    "exclude": ["src/**/*.spec.ts"]
}

To build the project run tsc -p tsconfig.build.json.

Upvotes: 1

Related Questions