How can I use ESLint no-unused-vars for a block of code?
JavascriptEslintJavascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
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 */
Solution 2 - Javascript
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
}
Solution 3 - Javascript
One more option...
function doStuff({
// eslint-disable-next-line no-unused-vars
unused,
usedA,
usedB
}) {
Solution 4 - Javascript
For typescript eslint users just add this at the end of line you wish to ignore:
// eslint-disable-line @typescript-eslint/no-unused-vars
Solution 5 - Javascript
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
Solution 6 - Javascript
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.