Skip to content

Commit 2b87e21

Browse files
docs: [no-else-return] clarify sample code. (#19991)
* Update no-else-return.md * fix: typo, grammar * fix: remove 'default' * fix: repeated listing of same option
1 parent c36570c commit 2b87e21

File tree

1 file changed

+59
-35
lines changed

1 file changed

+59
-35
lines changed

docs/src/rules/no-else-return.md

Lines changed: 59 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,6 @@ function foo() {
2121

2222
This rule is aimed at highlighting an unnecessary block of code following an `if` containing a `return` statement. As such, it will warn when it encounters an `else` following a chain of `if`s, all of them containing a `return` statement.
2323

24-
## Options
25-
26-
This rule has an object option:
27-
28-
* `allowElseIf: true` (default) allows `else if` blocks after a `return`
29-
* `allowElseIf: false` disallows `else if` blocks after a `return`
30-
31-
### allowElseIf: true
32-
3324
Examples of **incorrect** code for this rule:
3425

3526
::: incorrect
@@ -46,26 +37,14 @@ function foo1() {
4637
}
4738

4839
function foo2() {
49-
if (x) {
50-
return y;
51-
} else if (z) {
52-
return w;
53-
} else {
54-
return t;
55-
}
56-
}
57-
58-
function foo3() {
5940
if (x) {
6041
return y;
6142
} else {
6243
const t = "foo";
6344
}
64-
65-
return t;
6645
}
6746

68-
function foo4() {
47+
function foo3() {
6948
if (error) {
7049
return 'It failed';
7150
} else {
@@ -76,7 +55,7 @@ function foo4() {
7655
}
7756

7857
// Two warnings for nested occurrences
79-
function foo5() {
58+
function foo4() {
8059
if (x) {
8160
if (y) {
8261
return y;
@@ -109,37 +88,82 @@ function foo1() {
10988
function foo2() {
11089
if (x) {
11190
return y;
112-
} else if (z) {
113-
const t = "foo";
114-
} else {
115-
return w;
11691
}
92+
93+
const t = "foo";
11794
}
11895

11996
function foo3() {
97+
if (error) {
98+
return 'It failed';
99+
}
100+
101+
if (loading) {
102+
return "It's still loading";
103+
}
104+
}
105+
106+
function foo4() {
120107
if (x) {
121-
if (z) {
108+
if (y) {
122109
return y;
123110
}
124-
} else {
125-
return z;
111+
112+
return x;
126113
}
114+
115+
return z;
127116
}
128117

129-
function foo4() {
118+
function foo5() {
119+
if (x) {
120+
const t = "foo";
121+
} else {
122+
return y
123+
}
124+
}
125+
```
126+
127+
:::
128+
129+
## Options
130+
131+
### allowElseIf
132+
133+
This rule has an object option:
134+
135+
* `allowElseIf: true` (default) - If true, allows `else if` blocks after a `return`
136+
137+
Examples of **correct** code for the default `{"allowElseIf": true}` option:
138+
139+
::: correct
140+
141+
```js
142+
/*eslint no-else-return: ["error", {allowElseIf: true}]*/
143+
144+
function foo() {
130145
if (error) {
131146
return 'It failed';
132147
} else if (loading) {
133148
return "It's still loading";
134149
}
135150
}
151+
152+
// Using multiple `if` statements instead of `else if` is also allowed
153+
function foo2() {
154+
if (error) {
155+
return 'It failed';
156+
}
157+
158+
if (loading) {
159+
return "It's still loading";
160+
}
161+
}
136162
```
137163

138164
:::
139165

140-
### allowElseIf: false
141-
142-
Examples of **incorrect** code for this rule:
166+
Examples of **incorrect** code for the `{"allowElseIf": false}` option:
143167

144168
::: incorrect
145169

@@ -157,7 +181,7 @@ function foo() {
157181

158182
:::
159183

160-
Examples of **correct** code for this rule:
184+
Examples of **correct** code for the `{"allowElseIf": false}` option:
161185

162186
::: correct
163187

0 commit comments

Comments
 (0)