requireCurlyBraces one-statement / one-expression exception #244
Comments
+1 |
I would want to use This exception should not be hardcoded but should be togglable |
@Raynos +1 |
+1 on both settings. I never want to see blocks without curly braces, but I heartily support the both mine and the OP's right to configure it. |
Help me understand this, if don't use braces, you only allowed to use one statement, so if
or this if (key.charAt(0) === '$' || isFunction(o1[key])) continue; should be valid, but this isn't: if (key.charAt(0) === '$' || isFunction(o1[key]))
continue; ? |
Depends on what this additional option will allow for. As far as newlines — I don't have a preference. None of the bracketless statements have newlines in my code. Just checked Fabric. Out of ~11,6K LOC, we have:
I'm ok with only allowing plain |
JS as a language only allows single statements to not have curly braces. Asking for the option to allow single statements to not have curlies is the same as not using this rule at all. Now, if the conversation is to only allow certain statements to not have curlies, I can support that. I'd prefer to let the user specify exact statements to allow, so that we're not back here in a month asking for more exceptions. Sound fair? |
Yes, having some kind of whitelist of statements/expressions would work for me. Not sure how exactly you'd want to do it but, for example, I could use something like this in Fabric (see previous comment): allowStatementsWithoutCurly: [ 'ReturnStatement', 'BreakStatement', 'ContinueStatement' ] but then... what about some more granularity, allowing only specific expressions as part of return statement? allowExpressionsWithoutCurly: [ 'BooleanLiteral', 'StringLiteral', 'ReservedWord' ] I'm not sure. This could quickly become hairy. Any ideas? |
Maybe something like |
I would really prefer blacklist rather than whitelist. It would be tedious to list all statements except 1 or 2. Sent from my iPhone
|
+1 |
Is there any solution yet? I'd like to require curly braces in general, but allow if (...) {
} else if (...) {
} else {
} |
@bajtos that sounds like a bug. Can you please file a new issue for that? Thanks for contributing! |
@mikesherov IMO that is not a bug, as it was printed when my config included I find the behaviour perfectly correct in the context of the current implementation: if (...) {
} else {
if (...) {
} else {
}
} The problem will go away once we allow keywords to bypass the CurlyBraces and include "if" in the list of white-listed keywords. That is exactly the point of this GH issue if I understand the discussion here correctly. However, if you think that |
On Thursday, November 27, 2014, Miroslav Bajtoš notifications@github.com
Yes, I do think it should be treated differently. A pull request would be
Mike Sherov |
@bajtos please file and new bug for that issue you reported. |
@mikesherov done: #799 As I was trying to reproduce the problem, I have found that |
@kangax just pinging you here as I know this is an old issue at this point but I really can't figure out a good way to specify exceptions here. Have you any thoughts about it since the last time this topic was discussed? |
I guess whatever I brought up in #244 (comment) would be good for me. Although I ended up just adding |
I would like to see the "require curly braces only if the statement spans multiple lines" approach taken on board. The In combination with a horizontal character limit, I feel like this is good enough to stop the absence of curly braces being abused while still allowing for it to make simple statements clearer and more concise. |
|
So, is there any takers for this one? |
@markelog That's not so big issue but better to clarify what we really need and how to configure it. Options, as I see:
Not sure ;-( Can we discuss this? |
This sounds good to me |
Anyone want to summarize what is wanted here? Sounds like we can make an option |
That would work for me. Sent from my iPhone
|
Cool! |
Ok need to change it a bit more since the original option was an array so something like "requireCurlyBraces": {
"keywords": ["if", "else"],
"allExcept" : ['ReturnStatement', 'BreakStatement', 'ContinueStatement']`
} |
"keywords": ["if", "else"],
"allExcept" : ['ReturnStatement', 'BreakStatement', 'ContinueStatement']` Seems inconsistent, this rule already maps those entities together, so it should be pretty easy to use same type of lists. Generally it is good idea to distance from parser json |
What do you mean by this? combine them into a single list or either |
I meant if |
Oh ok you mean like |
yeah |
…ments Make an exception for return, break, continue statements Fixes jscs-dev#244 Closes jscs-devgh-1938
…ments Make an exception for return, break, continue statements Fixes jscs-dev#244 Closes jscs-devgh-1938
Fantastic, thank you! |
"requireCurlyBraces" is a great option but needs an exception. The only time it's acceptable to ignore it is for early returns or continuations.
Example from our Fabric.js:
Or with
continue
:Example from Underscore:
Example from Angular:
etc.
Most of the time, I want
if
statements to have curly braces, except when there's 1 statement in its body, such asreturn
orcontinue
.I couldn't find a way for JSCS to ignore these, hence the request.
I could think of 2 levels of allowance:
return;
,continue;
,break;
return false;
,return foo();
, etc.I'm leaning towards 2nd. I noticed that both Underscore and Angular use the latter as well.
The text was updated successfully, but these errors were encountered: