36

Is it possible to have different rules applied to files based on their files extension? For example I have [test].spec.js test spec files residing along my source code, but I like to add rules that only apply to them only.

2 Answers 2

69

This was added. Example from the docs:

{
  "rules": {
    "quotes": [ 2, "double" ]
  },

  "overrides": [
    {
      "files": [ "bin/*.js", "lib/*.js" ],
      "excludedFiles": "*.test.js",
      "rules": {
        "quotes": [ 2, "single" ]
      }
    }
  ]
}

Effecitively you can supply globs and a set of rules that override the default rules for files that match each glob.

Sign up to request clarification or add additional context in comments.

3 Comments

What I hate about this is that now, there is no way to share the rules.
Actually nvm, I figured out the way: You can just add a new object to the overrides section which includes all the file extensions, and then define your rules in this object
Doc link needs to be updated eslint.org/docs/user-guide/configuring/…
2

Currently it's not possible, unless you create multiple configuration files and run eslint with correct globs for each file type separately. However, there's an open PR here: https://github.com/eslint/eslint/pull/8081 to add this functionality.

2 Comments

What should happen to this answer now?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.