Skip to content

@stylistic/js/

lines-between-class-members

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:

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

Examples of correct code for this rule:

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

  foo() {
    //...
  }

  bar() {
    //...
  }
}
correct

Examples of additional correct code for this rule:

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

  ;in = 2
}
correct

Options

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

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

  • "always"(default) require an empty line after class members
  • "never" disallows an empty line after class members
  • 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:
      • blankLine: Can be set to either "always" or "never", indicating whether a blank line should be required or disallowed between the specified members.
      • 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.
      • next: Specifies the type of the following class member. It follows the same options as prev.

Second option is an object with a property named exceptAfterSingleLine:

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

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

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

    
bar(){}
baz(){}
}
incorrect

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

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

  bar(){}

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

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

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

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

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

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

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

When Not To Use It

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

Compatibility

Released under the MIT License.