padded-blocks

要求或不允许块内填充

🔧 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.

一些风格指南要求块语句以空行开头和结尾。目标是通过在视觉上分离块内容和周围代码来提高可读性。

¥Some style guides require block statements to start and end with blank lines. The goal is to improve readability by visually separating the block content and the surrounding code.

if (a) {

    b();

}

既然拥有一致的代码风格是件好事,那么你要么总是编写填充块,要么永远不要这样做。

¥Since it’s good to have a consistent code style, you should either always write padded blocks or never do it.

规则详情

¥Rule Details

此规则在块内强制执行一致的空行填充。

¥This rule enforces consistent empty line padding within blocks.

选项

¥Options

该规则有两个选项,第一个可以是字符串选项或对象选项。第二个是对象选项,它可以允许异常。

¥This rule has two options, the first one can be a string option or an object option. The second one is an object option, it can allow exceptions.

第一个选项

¥First option

字符串选项:

¥String option:

  • "always"(默认)要求块语句、函数体、类静态块、类和 switch 语句的开头和结尾有空行。

    ¥"always" (default) requires empty lines at the beginning and ending of block statements, function bodies, class static blocks, classes, and switch statements.

  • "never" 不允许在块语句、函数体、类静态块、类和 switch 语句的开头和结尾处出现空行。

    ¥"never" disallows empty lines at the beginning and ending of block statements, function bodies, class static blocks, classes, and switch statements.

对象选项:

¥Object option:

  • "blocks" 要求或禁止块语句、函数体和类静态块内的填充

    ¥"blocks" require or disallow padding within block statements, function bodies, and class static blocks

  • "classes" 要求或禁止类内填充

    ¥"classes" require or disallow padding within classes

  • "switches" 要求或禁止在 switch 语句中填充

    ¥"switches" require or disallow padding within switch statements

第二种选择

¥Second option

  • "allowSingleLineBlocks": true 允许单行块

    ¥"allowSingleLineBlocks": true allows single-line blocks

always

使用默认 "always" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the default "always" option:

在线运行
/*eslint 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" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the default "always" option:

在线运行
/*eslint padded-blocks: ["error", "always"]*/

if (a) {

    b();

}

if (a)
{

    b();

}

if (a) {

    // comment
    b();

}

class C {

    static {

        a();

    }

}

never

使用 "never" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint padded-blocks: ["error", "never"]*/

if (a) {

    b();

}

if (a)
{

    b();

}

if (a) {

    b();
}

if (a) {
    b();

}

class C {

    static {

        a();

    }

}

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

¥Examples of correct code for this rule with the "never" option:

在线运行
/*eslint padded-blocks: ["error", "never"]*/

if (a) {
    b();
}

if (a)
{
    b();
}

class C {
    static {
        a();
    }
}

blocks

使用 { "blocks": "always" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "blocks": "always" } option:

在线运行
/*eslint 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" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "blocks": "always" } option:

在线运行
/*eslint 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" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "blocks": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "blocks": "never" }]*/

if (a) {

    b();

}

if (a)
{

    b();

}

if (a) {

    b();
}

if (a) {
    b();

}

class C {
    static {

        a();

    }
}

使用 { "blocks": "never" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "blocks": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "blocks": "never" }]*/

if (a) {
    b();
}

if (a)
{
    b();
}

class C {
    static {
        a();
    }
}

class D {

    static {
        a();
    }

}

classes

使用 { "classes": "always" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "classes": "always" } option:

在线运行
/*eslint padded-blocks: ["error", { "classes": "always" }]*/

class  A {
    constructor(){
    }
}

使用 { "classes": "always" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "classes": "always" } option:

在线运行
/*eslint padded-blocks: ["error", { "classes": "always" }]*/

class  A {

    constructor(){
    }

}

使用 { "classes": "never" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "classes": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "classes": "never" }]*/

class  A {

    constructor(){
    }

}

使用 { "classes": "never" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "classes": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "classes": "never" }]*/

class  A {
    constructor(){
    }
}

switches

使用 { "switches": "always" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "switches": "always" } option:

在线运行
/*eslint padded-blocks: ["error", { "switches": "always" }]*/

switch (a) {
    case 0: foo();
}

使用 { "switches": "always" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "switches": "always" } option:

在线运行
/*eslint padded-blocks: ["error", { "switches": "always" }]*/

switch (a) {

    case 0: foo();

}

if (a) {
    b();
}

使用 { "switches": "never" } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "switches": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "switches": "never" }]*/

switch (a) {

    case 0: foo();

}

使用 { "switches": "never" } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "switches": "never" } option:

在线运行
/*eslint padded-blocks: ["error", { "switches": "never" }]*/

switch (a) {
    case 0: foo();
}

if (a) {

    b();

}

always + allowSingleLineBlocks

使用 "always", {"allowSingleLineBlocks": true} 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", {"allowSingleLineBlocks": true} options:

在线运行
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/

if (a) {
    b();
}

if (a) {

    b();
}

if (a) {
    b();

}

使用 "always", {"allowSingleLineBlocks": true} 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", {"allowSingleLineBlocks": true} options:

在线运行
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/

if (a) { b(); }

if (a) {

    b();

}

何时不使用

¥When Not To Use It

如果你不关心块内填充的一致性,你可以关闭此规则。

¥You can turn this rule off if you are not concerned with the consistency of padding within blocks.

版本

此规则是在 ESLint v0.9.0 中引入。

资源