eslint: disable warning - `defined but never used` for specific function?

Viewed 48254

So I have this function:

function render(){
    // do stuff
}

I do not call that function, because it is called from html as an event function, like:

<textarea id="input" class="input-box" onkeyup="render()"></textarea>

Well eslint does not see that, so it gives that warning (render is defined, but never used). Is there a way to specify that function is called elsewhere? Or just mute the warning?

For example if global variable is used, I can do /* global SomeVar*/ and it will mute warning of not defined variable. Maybe something similar could be done on functions like in example?

7 Answers

If you dont want to change the code.

ESLint provides both a way to disable, both to enable the linting via comments. You only added before functions /* eslint-disable */ and after functions /* eslint-enable */

Example

/* eslint-disable */ <-- Before function

function render(){
   // do stuff
}

/* eslint-enable */  <-- After function

More info

You can use an exported comment block for this, such as:

/* exported render */

This tells eslint that it is okay for the declaration be unused, which is more semantically correct than just silencing the warning. It might also work with certain other tools such as minifiers, but that is purely conjecture on my part.

Just put this rule in .eslintrc.js file , please don't forget to restart serve..

module.exports = {
      rules: { 
        "no-unused-vars": "off",
      },
    }

eslint has a caughtErrors option which is used to catch block arguments validation and can have each of below values:

  1. none (default) in order to avoid checking these errors.
  2. all

So you can simply ignore not-used errors by changing this option. Here's a general example:

eslint no-unused-vars: ["error", { "caughtErrors": "none" }]

Pretty similar to what Alex K. is proposing, there is also eslint-disable-next-line. I generally prefer that to not make the method definition line of code too long. You can use it like this:

// eslint-disable-next-line no-unused-vars
function render(){
    // do stuff
}

You can add the argsIgnorePattern option specifies exceptions not to check for usage: arguments whose names match a regexp pattern. For example, variables whose names begin with an underscore.

/* eslint no-unused-vars: ["error", { "argsIgnorePattern": "^_" }]
   @typescript-eslint/no-unused-vars: [1, { vars: 'all', 'argsIgnorePattern': '^_' }] */


function foo(x, _y) {
    return x + 1;
}
foo();

Please check on https://eslint.org/docs/latest/rules/no-unused-vars#options

Related