brace-style
为块强制执行一致的大括号样式
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
大括号风格与编程中的缩进风格密切相关,描述了大括号相对于其控制语句和主体的位置。世界上可能有十几种甚至更多的大括号风格。
🌐 Brace style is closely related to indent style in programming and describes the placement of braces relative to their control statement and body. There are probably a dozen, if not more, brace styles in the world.
唯一的真正大括号风格 是 JavaScript 中最常见的大括号风格之一,其中代码块的开括号与其对应的语句或声明位于同一行。例如:
🌐 The one true brace style is one of the most common brace styles in JavaScript, in which the opening brace of a block is placed on the same line as its corresponding statement or declaration. For example:
if (foo) {
bar();
} else {
baz();
}
一种常见的单一真实大括号风格的变体被称为 Stroustrup,其中 if-else 结构中的 else 语句,以及 catch 和 finally,必须在前一个闭合大括号之后单独占一行。例如:
🌐 One common variant of one true brace style is called Stroustrup, in which the else statements in an if-else construct, as well as catch and finally, must be on its own line after the preceding closing brace. For example:
if (foo) {
bar();
}
else {
baz();
}
另一种风格称为 Allman,其中所有大括号都应单独占一行,不需要额外缩进。例如:
🌐 Another style is called Allman, in which all the braces are expected to be on their own lines without any extra indentation. For example:
if (foo)
{
bar();
}
else
{
baz();
}
虽然没有一种风格被认为比另一种更好,但大多数开发者都同意在整个项目中保持一致的风格对于其长期可维护性很重要。
🌐 While no style is considered better than the other, most developers agree that having a consistent style throughout a project is important for its long-term maintainability.
规则详情
🌐 Rule Details
此规则强制块的大括号样式一致。
🌐 This rule enforces consistent brace style for blocks.
选项
🌐 Options
此规则有一个字符串选项:
🌐 This rule has a string option:
"1tbs"(默认)强制使用一种统一的大括号风格。"stroustrup"强制使用 Stroustrup 风格。"allman"强制使用 Allman 风格。
此规则有一个异常的对象选项:
🌐 This rule has an object option for an exception:
"allowSingleLine": true(默认值false)允许块的起始和结束大括号位于同一行。
1tbs
使用默认 "1tbs" 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
function foo()
{
return true;
}
if (foo)
{
bar();
}
try
{
somethingRisky();
} catch(e)
{
handleError();
}
if (foo) {
bar();
}
else {
baz();
}
class C
{
static
{
foo();
}
}
使用默认 "1tbs" 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default "1tbs" option:
/*eslint brace-style: "error"*/
function foo() {
return true;
}
if (foo) {
bar();
}
if (foo) {
bar();
} else {
baz();
}
try {
somethingRisky();
} catch(e) {
handleError();
}
class C {
static {
foo();
}
}
// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
使用 "1tbs", { "allowSingleLine": true } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "1tbs", { "allowSingleLine": true } options:
/*eslint brace-style: ["error", "1tbs", { "allowSingleLine": true }]*/
function nop() { return; }
if (foo) { bar(); }
if (foo) { bar(); } else { baz(); }
try { somethingRisky(); } catch(e) { handleError(); }
if (foo) { baz(); } else {
boom();
}
if (foo) { baz(); } else if (bar) {
boom();
}
if (foo) { baz(); } else
if (bar) {
boom();
}
if (foo) { baz(); } else if (bar) {
boom();
}
try { somethingRisky(); } catch(e) {
handleError();
}
class C {
static { foo(); }
}
class D { static { foo(); } }
stroustrup
使用 "stroustrup" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "stroustrup" option:
/*eslint brace-style: ["error", "stroustrup"]*/
function foo()
{
return true;
}
if (foo)
{
bar();
}
try
{
somethingRisky();
} catch(e)
{
handleError();
}
class C
{
static
{
foo();
}
}
if (foo) {
bar();
} else {
baz();
}
使用 "stroustrup" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "stroustrup" option:
/*eslint brace-style: ["error", "stroustrup"]*/
function foo() {
return true;
}
if (foo) {
bar();
}
if (foo) {
bar();
}
else {
baz();
}
try {
somethingRisky();
}
catch(e) {
handleError();
}
class C {
static {
foo();
}
}
// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
使用 "stroustrup", { "allowSingleLine": true } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "stroustrup", { "allowSingleLine": true } options:
/*eslint brace-style: ["error", "stroustrup", { "allowSingleLine": true }]*/
function nop() { return; }
if (foo) { bar(); }
if (foo) { bar(); }
else { baz(); }
try { somethingRisky(); }
catch(e) { handleError(); }
class C {
static { foo(); }
}
class D { static { foo(); } }
allman
使用 "allman" 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the "allman" option:
/*eslint brace-style: ["error", "allman"]*/
function foo() {
return true;
}
if (foo)
{
bar(); }
try
{
somethingRisky();
} catch(e)
{
handleError();
}
class C {
static {
foo();
}
}
if (foo) {
bar();
} else {
baz();
}
使用 "allman" 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "allman" option:
/*eslint brace-style: ["error", "allman"]*/
function foo()
{
return true;
}
if (foo)
{
bar();
}
if (foo)
{
bar();
}
else
{
baz();
}
try
{
somethingRisky();
}
catch(e)
{
handleError();
}
class C
{
static
{
foo();
}
}
// when there are no braces, there are no problems
if (foo) bar();
else if (baz) boom();
使用 "allman", { "allowSingleLine": true } 选项的此规则的正确代码示例:
🌐 Examples of correct code for this rule with the "allman", { "allowSingleLine": true } options:
/*eslint brace-style: ["error", "allman", { "allowSingleLine": true }]*/
function nop() { return; }
if (foo) { bar(); }
if (foo) { bar(); }
else { baz(); }
try { somethingRisky(); }
catch(e) { handleError(); }
class C
{
static { foo(); }
static
{ foo(); }
}
class D { static { foo(); } }
何时不使用
🌐 When Not To Use It
如果你不想强制执行特定的大括号样式,请不要启用此规则。
🌐 If you don’t want to enforce a particular brace style, don’t enable this rule.
相关规则
版本
此规则是在 ESLint v0.0.7 中引入。