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_TYPEis 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_TYPEis 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"isbreakstatements."break"是break语句。"case"iscaselabels."case"是case标签。"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-export"是 CommonJS 的export语句;如module.exports = 0、module.exports.foo = 1和exports.foo = 2。这是一种特殊的赋值。"cjs-import"isimportstatements 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"isclassdeclarations."class"是class声明。"const"isconstvariable declarations, both single-line and multiline."const"是const变量声明,包括单行和多行。"continue"iscontinuestatements."continue"是continue语句。"debugger"isdebuggerstatements."debugger"是debugger语句。"default"isdefaultlabels."default"是default标签。"directive"is directive prologues. This matches directives; e.g."use strict"."directive"是指令序言。它匹配指令,如"use strict"。"do"isdo-whilestatements. This matches all statements that the first token isdokeyword."do"是do-while语句。它匹配所有以do关键字开始的语句。"empty"is empty statements."empty"是空语句。"export"isexportdeclarations."export"是export声明。"expression"is expression statements."expression"是表达式语句。"for"isforloop families. This matches all statements that the first token isforkeyword."for"是for循环. 它匹配所有以for关键字开始的语句。"function"is function declarations."function"是函数声明。"if"isifstatements."if"是if语句。"iife"is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator."iife"是立即调用的函数表达式语句。这将匹配对函数表达式的调用,函数表达式可选前缀为一元运算符。"import"isimportdeclarations."import"是import语句。"let"isletvariable declarations, both single-line and multiline."let"是let变量声明,包括单行和多行。"multiline-block-like"is block like statements. This is the same asblock-liketype, but only if the block is multiline."multiline-block-like"是像块的语句。它和block-like类型相同,但只应用于多行块。"multiline-const"is multilineconstvariable declarations."multiline-const"是多行const变量声明。"multiline-expression"is expression statements. This is the same asexpressiontype, but only if the statement is multiline."multiline-expression"是表达式语句。同expression类型一下,但只应用于多行语句。"multiline-let"is multilineletvariable declarations."multiline-let"是多行let变量声明。"multiline-var"is multilinevarvariable declarations."multiline-var"是多行var变量声明。"return"isreturnstatements."return"是return语句。"singleline-const"is single-lineconstvariable declarations."singleline-const"是单行const变量声明。"singleline-let"is single-lineletvariable declarations."singleline-let"是单行let变量声明。"singleline-var"is single-linevarvariable declarations."singleline-var"是单行var变量声明。"switch"isswitchstatements."switch"是switch语句。"throw"isthrowstatements."throw"是throw语句。"try"istrystatements."try"是try语句。"var"isvarvariable declarations, both single-line and multiline."var"是var变量声明,包括单行和多行。"while"iswhileloop statements."while"是while循环。"with"iswithstatements."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 中被引入。