disallowNewlineBeforeBlockStatements

Disallows newline before opening curly brace of all block statements.

Type: Boolean or Array or Object

Values:

  • true always disallows newline before curly brace of block statements
  • Array specifies block-type keywords after which newlines are disallowed before curly brace
    • Valid types include: ['if', 'else', 'try', 'catch', 'finally', 'do', 'while', 'for', 'function', 'class', 'switch']
  • Object:
    • value: true or an Array
    • allExcept: Array of exceptions
      • "multiLine": if the conditions span on multiple lines, require a new line before the curly brace

Example

"disallowNewlineBeforeBlockStatements": 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

"disallowNewlineBeforeBlockStatements": ["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

"disallowNewlineBeforeBlockStatements": ["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;
}

Example

"disallowNewlineBeforeBlockStatements": {
    "value": true,
    "allExcept": ["multiLine"]
}
Valid
function myFunc(x,
                y)
{
    return x + y;
}

function foo() {
    if (bar && baz &&
        bat)
    {
        return true;
    }
}
Invalid
function myFunc(x,
                y) {
    return x + y;
}

function foo() {
    if (bar && baz &&
        bat) {
        return true;
    }
}
Rule source
Test source