padding-line-between-statements
要求或禁止语句之间的填充行
此规则报告的一些问题可通过 --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 个属性的对象;blankLine
、prev
和 next
。例如,{ 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) { }
, andwhile (a) { }
. Also matches immediately invoked function expression statements. -
"break"
是break
语句。¥
"break"
isbreak
statements. -
"case"
是switch
语句中的case
子句。¥
"case"
iscase
clauses inswitch
statements. -
"cjs-export"
是 CommonJS 的export
语句;例如module.exports = 0
、module.exports.foo = 1
和exports.foo = 2
。这是赋值的一种特殊情况。¥
"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-import"
是 CommonJS 的import
语句;例如const foo = require("foo")
。这是变量声明的一个特例。¥
"cjs-import"
isimport
statements of CommonJS; e.g.const foo = require("foo")
. This is a special case of variable declarations. -
"class"
是class
声明。¥
"class"
isclass
declarations. -
"const"
是const
变量声明,单行和多行。¥
"const"
isconst
variable declarations, both single-line and multiline. -
"continue"
是continue
语句。¥
"continue"
iscontinue
statements. -
"debugger"
是debugger
语句。¥
"debugger"
isdebugger
statements. -
"default"
是switch
语句中的default
子句。¥
"default"
isdefault
clauses inswitch
statements. -
"directive"
是指令序言。这符合指令;例如"use strict"
。¥
"directive"
is directive prologues. This matches directives; e.g."use strict"
. -
"do"
是do-while
语句。这匹配第一个标记是do
关键字的所有语句。¥
"do"
isdo-while
statements. This matches all statements that the first token isdo
keyword. -
"empty"
是空语句。¥
"empty"
is empty statements. -
"export"
是export
声明。¥
"export"
isexport
declarations. -
"expression"
是表达式语句。¥
"expression"
is expression statements. -
"for"
是for
循环族。这匹配第一个标记是for
关键字的所有语句。¥
"for"
isfor
loop families. This matches all statements that the first token isfor
keyword. -
"function"
是函数声明。¥
"function"
is function declarations. -
"if"
是if
语句。¥
"if"
isif
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"
isimport
declarations. -
"let"
是let
变量声明,单行和多行。¥
"let"
islet
variable declarations, both single-line and multiline. -
"multiline-block-like"
是块状语句。这与block-like
类型相同,但仅当块为多行时。¥
"multiline-block-like"
is block like statements. This is the same asblock-like
type, but only if the block is multiline. -
"multiline-const"
是多行const
变量声明。¥
"multiline-const"
is multilineconst
variable declarations. -
"multiline-expression"
是表达式语句。这与expression
类型相同,但仅当语句为多行时。¥
"multiline-expression"
is expression statements. This is the same asexpression
type, but only if the statement is multiline. -
"multiline-let"
是多行let
变量声明。¥
"multiline-let"
is multilinelet
variable declarations. -
"multiline-var"
是多行var
变量声明。¥
"multiline-var"
is multilinevar
variable declarations. -
"return"
是return
语句。¥
"return"
isreturn
statements. -
"singleline-const"
是单行const
变量声明。¥
"singleline-const"
is single-lineconst
variable declarations. -
"singleline-let"
是单行let
变量声明。¥
"singleline-let"
is single-linelet
variable declarations. -
"singleline-var"
是单行var
变量声明。¥
"singleline-var"
is single-linevar
variable declarations. -
"switch"
是switch
语句。¥
"switch"
isswitch
statements. -
"throw"
是throw
语句。¥
"throw"
isthrow
statements. -
"try"
是try
语句。¥
"try"
istry
statements. -
"var"
是var
变量声明,单行和多行。¥
"var"
isvar
variable declarations, both single-line and multiline. -
"while"
是while
循环语句。¥
"while"
iswhile
loop statements. -
"with"
是with
语句。¥
"with"
iswith
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 中引入。