DevNebulae
DevNebulae

Reputation: 4916

Disable eslint rules for folder

Is there a way to disable specific rules for a folder? For example, I don't want to have required JSDoc comments for all my test files in the test folder. Is there a way to do this?

Upvotes: 343

Views: 311619

Answers (7)

James
James

Reputation: 25543

For eslint 9+ (flat config), this has changed. You can now use either --ignore-pattern="mydir/" on the command line, or the ignores section of the flat config. The .gitignore and .eslintignore files are no longer supported.

There is a surprising effect with the ignores section of the flat config. It means two different things if it is on its own, or combined in the same block as other rules.

On its own it means 'ignore these files for everything', but in a block with other rules it means only 'ignore these files for these other changes to the rules'.

So to ignore specific rules for a specific set of directories:

eslint.config.js

const js = require('@eslint/js');

module.exports = [
  // base rules applied to everything:
  js.configs.recommended,

  // rules applied to all files except those in
  // './myFolder':
  {
    ignores: [
      // for non-global ignores a full glob pattern is required
      'myFolder/**/*',
    ],
    rules: {
        'no-use-before-define': 'error'
    }
  }
]

(for eslint.config.mjs or ES6 modules, replace the require() with import js from "@eslint/js")

Or to just ignore a directory globally for ALL your eslint rules:

const js = require('@eslint/js');

module.exports = [
  {
    ignores: [
      // for global ignores a directory name works:
      'alwaysIgnoredDir/'
    ]
  },
  js.configs.recommended,
  {
    // ... other config to override recommended config
  }
]

It's worth mentioning that for the flat config, instead of using 'ignores' to ignore a directory like this, you might be better specifying the opposite: which files which parts of your config should apply to, using the files property.

Upvotes: 5

ggorlen
ggorlen

Reputation: 57289

On the command line, you can use eslint . --ignore-pattern 'test/*' to ignore all files in the test directory.

Upvotes: 0

In ESLint 6.7.0+ use "ignorePatterns": [].

Paths can begin with /, like /dist/** or /test*/**, but cannot begin with ./, like ./build/**.

You can tell ESLint to ignore specific files and directories using ignorePatterns in your config files.

example of .eslintrc.js

module.exports = {
  env: {
    // ...
  },
  extends: [
    // ...
  ],
  parserOptions: {
    // ...
  },
  plugins: [
    // ...
  ],
  rules: {
    // ...
  },
  ignorePatterns: ["src/test/*"], // <<< ignore all files in test folder
};

Or you can ignore files with some extension:

ignorePatterns: ['**/*.js']

You can read the user-guide doc here.

https://eslint.org/docs/user-guide/configuring/ignoring-code

Upvotes: 69

Amir H Rahimi
Amir H Rahimi

Reputation: 177

Unfortunately, for some reason ignoring a path using .eslintignore may not work. If you are using a typescript project, the best practice is to exclude those paths in tsconfig.json like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "rootDir": "src",
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true
  },
  "include": [
    "src",
    "declaration.d.ts"
  ],
  "exclude": [
    "node_modules",
    "src/your-custom-path"
  ]
}

This will ensure that your build will be completed successfully.

Upvotes: -1

Alberto Perez
Alberto Perez

Reputation: 2922

The previous answers were in the right track, but the complete answer for this is going to be about disabling rules only for a group of files, there you'll find the documentation needed to disable/enable rules for certain folders (Because in some cases you don't want to ignore the whole thing, only disable certain rules). Example:

    {
        "env": {},
        "extends": [],
        "parser": "",
        "plugins": [],
        "rules": {},
        "overrides": [
          {
            "files": ["test/*.spec.js"], // Or *.test.js
            "rules": {
              "require-jsdoc": "off"
            }
          }
        ],
        "settings": {}
    }

Upvotes: 165

user2226755
user2226755

Reputation: 13223

YAML version :

overrides:
  - files: *-tests.js
    rules:
      no-param-reassign: 0

Example of specific rules for mocha tests :

You can also set a specific env for a folder, like this :

overrides:
  - files: test/*-tests.js
    env:
      mocha: true

This configuration will fix error message about describe and it not defined, only for your test folder:

/myproject/test/init-tests.js
6:1 error 'describe' is not defined no-undef
9:3 error 'it' is not defined no-undef

Upvotes: 3

YaTaras
YaTaras

Reputation: 5329

To ignore some folder from eslint rules we could create the file .eslintignore in root directory and add there the path to the folder we want omit (the same way as for .gitignore).

Here is the example from the ESLint docs on Ignoring Files and Directories:

# path/to/project/root/.eslintignore
# /node_modules/* and /bower_components/* in the project root are ignored by default

# Ignore built files except build/index.js
build/*
!build/index.js

Upvotes: 486

Related Questions