# lines-between-class-members
要求或禁止类成员之间有空行
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
此规则通过强制类成员之间的线来提高可读性。它不会检查第一个成员之前和最后一个成员之后的空行,因为它已经被填充块处理了。
# 规则详情
此规则的错误代码示例:
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
此规则的正确代码示例:
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
x;
foo() {
//...
}
bar() {
//...
}
}
此规则的附加正确代码示例:
/* eslint lines-between-class-members: ["error", "always"]*/
class MyClass {
x = 1
;in = 2
}
# 选项
该规则有一个字符串选项和一个对象选项。
字符串选项:
"always"
(默认)在类成员之后需要一个空行"never"
不允许在类成员之后出现空行
对象选项:
"exceptAfterSingleLine": false
(默认)在单行类成员之后不跳过检查空行"exceptAfterSingleLine": true
在单行类成员之后跳过检查空行
带有字符串选项的此规则的错误代码示例:
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
x;
bar(){}
baz(){}
}
/* eslint lines-between-class-members: ["error", "never"]*/
class Foo{
x;
bar(){}
baz(){}
}
带有字符串选项的此规则的正确代码示例:
/* eslint lines-between-class-members: ["error", "always"]*/
class Foo{
x;
bar(){}
baz(){}
}
/* eslint lines-between-class-members: ["error", "never"]*/
class Foo{
x;
bar(){}
baz(){}
}
带有 object 选项的此规则的正确代码示例:
/* 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(){}
}
# 何时不使用
如果您不想在类成员之间强制使用空行,则可以禁用此规则。
# 兼容性
requirePaddingNewLinesAfterBlocks
disallowPaddingNewLinesAfterBlocks