padded-blocks
一些风格指南要求块语句以空行开头和结尾。 目标是通过视觉上分离块内容和周围代码来提高可读性。
if (a) {
b();
}由于保持一致的代码风格很重要,因此您应该始终编写填充块或从不这样做。
规则详细信息
此规则强制在块内一致的空行填充。
选项
此规则有两个选项,第一个选项可以是字符串选项或对象选项。 第二个选项是对象选项,它可以允许例外。
第一个选项
字符串选项
"always"(默认)要求在块语句、函数体、类静态块、类和switch语句的开头和结尾处使用空行。"never"不允许在块语句、函数体、类静态块、类和switch语句的开头和结尾处使用空行。
对象选项
"blocks"要求或禁止在块语句、函数体和类静态块中使用填充"classes"要求或禁止在类中使用填充"switches"要求或禁止在switch语句中使用填充
第二种选择
"allowSingleLineBlocks": true允许单行块
始终
使用默认 "always" 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "always"]*/
if (a) {
b();
}
if (a) { b(); }
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}使用默认 "always" 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "always"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}从不
使用 "never" 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}使用 "never" 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "never"]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}块
使用 { "blocks": "always" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "blocks": "always" }]*/
if (a) {
b();
}
if (a) { b(); }
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}使用 { "blocks": "always" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "blocks": "always" }]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
// comment
b();
}
class C {
static {
a();
}
}
class D {
static {
a();
}
}使用 { "blocks": "never" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
if (a) {
b();
}
if (a) {
b();
}
class C {
static {
a();
}
}使用 { "blocks": "never" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "blocks": "never" }]*/
if (a) {
b();
}
if (a)
{
b();
}
class C {
static {
a();
}
}
class D {
static {
a();
}
}类
使用 { "classes": "always" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}使用 { "classes": "always" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "classes": "always" }]*/
class A {
constructor(){
}
}使用 { "classes": "never" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}使用 { "classes": "never" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "classes": "never" }]*/
class A {
constructor(){
}
}开关
使用 { "switches": "always" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}使用 { "switches": "always" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "switches": "always" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}使用 { "switches": "never" } 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}使用 { "switches": "never" } 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", { "switches": "never" }]*/
switch (a) {
case 0: foo();
}
if (a) {
b();
}始终 + allowSingleLineBlocks
使用 "always", {"allowSingleLineBlocks": true} 选项时,此规则的不正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) {
b();
}
if (a) {
b();
}
if (a) {
b();
}使用 "always", {"allowSingleLineBlocks": true} 选项时,此规则的正确代码示例
/*eslint @stylistic/padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/
if (a) { b(); }
if (a) {
b();
}何时不使用它
如果您不关心块内填充的一致性,可以关闭此规则。