You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/rules/no-else-return.md
+59-35Lines changed: 59 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -21,15 +21,6 @@ function foo() {
21
21
22
22
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.
23
23
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
-
33
24
Examples of **incorrect** code for this rule:
34
25
35
26
::: incorrect
@@ -46,26 +37,14 @@ function foo1() {
46
37
}
47
38
48
39
functionfoo2() {
49
-
if (x) {
50
-
return y;
51
-
} elseif (z) {
52
-
return w;
53
-
} else {
54
-
return t;
55
-
}
56
-
}
57
-
58
-
functionfoo3() {
59
40
if (x) {
60
41
return y;
61
42
} else {
62
43
constt="foo";
63
44
}
64
-
65
-
return t;
66
45
}
67
46
68
-
functionfoo4() {
47
+
functionfoo3() {
69
48
if (error) {
70
49
return'It failed';
71
50
} else {
@@ -76,7 +55,7 @@ function foo4() {
76
55
}
77
56
78
57
// Two warnings for nested occurrences
79
-
functionfoo5() {
58
+
functionfoo4() {
80
59
if (x) {
81
60
if (y) {
82
61
return y;
@@ -109,37 +88,82 @@ function foo1() {
109
88
functionfoo2() {
110
89
if (x) {
111
90
return y;
112
-
} elseif (z) {
113
-
constt="foo";
114
-
} else {
115
-
return w;
116
91
}
92
+
93
+
constt="foo";
117
94
}
118
95
119
96
functionfoo3() {
97
+
if (error) {
98
+
return'It failed';
99
+
}
100
+
101
+
if (loading) {
102
+
return"It's still loading";
103
+
}
104
+
}
105
+
106
+
functionfoo4() {
120
107
if (x) {
121
-
if (z) {
108
+
if (y) {
122
109
return y;
123
110
}
124
-
} else {
125
-
returnz;
111
+
112
+
returnx;
126
113
}
114
+
115
+
return z;
127
116
}
128
117
129
-
functionfoo4() {
118
+
functionfoo5() {
119
+
if (x) {
120
+
constt="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:
0 commit comments