Index

lines-between-class-members

要求或不允许类成员之间有一个空行

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

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

🌐 This rule improves readability by enforcing lines between class members. It will not check empty lines before the first member and after the last member, since that is already taken care of by padded-blocks.

规则详情

🌐 Rule Details

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;
  foo() {
    //...
  }
  bar() {
    //...
  }
}

符合此规则的正确代码示例:

🌐 Examples of correct code for this rule:

在线运行
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x;

  foo() {
    //...
  }

  bar() {
    //...
  }
}

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

🌐 Examples of additional correct code for this rule:

在线运行
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
  x = 1

  ;in = 2
}

选项

🌐 Options

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

🌐 This rule has two options, first option can be string or object, second option is object.

第一个选项可以是字符串 "always""never",也可以是一个具有名为 enforce 属性的对象:

🌐 First option can be string "always" or "never" or an object with a property named enforce:

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

第二个选项是一个具有名为 exceptAfterSingleLine 的属性的对象:

🌐 Second option is an object with a property named exceptAfterSingleLine:

  • "exceptAfterSingleLine": false(默认) 不要跳过检查单行类成员后的空行
  • "exceptAfterSingleLine": true 跳过检查单行类成员后的空行

带字符串选项的此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the string option:

在线运行
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
  x;
  bar(){}
  baz(){}
}
在线运行
/* eslint lines-between-class-members: ["error", "never"]*/
class Bar{
  x;

  bar(){}

  baz(){}
}

带有字符串选项的此规则的正确代码示例:

🌐 Examples of correct code for this rule with the string option:

在线运行
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
  x;

  bar(){}

  baz(){}
}
在线运行
/* eslint lines-between-class-members: ["error", "never"]*/
class Bar{
  x;
  bar(){}
  baz(){}
}

使用配置数组选项违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the array of configurations option:

在线运行
// 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() {}
}
在线运行
// 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() {}
}

使用配置数组选项遵循此规则的正确代码示例:

🌐 Examples of correct code for this rule with the array of configurations option:

在线运行
// 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() {}
}
在线运行
// 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() {}
}

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

🌐 Examples of correct code for this rule with the object option:

在线运行
/* 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(){}
}
在线运行
/*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() {}
}

何时不使用

🌐 When Not To Use It

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

🌐 If you don’t want to enforce empty lines between class members, you can disable this rule.

兼容性

🌐 Compatibility

版本

此规则是在 ESLint v4.9.0 中引入。

资源