Request for New "curly" Option: "multi-or-nest-and-exit"
See original GitHub issueThe current options for the “curly” rule are great, but it would be really nice if it was possible to express an added constraint: in order for a one-line statement to be allowed, it has to exit somehow (ie. return
or break
or continue
). In other words, this proposed “multi-or-nest-and-exit” option would be identical to “multi-or-nest”, except that:
if (foo) bar += 1;
would be invalid. Only:
if (foo) return bar + 1;
or:
if (foo) {
bar += 1;
}
would be allowed. The reason for this distinction comes from the purpose of the rule itself. “curly” exists because it’s very easy to misunderstand code like:
if (foo) bar += 1;
baz += 1;
It’s easy to read that code and (especially if the indentation is wrong) think that baz += 1
will only trigger if (foo)
. However, by limiting such one-line conditionals to exit statements, you eliminate that confusion. It’s much harder for one to misunderstand:
if (foo) return bar + 1;
baz += 1;
because the reader knows that any code after a return
won’t be run.
While I’m not sure how popular this rule is, it’s used at my company and I’ve seen it used at at least one other company, so I suspect that others might appreciate this rule option also.
Thanks for your consideration.
P.S. While I don’t have a pull request ready or anything (TBH I’ve never even looked at the ESLint source), if the ESLint maintainers want to add this option I would be more than happy to either create the pull request myself or use a “bounty” to help get this implemented by someone who is more familiar with ESLint’s source code.
<bountysource-plugin>
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource. </bountysource-plugin>
Issue Analytics
- State:
- Created 8 years ago
- Comments:6 (3 by maintainers)
Top GitHub Comments
I find this a bit too specific to be part of the core.
I agree. This is more of a personal preference. It’s a good case for a plugin though. @machineghost if you run into problems creating this, stop by our gitter channel and we’ll try to help. Also, if you do end up creating this, please post a link here in case somebody wants to use the same style.