brace-style
为块强制执行一致的大括号样式
此规则报告的一些问题可通过 --fix
命令行选项自动修复
此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
大括号样式与编程中的 缩进风格 密切相关,描述了大括号相对于其控制语句和主体的位置。世界上可能有十几种(如果不是更多的话)牙套样式。
¥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"
(默认)强制执行一种真正的大括号样式¥
"1tbs"
(default) enforces one true brace style -
"stroustrup"
强化 Stroustrup 风格¥
"stroustrup"
enforces Stroustrup style -
"allman"
强化奥尔曼风格¥
"allman"
enforces Allman style
此规则有一个异常的对象选项:
¥This rule has an object option for an exception:
-
"allowSingleLine": true
(默认false
)允许块的左大括号和右大括号位于同一行¥
"allowSingleLine": true
(defaultfalse
) allows the opening and closing braces for a block to be on the same line
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 中引入。