Option Proposal: for `no-unused-vars`, add `exceptions` and `exceptionsIgnorePattern` options
See original GitHub issueFollowing from the discussion in #3543, I propose the addition of options to the no-unused-vars
rule, allowing us to specify how we want to handle exception variables defined in the catch()
clause of a try/catch statement.
Currently, the documentation doesn’t address this, and the rule allows caught exception variables to remain unused, even when the "all"
settings are used for the current options (tested via ESLint 1.4.3, though this behavior was also present in previous versions):
/*eslint no-unused-vars: [2, {"vars": "all", "args": "all"}]*/
function example(){
try {
doSomething();
} catch (err) { // unused exception variable isn't flagged as an error
return false;
}
return true;
}
Therefore, I propose the addition of exceptions
and exceptionsIgnorePattern
options, mirroring the functionality of the vars
/varsIgnorePattern
and args
/argsIgnorePattern
options. Valid settings for exceptions
would be:
none
- the default, matches the current behavior of not reporting any unused exception variablesall
- reports all unused exception variables, aside from those matchingexceptionsIgnorePattern
/*eslint no-unused-vars: [2, {"exceptions": "all", "exceptionsIgnorePattern": "^ignore"}]*/
function hypotheticalExample(){
try {
doSomething();
} catch (err) { // unused exception variable IS flagged as an error
return false;
}
try {
doSomethingElse();
} catch (ignoreErr) { // unused exception variable matches ignore pattern, is NOT flagged as an error
return false;
}
return true;
}
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
I am working on this , will create a pull request for review shortly
Thanks for this feature, but I needed this for function names. It works for variables, given the provided “catch (ignoreErr)” example. But doesn’t work for functions, say for example: function ignoreTest() { }. I was expecting this “no-unused-vars” exceptions solution to work for functions too, since eslint is showing my unused function error as a “no-unused-vars” error.