版本

space-before-blocks

強制區塊前的一致間距

🔧 可修正

此規則報告的某些問題可以透過 --fix 命令列 選項自動修正

重要事項

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中的對應規則

了解更多

一致性是任何風格指南的重要部分。雖然區塊的左大括號要放在哪裡是個人偏好,但整個專案中應該保持一致。風格不一致會分散讀者注意力,使其無法看到程式碼的重要部分。

規則詳情

此規則將強制區塊前間距的一致性。它僅適用於非始於新行的區塊。

  • 此規則忽略 => 和區塊之間的間距。間距由 arrow-spacing 規則處理。
  • 此規則忽略關鍵字和區塊之間的間距。間距由 keyword-spacing 規則處理。
  • 此規則忽略 switch case 的 : 和區塊之間的間距。間距由 switch-colon-spacing 規則處理。

選項

此規則接受一個參數。如果它是 "always",則區塊必須始終至少有一個前導空格。如果 "never",則所有區塊都不應有任何前導空格。如果函數區塊、關鍵字區塊和類別需要不同的間距,則可以傳遞一個可選的設定物件作為規則參數,以分別設定這些情況。如果設定物件中的任何值為 "off",則不會對該類型的區塊強制執行任何樣式。

( 例如 { "functions": "never", "keywords": "always", "classes": "always" } )

預設值為 "always"

“always”

使用 “always” 選項時,不正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: "error"*/

if (a){
    b();
}

function a(){}

for (;;){
    b();
}

try {} catch(a){}

class Foo{
  constructor(){}
}

使用 "always" 選項時,正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: "error"*/

if (a) {
    b();
}

if (a) {
    b();
} else{ /*no error. this is checked by `keyword-spacing` rule.*/
    c();
}

class C {
    static{} /*no error. this is checked by `keyword-spacing` rule.*/
}

function a() {}

for (;;) {
    b();
}

try {} catch(a) {}

“never”

使用 "never" 選項時,不正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", "never"]*/

if (a) {
    b();
}

function a() {}

for (;;) {
    b();
}

try {} catch(a) {}

使用 "never" 選項時,正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", "never"]*/

if (a){
    b();
}

function a(){}

for (;;){
    b();
}

try{} catch(a){}

class Foo{
  constructor(){}
}

當設定為 { "functions": "never", "keywords": "always", "classes": "never" } 時,不正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/

function a() {}

try {} catch(a){}

class Foo{
  constructor() {}
}

當設定為 { "functions": "never", "keywords": "always", "classes": "never" } 時,正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "always", "classes": "never" }]*/

for (;;) {
  // ...
}

describe(function(){
  // ...
});

class Foo{
  constructor(){}
}

當設定為 { "functions": "always", "keywords": "never", "classes": "never" } 時,不正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/

function a(){}

try {} catch(a) {}

class Foo {
  constructor(){}
}

當設定為 { "functions": "always", "keywords": "never", "classes": "never" } 時,正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "always", "keywords": "never", "classes": "never" }]*/

if (a){
  b();
}

var a = function() {}

class Foo{
  constructor() {}
}

當設定為 { "functions": "never", "keywords": "never", "classes": "always" } 時,不正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/

class Foo{
  constructor(){}
}

當設定為 { "functions": "never", "keywords": "never", "classes": "always" } 時,正確程式碼範例

在 Playground 中開啟
/*eslint space-before-blocks: ["error", { "functions": "never", "keywords": "never", "classes": "always" }]*/

class Foo {
  constructor(){}
}

何時不該使用

如果您不關心區塊前間距的一致性,則可以關閉此規則。

版本

此規則在 ESLint v0.9.0 中引入。

資源

變更語言