コンテンツへスキップ

@stylistic/js/

lines-between-class-members

このルールは、クラスメンバー間に改行を強制することで可読性を向上させます。最初のメンバーの前と最後のメンバーの後にある空行は、`padded-blocks`によって既に処理されているため、チェックされません。

ルール詳細

このルールの不正なコードの例

js
/* eslint @stylistic/js/lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;
    
foo() {
//... }
bar() {
//... } }
不正な例

このルールの正しいコードの例

js
/* eslint @stylistic/js/lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;

  foo() {
    //...
  }

  bar() {
    //...
  }
}
正しい例

このルールの追加の正しいコードの例

js
/* eslint @stylistic/js/lines-between-class-members: ["error", "always"]*/
class MyClass {
  x = 1

  ;in = 2
}
正しい例

オプション

このルールには2つのオプションがあります。最初のオプションは文字列またはオブジェクト、2番目のオプションはオブジェクトです。

最初のオプションは、文字列` "always" `または` "never" `、または` enforce `という名前のプロパティを持つオブジェクトです。

  • ` "always" `(デフォルト) クラスメンバーの後に空行を要求します。
  • ` "never" ` クラスメンバーの後の空行を許可しません。
  • `オブジェクト`: `enforce`という名前のプロパティを持つオブジェクト。`enforce`プロパティはオブジェクトの配列である必要があり、それぞれが特定のクラスメンバーのペア間の空行を適用するための設定を指定します。
    • **enforce**: 任意の数の設定を指定できます。メンバーペアが複数の設定に一致する場合は、最後に一致した設定が使用されます。メンバーペアがどの設定にも一致しない場合は、無視されます。各オブジェクトには、次のプロパティが必要です。
      • **blankLine**: 指定されたメンバー間で空行を要求または許可するかどうかを示す` "always" `または` "never" `のいずれかに設定できます。
      • **prev**: 前のクラスメンバーのタイプを指定します。クラスメソッドの場合は` "method" `、クラスフィールドの場合は` "field" `、クラスメンバーのいずれかの場合は` "*" `です。
      • **next**: 後のクラスメンバーのタイプを指定します。`prev`と同じオプションに従います。

2番目のオプションは、`exceptAfterSingleLine`という名前のプロパティを持つオブジェクトです。

  • ` "exceptAfterSingleLine": false `(デフォルト) シングルラインのクラスメンバーの後にある空行のチェックを**スキップしません**。
  • ` "exceptAfterSingleLine": true ` シングルラインのクラスメンバーの後にある空行のチェックをスキップします。

文字列オプションを使用したこのルールの不正なコードの例

js
/* eslint @stylistic/js/lines-between-class-members: ["error", "always"]*/
class Foo{
  x;
    
bar(){}
baz(){}
}
不正な例
js
/* eslint @stylistic/js/lines-between-class-members: ["error", "never"]*/
class Bar{
  x;

    
bar(){}
baz(){}
}
不正な例

文字列オプションを使用したこのルールの正しいコードの例

js
/* eslint @stylistic/js/lines-between-class-members: ["error", "always"]*/
class Foo{
  x;

  bar(){}

  baz(){}
}
正しい例
js
/* eslint @stylistic/js/lines-between-class-members: ["error", "never"]*/
class Bar{
  x;
  bar(){}
  baz(){}
}
正しい例

設定の配列オプションを使用したこのルールの不正なコードの例

js
// disallows blank lines between methods
/*eslint @stylistic/js/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() {}
}
不正な例
js
// requires blank lines around fields, disallows blank lines between methods
/*eslint @stylistic/js/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() {}
}
不正な例

設定の配列オプションを使用したこのルールの正しいコードの例

js
// disallows blank lines between methods
/*eslint @stylistic/js/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() {}
}
正しい例
js
// requires blank lines around fields, disallows blank lines between methods
/*eslint @stylistic/js/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() {}
}
正しい例

オブジェクトオプションを使用したこのルールの正しいコードの例

js
/* eslint @stylistic/js/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(){}
}
正しい例
js
/*eslint @stylistic/js/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() {}
}
正しい例

使用しない場合

クラスメンバー間の空行を適用したくない場合は、このルールを無効にできます。

互換性

MITライセンスの下でリリースされています。