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.
此规则要求或禁止在给定的两种语句之间留空行。适当的空行有助于开发者理解代码。
🌐 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.
配置是一个具有三个属性的对象: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是以下之一。"any"只是忽略了这对陈述。"never"不允许空行。"always"需要一个或多个空行。请注意,包含注释的行不算作空行。
STATEMENT_TYPE是以下之一,或者是以下内容的数组。"*"是通配符。这可以匹配任何语句。"block"是孤独的方块。"block-like"是类似块的语句。这匹配最后一个标记是块的闭合大括号的语句;例如{ }、if (a) { }和while (a) { }。也匹配立即调用的函数表达式语句。"break"是break条陈述。"case"是switch语句中的case个从句。"cjs-export"是 CommonJS 的export语句;例如module.exports = 0、module.exports.foo = 1和exports.foo = 2。这是赋值的一种特殊情况。"cjs-import"是 CommonJS 的import语句;例如const foo = require("foo")。这是变量声明的一种特殊情况。"class"是class声明。"const"是const变量声明,包括单行和多行。"continue"是continue条陈述。"debugger"是debugger条陈述。"default"是switch语句中的default个子句。"directive"是指令开头。这与指令匹配;例如"use strict"。"do"是do-while语句。这匹配所有第一个标记是do关键字的语句。"empty"是空语句。"export"是export声明。"expression"是表达式语句。"for"是for循环族。这匹配所有第一个符号是for关键字的语句。"function"是函数声明。"if"是if条陈述。"iife"是立即调用函数表达式语句。这与函数表达式的调用相匹配,可选择性地在前面加上单目运算符。"import"是import声明。"let"是let变量声明,包括单行和多行。"multiline-block-like"是类似块的语句。这与block-like类型相同,但仅当该块是多行时。"multiline-const"是多行的const变量声明。"multiline-expression"是表达式语句。这与expression类型相同,但仅当语句是多行时。"multiline-let"是多行的let变量声明。"multiline-var"是多行的var变量声明。"return"是return条陈述。"singleline-const"是单行const变量声明。"singleline-let"是单行let变量声明。"singleline-var"是单行var变量声明。"switch"是switch条陈述。"throw"是throw条陈述。"try"是try条陈述。"var"是var变量声明,包括单行和多行。"while"是while循环语句。"with"是with条陈述。
此配置要求在所有 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
- JSCS: requirePaddingNewLineAfterVariableDeclaration
- JSCS: requirePaddingNewLinesAfterBlocks
- JSCS: disallowPaddingNewLinesAfterBlocks
- JSCS: requirePaddingNewLinesAfterUseStrict
- JSCS: disallowPaddingNewLinesAfterUseStrict
- JSCS: requirePaddingNewLinesBeforeExport
- JSCS: disallowPaddingNewLinesBeforeExport
- JSCS: requirePaddingNewlinesBeforeKeywords
- JSCS: disallowPaddingNewlinesBeforeKeywords
版本
此规则是在 ESLint v4.0.0-beta.0 中引入。