lines-between-class-members

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

🔧 Fixable

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

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

此规则通过强制类成员之间的线来提高可读性。它不会检查第一个成员之前和最后一个成员之后的空行,因为它已经被填充块处理了。

¥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"(默认)需要类成员后有一个空行

    ¥"always"(default) require an empty line after class members

  • "never" 不允许类成员后有空行

    ¥"never" disallows an empty line after class members

  • Object:具有名为 enforce 的属性的对象。enforce 属性应该是一个对象数组,每个对象指定用于在特定的类成员对之间强制执行空行的配置。

    ¥Object: An object with a property named enforce. The enforce property should be an array of objects, each specifying the configuration for enforcing empty lines between specific pairs of class members.

    • 执行:你可以提供任意数量的配置。如果一个成员对匹配多个配置,则将使用最后匹配的配置。如果成员对不匹配任何配置,它将被忽略。每个对象应具有以下属性:

      ¥enforce: You can supply any number of configurations. If a member pair matches multiple configurations, the last matched configuration will be used. If a member pair does not match any configurations, it will be ignored. Each object should have the following properties:

      • 空行:可以设置为 "always""never",指示指定成员之间是否需要或不允许有空行。

        ¥blankLine: Can be set to either "always" or "never", indicating whether a blank line should be required or disallowed between the specified members.

      • 上一篇:指定前一个类成员的类型。对于类方法,它可以是 "method";对于类字段,它可以是 "field";对于任何类成员,它可以是 "*"

        ¥prev: Specifies the type of the preceding class member. It can be "method" for class methods, "field" for class fields, or "*" for any class member.

      • 下一个:指定以下类成员的类型。它遵循与 prev 相同的选项。

        ¥next: Specifies the type of the following class member. It follows the same options as prev.

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

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

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

    ¥"exceptAfterSingleLine": false(default) do not skip checking empty lines after single-line class members

  • "exceptAfterSingleLine": true 跳过检查单行类成员之后的空行

    ¥"exceptAfterSingleLine": true skip checking empty lines after single-line class members

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

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

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

¥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 中引入。

资源

ESLint 中文网
粤ICP备13048890号