Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use ESLint plugin only for some files/directories

I'm using the ESLint plugin for Jest to lint my Jest tests. The structure of my project is

my-project
|
|--tests
   |
   |--unit
   |
   |--e2e

I only want to use the Jest plugin when linting files in the tests/unit dir (because the files in tests/e2e are not Jest tests), but when I run the linting, this plugin is applied to all files under tests/unit, which results in spurious errors in the tests/e2e files.

I can disable individual rules in the tests/e2e dir by adding the following to eslintrc.js

module.exports = {
  root: true,
  env: {
    node: true,
    'jest/globals': true
  },
  extends: [
    'eslint:recommended',
    'plugin:jest/recommended',
  ],
  parserOptions: {
    parser: 'babel-eslint'
  },
  plugins: ['jest'],
  "overrides": [
    {
      "files": ["tests/e2e/*.js"],
      "rules": {
        "jest/no-disabled-tests": "off",
        "jest/expect-expect": "off",
        "jest/valid-expect-in-promise": "off"
      }
    }
  ]
}

But rather than disabling individual rules in the tests/e2e dir, I would like to disable the Jest plugin completely.

like image 237
Antonio Dragos Avatar asked Jan 24 '20 16:01

Antonio Dragos


1 Answers

From the eslint docs regarding overrides:

A glob specific configuration works almost the same as any other ESLint config. Override blocks can contain any configuration options that are valid in a regular config, with the exception of root and ignorePatterns.

Credit to the SO answer that helped me: Eslint allow multiple parsers based on glob patterns

The example in that answer also demonstrates overriding plugins.

My example uses two overrides sections, one for JavaScript files and one for TypeScript. This could easily be two different directories.

{
"overrides": [
  {
    "files": "**/*.ts",
    "plugins": [
      "prettier",
      "@typescript-eslint"
    ],
    "extends": [
      "eslint-config-prettier",
      "prettier",
      "prettier/@typescript-eslint",
      "plugin:import/errors",
      "plugin:import/warnings",
      "plugin:import/typescript",
      "plugin:@typescript-eslint/recommended",
      "plugin:@typescript-eslint/recommended-requiring-type-checking",
      "plugin:prettier/recommended"
    ],
    "parser": "@typescript-eslint/parser",
    "parserOptions": {
      "project": "./tsconfig.json"
    },
    "rules": {
     
    }
  },
  {
    // Disabling TypeScript lint rules for JS files.
    "files": "**/*.js",
    "plugins": [
      "prettier"
    ],
    "extends": [
      "esnext",
      "eslint-config-prettier",
      "prettier",
      "plugin:import/errors",
      "plugin:import/warnings",
      "plugin:prettier/recommended"
    ],
    "parser": "babel-eslint", 
    "parserOptions": {
    },
    "env": {
      "node": true
    },
    "rules": {
      
    }
  }
]

}

like image 190
markdon Avatar answered Sep 22 '22 17:09

markdon