在线文档教程
Eslint
规则 | Rules

padding-line-between-statements

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

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

此规则要求或不允许给定2种语句之间的空行。适当的空白行可帮助开发人员理解代码。

例如,以下配置需要变量声明和return语句之间的空行。

/*eslint padding-line-between-statements: [ "error", { blankLine: "always", prev: "var", next: "return" } ]*/ function foo() { var a = 1; return a; }

规则细节

如果没有配置,此规则不做任何事

配置是具有3个属性的对象; 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 }, ... ] }

  • LINEBREAK_TYPE 是以下之一。

例子

此配置在所有return语句之前需要空行,如换行前换行符规则。

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

/*eslint padding-line-between-statements: [ "error", { blankLine: "always", prev: "*", next: "return" } ]*/ function foo() { bar( return; }

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

/*eslint padding-line-between-statements: [ "error", { blankLine: "always", prev: "*", next: "return" } ]*/ function foo() { bar( return; } function foo() { return; }

这种配置在变量声明的每个序列之后需要空行,如newline-after-var规则。

配置错误代码示例[{ 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( }

正确[{ 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( }

这种配置在所有的指令序言之后都需要空行,例如lines-around-directive规则。

配置错误代码示例[{ 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(

正确[{ 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(

兼容性

  • JSCS: requirePaddingNewLineAfterVariableDeclaration

何时不使用它

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

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

资源