lines-between-class-members
Require or disallow an empty line between class members
Some problems reported by this rule are automatically fixable by the --fix
command line option
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;
}
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.
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 membersObject
: An object with a property namedenforce
. 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
.
- blankLine: Can be set to either
- 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:
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:
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
x;
}
/* eslint lines-between-class-members: ["error", "never"]*/
class Bar{
x;
}
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() {}
}
// 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;
}
}
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
Related Rules
Version
This rule was introduced in ESLint v4.9.0.