96

Is it possible with ESLint to ignore one specific rule for an entire directory?

In my case, I would like to ignore import/prefer-default-export for a directory named commonComponents

Kevin Amiranoff
  • 13,440
  • 11
  • 59
  • 90

4 Answers4

168

ESLint configuration (.eslintrc) files are hierarchical:

ESLint will automatically look for them in the directory of the file to be linted, and in successive parent directories all the way up to the root directory of the filesystem. This option is useful when you want different configurations for different parts of a project or when you want others to be able to use ESLint directly without needing to remember to pass in the configuration file.

You can disable the import/prefer-default-export rule for the commonComponents directory by creating a .eslintrc file with the following content in that directory:

{
    "rules": {
        "import/prefer-default-export": "off"
    }
}
user2226755
  • 12,494
  • 5
  • 50
  • 73
cartant
  • 57,105
  • 17
  • 163
  • 197
  • 3
    Can you tell me where did you find the documentation for how to toggle this rule? The eslint-plugin-import docs were no help – Dimitris Karagiannis Mar 22 '17 at 00:40
  • 1
    @Dimitris Not sure what you mean. The rule has [no options](https://github.com/benmosher/eslint-plugin-import/blob/master/docs/rules/prefer-default-export.md), so it's just a simple on/off as per the [ESLint documentation](http://eslint.org/docs/user-guide/configuring#configuring-rules). – cartant Mar 22 '17 at 00:47
101

You can also use the "overrides" key to declare rules for different glob patterns.

Have a read of Configuration Based on Glob Patterns

Sometimes a more fine-controlled configuration is necessary, for example if the configuration for files within the same directory has to be different. Therefore you can provide configurations under the overrides key that will only apply to files that match specific glob patterns, using the same format you would pass on the command line (e.g., app/**/*.test.js).

I use this to remove the no-unused-expressions rule from my test files like so;

"overrides": [{
  "files": [ "*.spec.js" ],
  "rules": {
    "no-unused-expressions": 0
  }
}]
user2226755
  • 12,494
  • 5
  • 50
  • 73
Patrick
  • 6,495
  • 6
  • 51
  • 78
3

If there are multiple directories that you want to apply your rules to, then you can create different configs for different purposes. For example:

  • .eslintrc.json for common config
  • .eslintrc-main.json for main linting and run eslint -c .eslintrc-main src test
  • .eslintrc-comp.json for components and run eslint -c .eslintrc-comp commonComponents fooBarComponent
tibalt
  • 15,201
  • 1
  • 29
  • 22
-2

YAML version :

rules:
  no-unused-expressions: true

overrides:
  - files: *-tests.js
    rules:
      no-unused-expressions: false
user2226755
  • 12,494
  • 5
  • 50
  • 73