2020import { ruleTester } from '../rule-tester' ;
2121import rule = require( '../../src/rules/no-identical-conditions' ) ;
2222
23+ const SONAR_RUNTIME = 'sonar-runtime' ;
24+
2325ruleTester . run ( 'no-identical-conditions' , rule , {
2426 valid : [
2527 {
@@ -28,6 +30,18 @@ ruleTester.run('no-identical-conditions', rule, {
2830 {
2931 code : 'if (a) {} else {}' ,
3032 } ,
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+ } ,
3145 {
3246 code : `
3347 switch (a) {
@@ -47,7 +61,7 @@ ruleTester.run('no-identical-conditions', rule, {
4761 ` ,
4862 errors : [
4963 {
50- messageId : 'duplicatedBranch ' ,
64+ messageId : 'duplicatedCondition ' ,
5165 data : {
5266 line : 2 ,
5367 } ,
@@ -64,7 +78,7 @@ ruleTester.run('no-identical-conditions', rule, {
6478 else if (a) {}
6579 // ^
6680 ` ,
67- options : [ 'sonar-runtime' ] ,
81+ options : [ SONAR_RUNTIME ] ,
6882 errors : [
6983 {
7084 messageId : 'sonarRuntime' ,
@@ -77,10 +91,10 @@ ruleTester.run('no-identical-conditions', rule, {
7791 column : 12 ,
7892 endLine : 2 ,
7993 endColumn : 13 ,
80- message : 'Original ' ,
94+ message : 'Covering ' ,
8195 } ,
8296 ] ,
83- message : 'This branch duplicates the one on line 2' ,
97+ message : 'This condition is covered by the one on line 2' ,
8498 } ) ,
8599 } ,
86100 } ,
@@ -94,7 +108,7 @@ ruleTester.run('no-identical-conditions', rule, {
94108 ` ,
95109 errors : [
96110 {
97- messageId : 'duplicatedBranch ' ,
111+ messageId : 'duplicatedCondition ' ,
98112 data : {
99113 line : 3 ,
100114 } ,
@@ -112,7 +126,7 @@ ruleTester.run('no-identical-conditions', rule, {
112126 ` ,
113127 errors : [
114128 {
115- messageId : 'duplicatedBranch ' ,
129+ messageId : 'duplicatedCondition ' ,
116130 data : {
117131 line : 2 ,
118132 } ,
@@ -122,6 +136,126 @@ ruleTester.run('no-identical-conditions', rule, {
122136 } ,
123137 ] ,
124138 } ,
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+ } ,
125259 {
126260 code : `
127261 switch (a) {
@@ -136,7 +270,7 @@ ruleTester.run('no-identical-conditions', rule, {
136270 break;
137271 }
138272 ` ,
139- options : [ 'sonar-runtime' ] ,
273+ options : [ SONAR_RUNTIME ] ,
140274 errors : [
141275 {
142276 messageId : 'sonarRuntime' ,
0 commit comments