]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-dupe-else-if.md
2ab75a68d9a4504630724bd76cb46fedb80fa899
[pve-eslint.git] / eslint / docs / src / rules / no-dupe-else-if.md
1 ---
2 title: no-dupe-else-if
3 layout: doc
4 rule_type: problem
5 related_rules:
6 - no-duplicate-case
7 - no-lonely-if
8 ---
9
10
11
12 `if-else-if` chains are commonly used when there is a need to execute only one branch (or at most one branch) out of several possible branches, based on certain conditions.
13
14 ```js
15 if (a) {
16 foo();
17 } else if (b) {
18 bar();
19 } else if (c) {
20 baz();
21 }
22 ```
23
24 Two identical test conditions in the same chain are almost always a mistake in the code. Unless there are side effects in the expressions, a duplicate will evaluate to the same `true` or `false` value as the identical expression earlier in the chain, meaning that its branch can never execute.
25
26 ```js
27 if (a) {
28 foo();
29 } else if (b) {
30 bar();
31 } else if (b) {
32 baz();
33 }
34 ```
35
36 In the above example, `baz()` can never execute. Obviously, `baz()` could be executed only when `b` evaluates to `true`, but in that case `bar()` would be executed instead, since it's earlier in the chain.
37
38 ## Rule Details
39
40 This rule disallows duplicate conditions in the same `if-else-if` chain.
41
42 Examples of **incorrect** code for this rule:
43
44 ::: incorrect
45
46 ```js
47 /*eslint no-dupe-else-if: "error"*/
48
49 if (isSomething(x)) {
50 foo();
51 } else if (isSomething(x)) {
52 bar();
53 }
54
55 if (a) {
56 foo();
57 } else if (b) {
58 bar();
59 } else if (c && d) {
60 baz();
61 } else if (c && d) {
62 quux();
63 } else {
64 quuux();
65 }
66
67 if (n === 1) {
68 foo();
69 } else if (n === 2) {
70 bar();
71 } else if (n === 3) {
72 baz();
73 } else if (n === 2) {
74 quux();
75 } else if (n === 5) {
76 quuux();
77 }
78 ```
79
80 :::
81
82 Examples of **correct** code for this rule:
83
84 ::: correct
85
86 ```js
87 /*eslint no-dupe-else-if: "error"*/
88
89 if (isSomething(x)) {
90 foo();
91 } else if (isSomethingElse(x)) {
92 bar();
93 }
94
95 if (a) {
96 foo();
97 } else if (b) {
98 bar();
99 } else if (c && d) {
100 baz();
101 } else if (c && e) {
102 quux();
103 } else {
104 quuux();
105 }
106
107 if (n === 1) {
108 foo();
109 } else if (n === 2) {
110 bar();
111 } else if (n === 3) {
112 baz();
113 } else if (n === 4) {
114 quux();
115 } else if (n === 5) {
116 quuux();
117 }
118 ```
119
120 :::
121
122 This rule can also detect some cases where the conditions are not identical, but the branch can never execute due to the logic of `||` and `&&` operators.
123
124 Examples of additional **incorrect** code for this rule:
125
126 ::: incorrect
127
128 ```js
129 /*eslint no-dupe-else-if: "error"*/
130
131 if (a || b) {
132 foo();
133 } else if (a) {
134 bar();
135 }
136
137 if (a) {
138 foo();
139 } else if (b) {
140 bar();
141 } else if (a || b) {
142 baz();
143 }
144
145 if (a) {
146 foo();
147 } else if (a && b) {
148 bar();
149 }
150
151 if (a && b) {
152 foo();
153 } else if (a && b && c) {
154 bar();
155 }
156
157 if (a || b) {
158 foo();
159 } else if (b && c) {
160 bar();
161 }
162
163 if (a) {
164 foo();
165 } else if (b && c) {
166 bar();
167 } else if (d && (c && e && b || a)) {
168 baz();
169 }
170 ```
171
172 :::
173
174 Please note that this rule does not compare conditions from the chain with conditions inside statements, and will not warn in the cases such as follows:
175
176 ```js
177 if (a) {
178 if (a) {
179 foo();
180 }
181 }
182
183 if (a) {
184 foo();
185 } else {
186 if (a) {
187 bar();
188 }
189 }
190 ```
191
192 ## When Not To Use It
193
194 In rare cases where you really need identical test conditions in the same chain, which necessarily means that the expressions in the chain are causing and relying on side effects, you will have to turn this rule off.