跳至内容

@stylistic/

类成员间行

此规则通过强制在类成员之间添加行来提高可读性。它不会检查第一个成员之前和最后一个成员之后的空行,因为这已经由 padded-blocks 规则处理。

规则详情

此规则的错误代码示例

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

此规则的正确代码示例

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

  foo() {
    //...
  }

  bar() {
    //...
  }
}
正确

此规则的额外正确代码示例

js
/* eslint @stylistic/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/lines-between-class-members: ["error", "always"]*/
class Foo{
  x;
    
bar(){}
baz(){}
}
错误
js
/* eslint @stylistic/lines-between-class-members: ["error", "never"]*/
class Bar{
  x;

    
bar(){}
baz(){}
}
错误

使用字符串选项时,此规则的正确代码示例

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

  bar(){}

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

使用配置数组选项时,此规则的不正确代码示例

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() {}
}
错误
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() {}
}
错误

使用配置数组选项时,此规则的正确代码示例

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() {}
}
正确
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() {}
}
正确

使用对象选项时,此规则的正确代码示例

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(){}
}
正确
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() {}
}
正确

何时不使用它

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

兼容性

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() {}
}