Overrides for Specific Files
You'll learn how to apply different ESLint rules to specific files or directories without changing your entire project's configuration.
Why Overrides Matter
Imagine your linting rules are like a company dress code: professional attire for the office. But what about the gym? You wouldn't wear a suit on the treadmill. Similarly, your test files, configuration files, or build scripts often need different linting rules than your main application code.
The overrides key in your ESLint config lets you define custom rules for specific file patterns while keeping your base configuration intact.
How Overrides Work
In your .eslintrc file, you add an overrides array. Each override object specifies:
files: A glob pattern matching the files to targetrules: The different rules to apply to those files
{
"rules": {
"no-console": "error"
},
"overrides": [
{
"files": ["**/*.test.js", "**/*.spec.js"],
"rules": {
"no-console": "off"
}
}
]
}
In this example, no-console is an error everywhere except in test files, where console logging is helpful for debugging.
Common Override Patterns
You might disable no-undef in configuration files that use Node.js globals, relax strict rules in legacy directories, or allow unused expressions in build scripts. Each file type can have rules that make sense for its context.
{
"overrides": [
{
"files": ["scripts/**/*.js"],
"rules": {
"no-console": "off"
}
}
]
}
Key Takeaway: Overrides let you tailor ESLint rules to specific file patterns, ensuring appropriate linting standards for different parts of your codebase without compromising your baseline quality standards.