]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/newline-before-return.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / newline-before-return.md
CommitLineData
8f9d1d4d
DC
1---
2title: newline-before-return
8f9d1d4d
DC
3rule_type: layout
4related_rules:
5- newline-after-var
6---
eb39fafa 7
8f9d1d4d
DC
8
9
10This rule was **deprecated** in ESLint v4.0.0 and replaced by the [padding-line-between-statements](padding-line-between-statements) rule.
eb39fafa
DC
11
12There 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:
13
14```js
15function foo(bar) {
16 var baz = 'baz';
17 if (!bar) {
18 bar = baz;
19 return bar;
20 }
21 return bar;
22}
23```
24
25Adding newlines visibly separates the return statements from the previous lines, making it clear where the function exits and what value it returns:
26
27```js
28function foo(bar) {
29 var baz = 'baz';
30
31 if (!bar) {
32 bar = baz;
33
34 return bar;
35 }
36
37 return bar;
38}
39```
40
41## Rule Details
42
43This 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.
44
45Examples of **incorrect** code for this rule:
46
8f9d1d4d
DC
47::: incorrect
48
eb39fafa
DC
49```js
50/*eslint newline-before-return: "error"*/
51
52function foo(bar) {
53 if (!bar) {
54 return;
55 }
56 return bar;
57}
58
59function foo(bar) {
60 if (!bar) {
61 return;
62 }
63 /* multi-line
64 comment */
65 return bar;
66}
67```
68
8f9d1d4d
DC
69:::
70
eb39fafa
DC
71Examples of **correct** code for this rule:
72
8f9d1d4d
DC
73::: correct
74
eb39fafa
DC
75```js
76/*eslint newline-before-return: "error"*/
77
78function foo() {
79 return;
80}
81
82function foo() {
83
84 return;
85}
86
87function foo(bar) {
88 if (!bar) return;
89}
90
91function foo(bar) {
92 if (!bar) { return };
93}
94
95function foo(bar) {
96 if (!bar) {
97 return;
98 }
99}
100
101function foo(bar) {
102 if (!bar) {
103 return;
104 }
105
106 return bar;
107}
108
109function foo(bar) {
110 if (!bar) {
111
112 return;
113 }
114}
115
116function foo() {
117
118 // comment
119 return;
120}
121```
122
8f9d1d4d
DC
123:::
124
eb39fafa
DC
125## When Not To Use It
126
127You can safely disable this rule if you do not have any strict conventions about whitespace before `return` statements.