
brace-style
強制區塊使用一致的括號樣式
此規則報告的某些問題可以透過 --fix
命令列 選項自動修復。
括號樣式與程式設計中的 縮排樣式 密切相關,並描述了括號相對於其控制語句和主體的位置。世界上可能有一打甚至更多的括號樣式。
One true brace style 是 JavaScript 中最常見的括號樣式之一,其中區塊的左大括號與其對應的語句或宣告放在同一行。例如
if (foo) {
bar();
} else {
baz();
}
Stroustrup 是 one true brace style 的一種常見變體,其中 if-else
結構中的 else
語句,以及 catch
和 finally
,都必須在前一個右大括號之後另起一行。例如
if (foo) {
bar();
}
else {
baz();
}
另一種樣式稱為 Allman,其中所有括號都應位於各自的行上,且沒有額外的縮排。例如
if (foo)
{
bar();
}
else
{
baz();
}
雖然沒有哪種樣式被認為比其他樣式更好,但大多數開發人員都同意,在整個專案中保持一致的樣式對於其長期可維護性非常重要。
規則詳情
此規則強制區塊使用一致的括號樣式。
選項
此規則有一個字串選項
"1tbs"
(預設)強制使用 one true brace style。"stroustrup"
強制使用 Stroustrup 樣式。"allman"
強制使用 Allman 樣式。
此規則有一個物件選項用於例外情況
"allowSingleLine": true
(預設false
)允許區塊的左大括號和右大括號位於同一行。
1tbs
使用預設 "1tbs"
選項時,此規則的錯誤程式碼範例
/*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"
選項時,此規則的正確程式碼範例
/*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 }
選項時,此規則的正確程式碼範例
/*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"
選項時,此規則的錯誤程式碼範例
/*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"
選項時,此規則的正確程式碼範例
/*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 }
選項時,此規則的正確程式碼範例
/*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"
選項時,此規則的錯誤程式碼範例
/*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"
選項時,此規則的正確程式碼範例
/*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 }
選項時,此規則的正確程式碼範例
/*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(); } }
何時不該使用
如果您不想強制執行特定的括號樣式,請不要啟用此規則。
相關規則
版本
此規則在 ESLint v0.0.7 中引入。
延伸閱讀
