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": truerequires 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. Ifconsistentis 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 theminElementsoption. (SeeminElementsabove 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 }
}]
}"IfStatementConsequent"- Anifstatement body"IfStatementAlternative"- Anelsestatement body"ForStatement"- Aforstatement body"ForInStatement"- Afor..instatement body"ForOfStatement"- Afor..ofstatement body"WhileStatement"- Awhilestatement body"DoWhileStatement"- Ado..whilestatement body"SwitchStatement"- Aswitchstatement body"SwitchCase"- Aswitchcasebody"TryStatementBlock"- Atry..catch..finallystatement main body"TryStatementHandler"- Atry..catch..finallystatement handler body"TryStatementFinalizer"- Atry..catch..finallystatement finalizer body"BlockStatement"- A lone block"ArrowFunctionExpression"- An arrow function expression body"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"TSModuleBlock"- A TypeScript module block
always
Examples of incorrect code for this rule with the "always" option:
/* eslint @stylistic/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 @stylistic/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 @stylistic/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 @stylistic/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 @stylistic/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 @stylistic/curly-newline: ["error", { "multiline": true }] */
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 @stylistic/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 @stylistic/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 @stylistic/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 @stylistic/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.