Require or disallow padding lines between statements (padding-line-between-statements)

要求或禁止在语句间填充空行 (padding-line-between-statements)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

This rule requires or disallows blank lines between the given 2 kinds of statements. Properly blank lines help developers to understand the code.

该规则要求或禁止在给定的两种语句间填充空行。正确的空行可以帮助开发人员理解代码。

For example, the following configuration requires a blank line between a variable declaration and a return statement.

例如,以下配置要求在变量声明和 return 语句之间有一行空行。

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: "var", next: "return" }
]*/

function foo() {
    var a = 1;

    return a;
}

Rule Details

This rule does nothing if no configuration.

如果没有配置,该规则不生效。

A configuration is an object which has 3 properties; blankLine, prev and next. For example, { blankLine: "always", prev: "var", next: "return" } means “it requires one or more blank lines between a variable declaration and a return statement.” You can supply any number of configurations. If a statement pair matches multiple configurations, the last matched configuration will be used.

该规则有一个对象配置,有三个属性:blankLineprevnext。例如 { blankLine: "always", prev: "var", next: "return" } 意为 “要求在变量声明和 return 语句之间有一行或多行空行。”你可以在配置中提供任意数量。如果语句匹配多个配置,则使用最后匹配的配置。

{
    "padding-line-between-statements": [
        "error",
        { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
        { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
        { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
        { "blankLine": LINEBREAK_TYPE, "prev": STATEMENT_TYPE, "next": STATEMENT_TYPE },
        ...
    ]
}

Examples

This configuration would require blank lines before all return statements, like the newline-before-return rule.

该配置会要求所有的 return 语句前都有空行,就像 newline-before-return规则一样。

Examples of incorrect code for the [{ blankLine: "always", prev: "*", next: "return" }] configuration:

配置 [{ blankLine: "always", prev: "*", next: "return" }]错误 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: "*", next: "return" }
]*/

function foo() {
    bar();
    return;
}

Examples of correct code for the [{ blankLine: "always", prev: "*", next: "return" }] configuration:

配置 [{ blankLine: "always", prev: "*", next: "return" }]正确 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: "*", next: "return" }
]*/

function foo() {
    bar();

    return;
}

function foo() {
    return;
}

This configuration would require blank lines after every sequence of variable declarations, like the newline-after-var rule.

该配置要求每一个变量声明之后都有空行,就像 newline-after-var 规则。

Examples of incorrect code for the [{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}] configuration:

配置 [{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]错误 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: ["const", "let", "var"], next: "*"},
    { blankLine: "any",    prev: ["const", "let", "var"], next: ["const", "let", "var"]}
]*/

function foo() {
    var a = 0;
    bar();
}

function foo() {
    let a = 0;
    bar();
}

function foo() {
    const a = 0;
    bar();
}

Examples of correct code for the [{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}] configuration:

配置 [{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "var"]}]正确 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: ["const", "let", "var"], next: "*"},
    { blankLine: "any",    prev: ["const", "let", "var"], next: ["const", "let", "var"]}
]*/

function foo() {
    var a = 0;
    var b = 0;

    bar();
}

function foo() {
    let a = 0;
    const b = 0;

    bar();
}

function foo() {
    const a = 0;
    const b = 0;

    bar();
}

This configuration would require blank lines after all directive prologues, like the lines-around-directive rule.

该配置要所有的指令序言之后都有空行,就像 lines-around-directive 规则。

Examples of incorrect code for the [{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }] configuration:

配置 [{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]错误 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: "directive", next: "*" },
    { blankLine: "any",    prev: "directive", next: "directive" }
]*/

"use strict";
foo();

Examples of correct code for the [{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }] configuration:

配置 [{ blankLine: "always", prev: "directive", next: "*" }, { blankLine: "any", prev: "directive", next: "directive" }]正确 代码示例:

/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: "directive", next: "*" },
    { blankLine: "any",    prev: "directive", next: "directive" }
]*/

"use strict";
"use asm";

foo();

Compatibility

When Not To Use It

If you don’t want to notify warnings about linebreaks, then it’s safe to disable this rule.

如果你不想受到关于换行的通知,可以禁用此规则。

Version

This rule was introduced in ESLint 4.0.0-beta.0.

该规则在 ESLint 4.0.0-beta.0 中被引入。

Resources