plus/curly-newline
A number of style guides require or disallow line breaks inside of block statements and block-like code.
Rule Details
This rule requires or disallows a line break between {
and its following token, and between }
and its preceding token of block-like structures.
Options
This rule has either a string option:
"always"
requires line breaks after opening and before closing braces"never"
disallows line breaks after opening and before closing braces
Or an object option:
"multiline": true
requires line breaks if there are line breaks inside properties or between properties. Otherwise, it disallows line breaks."minElements"
requires line breaks if the number of elements in the block (usually statements) is at least the given integer. Ifconsistent
is set totrue
, an error will also be reported if a block contains linebreaks and has fewer elements than the given integer."consistent": true
(default) requires that either both curly braces, or neither, directly enclose newlines. Note that enabling this option will also change the behavior of theminElements
option. (SeeminElements
above for more information)
You can specify different options for different kinds of blocks:
{
"curly-newline": ["error", {
"ForInStatement": "always",
"ForOfStatement": { "multiline": true },
"ForStatement": "never",
"WhileStatement": { "multiline": true, "minElements": 3, "consistent": true }
}]
}
"IfStatement"
- Anif
statement body"ForStatement"
- Afor
statement body"ForInStatement"
- Afor..in
statement body"ForOfStatement"
- Afor..of
statement body"WhileStatement"
- Awhile
statement body"DoWhileStatement"
- Ado..while
statement body"SwitchStatement"
- Aswitch
statement body"SwitchCase"
- Aswitch
case
body"TryStatementBlock"
- Atry..catch..finally
statement main body"TryStatementHandler"
- Atry..catch..finally
statement handler body"TryStatementFinalizer"
- Atry..catch..finally
statement finalizer body"BlockStatement"
- A lone block"FunctionDeclaration"
- A function declaration body"FunctionExpression"
- A function expression body"Property"
- An object method shorthand body"ClassBody"
- A class body"StaticBlock"
- A static declaration block"WithStatement"
- A with statement body"TSEnumBody"
- A TypeScriptenum
declaration body"TSInterfaceBody"
- A TypeScriptinterface
declaration body"TSModuleBlock"
- A TypeScript module block
always
Examples of incorrect code for this rule with the "always"
option:
/*eslint curly-newline: ["error", "always"]*/
if (true) {}
if (true) { foo(); }
if (true) { foo(); bar(); }
if (true) { foo();
bar(); };
if (true) { let a = {
}; };
Examples of correct code for this rule with the "always"
option:
/*eslint curly-newline: ["error", "always"]*/
if (true) {
}
if (true) {
foo();
}
if (true) {
foo(); bar();
}
if (true) {
foo();
bar();
}
if (true) {
let a = function() {
foo();
}
}
never
Examples of incorrect code for this rule with the "never"
option:
/*eslint curly-newline: ["error", "never"]*/
if (true) {
}
if (true) {
foo();
}
if (true) {
foo(); bar();
}
if (true) {
foo();
bar();
}
if (true) {
let a = function() {
foo();
}
}
Examples of correct code for this rule with the "never"
option:
/*eslint curly-newline: ["error", "never"]*/
if (true) {}
if (true) { foo(); }
if (true) { foo(); bar(); }
if (true) { foo();
bar(); };
if (true) { let a = {
}; };
multiline
Examples of incorrect code for this rule with the { "multiline": true }
option:
/*eslint curly-newline: ["error", { "multiline": true }]*/
if (true) {
}
if (true) {
foo();
}
if (true) {
foo(); bar();
}
if (true) { foo();
bar(); };
if (true) { let a = {
}; };
Examples of correct code for this rule with the { "multiline": true }
option:
/*eslint curly-newline: ["error", { "multiline": true }]*/
/*eslint-env es6*/
if (true) {}
if (true) { foo(); }
if (true) { foo(); bar(); }
if (true) {
foo();
bar();
}
if (true) {
let a = {
};
}
minElements
Examples of incorrect code for this rule with the { "minElements": 2 }
option:
/*eslint curly-newline: ["error", { "minElements": 2 }]*/
if (true) {
}
if (true) {
foo();
}
if (true) { foo(); bar(); }
if (true) { foo();
bar(); }
if (true) {
let a = {
}
}
Examples of correct code for this rule with the { "minElements": 2 }
option:
/*eslint curly-newline: ["error", { "minElements": 2 }]*/
if (true) {}
if (true) { foo(); }
if (true) {
foo(); bar();
}
if (true) {
foo();
bar();
}
if (true) { var a = {
} }
consistent
Examples of incorrect code for this rule with the default { "consistent": true }
option:
/*eslint curly-newline: ["error", { "consistent": true }]*/
if (true) { foo();
}
if (true) {
foo(); }
if (true) { foo(); bar();
}
if (true) {
foo(); bar(); }
if (true) { var a = {
}
}
if (true) {
var a = {
}}
Examples of correct code for this rule with the default { "consistent": true }
option:
/*eslint curly-newline: ["error", { "consistent": true }]*/
if (true) {}
if (true) {
}
if (true) { foo(); }
if (true) {
foo();
}
if (true) {
foo(); bar();
}
if (true) {
foo();
bar();
}
When Not To Use It
If you don't want to enforce consistent line breaks after opening and before closing braces, then it's safe to disable this rule.