newline-before-return
Require an empty line before return
statements
Some problems reported by this rule are automatically fixable by the --fix
command line option
This rule was deprecated in ESLint v4.0.0 and replaced by the padding-line-between-statements rule.
There is no hard and fast rule about whether empty lines should precede return
statements in JavaScript. However, clearly delineating where a function is returning can greatly increase the readability and clarity of the code. For example:
function foo(bar) {
var baz = 'baz';
if (!bar) {
bar = baz;
return bar;
}
return bar;
}
Adding newlines visibly separates the return statements from the previous lines, making it clear where the function exits and what value it returns:
function foo(bar) {
var baz = 'baz';
if (!bar) {
bar = baz;
return bar;
}
return bar;
}
Rule Details
This rule requires an empty line before return
statements to increase code clarity, except when the return
is alone inside a statement group (such as an if statement). In the latter case, the return
statement does not need to be delineated by virtue of it being alone. Comments are ignored and do not count as empty lines.
Examples of incorrect code for this rule:
/*eslint newline-before-return: "error"*/
function foo1(bar) {
if (!bar) {
return;
}
}
function foo2(bar) {
if (!bar) {
return;
}
/* multi-line
comment */
}
Examples of correct code for this rule:
/*eslint newline-before-return: "error"*/
function foo1() {
return;
}
function foo2() {
return;
}
function foo3(bar) {
if (!bar) return;
}
function foo4(bar) {
if (!bar) { return };
}
function foo5(bar) {
if (!bar) {
return;
}
}
function foo6(bar) {
if (!bar) {
return;
}
return bar;
}
function foo7(bar) {
if (!bar) {
return;
}
}
function foo8() {
// comment
return;
}
When Not To Use It
You can safely disable this rule if you do not have any strict conventions about whitespace before return
statements.
Related Rules
Version
This rule was introduced in ESLint v2.3.0.