lines-between-class-members
このルールは、クラスメンバ間に空行を強制することで、可読性を向上させます。`padded-blocks` ルールで既に処理されているため、最初のメンバの前と最後のメンバの後にある空行はチェックしません。
ルール詳細
このルールの**不正な**コード例
js
/* eslint @stylistic/lines-between-class-members: ["error", "always"]*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
incorrect
このルールの**正しい**コード例
js
/* eslint @stylistic/lines-between-class-members: ["error", "always"]*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
correct
このルールの追加の**正しい**コード例
js
/* eslint @stylistic/lines-between-class-members: ["error", "always"]*/
class MyClass {
x = 1
;in = 2
}
correct
オプション
このルールには2つのオプションがあります。最初のオプションは文字列またはオブジェクト、2番目のオプションはオブジェクトです。
最初のオプションは文字列` "always" `または` "never" `、または` enforce `という名前のプロパティを持つオブジェクトです。
- `"always"`(デフォルト)クラスメンバの後に空行を要求します。
- `"never"` クラスメンバの後の空行を許可しません。
- `オブジェクト`:`enforce`という名前のプロパティを持つオブジェクト。`enforce`プロパティはオブジェクトの配列で、それぞれが特定のクラスメンバのペア間の空行の強制に関する設定を指定します。
- **enforce**:任意の数の設定を指定できます。メンバペアが複数の設定に一致する場合は、最後に一致した設定が使用されます。メンバペアがどの設定にも一致しない場合は、無視されます。各オブジェクトには次のプロパティが必要です。
- **blankLine**:指定されたメンバ間に空行が必要かどうか、または許可されないかどうかを示す` "always" `または` "never" `のいずれかに設定できます。
- **prev**:先行するクラスメンバのタイプを指定します。クラスメソッドの場合は` "method" `、クラスフィールドの場合は` "field" `、任意のクラスメンバの場合は` "*" `とすることができます。
- **next**:後続するクラスメンバのタイプを指定します。`prev`と同じオプションに従います。
- **enforce**:任意の数の設定を指定できます。メンバペアが複数の設定に一致する場合は、最後に一致した設定が使用されます。メンバペアがどの設定にも一致しない場合は、無視されます。各オブジェクトには次のプロパティが必要です。
2番目のオプションは、`exceptAfterSingleLine`という名前のプロパティを持つオブジェクトです。
- `"exceptAfterSingleLine": false`(デフォルト)1行のクラスメンバの後の空行のチェックを**スキップしません**。
- `"exceptAfterSingleLine": true` 1行のクラスメンバの後の空行のチェックをスキップします。
文字列オプションを使用したこのルールの**不正な**コード例
js
/* eslint @stylistic/lines-between-class-members: ["error", "always"]*/
class Foo{
x;
bar(){}
baz(){}
}
incorrect
js
/* eslint @stylistic/lines-between-class-members: ["error", "never"]*/
class Bar{
x;
bar(){}
baz(){}
}
incorrect
文字列オプションを使用したこのルールの**正しい**コード例
js
/* eslint @stylistic/lines-between-class-members: ["error", "always"]*/
class Foo{
x;
bar(){}
baz(){}
}
correct
js
/* eslint @stylistic/lines-between-class-members: ["error", "never"]*/
class Bar{
x;
bar(){}
baz(){}
}
correct
設定の配列オプションを使用したこのルールの**不正な**コード例
js
// disallows blank lines between methods
/*eslint @stylistic/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() {}
}
incorrect
js
// requires blank lines around fields, disallows blank lines between methods
/*eslint @stylistic/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() {}
}
incorrect
設定の配列オプションを使用したこのルールの**正しい**コード例
js
// disallows blank lines between methods
/*eslint @stylistic/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() {}
}
correct
js
// requires blank lines around fields, disallows blank lines between methods
/*eslint @stylistic/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() {}
}
correct
オブジェクトオプションを使用したこのルールの**正しい**コード例
js
/* eslint @stylistic/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(){}
}
correct
js
/*eslint @stylistic/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() {}
}
correct
使用しない場合
クラスメンバ間の空行を強制したくない場合は、このルールを無効にできます。
互換性
TypeScript固有
ts/lines-between-class-members
オプション
`js/lines-between-class-members`ルールでサポートされているオプションに加えて、このルールは次のオプションを追加します。
オブジェクトオプション
- `"exceptAfterOverload": true`(デフォルト)オーバーロードクラスメンバの後の空行のチェックをスキップします。
- `"exceptAfterOverload": false` オーバーロードクラスメンバの後の空行のチェックを**スキップしません**。
`exceptAfterOverload: true`
`{ "exceptAfterOverload": true }` オプションの**正しい**コード例
ts
/*eslint @stylistic/ts/lines-between-class-members: ["error", "always", { "exceptAfterOverload": true }]*/
class foo {
bar(a: string): void;
bar(a: string, b: string): void;
bar(a: string, b: string) {}
baz() {}
qux() {}
}
`exceptAfterOverload: false`
`{ "exceptAfterOverload": false }` オプションの**正しい**コード例
ts
/*eslint @stylistic/ts/lines-between-class-members: ["error", "always", { "exceptAfterOverload": false }]*/
class foo {
bar(a: string): void;
bar(a: string, b: string): void;
bar(a: string, b: string) {}
baz() {}
qux() {}
}