How can I use ESLint no-unused-vars for a block of code?

Viewed 66830

I need to disable some variable checks in ESLint.

Currently, I am using this code, but am not getting the desired result:

/* eslint no-unused-vars: ["error", { "caughtErrorsIgnorePattern": "Hey" }] */
export type Hey = {
  a: string,
  b: object
}

Two questions:

  • Is there a variant which can enable no-unused-vars for a block of code?

Something like...

/* eslint rule disable"*/

// I want to place my block of code, here

/* eslint rule disable"*/
  • Or could I make Hey a global variable so that it can be ignored everywhere?
7 Answers

Alternatively, you can disable the rule for one line:

// Based on your Typescript example

export type Hey = { // eslint-disable-line no-unused-vars
  a: string,
  b: object
}

One more option...

function doStuff({
  // eslint-disable-next-line no-unused-vars
  unused,
  usedA,
  usedB
}) {

For typescript eslint users just add this at the end of line you wish to ignore:

// eslint-disable-line @typescript-eslint/no-unused-vars

If you've got multiple overlapping rules that you want to ignore (e.g. typescript and standard js), you can specify more than one rule to ignore by separating by a comma:

// eslint-disable-next-line no-unused-vars, @typescript-eslint/no-unused-vars

For anyone wondering why it doesnt work with

// eslint-disable some-rule/specific-rule

just enclose the same disable statement in multiline comment and it will work.

/* eslint-disable some-rule/specific-rule  */

encapsulating eslint rules in multiline comment work for the whole block. So if you put multiline comment at the start of a function, it will disable that rule for the whole function block. If you put it at the start of a file, it will disable that rule for the whole file.

Define ESLint configuration in package.json like this

{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "no-unused-vars": "off"
  }
}
Related