Require or disallow padding lines between statements (padding-line-between-statements)
The --fix option on the command line can automatically fix some of the problems reported by this rule.
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.
/*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 configuration.
A configuration is an object which has 3 properties; blankLine, prev and next. For example, { blankLine: "always", prev: "var", next: "return" } is meaning “it requires one or more blank lines between a variable declaration and a return statement.”
You can supply any number of configurations. If an 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_TYPEis one of the following."any"just ignores the statement pair."never"disallows blank lines."always"requires one or more blank lines. Note it does not count lines that comments exist as blank lines.
STATEMENT_TYPEis one of the following, or an array of the following."*"is wildcard. This matches any statements."block"is lonely blocks."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) { }."break"isbreakstatements."case"iscaselabels."cjs-export"isexportstatements of CommonJS; e.g.module.exports = 0,module.exports.foo = 1, andexports.foo = 2. This is the special cases of assignment."cjs-import"isimportstatements of CommonJS; e.g.const foo = require("foo"). This is the special cases of variable declarations."class"isclassdeclarations."const"isconstvariable declarations."continue"iscontinuestatements."debugger"isdebuggerstatements."default"isdefaultlabels."directive"is directive prologues. This matches directives; e.g."use strict"."do"isdo-whilestatements. This matches all statements that the first token isdokeyword."empty"is empty statements."export"isexportdeclarations."expression"is expression statements."for"isforloop families. This matches all statements that the first token isforkeyword."function"is function declarations."if"isifstatements."import"isimportdeclarations."let"isletvariable declarations."multiline-block-like"is block like statements. This is the same asblock-liketype, but only the block is multiline."return"isreturnstatements."switch"isswitchstatements."throw"isthrowstatements."try"istrystatements."var"isvarvariable declarations."while"iswhileloop statements."with"iswithstatements.
Examples
This configuration would require blank lines before all return statements, like the newline-before-return rule.
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;
}
Examples of correct code for the [{ blankLine: "always", prev: "*", next: "return" }] configuration:
/*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.
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 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:
/*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.
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();
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();
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.