79

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?
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Radex
  • 7,815
  • 23
  • 54
  • 86
  • See [adding global variable in ESLint](https://stackoverflow.com/questions/41552041/global-variables-in-javascript-and-eslint) – mhodges Sep 18 '17 at 16:49
  • Possible duplicate of [Global variables in Javascript and ESLint](https://stackoverflow.com/questions/41552041/global-variables-in-javascript-and-eslint) – mhodges Sep 18 '17 at 16:52

7 Answers7

128

To disable the @typescript-eslint/no-unused-vars warning:

  • For the current line:
const unusedVar = 1; // eslint-disable-line @typescript-eslint/no-unused-vars
  • For the next line:
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const unusedVar = 1;
  • For a block:
/* eslint-disable @typescript-eslint/no-unused-vars */
const unusedVar1 = 1;
const unusedVar2 = 2;
/* eslint-enable @typescript-eslint/no-unused-vars */

Original answer

Just use pair of lines:

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

// ... your code here with unused vars...

/* eslint-enable no-unused-vars */
Onkeltem
  • 1,889
  • 1
  • 18
  • 27
Ville Venäläinen
  • 2,444
  • 1
  • 15
  • 11
  • I have tried with no success, the error is thrown by standardjs – Radex Sep 18 '17 at 17:13
  • Hacking linting rules for something that is built in to ESLint (defining globals) is pretty frowned upon. – mhodges Sep 18 '17 at 17:15
  • Good habit is to set these on .eslintrc. This example might not be the wisest way normally, but I can imagine that there comes cases when you are forced to do this kind of things. – Ville Venäläinen Sep 18 '17 at 17:19
  • @Radex, have you checked your eslint version? Could it be so old that it does not support eslint-disable? I checked my example before posting it to here and it functioned, so I think your problem is now elsewhere in a setup. – Ville Venäläinen Sep 18 '17 at 17:22
  • @Ville Venäläinen, Your answer worked for me. I'm using eslint version 4.18.1 – PatS Jun 11 '18 at 20:53
  • Glad to help :) I guess the thing is up to eslint version. – Ville Venäläinen Jun 12 '18 at 19:41
  • 4
    A shame it does'nt work with `// eslint-disable no-unused-vars` – avalanche1 Jun 18 '19 at 12:52
  • 4
    If using `TypeScript`, do this `/* eslint-enable @typescript-eslint/no-unused-vars */`. @Tim J answer was [helpful in that cause](https://stackoverflow.com/a/57610342/10849438) – MwamiTovi Sep 14 '20 at 15:57
  • A handy quick-fix when your editor and a project don't agree on how a block should be indented (using the `indent` rule, of course) – AverageHelper Sep 29 '20 at 20:39
33

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
}
MwamiTovi
  • 2,425
  • 17
  • 25
zdolny
  • 999
  • 1
  • 11
  • 21
22

One more option...

function doStuff({
  // eslint-disable-next-line no-unused-vars
  unused,
  usedA,
  usedB
}) {
Martin
  • 1,289
  • 3
  • 13
  • 22
15

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

// eslint-disable-line @typescript-eslint/no-unused-vars
Tim J
  • 245
  • 2
  • 6
9

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
Sam J
  • 714
  • 1
  • 9
  • 15
4

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.

Mamoon Ahmed
  • 135
  • 1
  • 9
2

Define ESLint configuration in package.json like this

{
  "plugins": [
    // ...
    "react-hooks"
  ],
  "rules": {
    // ...
    "no-unused-vars": "off"
  }
}
GMKHussain
  • 3,342
  • 1
  • 21
  • 19