unicorn/prefer-optional-catch-binding
Require catch {} without a parameter when the error is not used
fixable
catch doesn't need a parameter. A name that's never read sends the reader looking for where the error is handled.
--fix removes the parameter and its parentheses.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"unicorn/prefer-optional-catch-binding" = "error" # or "warn"Examples#
example.js2 problems
function parse(text) {
try {
return JSON.parse(text);
} catch (errorPrefer omitting the catch binding parameter if it is unused) {
return undefined;
}
}
try {
await upload(file);
} catch ({ message }Prefer omitting the catch binding parameter if it is unused) {
retry(file);
}example.jsNo problems
function parse(text) {
try {
return JSON.parse(text);
} catch {
return undefined;
}
}
try {
await upload(file);
} catch ({ message }) {
console.error(message);
}