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