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 configurations are provided.
如果没有提供配置,则此规则不执行任何操作。
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.
配置是具有三个属性的对象::blankLine
、prev
和 next
。例如,{ 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
is one of the following.LINEBREAK_TYPE
是以下一种:"any"
just ignores the statement pair."any"
忽略成对语句。"never"
disallows blank lines."never"
禁止出现空行。"always"
requires one or more blank lines. Note it does not count lines that comments exist as blank lines."always"
要求一行或多行空行。注意它不计算注释中存在的空行。
STATEMENT_TYPE
is one of the following, or an array of the following.STATEMENT_TYPE
是以下一个或多个:"*"
is wildcard. This matches any statements."*"
通配符。匹配任何语句。"block"
is lonely blocks."block"
是单个块。"block-like"
is block like statements. This matches statements that the last token is the closing brace of blocks; e.g.{ }
,if (a) { }
, andwhile (a) { }
. Also matches immediately invoked function expression statements."block-like"
像块的语句。匹配语句的最后一个 token 是闭括号的块;如{ }
、if (a) { }
和while (a) { }
。也匹配立即调用的函数表达式语句。"break"
isbreak
statements."break"
是break
语句。"case"
iscase
labels."case"
是case
标签。"cjs-export"
isexport
statements of CommonJS; e.g.module.exports = 0
,module.exports.foo = 1
, andexports.foo = 2
. This is a special case of assignment."cjs-export"
是 CommonJS 的export
语句;如module.exports = 0
、module.exports.foo = 1
和exports.foo = 2
。这是一种特殊的赋值。"cjs-import"
isimport
statements of CommonJS; e.g.const foo = require("foo")
. This is a special case of variable declarations."cjs-import"
是 CommonJS 的import
语句; 如const foo = require("foo")
。这是一种特殊的变量声明。"class"
isclass
declarations."class"
是class
声明。"const"
isconst
variable declarations, both single-line and multiline."const"
是const
变量声明,包括单行和多行。"continue"
iscontinue
statements."continue"
是continue
语句。"debugger"
isdebugger
statements."debugger"
是debugger
语句。"default"
isdefault
labels."default"
是default
标签。"directive"
is directive prologues. This matches directives; e.g."use strict"
."directive"
是指令序言。它匹配指令,如"use strict"
。"do"
isdo-while
statements. This matches all statements that the first token isdo
keyword."do"
是do-while
语句。它匹配所有以do
关键字开始的语句。"empty"
is empty statements."empty"
是空语句。"export"
isexport
declarations."export"
是export
声明。"expression"
is expression statements."expression"
是表达式语句。"for"
isfor
loop families. This matches all statements that the first token isfor
keyword."for"
是for
循环. 它匹配所有以for
关键字开始的语句。"function"
is function declarations."function"
是函数声明。"if"
isif
statements."if"
是if
语句。"iife"
is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator."iife"
是立即调用的函数表达式语句。这将匹配对函数表达式的调用,函数表达式可选前缀为一元运算符。"import"
isimport
declarations."import"
是import
语句。"let"
islet
variable declarations, both single-line and multiline."let"
是let
变量声明,包括单行和多行。"multiline-block-like"
is block like statements. This is the same asblock-like
type, but only if the block is multiline."multiline-block-like"
是像块的语句。它和block-like
类型相同,但只应用于多行块。"multiline-const"
is multilineconst
variable declarations."multiline-const"
是多行const
变量声明。"multiline-expression"
is expression statements. This is the same asexpression
type, but only if the statement is multiline."multiline-expression"
是表达式语句。同expression
类型一下,但只应用于多行语句。"multiline-let"
is multilinelet
variable declarations."multiline-let"
是多行let
变量声明。"multiline-var"
is multilinevar
variable declarations."multiline-var"
是多行var
变量声明。"return"
isreturn
statements."return"
是return
语句。"singleline-const"
is single-lineconst
variable declarations."singleline-const"
是单行const
变量声明。"singleline-let"
is single-linelet
variable declarations."singleline-let"
是单行let
变量声明。"singleline-var"
is single-linevar
variable declarations."singleline-var"
是单行var
变量声明。"switch"
isswitch
statements."switch"
是switch
语句。"throw"
isthrow
statements."throw"
是throw
语句。"try"
istry
statements."try"
是try
语句。"var"
isvar
variable declarations, both single-line and multiline."var"
是var
变量声明,包括单行和多行。"while"
iswhile
loop statements."while"
是while
循环。"with"
iswith
statements."with"
是with
语句。
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
- JSCS: requirePaddingNewLineAfterVariableDeclaration
- JSCS: requirePaddingNewLinesAfterBlocks
- JSCS: disallowPaddingNewLinesAfterBlocks
- JSCS: requirePaddingNewLinesAfterUseStrict
- JSCS: disallowPaddingNewLinesAfterUseStrict
- JSCS: requirePaddingNewLinesBeforeExport
- JSCS: disallowPaddingNewLinesBeforeExport
- JSCS: requirePaddingNewlinesBeforeKeywords
- JSCS: disallowPaddingNewlinesBeforeKeywords
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 中被引入。