New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Option to have no-empty rule flag empty 'catch' blocks #1841
Comments
Sounds helpful to me |
The one question I have is if this should be a separate rule or not. My concern is that you might want to allow empty catch block in one spot but not in another within the same for. Since you can only disable rules inline, not reconfigure them, having this as an option in |
Could requiring a comment like |
Ooh, that's a good idea. Maybe nice and short |
Will submit a PR on this by today |
I created a separate rule (no-empty-catch) that allows to flag empty catch blocks with an option if a block only containing a comment is regarded as empty that you rejected because it is not what was agreed upon. |
The issue is really about opting-in to While I originally thought a separate rule might be in order, @btmills's suggestion made me realize that didn't make sense because we would have two rules doing essentially the same thing. So it makes the most sense to introduce a breaking change to |
Breaking: rule no-empty also checking for empty catch blocks. (fixes #1841)
The documentation for this still claims it allows empty catch blocks. I want empty catch blocks in a number of places, and this change appears to prevent me from keeping this rule turned on, unless I change the contents to just "// empty" - when in fact I want a different comment, or none at all. Can there please be an option instead of just a breaking change?? |
@ljharb - I had originally thought the same thing, as per my comment when I opened the issue. On further reflection, though, not doing error handling is a pretty bad code smell. For the few places where I specifically don't want to handle an error, putting in the |
As a followup, it looks to me like the documentation has been updated for this newer behavior. Where are you seeing the claim that it still allows empty catch blocks? (NB: I'm looking at the online docs for 0.18) |
@ljharb the docs look correct to me, but if you find a problem, please open another issue. Also, we made this change because we always favor explicit over implicit. We really shouldn't have allowed empty blocks anywhere by default, and using a comment is an established pattern we have in other rules. If you find the comment check is too specific, please open an issue and describe how you'd like to enhance it. |
Re the docs, I think I just didn't understand that "// empty" was required, but I realized that when reading this thread. They do say "A block will not be considered a warning if it contains a comment line with only the word empty." which does suggest that any other comments wouldn't work - but in fact as long as try { … }
catch (e) {
// try the shim if the real one doesn't work
// empty
} Having my style linter force code changes on me is very I do find a proscribed comment an onerous burden in even one approach - in my opinion any comments in a codebase describe "what" are something to avoid and a code smell. Also, while others may have run into this problem, I've never run into a problem from having empty catch blocks - in shim code especially, it's very common to use them. |
Due to eslint/eslint#1841 this can only be a warning.
Just to address our concerns: I'm sorry you feel like the linter is forcing you to make changes you don't want. We do our best to assure that our rules are rational and not onerous to work with. However, we do mistakes, and we reserve the right to fix those mistakes when they are made. In this case, it was a mistake to implicitly ignore empty blocks in certain positions but not others. For instance, there was no way to flag empty While ESLint has no agenda as it relates to code style, we do believe in having consistent rules that minimize false negatives. For this rule, it was necessary to make a breaking change to do that, and while that may cause you to make changes to your code, it makes the rule completely consistent across the board. In the long term, that has a lot more value than keeping it inconsistent for the sake of backwards compatibility. Once we reach 1.0.0, we'll be a lot stricter about making such changes, but up until then, we want to keep getting things into a better and more consistent shape. |
@nzakas - Excellent explanation. The reason that eslint is, IMHO, representative of the best stewardship of an open source JavaScript project is the time and consideration you continue to put into the entire development model and cycle. Thank you. |
@nzakas I totally agree with all of your policies here, and your position. I think that absolutely the default behavior the rule now evinces is the sanest. However, I think that in every case, regardless of pre-1.0 or post-1.0 (since 1.0 is not a magic milestone, it's just a number), whenever it makes sense, a breaking change should be done such that the behavior can be re-opted-into via a configuration flag. |
@ljharb - I would have to disagree with your statement that 1.0 is not a 'magic number'. To this project (and @nzakas has been very clear on this point) it is: it defines when a breaking change will have to be opted in to rather than opted out of. |
@bedney Some versions of semver say that a minor version bump pre-1.0 is a breaking change, and a major bump post-1.0 is a breaking change - which means that That said, of course the owner of a project is always entitled to do whatever they like with it. My opinion remains, however, that when the breaking change is a change of defaults, it keeps the upgrade path clear to allow old behavior to remain simply by changing configuration options, rather than forcing me to change code or abandon a rule in order to continue upgrading. |
Okay guys, good discussion. We have a different of opinion here so it's time to close the discussion. |
Many times when just prototyping for the first time, folks will use this construct because they're not interested in the error:
But later, for production-ready code, you want to make sure that there is indeed error handling code in those 'catch' blocks.
The current rule doesn't flag 'catch' blocks. I'd like to propose a config option for the 'no-empty' rule that would flag them.
Maybe something like:
"no-empty": [2, {"includeCatchBlocks": true}]
or some such.Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: