Make no-useless-catch a core rule
See original GitHub issuePlease 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:
- Created 5 years ago
- Reactions:4
- Comments:6 (6 by maintainers)
Cool. I’ll look at existing rules and other PRs and try to come up with something.
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.