padding-line-between-statements
要求或禁止语句之间的填充行
此规则报告的一些问题可通过 --fix 命令行 选项自动修复
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.
此规则要求或不允许在给定的 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_TYPEis 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_TYPEis 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"isbreakstatements. -
"case"是switch语句中的case子句。¥
"case"iscaseclauses inswitchstatements. -
"cjs-export"是 CommonJS 的export语句;例如module.exports = 0、module.exports.foo = 1和exports.foo = 2。这是赋值的一种特殊情况。¥
"cjs-export"isexportstatements 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"isimportstatements of CommonJS; e.g.const foo = require("foo"). This is a special case of variable declarations. -
"class"是class声明。¥
"class"isclassdeclarations. -
"const"是const变量声明,单行和多行。¥
"const"isconstvariable declarations, both single-line and multiline. -
"continue"是continue语句。¥
"continue"iscontinuestatements. -
"debugger"是debugger语句。¥
"debugger"isdebuggerstatements. -
"default"是switch语句中的default子句。¥
"default"isdefaultclauses inswitchstatements. -
"directive"是指令序言。这符合指令;例如"use strict"。¥
"directive"is directive prologues. This matches directives; e.g."use strict". -
"do"是do-while语句。这匹配第一个标记是do关键字的所有语句。¥
"do"isdo-whilestatements. This matches all statements that the first token isdokeyword. -
"empty"是空语句。¥
"empty"is empty statements. -
"export"是export声明。¥
"export"isexportdeclarations. -
"expression"是表达式语句。¥
"expression"is expression statements. -
"for"是for循环族。这匹配第一个标记是for关键字的所有语句。¥
"for"isforloop families. This matches all statements that the first token isforkeyword. -
"function"是函数声明。¥
"function"is function declarations. -
"if"是if语句。¥
"if"isifstatements. -
"iife"是立即调用函数表达式的语句。这匹配对函数表达式的调用,可选地以一元运算符为前缀。¥
"iife"is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator. -
"import"是import声明。¥
"import"isimportdeclarations. -
"let"是let变量声明,单行和多行。¥
"let"isletvariable declarations, both single-line and multiline. -
"multiline-block-like"是块状语句。这与block-like类型相同,但仅当块为多行时。¥
"multiline-block-like"is block like statements. This is the same asblock-liketype, but only if the block is multiline. -
"multiline-const"是多行const变量声明。¥
"multiline-const"is multilineconstvariable declarations. -
"multiline-expression"是表达式语句。这与expression类型相同,但仅当语句为多行时。¥
"multiline-expression"is expression statements. This is the same asexpressiontype, but only if the statement is multiline. -
"multiline-let"是多行let变量声明。¥
"multiline-let"is multilineletvariable declarations. -
"multiline-var"是多行var变量声明。¥
"multiline-var"is multilinevarvariable declarations. -
"return"是return语句。¥
"return"isreturnstatements. -
"singleline-const"是单行const变量声明。¥
"singleline-const"is single-lineconstvariable declarations. -
"singleline-let"是单行let变量声明。¥
"singleline-let"is single-lineletvariable declarations. -
"singleline-var"是单行var变量声明。¥
"singleline-var"is single-linevarvariable declarations. -
"switch"是switch语句。¥
"switch"isswitchstatements. -
"throw"是throw语句。¥
"throw"isthrowstatements. -
"try"是try语句。¥
"try"istrystatements. -
"var"是var变量声明,单行和多行。¥
"var"isvarvariable declarations, both single-line and multiline. -
"while"是while循环语句。¥
"while"iswhileloop statements. -
"with"是with语句。¥
"with"iswithstatements.
-
示例
¥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 中引入。