Different Ways to Make ESLint Hush (Globally)
Okay, you've weighed the pros and cons and decided that a global ignore is the way to go. Here are a few methods you can use:
2. Method 1
This is the most common and recommended approach. The .eslintignore
file is a simple text file that tells ESLint which files and directories to ignore. Think of it as a "Do Not Disturb" sign for your code.
To use it, create a file named .eslintignore
in the root directory of your project. Then, list the files or directories you want ESLint to ignore, one per line. You can use glob patterns to match multiple files at once. For example, to ignore all files in the vendor
directory, you'd add the line vendor/
to the .eslintignore
file.
The beauty of this method is that it's clean and explicit. It clearly communicates which files are being ignored and why. It's also easy to share with other developers working on the same project.
For instance, if you want to ignore all JavaScript files in a "dist" folder as well as a specific file called "legacy.js" you'd put these lines in your .eslintignore:
dist/
.jslegacy.js
3. Method 2
You can also configure ESLint directly in your package.json
file. This approach is useful if you don't want to create a separate .eslintignore
file.
To do this, add an eslintConfig
section to your package.json
file. Within the eslintConfig
, you can specify the ignorePatterns
array. This array contains the files and directories you want ESLint to ignore, just like in the .eslintignore
file.
For example:
{ "name": "my-project", "version": "1.0.0", "eslintConfig": { "ignorePatterns": ["vendor/
", "dist/"] }}
While convenient, this method can make your package.json
file a bit cluttered, especially if you have a lot of ignore patterns.
4. Method 3
Finally, you can specify ignore patterns directly on the command line when running ESLint. This is the least recommended approach for global ignoring, as it's easy to forget the arguments and accidentally lint files you meant to ignore.
To use this method, add the --ignore-pattern
flag followed by the ignore pattern to your ESLint command. For example:
eslint . --ignore-pattern "vendor/
"
This only ignores the "vendor" directory for that specific command execution. It's more useful for one-off situations than for truly global ignores.
Imagine having to type that out every single time! It's a recipe for typos and frustration.
Best Practices for Ignoring ESLint
Regardless of which method you choose, here are some best practices to keep in mind:
5. Document Your Ignores
Add comments to your .eslintignore
file or package.json
explaining why certain files or directories are being ignored. This will help other developers (and your future self) understand the reasoning behind the ignores.
For example:
# Ignore third-party library that doesn't conform to our linting rulesvendor/
# Ignore generated filesdist/
A little documentation goes a long way in preventing confusion and ensuring that ignores aren't accidentally removed later on.
Trust me, future you will thank you!
6. Be Specific
Avoid using overly broad ignore patterns. The more specific you are, the less likely you are to accidentally ignore files that you should be linting.
For example, instead of ignoring all .js
files in the src
directory, try ignoring only the specific files that need to be ignored.
Instead of src/
.js
, use src/problematic_file.js
.
7. Regularly Review Your Ignores
Periodically review your .eslintignore
file or package.json
to ensure that the ignores are still necessary. As your project evolves, you may be able to remove some ignores and start linting those files.
Think of it as spring cleaning for your codebase. Get rid of the unnecessary ignores and let ESLint do its job.
Schedule a recurring task in your project management system to remind yourself to review the ESLint ignores.