ni / javascript-styleguide

JavaScript and TypeScript Style Guide
MIT License
9 stars 9 forks source link

Javascript blocks should always require new lines and curly braces #86

Closed mollykreis closed 1 year ago

mollykreis commented 2 years ago

For consistency, the body of all blocks in javascript should require new lines and curly braces.

eg:

if (true) {
    callSomeFunction();
}

not

if (true) callSomeFunction();

Note: This discussion originally started on this PR.

jattasNI commented 2 years ago

We discussed in person and agreed that the curly rule set to all is our best option for now. We would be ok with allowing just the single line if syntax but there's no way to allow that without allowing undesirable syntaxes (the multi config comes closest).

jattasNI commented 2 years ago

I realized while testing the fix for this in #87 that just using the curly rule will still allow this syntax: if (foo) { doSomething; }

Our options are:

  1. Allow that syntax by only enabling the curly rule
  2. Set 'brace-style': ['error', '1tbs', { allowSingleLine: false }] in addition to curly which forces the body and closing brace to be on subsequent lines.
  3. Only set 'brace-style': ['error', '1tbs', { allowSingleLine: false }] but NOT curly, which allows single-line statements on the same line if they don't have braces.
// Example A. Allowed by 3
if (foo) doSomething();

// Example B. Allowed by 3
if (foo) doSomethingConditionally(); doSomethingUnconditionallyButItLooksConditional();

// Example C. Allowed by 1
if (foo) { doSomething(); }

// Example D. Allowed by 1
if (foo) { doSomething(); doSomethingElse(); }

// Example E. Always disallowed by Airbnb config for non-block-statement-body-position
if (foo)
   doSomething(); 

// Example F. Always allowed
if (foo) {
   doSomething();
} 

The original bug report requested 2. That's my vote too because I don't like permitting example B above and find the example C syntax ugly. But if anyone wants to argue for 1 or 3, we can discuss it again. @rajsite @mure @TrevorKarjanis

jattasNI commented 2 years ago

We agreed in person to require the body to be on a new line (option 2). @jattasNI to revive the PR to make this happen.