padding-line-between-statements

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

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

此规则要求或不允许在给定的 2 种语句之间使用空行。适当的空行有助于开发者理解代码。

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

配置是一个具有 3 个属性的对象;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 是以下之一。

    ¥LINEBREAK_TYPE is one of the following.

    • "any" 只是忽略语句对。

      ¥"any" just ignores the statement pair.

    • "never" 不允许空行。

      ¥"never" disallows blank lines.

    • "always" 需要一个或多个空行。请注意,它不计算注释作为空行存在的行。

      ¥"always" requires one or more blank lines. Note it does not count lines that comments exist as blank lines.

  • STATEMENT_TYPE 是以下之一,或以下数组。

    ¥STATEMENT_TYPE is one of the following, or an array of the following.

    • "*" 是通配符。这匹配任何语句。

      ¥"*" is wildcard. This matches any statements.

    • "block" 是孤块。

      ¥"block" is lonely blocks.

    • "block-like" 是块状语句。这匹配最后一个标记是块的右大括号的语句;例如 { }if (a) { }while (a) { }。还匹配立即调用的函数表达式语句。

      ¥"block-like" is block like statements. This matches statements that the last token is the closing brace of blocks; e.g. { }, if (a) { }, and while (a) { }. Also matches immediately invoked function expression statements.

    • "break"break 语句。

      ¥"break" is break statements.

    • "case"switch 语句中的 case 子句。

      ¥"case" is case clauses in switch statements.

    • "cjs-export" 是 CommonJS 的 export 语句;例如 module.exports = 0module.exports.foo = 1exports.foo = 2。这是赋值的一种特殊情况。

      ¥"cjs-export" is export statements of CommonJS; e.g. module.exports = 0, module.exports.foo = 1, and exports.foo = 2. This is a special case of assignment.

    • "cjs-import" 是 CommonJS 的 import 语句;例如 const foo = require("foo")。这是变量声明的一个特例。

      ¥"cjs-import" is import statements of CommonJS; e.g. const foo = require("foo"). This is a special case of variable declarations.

    • "class"class 声明。

      ¥"class" is class declarations.

    • "const"const 变量声明,单行和多行。

      ¥"const" is const variable declarations, both single-line and multiline.

    • "continue"continue 语句。

      ¥"continue" is continue statements.

    • "debugger"debugger 语句。

      ¥"debugger" is debugger statements.

    • "default"switch 语句中的 default 子句。

      ¥"default" is default clauses in switch statements.

    • "directive" 是指令序言。这符合指令;例如 "use strict"

      ¥"directive" is directive prologues. This matches directives; e.g. "use strict".

    • "do"do-while 语句。这匹配第一个标记是 do 关键字的所有语句。

      ¥"do" is do-while statements. This matches all statements that the first token is do keyword.

    • "empty" 是空语句。

      ¥"empty" is empty statements.

    • "export"export 声明。

      ¥"export" is export declarations.

    • "expression" 是表达式语句。

      ¥"expression" is expression statements.

    • "for"for 循环族。这匹配第一个标记是 for 关键字的所有语句。

      ¥"for" is for loop families. This matches all statements that the first token is for keyword.

    • "function" 是函数声明。

      ¥"function" is function declarations.

    • "if"if 语句。

      ¥"if" is if statements.

    • "iife" 是立即调用函数表达式的语句。这匹配对函数表达式的调用,可选地以一元运算符为前缀。

      ¥"iife" is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator.

    • "import"import 声明。

      ¥"import" is import declarations.

    • "let"let 变量声明,单行和多行。

      ¥"let" is let variable declarations, both single-line and multiline.

    • "multiline-block-like" 是块状语句。这与 block-like 类型相同,但仅当块为多行时。

      ¥"multiline-block-like" is block like statements. This is the same as block-like type, but only if the block is multiline.

    • "multiline-const" 是多行 const 变量声明。

      ¥"multiline-const" is multiline const variable declarations.

    • "multiline-expression" 是表达式语句。这与 expression 类型相同,但仅当语句为多行时。

      ¥"multiline-expression" is expression statements. This is the same as expression type, but only if the statement is multiline.

    • "multiline-let" 是多行 let 变量声明。

      ¥"multiline-let" is multiline let variable declarations.

    • "multiline-var" 是多行 var 变量声明。

      ¥"multiline-var" is multiline var variable declarations.

    • "return"return 语句。

      ¥"return" is return statements.

    • "singleline-const" 是单行 const 变量声明。

      ¥"singleline-const" is single-line const variable declarations.

    • "singleline-let" 是单行 let 变量声明。

      ¥"singleline-let" is single-line let variable declarations.

    • "singleline-var" 是单行 var 变量声明。

      ¥"singleline-var" is single-line var variable declarations.

    • "switch"switch 语句。

      ¥"switch" is switch statements.

    • "throw"throw 语句。

      ¥"throw" is throw statements.

    • "try"try 语句。

      ¥"try" is try statements.

    • "var"var 变量声明,单行和多行。

      ¥"var" is var variable declarations, both single-line and multiline.

    • "while"while 循环语句。

      ¥"while" is while loop statements.

    • "with"with 语句。

      ¥"with" is with statements.

示例

¥Examples

此配置将要求所有 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 中引入。

资源

ESLint 中文网
粤ICP备13048890号