brace-style
Brace style determines where opening and closing braces are placed relative to control statements and code blocks.
Rule Type: Layout
Fixable: Yes (automatically fixable)
Deprecated: Consider using Prettier
Rule Details
This rule enforces consistent brace style for blocks.
Brace Styles
One True Brace Style (1tbs)
Opening brace on same line as statement:
if (foo) {
bar();
} else {
baz();
}
Stroustrup
else, catch, finally on own line:
if (foo) {
bar();
}
else {
baz();
}
Allman
All braces on their own lines:
if (foo)
{
bar();
}
else
{
baz();
}
Options
"1tbs" (default)
Enforce one true brace style.
Incorrect:
function foo()
{
return true;
}
if (foo)
{
bar();
}
if (foo) {
bar();
}
else {
baz();
}
Correct:
function foo() {
return true;
}
if (foo) {
bar();
}
if (foo) {
bar();
} else {
baz();
}
try {
somethingRisky();
} catch(e) {
handleError();
}
"stroustrup"
Incorrect:
function foo()
{
return true;
}
if (foo) {
bar();
} else {
baz();
}
Correct:
function foo() {
return true;
}
if (foo) {
bar();
}
else {
baz();
}
try {
somethingRisky();
}
catch(e) {
handleError();
}
"allman"
Incorrect:
function foo() {
return true;
}
if (foo) {
bar();
} else {
baz();
}
Correct:
function foo()
{
return true;
}
if (foo)
{
bar();
}
else
{
baz();
}
try
{
somethingRisky();
}
catch(e)
{
handleError();
}
allowSingleLine
Allow opening and closing braces on same line.
// With { "allowSingleLine": true }
if (foo) { bar(); } // OK
if (foo) { bar(); } else { baz(); } // OK
function nop() { return; } // OK
Configuration Examples
// Default: 1tbs
{
"rules": {
"brace-style": "error"
}
}
// Stroustrup
{
"rules": {
"brace-style": ["error", "stroustrup"]
}
}
// Allman
{
"rules": {
"brace-style": ["error", "allman"]
}
}
// With single-line exception
{
"rules": {
"brace-style": ["error", "1tbs", {
"allowSingleLine": true
}]
}
}
When Not to Use It
Most modern projects use Prettier for formatting. If you use Prettier, disable this rule.
Disable if:
- You use Prettier or another formatter
- Your team hasn’t agreed on a brace style
- You don’t want to enforce consistency
Further Reading