]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-dupe-else-if.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / no-dupe-else-if.md
1 # Disallow duplicate conditions in `if-else-if` chains (no-dupe-else-if)
2
3 `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.
4
5 ```js
6 if (a) {
7 foo();
8 } else if (b) {
9 bar();
10 } else if (c) {
11 baz();
12 }
13 ```
14
15 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.
16
17 ```js
18 if (a) {
19 foo();
20 } else if (b) {
21 bar();
22 } else if (b) {
23 baz();
24 }
25 ```
26
27 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.
28
29 ## Rule Details
30
31 This rule disallows duplicate conditions in the same `if-else-if` chain.
32
33 Examples of **incorrect** code for this rule:
34
35 ```js
36 /*eslint no-dupe-else-if: "error"*/
37
38 if (isSomething(x)) {
39 foo();
40 } else if (isSomething(x)) {
41 bar();
42 }
43
44 if (a) {
45 foo();
46 } else if (b) {
47 bar();
48 } else if (c && d) {
49 baz();
50 } else if (c && d) {
51 quux();
52 } else {
53 quuux();
54 }
55
56 if (n === 1) {
57 foo();
58 } else if (n === 2) {
59 bar();
60 } else if (n === 3) {
61 baz();
62 } else if (n === 2) {
63 quux();
64 } else if (n === 5) {
65 quuux();
66 }
67 ```
68
69 Examples of **correct** code for this rule:
70
71 ```js
72 /*eslint no-dupe-else-if: "error"*/
73
74 if (isSomething(x)) {
75 foo();
76 } else if (isSomethingElse(x)) {
77 bar();
78 }
79
80 if (a) {
81 foo();
82 } else if (b) {
83 bar();
84 } else if (c && d) {
85 baz();
86 } else if (c && e) {
87 quux();
88 } else {
89 quuux();
90 }
91
92 if (n === 1) {
93 foo();
94 } else if (n === 2) {
95 bar();
96 } else if (n === 3) {
97 baz();
98 } else if (n === 4) {
99 quux();
100 } else if (n === 5) {
101 quuux();
102 }
103 ```
104
105 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.
106
107 Examples of additional **incorrect** code for this rule:
108
109 ```js
110 /*eslint no-dupe-else-if: "error"*/
111
112 if (a || b) {
113 foo();
114 } else if (a) {
115 bar();
116 }
117
118 if (a) {
119 foo();
120 } else if (b) {
121 bar();
122 } else if (a || b) {
123 baz();
124 }
125
126 if (a) {
127 foo();
128 } else if (a && b) {
129 bar();
130 }
131
132 if (a && b) {
133 foo();
134 } else if (a && b && c) {
135 bar();
136 }
137
138 if (a || b) {
139 foo();
140 } else if (b && c) {
141 bar();
142 }
143
144 if (a) {
145 foo();
146 } else if (b && c) {
147 bar();
148 } else if (d && (c && e && b || a)) {
149 baz();
150 }
151 ```
152
153 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:
154
155 ```js
156 if (a) {
157 if (a) {
158 foo();
159 }
160 }
161
162 if (a) {
163 foo();
164 } else {
165 if (a) {
166 bar();
167 }
168 }
169 ```
170
171 ## When Not To Use It
172
173 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.
174
175 ## Related Rules
176
177 * [no-duplicate-case](no-duplicate-case.md)
178 * [no-lonely-if](no-lonely-if.md)