Wednesday, May 14, 2025

How to disable ESLint for some lines, files or folders

 In this example we will explore how to disable ESLint for specific lines, files and folder with particle examples. Disabling ESLint for specific lines, files, or folders is often necessary when you want to bypass certain rules temporarily or for parts of your codebase. Below is a detailed explanation of how to do this in various scopes:

How to disable ESLint for some lines, files or folders


Disable ESLint for a Single Line

To ignore ESLint on a specific line, use the comment. // eslint-disable-line no-unused-vars

Use an inline comment:

const foo = 'bar'; // eslint-disable-line no-unused-vars


Or to disable multiple rules on the line:

const foo = 'bar'; // eslint-disable-line no-unused-vars, no-console


Disable ESLint for the next line

To ignore ESLine for next line, use the comment.  //eslint-disable-next-line no-unused-vars

// eslint-disable-next-line no-unused-vars
const foo = 'bar';

Disable ESLint for a Block of Code

To ignore a block of code, wrap it with eslint-disable and eslint-enable.

/* eslint-disable no-console */
console.log("Logging...");
console.error("Error");
/* eslint-enable no-console */

You can disable multiple rules or all rules for that block:

/* eslint-disable */  // Disables ALL rules
someUnknownFunction();
/* eslint-enable */


Disable ESLint for a Whole File

At the top of the file, add:

/* eslint-disable */

 //code block

Or, to disable specific rules:

/* eslint-disable no-console, no-unused-vars */

 //code block

Disable ESLint for a File or Folder via Configuration

You can tell ESLint to completely ignore files or folders using:

1. .eslintignore file (like .gitignore)

Create a file named .eslintignore in the root of your project:

dist/
node_modules/
build/
src/legacy_code/fileToIgnore.js
This method is great for build artifacts, third-party libraries, or legacy code.


2. ESLint CLI using --ignore-pattern

When running ESLint from the CLI:

eslint . --ignore-pattern "src/vendor/**"


Disable via ESLint Config File

You can also exclude files using the overrides and excludedFiles in .eslintrc.js or .eslintrc.json:

// .eslintrc.js
module.exports = {
  overrides: [
    {
      files: ["legacy/*.js"],
      excludedFiles: "*.test.js",
      rules: {
        // Optional: relax rules for legacy code
        "no-console": "off",
      },
    },
  ],
};


NOTES :

  1. Prefer fixing the rule violation instead of disabling it unless you have a clear reason.
  2. Avoid disabling all ESLint rules (/* eslint-disable */) unless absolutely necessary.
  3. Leave comments explaining why you're disabling a rule.



No comments:

Post a Comment