版本

lines-between-class-members

要求或禁止類別成員之間有空行

🔧 可修正

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

重要

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

了解更多

此規則透過強制類別成員之間有空行來提高可讀性。它不會檢查第一個成員之前和最後一個成員之後的空行,因為 padded-blocks 已經處理了這些情況。

規則詳情

此規則的錯誤程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;
  foo() {
    //...
  }
  bar() {
    //...
  }
}

此規則的正確程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;

  foo() {
    //...
  }

  bar() {
    //...
  }
}

此規則的其他正確程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x = 1

  ;in = 2
}

選項

此規則有兩個選項,第一個選項可以是字串或物件,第二個選項是物件。

第一個選項可以是字串 "always""never",或具有名為 enforce 屬性的物件

  • "always" (預設) 要求類別成員之後有空行
  • "never" 禁止類別成員之後有空行
  • Object:具有名為 enforce 屬性的物件。enforce 屬性應該是一個物件陣列,每個物件都指定強制特定類別成員對之間空行的設定。
    • enforce:您可以提供任意數量的設定。如果成員對符合多個設定,將使用最後一個符合的設定。如果成員對不符合任何設定,則會被忽略。每個物件應具有以下屬性
      • blankLine:可以設定為 "always""never",表示是否應該要求或禁止指定成員之間有空行。
      • prev:指定前一個類別成員的類型。它可以是類別方法的 "method"、類別欄位的 "field",或任何類別成員的 "*"
      • next:指定下一個類別成員的類型。它遵循與 prev 相同的選項。

第二個選項是一個具有名為 exceptAfterSingleLine 屬性的物件

  • "exceptAfterSingleLine": false (預設) 跳過檢查單行類別成員後的空行
  • "exceptAfterSingleLine": true 跳過檢查單行類別成員後的空行

此規則使用字串選項的錯誤程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
  x;
  bar(){}
  baz(){}
}
在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "never"]*/
class Bar{
  x;

  bar(){}

  baz(){}
}

此規則使用字串選項的正確程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
  x;

  bar(){}

  baz(){}
}
在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "never"]*/
class Bar{
  x;
  bar(){}
  baz(){}
}

此規則使用設定陣列選項的錯誤程式碼範例

在 Playground 中開啟
// disallows blank lines between methods
/*eslint lines-between-class-members: [
    "error",
    {
      enforce: [
        { blankLine: "never", prev: "method", next: "method" }
      ]
    },
]*/

class MyClass {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }

  fieldA = 'Field A';
  #fieldB = 'Field B';

  method1() {}

  get area() {
    return this.method1();
  }

  method2() {}
}
在 Playground 中開啟
// requires blank lines around fields, disallows blank lines between methods
/*eslint lines-between-class-members: [
    "error",
    {
      enforce: [
        { blankLine: "always", prev: "*", next: "field" },
        { blankLine: "always", prev: "field", next: "*" },
        { blankLine: "never", prev: "method", next: "method" }
      ]
    },
]*/

class MyClass {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }
  fieldA = 'Field A';
  #fieldB = 'Field B';
  method1() {}

  get area() {
    return this.method1();
  }

  method2() {}
}

此規則使用設定陣列選項的正確程式碼範例

在 Playground 中開啟
// disallows blank lines between methods
/*eslint lines-between-class-members: [
    "error",
    {
      enforce: [
        { blankLine: "never", prev: "method", next: "method" }
      ]
    },
]*/

class MyClass {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }

  fieldA = 'Field A';

  #fieldB = 'Field B';

  method1() {}
  get area() {
    return this.method1();
  }
  method2() {}
}
在 Playground 中開啟
// requires blank lines around fields, disallows blank lines between methods
/*eslint lines-between-class-members: [
    "error",
    {
      enforce: [
        { blankLine: "always", prev: "*", next: "field" },
        { blankLine: "always", prev: "field", next: "*" },
        { blankLine: "never", prev: "method", next: "method" }
      ]
    },
]*/

class MyClass {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }

  fieldA = 'Field A';

  #fieldB = 'Field B';

  method1() {}
  get area() {
    return this.method1();
  }
  method2() {}
}

此規則使用物件選項的正確程式碼範例

在 Playground 中開啟
/* eslint lines-between-class-members: ["error", "always", { "exceptAfterSingleLine": true }]*/
class Foo{
  x; // single line class member
  bar(){} // single line class member
  baz(){
    // multi line class member
  }

  qux(){}
}
在 Playground 中開啟
/*eslint lines-between-class-members: [
    "error",
    {
      enforce: [
        { blankLine: "always", prev: "*", next: "method" },
        { blankLine: "always", prev: "method", next: "*" },
        { blankLine: "always", prev: "field", next: "field" }
      ]
    },
    { exceptAfterSingleLine: true }
]*/

class MyClass {
  constructor(height, width) {
      this.height = height;
      this.width = width;
  }

  fieldA = 'Field A';
  #fieldB = 'Field B';
  method1() {}
  get area() {
    return this.method1();
  }

  method2() {}
}

何時不應使用

如果您不想強制類別成員之間有空行,您可以停用此規則。

相容性

版本

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

資源

變更語言