ESLint: disable multiple rules or a rule for multiple lines

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# Table of Contents

  1. ESLint: how to disable multiple rules
  2. Disabling multiple ESLint rules for the current line
  3. Disable multiple ESLint rules for multiple lines (a code block)
  4. Disable a single ESLint rule for multiple lines (a code block)
  5. Disable multiple ESLint rules for an entire file
  6. Disable a single ESLint rule for an entire file
  7. Disable all ESLint rules for a block of code
  8. Disable all ESLint rules for an entire file
  9. Disable multiple ESLint rules in your .eslintrc file globally

# ESLint: how to disable multiple rules

You can disable multiple rules by separating them with a comma in a comment.

Suppose you have the following code.

index.js
let a = 5;

eslint multiple rules

As shown in the screenshot multiple rules cause ESLint errors for the line of code.

Here is an example of disabling all 3 rules for the next line by separating them with a comma.

index.js
// eslint-disable-next-line unused-imports/no-unused-vars, prefer-const, @typescript-eslint/no-unused-vars let a = 5;

multiple eslint rules disabled for next line

The example disables the following ESLint rules for the next line:

  • unused-imports/no-unused-vars
  • prefer-const
  • @typescript-eslint/no-unused-vars

Notice that the comment starts with eslint-disable-next-line.

The comment only applies to the next line so it has to be placed right above the code that causes the ESLint errors.

# Disabling multiple ESLint rules for the current line

If you want to disable multiple ESLint rules for the current line, place the comment on the line that causes the errors and separate the rules by a comma.

index.js
let a = 5; // eslint-disable-line unused-imports/no-unused-vars, prefer-const, @typescript-eslint/no-unused-vars

disable multiple rules for current line

The comment must be placed on the same line as the code that causes the ESLint errors and must start with eslint-disable-line.

# Disable multiple ESLint rules for multiple lines (a code block)

If you need to disable multiple ESLint rules for multiple lines (a code block), use a multiline comment and separate the rules with a comma.

index.js
/* eslint-disable unused-imports/no-unused-vars, prefer-const, @typescript-eslint/no-unused-vars */ let a = 5; let b = 10; /* eslint-enable unused-imports/no-unused-vars, prefer-const, @typescript-eslint/no-unused-vars */ // ๐Ÿ‘‰๏ธ rules are no longer disabled here

disable multiple rules for multiple lines

The example disables the following rules for the block of code:

  • unused-imports/no-unused-vars
  • prefer-const
  • @typescript-eslint/no-unused-vars

Notice that we used multiline comments (/* */) and not inline comments (//).

The first multiline comment should start with eslint-disable and the second multiline comment should start with eslint-enable.

The ESLint rules are enabled after the second comment.

# Disable a single ESLint rule for multiple lines (a code block)

The same approach can be used to disable a single ESLint rule for multiple lines.

index.js
/* eslint-disable unused-imports/no-unused-vars */ let a = 5; let b = 10; /* eslint-enable unused-imports/no-unused-vars */ // ๐Ÿ‘‰๏ธ rule is no longer disabled here

The example above disables the unused-imports/no-unused-vars rule for a block of code.

# Disable multiple ESLint rules for an entire file

The same approach can be used if you need to disable multiple ESLint rules for an entire file.

Add an eslint-disable command at the top of the file and specify the ESLint rules you want to disable separated by commas.

index.js
/* eslint-disable unused-imports/no-unused-vars, prefer-const, @typescript-eslint/no-unused-vars */ let a = 5; let b = 10;

The comment should start with eslint-disable.

Notice that the rules are separated by commas.

We didn't add an eslint-enable comment at the end of the code block, so the rules are disabled for the entire file.

# Disable a single ESLint rule for an entire file

The same approach is used if you want to disable a single ESLint rule for an entire file

index.js
/* eslint-disable unused-imports/no-unused-vars */ let a = 5; let b = 10;

The comment disables the unused-imports/no-unused-vars ESLint rule for the entire file.

# Disable all ESLint rules for a block of code

If you need to disable all ESLint rules for a block of code, use the eslint-disable and eslint-enable multiline comments.

index.js
/* eslint-disable */ let a = 5; let b = 10; /* eslint-enable */ // ๐Ÿ‘‰๏ธ ESLint rules are enabled here

When you use an eslint-disable comment without specifying rules, then all ESLint rules are disabled.

The eslint-enable comment enables the ESLint rules.

# Disable all ESLint rules for an entire file

You can also use a comment if you want to disable all ESLint rules for an entire file.

index.js
/* eslint-disable */ let a = 5; let b = 10;

Notice that we didn't use an eslint-enable comment, so the disable rule applies to the entire file.

Make sure to add the rule at the top of your file.

# Disable multiple ESLint rules in your .eslintrc file globally

To disable multiple ESLint rules globally in your ESLint config file, specify the rules in the rules object and set them to off.

If you use a .js config file (.eslintrc.js), use the following syntax to disable multiple rules.

.eslintrc.js
module.exports = { rules: { "unused-imports/no-unused-vars": "off", "@typescript-eslint/no-unused-vars": "off", "prefer-const": "off" }, };

disable multiple eslint rules in eslintrc

Notice that the name of each rule is set as a property in the rules object and the value is set to off to disable the rule globally.

If you use a JSON ESLint config (.eslintrc or .eslintrc.json), use the following syntax instead.

.eslintrc.json
{ "rules": { "unused-imports/no-unused-vars": "off", "@typescript-eslint/no-unused-vars": "off", "prefer-const": "off" } }

Notice that each string property and value must be double-quoted.

Make sure you don't have a trailing comma if you manage your ESLint config using JSON.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev