20
20
import { ruleTester } from '../rule-tester' ;
21
21
import rule = require( '../../src/rules/no-identical-conditions' ) ;
22
22
23
+ const SONAR_RUNTIME = 'sonar-runtime' ;
24
+
23
25
ruleTester . run ( 'no-identical-conditions' , rule , {
24
26
valid : [
25
27
{
@@ -28,6 +30,18 @@ ruleTester.run('no-identical-conditions', rule, {
28
30
{
29
31
code : 'if (a) {} else {}' ,
30
32
} ,
33
+ {
34
+ code : 'if (a && b) {} else if (a) {}' ,
35
+ } ,
36
+ {
37
+ code : 'if (a && b) {} else if (c && d) {}' ,
38
+ } ,
39
+ {
40
+ code : 'if (a || b) {} else if (c || d) {}' ,
41
+ } ,
42
+ {
43
+ code : 'if (a ?? b) {} else if (c) {}' ,
44
+ } ,
31
45
{
32
46
code : `
33
47
switch (a) {
@@ -47,7 +61,7 @@ ruleTester.run('no-identical-conditions', rule, {
47
61
` ,
48
62
errors : [
49
63
{
50
- messageId : 'duplicatedBranch ' ,
64
+ messageId : 'duplicatedCondition ' ,
51
65
data : {
52
66
line : 2 ,
53
67
} ,
@@ -64,7 +78,7 @@ ruleTester.run('no-identical-conditions', rule, {
64
78
else if (a) {}
65
79
// ^
66
80
` ,
67
- options : [ 'sonar-runtime' ] ,
81
+ options : [ SONAR_RUNTIME ] ,
68
82
errors : [
69
83
{
70
84
messageId : 'sonarRuntime' ,
@@ -77,10 +91,10 @@ ruleTester.run('no-identical-conditions', rule, {
77
91
column : 12 ,
78
92
endLine : 2 ,
79
93
endColumn : 13 ,
80
- message : 'Original ' ,
94
+ message : 'Covering ' ,
81
95
} ,
82
96
] ,
83
- message : 'This branch duplicates the one on line 2' ,
97
+ message : 'This condition is covered by the one on line 2' ,
84
98
} ) ,
85
99
} ,
86
100
} ,
@@ -94,7 +108,7 @@ ruleTester.run('no-identical-conditions', rule, {
94
108
` ,
95
109
errors : [
96
110
{
97
- messageId : 'duplicatedBranch ' ,
111
+ messageId : 'duplicatedCondition ' ,
98
112
data : {
99
113
line : 3 ,
100
114
} ,
@@ -112,7 +126,7 @@ ruleTester.run('no-identical-conditions', rule, {
112
126
` ,
113
127
errors : [
114
128
{
115
- messageId : 'duplicatedBranch ' ,
129
+ messageId : 'duplicatedCondition ' ,
116
130
data : {
117
131
line : 2 ,
118
132
} ,
@@ -122,6 +136,126 @@ ruleTester.run('no-identical-conditions', rule, {
122
136
} ,
123
137
] ,
124
138
} ,
139
+ {
140
+ code : `
141
+ if (a || b) {}
142
+ // >^^^^^^
143
+ else if (a) {}
144
+ // ^` ,
145
+ options : [ SONAR_RUNTIME ] ,
146
+ errors : [
147
+ {
148
+ messageId : 'sonarRuntime' ,
149
+ line : 4 ,
150
+ column : 18 ,
151
+ endLine : 4 ,
152
+ endColumn : 19 ,
153
+ data : {
154
+ line : 2 ,
155
+ sonarRuntimeData : JSON . stringify ( {
156
+ secondaryLocations : [
157
+ {
158
+ line : 2 ,
159
+ column : 12 ,
160
+ endLine : 2 ,
161
+ endColumn : 18 ,
162
+ message : 'Covering' ,
163
+ } ,
164
+ ] ,
165
+ message : 'This condition is covered by the one on line 2' ,
166
+ } ) ,
167
+ } ,
168
+ } ,
169
+ ] ,
170
+ } ,
171
+ {
172
+ code : `if (a || b) {} else if (b) {}` ,
173
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
174
+ } ,
175
+ {
176
+ code : `if ((a === b && fn(c)) || d) {} else if (a === b && fn(c)) {}` ,
177
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
178
+ } ,
179
+ {
180
+ code : `if (a) {} else if (a && b) {}` ,
181
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
182
+ } ,
183
+ {
184
+ code : `if (a && b) ; else if (b && a) {}` ,
185
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
186
+ } ,
187
+ {
188
+ code : `if (a) {} else if (b) {} else if (c && a || b) {}` ,
189
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
190
+ } ,
191
+ {
192
+ code : `if (a) {} else if (b) {} else if (c && (a || b)) {}` ,
193
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
194
+ } ,
195
+ {
196
+ code : `if (a) {} else if (b && c) {} else if (d && (a || e && c && b)) {}` ,
197
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
198
+ } ,
199
+ {
200
+ code : `if (a || b && c) {} else if (b && c && d) {}` ,
201
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
202
+ } ,
203
+ {
204
+ code : `if (a || b) {} else if (b && c) {}` ,
205
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
206
+ } ,
207
+ {
208
+ code : `if (a) {} else if (b) {} else if ((a || b) && c) {}` ,
209
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
210
+ } ,
211
+ {
212
+ code : `if ((a && (b || c)) || d) {} else if ((c || b) && e && a) {}` ,
213
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
214
+ } ,
215
+ {
216
+ code : `if (a && b || b && c) {} else if (a && b && c) {}` ,
217
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
218
+ } ,
219
+ {
220
+ code : `if (a) {} else if (b && c) {} else if (d && (c && e && b || a)) {}` ,
221
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
222
+ } ,
223
+ {
224
+ code : `if (a || (b && (c || d))) {} else if ((d || c) && b) {}` ,
225
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
226
+ } ,
227
+ {
228
+ code : `if (a || b) {} else if ((b || a) && c) {}` ,
229
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
230
+ } ,
231
+ {
232
+ code : `if (a || b) {} else if (c) {} else if (d) {} else if (b && (a || c)) {}` ,
233
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
234
+ } ,
235
+ {
236
+ code : `if (a || b || c) {} else if (a || (b && d) || (c && e)) {}` ,
237
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
238
+ } ,
239
+ {
240
+ code : `if (a || (b || c)) {} else if (a || (b && c)) {}` ,
241
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
242
+ } ,
243
+ {
244
+ code : `if (a || b) {} else if (c) {} else if (d) {} else if ((a || c) && (b || d)) {}` ,
245
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
246
+ } ,
247
+ {
248
+ code : `if (a) {} else if (b) {} else if (c && (a || d && b)) {}` ,
249
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
250
+ } ,
251
+ {
252
+ code : `if (a) {} else if (a || a) {}` ,
253
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
254
+ } ,
255
+ {
256
+ code : `if (a) {} else if (a && a) {}` ,
257
+ errors : [ { messageId : 'duplicatedCondition' } ] ,
258
+ } ,
125
259
{
126
260
code : `
127
261
switch (a) {
@@ -136,7 +270,7 @@ ruleTester.run('no-identical-conditions', rule, {
136
270
break;
137
271
}
138
272
` ,
139
- options : [ 'sonar-runtime' ] ,
273
+ options : [ SONAR_RUNTIME ] ,
140
274
errors : [
141
275
{
142
276
messageId : 'sonarRuntime' ,
0 commit comments