Index

padding-line-between-statements

要求或禁止语句之间的填充行

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

此规则要求或禁止在给定的两种语句之间留空行。适当的空行有助于开发者理解代码。

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

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

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

/*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 configurations are provided.

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

🌐 A configuration is an object which has 3 properties; blankLine, prev and next. For example, { blankLine: "always", prev: "var", next: "return" } means “one or more blank lines are required 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.

{
    "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 },
        ...
    ]
}
  • LINEBREAK_TYPE 是以下之一。
    • "any" 只是忽略了这对陈述。
    • "never" 不允许空行。
    • "always" 需要一个或多个空行。请注意,包含注释的行不算作空行。
  • STATEMENT_TYPE 是以下之一,或者是以下内容的数组。
    • "*" 是通配符。这可以匹配任何语句。
    • "block" 是孤独的方块。
    • "block-like" 是类似块的语句。这匹配最后一个标记是块的闭合大括号的语句;例如 { }if (a) { }while (a) { }。也匹配立即调用的函数表达式语句。
    • "break"break 条陈述。
    • "case"switch 语句中的 case 个从句。
    • "cjs-export" 是 CommonJS 的 export 语句;例如 module.exports = 0module.exports.foo = 1exports.foo = 2。这是赋值的一种特殊情况。
    • "cjs-import" 是 CommonJS 的 import 语句;例如 const foo = require("foo")。这是变量声明的一种特殊情况。
    • "class"class 声明。
    • "const"const 变量声明,包括单行和多行。
    • "continue"continue 条陈述。
    • "debugger"debugger 条陈述。
    • "default"switch 语句中的 default 个子句。
    • "directive" 是指令开头。这与指令匹配;例如 "use strict"
    • "do"do-while 语句。这匹配所有第一个标记是 do 关键字的语句。
    • "empty" 是空语句。
    • "export"export 声明。
    • "expression" 是表达式语句。
    • "for"for 循环族。这匹配所有第一个符号是 for 关键字的语句。
    • "function" 是函数声明。
    • "if"if 条陈述。
    • "iife" 是立即调用函数表达式语句。这与函数表达式的调用相匹配,可选择性地在前面加上单目运算符。
    • "import"import 声明。
    • "let"let 变量声明,包括单行和多行。
    • "multiline-block-like" 是类似块的语句。这与 block-like 类型相同,但仅当该块是多行时。
    • "multiline-const" 是多行的 const 变量声明。
    • "multiline-expression" 是表达式语句。这与 expression 类型相同,但仅当语句是多行时。
    • "multiline-let" 是多行的 let 变量声明。
    • "multiline-var" 是多行的 var 变量声明。
    • "return"return 条陈述。
    • "singleline-const" 是单行 const 变量声明。
    • "singleline-let" 是单行 let 变量声明。
    • "singleline-var" 是单行 var 变量声明。
    • "switch"switch 条陈述。
    • "throw"throw 条陈述。
    • "try"try 条陈述。
    • "var"var 变量声明,包括单行和多行。
    • "while"while 循环语句。
    • "with"with 条陈述。

此配置要求在所有 return 语句之前留空行,就像 newline-before-return 规则一样。

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

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

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

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

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

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

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

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

function foo1() {
    bar();

    return;
}

function foo2() {
    return;
}

此配置要求在每一组变量声明之后添加空行,就像 newline-after-var 规则一样。

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

针对 [{ blankLine: "always", prev: ["const", "let", "var"], next: "*"}, { blankLine: "any", prev: ["const", "let", "var"], next: ["const", "let", "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:

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

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

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

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

class C {
    static {
        let a = 0;
        bar();
    }
}

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

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

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

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

    bar();
}

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

    bar();
}

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

    bar();
}

class C {
    static {
        let a = 0;
        let b = 0;

        bar();
    }
}

此配置要求在所有指令前言之后添加空行,就像 lines-around-directive 规则一样。

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

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

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

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

"use strict";
foo();

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

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

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

"use strict";
"use asm";

foo();

此配置要求在 switch 语句的各个子句之间留空行。

🌐 This configuration would require blank lines between clauses in switch statements.

针对 [{ blankLine: "always", prev: ["case", "default"], next: "*" }] 配置的错误代码示例:

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

在线运行
/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: ["case", "default"], next: "*" }
]*/

switch (foo) {
    case 1:
        bar();
        break;
    case 2:
    case 3:
        baz();
        break;
    default:
        quux();
}

适用于 [{ blankLine: "always", prev: ["case", "default"], next: "*" }] 配置的正确代码示例:

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

在线运行
/*eslint padding-line-between-statements: [
    "error",
    { blankLine: "always", prev: ["case", "default"], next: "*" }
]*/

switch (foo) {
    case 1:
        bar();
        break;

    case 2:

    case 3:
        baz();
        break;

    default:
        quux();
}

何时不使用

🌐 When Not To Use It

如果你不想通知有关换行符的警告,那么禁用此规则是安全的。

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

兼容性

🌐 Compatibility

版本

此规则是在 ESLint v4.0.0-beta.0 中引入。

资源