question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option Proposal: for `no-unused-vars`, add `exceptions` and `exceptionsIgnorePattern` options

See original GitHub issue

Following 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 variables
  • all - reports all unused exception variables, aside from those matching exceptionsIgnorePattern
/*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:closed
  • Created 8 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pvamshicommented, Mar 18, 2016

I am working on this , will create a pull request for review shortly

0reactions
ryanbriscallcommented, Jul 11, 2016

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-unused-vars - ESLint - Pluggable JavaScript Linter
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern. For example, variables whose names ...
Read more >
The 3 Best ESLint No-Unused-Vars Option Settings (Make it ...
However, there are a few cases where we want exceptions to the rule. ... 1 No-Unused-Vars ignoreRestSiblings: Allow Unused Values When Using ...
Read more >
eslint no-unused-vars varIgnorePattern not working
The varsIgnorePattern option specifies exceptions not to check for usage: variables whose names match a regexp pattern.
Read more >
no-unused-vars - TypeScript ESLint
This rule extends the base eslint/no-unused-vars rule. It adds support for TypeScript features, such as types. How to Use​ .eslintrc.cjs. module ...
Read more >
Rule no-unused-vars - ESLint中文文档
/*eslint no-unused-vars: 2*/ /*global some_unsed_var */ /*error ... Options. By default this rule is enabled with all option for variables and after-used ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found