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.

Proposal: no-optional-catch-binding rule

See original GitHub issue

Please describe what the rule should do:

Prevent use of the new optional catch binding syntax.

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

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

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

try {
  throw new Error('error that should not be ignored');
} catch {
  console.log('something happened');
}

Why should this rule be included in ESLint (instead of a plugin)? It seems like it would be applicable across all environments that support the new syntax. Generally, errors should not be ignored. If an error is ignored, I’d prefer an explicit opt eslint-disable opt out, rather than not knowing if it was ignored accidentally or lazily.

I’m volunteering to do the work. I have a proof of concept already.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:13
  • Comments:53 (51 by maintainers)

github_iconTop GitHub Comments

8reactions
mysticateacommented, Jul 14, 2018

@ljharb Looks reasonable proposition. Yes, “disallow error disposal” rule is nice. However, currently, I think that each core rule is smaller than it. For example, if I want semicolon-less style, I need the following rules:

{
    "semi": ["error", "never", {"beforeStatementContinuationChars": "never"}],
    "semi-spacing": ["error", {"after": true, "before": false}],
    "semi-style": ["error", "first"],
    "no-extra-semi": "error",
    "no-unexpected-multiline": "error"
}

In the try-catch syntax case, I have used the combination of no-unused-vars, no-unused-expression, and some rules in order to disallow error disposal. Then, I want to add the require-catch-binding rule to the rule set.

The combination of small rules lets me get the goal. Each rule might be too small, but I think it’s another issue that consideration of the proper rule size.


Currently, I’m considering the following rule:

{
    "require-catch-binding": ["error", "always" | "never"]
}

Options:

  • "always" … enforce writing catch binding.
  • "never" … enforce omitting catch binding if it’s unused. This supports autofix.
4reactions
mysticateacommented, Jun 1, 2018

We can develop this after #10392 which adds the support of optional catch binding is merged.

I will champion this.

  • Generally, errors should not be ignored in order to investigate bugs fastly. Ignoring errors would help to hide the cause of bugs.
  • The no-restricted-syntax rule with CatchClause[param=null] can catch this. However, it’s inconvenient if sharable config is using the no-restricted-syntax rule. It overrides and mess the configuration of shareble configs. Independent rule is useful.
Read more comments on GitHub >

github_iconTop Results From Across the Web

ES2019: optional catch binding - 2ality
The proposal “Optional catch binding” by Michael Ficarra is at stage 4 and therefore part of ECMAScript 2019. This blog post explains how...
Read more >
babel/plugin-proposal-optional-catch-binding
NOTE**: This plugin is included in `@babel/preset-env`, in [ES2019](https://github.com/tc39/proposals/blob/master/finished-proposals.md)
Read more >
no-optional-catch-binding - npm
ESLint rule to flag catch clauses with no error variable binding. Latest version: 1.0.0, last published: 5 years ago.
Read more >
@babel/plugin-proposal-optional-catch-binding | Yarn - Yarn
Intro. Babel is a tool that helps you write code in the latest version of JavaScript. When your supported environments don't support certain...
Read more >
ES10 - Optional Catch binding javascript - W3schools.io
As you can see above example, the catch has an error variable Sometimes, we need to have a catch block with unused variables...
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