跳至内容

@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
}
正确

选项

此规则有两个选项,第一个选项可以是字符串或对象,第二个选项是对象。

第一个选项可以是字符串 "always""never" 或一个名为 enforce 的属性的对象

  • "always"(默认) 要求在类成员之后添加空行
  • "never" 禁止在类成员之后添加空行
  • Object: 一个名为 enforce 的属性的对象。enforce 属性应该是一个对象数组,每个对象指定在特定类成员对之间强制添加空行的配置。
    • enforce: 你可以提供任意数量的配置。如果一个成员对匹配多个配置,则使用最后匹配的配置。如果一个成员对不匹配任何配置,则忽略它。每个对象应该具有以下属性
      • blankLine: 可以设置为 "always""never",表示在指定成员之间是否需要或禁止添加空行。
      • prev: 指定前一个类成员的类型。它可以是 "method"(表示类方法)、"field"(表示类字段)或 "*"(表示任何类成员)。
      • next: 指定以下类成员的类型。它遵循与 prev 相同的选项。

第二个选项是一个具有名为 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() {}
}
正确

何时不使用它

如果您不想在类成员之间强制执行空行,可以禁用此规则。

兼容性