requireNewlineBeforeBlockStatements

Requires newline before opening curly brace of all block statements.

Type: Boolean or Array

Values:

  • true always requires newline before curly brace of block statements
  • Array specifies block-type keywords after which newlines are required before curly brace
    • Valid types include: ['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'switch']

Example

"requireNewlineBeforeBlockStatements": true
Valid
function good()
{
    var obj =
    {
        val: true
    };

    return {
        data: obj
    };
}

if (cond)
{
    foo();
}

for (var e in elements)
{
    bar(e);
}

while (cond)
{
    foo();
}
Invalid
function bad(){
    var obj = {
        val: true
    };

    return {
        data: obj
    };
}

if (cond){
    foo();
}

for (var e in elements){
    bar(e);
}

while (cond){
    foo();
}

Example

"requireNewlineBeforeBlockStatements": ["if", "else", "for"]
Valid
if (i > 0)
{
    positive = true;
}

if (i < 0)
{
    negative = true;
}
else
{
    negative = false;
}

for (var i = 0, len = myList.length; i < len; ++i)
{
    newList.push(myList[i]);
}

// this is fine, since "function" wasn't configured
function myFunc(x) {
    return x + 1;
}
Invalid
if (i < 0) {
    negative = true;
}

if (i < 0) {
    negative = true;
} else {
    negative = false;
}

for (var i = 0, len = myList.length; i < len; ++i) {
    newList.push(myList[i]);
}

Example

"requireNewlineBeforeBlockStatements": ["function", "while"]
Valid
function myFunc(x)
{
    return x + 1;
}

var z = function(x)
{
    return x - 1;
}

// this is fine, since "for" wasn't configured
for (var i = 0, len = myList.length; i < len; ++i) {
    newList.push(myList[i]);
}
Invalid
function myFunc(x) {
    return x + 1;
}

var z = function(x) {
    return x - 1;
}
Rule source
Test source