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.

Make no-useless-catch a core rule

See original GitHub issue

Please describe what the rule should do:

Prevents useless catch clauses that do nothing but rethrow the caught error. This rule already exists as an ESLint internal rule. Instead of just an internal rule, this should be a core rule.

What category of rule is this? (place an “X” next to just one item)

[ ] Warns about a potential error (problem) [X] Suggests an alternate way of doing something (suggestion) [ ] Enforces code style (layout) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

/* warn about useless try/catch statement */
try {
  throw new Error('whoops');
} catch (e) {
  throw e;
}

/* only warn about catch clause */
try {
  throw new Error('whoops');
} catch (e) {
  throw e;
} finally {
  console.log('done');
}

Why should this rule be included in ESLint (instead of a plugin)?

As far as I know, there is no semantic difference between

try {
  throw new Error('whoops');
} catch (e) {
  throw e;
}

and

throw new Error('whoops');

Because of that, I can’t think of a good reason why you would ever want a useless catch clause in any javascript codebase, seeing as how it just adds unnecessary clutter and complexity to your source code. It seems ESLint devs themselves agree since it exists as an internal rule.

Are you willing to submit a pull request to implement this rule?

If it is something that people want as a core rule, then sure.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
agrasleycommented, Dec 10, 2018

Cool. I’ll look at existing rules and other PRs and try to come up with something.

1reaction
platinumazurecommented, Dec 9, 2018

I’ll support this!

Although the rule definition exists, we would definitely need documentation and tests if we want to expose the rule as part of ESLint core.

Read more comments on GitHub >

github_iconTop Results From Across the Web

no-useless-catch - Pluggable JavaScript Linter - ESLint
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
No-useless-catch - ESLint - W3cubDocs
A catch clause that only rethrows the original error is redundant, and has no effect on the runtime behavior of the program. These...
Read more >
JavaScript Best Practices — Useless Catch, Concatenation ...
In this article, we'll look at more useless code that we should remove, including useless catch clauses, concatenation, escape characters, ...
Read more >
TSLint core rules - Palantir Open Source
no -unnecessary-type-assertion - Warns if a type assertion does not change the ... These rules catch common errors in JS programming or otherwise...
Read more >
Linter rules - Dart
We recommend using at least the core rule set, which is used when scoring packages ... Using catch clauses without on clauses make...
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