@@ -21,148 +21,314 @@ ruleTester.run("yoda", rule, {
21
21
valid : [
22
22
23
23
// "never" mode
24
- { code : " if (value === \ "red\ ") {}" , options : [ "never" ] } ,
24
+ { code : ' if (value === "red") {}' , options : [ "never" ] } ,
25
25
{ code : "if (value === value) {}" , options : [ "never" ] } ,
26
26
{ code : "if (value != 5) {}" , options : [ "never" ] } ,
27
27
{ code : "if (5 & foo) {}" , options : [ "never" ] } ,
28
28
{ code : "if (5 === 4) {}" , options : [ "never" ] } ,
29
- { code : "if (value === `red`) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
30
- { code : "if (`red` === `red`) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
31
- { code : "if (`${foo}` === `red`) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
32
- { code : "if (`${\"\"}` === `red`) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
33
- { code : "if (`${\"red\"}` === foo) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
34
- { code : "if (b > `a` && b > `a`) {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
35
- { code : "if (`b` > `a` && \"b\" > \"a\") {}" , options : [ "never" ] , parserOptions : { ecmaVersion : 2015 } } ,
29
+ {
30
+ code : "if (value === `red`) {}" ,
31
+ options : [ "never" ] ,
32
+ parserOptions : { ecmaVersion : 2015 }
33
+ } ,
34
+ {
35
+ code : "if (`red` === `red`) {}" ,
36
+ options : [ "never" ] ,
37
+ parserOptions : { ecmaVersion : 2015 }
38
+ } ,
39
+ {
40
+ code : "if (`${foo}` === `red`) {}" ,
41
+ options : [ "never" ] ,
42
+ parserOptions : { ecmaVersion : 2015 }
43
+ } ,
44
+ {
45
+ code : 'if (`${""}` === `red`) {}' ,
46
+ options : [ "never" ] ,
47
+ parserOptions : { ecmaVersion : 2015 }
48
+ } ,
49
+ {
50
+ code : 'if (`${"red"}` === foo) {}' ,
51
+ options : [ "never" ] ,
52
+ parserOptions : { ecmaVersion : 2015 }
53
+ } ,
54
+ {
55
+ code : "if (b > `a` && b > `a`) {}" ,
56
+ options : [ "never" ] ,
57
+ parserOptions : { ecmaVersion : 2015 }
58
+ } ,
59
+ {
60
+ code : 'if (`b` > `a` && "b" > "a") {}' ,
61
+ options : [ "never" ] ,
62
+ parserOptions : { ecmaVersion : 2015 }
63
+ } ,
36
64
37
65
// "always" mode
38
- { code : " if (\ "blue\ " === value) {}" , options : [ "always" ] } ,
66
+ { code : ' if ("blue" === value) {}' , options : [ "always" ] } ,
39
67
{ code : "if (value === value) {}" , options : [ "always" ] } ,
40
68
{ code : "if (4 != value) {}" , options : [ "always" ] } ,
41
69
{ code : "if (foo & 4) {}" , options : [ "always" ] } ,
42
70
{ code : "if (5 === 4) {}" , options : [ "always" ] } ,
43
- { code : "if (`red` === value) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
44
- { code : "if (`red` === `red`) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
45
- { code : "if (`red` === `${foo}`) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
46
- { code : "if (`red` === `${\"\"}`) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
47
- { code : "if (foo === `${\"red\"}`) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
48
- { code : "if (`a` > b && `a` > b) {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
49
- { code : "if (`b` > `a` && \"b\" > \"a\") {}" , options : [ "always" ] , parserOptions : { ecmaVersion : 2015 } } ,
71
+ {
72
+ code : "if (`red` === value) {}" ,
73
+ options : [ "always" ] ,
74
+ parserOptions : { ecmaVersion : 2015 }
75
+ } ,
76
+ {
77
+ code : "if (`red` === `red`) {}" ,
78
+ options : [ "always" ] ,
79
+ parserOptions : { ecmaVersion : 2015 }
80
+ } ,
81
+ {
82
+ code : "if (`red` === `${foo}`) {}" ,
83
+ options : [ "always" ] ,
84
+ parserOptions : { ecmaVersion : 2015 }
85
+ } ,
86
+ {
87
+ code : 'if (`red` === `${""}`) {}' ,
88
+ options : [ "always" ] ,
89
+ parserOptions : { ecmaVersion : 2015 }
90
+ } ,
91
+ {
92
+ code : 'if (foo === `${"red"}`) {}' ,
93
+ options : [ "always" ] ,
94
+ parserOptions : { ecmaVersion : 2015 }
95
+ } ,
96
+ {
97
+ code : "if (`a` > b && `a` > b) {}" ,
98
+ options : [ "always" ] ,
99
+ parserOptions : { ecmaVersion : 2015 }
100
+ } ,
101
+ {
102
+ code : 'if (`b` > `a` && "b" > "a") {}' ,
103
+ options : [ "always" ] ,
104
+ parserOptions : { ecmaVersion : 2015 }
105
+ } ,
50
106
51
107
// Range exception
52
108
{
53
- code : " if (0 < x && x <= 1 ) {}" ,
109
+ code : ' if ("a" < x && x < MAX ) {}' ,
54
110
options : [ "never" , { exceptRange : true } ]
55
- } , {
56
- code : "if (x < 0 || 1 <= x) {}" ,
111
+ } ,
112
+ {
113
+ code : "if (1 < x && x < MAX ) {}" ,
114
+ options : [ "never" , { exceptRange : true } ]
115
+ } ,
116
+ {
117
+ code : "if ('a' < x && x < MAX ) {}" ,
118
+ options : [ "never" , { exceptRange : true } ]
119
+ } ,
120
+ {
121
+ code : "if (x < `x` || `x` <= x) {}" ,
122
+ options : [ "never" , { exceptRange : true } ] ,
123
+ parserOptions : { ecmaVersion : 2015 }
124
+ } ,
125
+ {
126
+ code : "if (0 < x && x <= 1) {}" ,
57
127
options : [ "never" , { exceptRange : true } ]
58
- } , {
128
+ } ,
129
+ {
59
130
code : "if (0 <= x && x < 1) {}" ,
60
131
options : [ "always" , { exceptRange : true } ]
61
- } , {
62
- code : "if (x <= 'bar' || 'foo' < x) {}" ,
63
- options : [ "always" , { exceptRange : true } ]
64
- } , {
132
+ } ,
133
+ {
65
134
code : "if ('blue' < x.y && x.y < 'green') {}" ,
66
135
options : [ "never" , { exceptRange : true } ]
67
- } , {
136
+ } ,
137
+ {
68
138
code : "if (0 < x[``] && x[``] < 100) {}" ,
69
139
options : [ "never" , { exceptRange : true } ] ,
70
140
parserOptions : { ecmaVersion : 2015 }
71
- } , {
141
+ } ,
142
+ {
72
143
code : "if (0 < x[''] && x[``] < 100) {}" ,
73
144
options : [ "never" , { exceptRange : true } ] ,
74
145
parserOptions : { ecmaVersion : 2015 }
75
- } , {
146
+ } ,
147
+ {
148
+ code :
149
+ "if (a < 4 || (b[c[0]].d['e'] < 0 || 1 <= b[c[0]].d['e'])) {}" ,
150
+ options : [ "never" , { exceptRange : true } ]
151
+ } ,
152
+ {
76
153
code : "if (0 <= x['y'] && x['y'] <= 100) {}" ,
77
154
options : [ "never" , { exceptRange : true } ]
78
- } , {
155
+ } ,
156
+ {
79
157
code : "if (a < 0 && (0 < b && b < 1)) {}" ,
80
158
options : [ "never" , { exceptRange : true } ]
81
- } , {
159
+ } ,
160
+ {
82
161
code : "if ((0 < a && a < 1) && b < 0) {}" ,
83
162
options : [ "never" , { exceptRange : true } ]
84
- } , {
85
- code : "if (a < 4 || (b[c[0]].d['e'] < 0 || 1 <= b[c[0]].d['e'])) {}" ,
86
- options : [ "never" , { exceptRange : true } ]
87
- } , {
163
+ } ,
164
+ {
88
165
code : "if (-1 < x && x < 0) {}" ,
89
166
options : [ "never" , { exceptRange : true } ]
90
- } , {
167
+ } ,
168
+ {
91
169
code : "if (0 <= this.prop && this.prop <= 1) {}" ,
92
170
options : [ "never" , { exceptRange : true } ]
93
- } , {
171
+ } ,
172
+ {
94
173
code : "if (0 <= index && index < list.length) {}" ,
95
174
options : [ "never" , { exceptRange : true } ]
96
- } , {
175
+ } ,
176
+ {
97
177
code : "if (ZERO <= index && index < 100) {}" ,
98
178
options : [ "never" , { exceptRange : true } ]
99
- } , {
179
+ } ,
180
+ {
100
181
code : "if (value <= MIN || 10 < value) {}" ,
101
182
options : [ "never" , { exceptRange : true } ]
102
- } , {
183
+ } ,
184
+ {
103
185
code : "if (value <= 0 || MAX < value) {}" ,
104
186
options : [ "never" , { exceptRange : true } ]
105
- } , {
106
- code : "if (0 <= a.b && a[\"b\"] <= 100) {}" ,
187
+ } ,
188
+ {
189
+ code : 'if (0 <= a.b && a["b"] <= 100) {}' ,
107
190
options : [ "never" , { exceptRange : true } ]
108
- } , {
191
+ } ,
192
+ {
109
193
code : "if (0 <= a.b && a[`b`] <= 100) {}" ,
110
194
options : [ "never" , { exceptRange : true } ] ,
111
195
parserOptions : { ecmaVersion : 2015 }
112
- } , {
196
+ } ,
197
+ {
113
198
code : "if (-1n < x && x <= 1n) {}" ,
114
199
options : [ "never" , { exceptRange : true } ] ,
115
200
parserOptions : { ecmaVersion : 2020 }
116
- } , {
117
- code : "if (x < -1n || 1n <= x) {}" ,
118
- options : [ "never" , { exceptRange : true } ] ,
119
- parserOptions : { ecmaVersion : 2020 }
120
- } , {
201
+ } ,
202
+ {
121
203
code : "if (-1n <= x && x < 1n) {}" ,
122
204
options : [ "always" , { exceptRange : true } ] ,
123
205
parserOptions : { ecmaVersion : 2020 }
124
- } , {
125
- code : "if (x < -1n || 1n <= x) {}" ,
126
- options : [ "always" , { exceptRange : true } ] ,
127
- parserOptions : { ecmaVersion : 2020 }
128
- } , {
206
+ } ,
207
+ {
129
208
code : "if (x < `1` || `1` < x) {}" ,
130
209
options : [ "always" , { exceptRange : true } ] ,
131
210
parserOptions : { ecmaVersion : 2020 }
132
- } , {
211
+ } ,
212
+ {
133
213
code : "if (1 <= a['/(?<zero>0)/'] && a[/(?<zero>0)/] <= 100) {}" ,
134
214
options : [ "never" , { exceptRange : true } ] ,
135
215
parserOptions : { ecmaVersion : 2018 }
136
- } , {
216
+ } ,
217
+ {
137
218
code : "if (x <= `bar` || `foo` < x) {}" ,
138
219
options : [ "always" , { exceptRange : true } ] ,
139
220
parserOptions : { ecmaVersion : 2015 }
140
- } , {
221
+ } ,
222
+ {
223
+ code : "if ('a' < x && x < MAX ) {}" ,
224
+ options : [ "always" , { exceptRange : true } ] ,
225
+ parserOptions : { ecmaVersion : 2015 }
226
+ } ,
227
+ {
228
+ code : "if ('a' < x && x < MAX ) {}" ,
229
+ options : [ "always" ] ,
230
+ parserOptions : { ecmaVersion : 2015 }
231
+ } ,
232
+ {
233
+ code : "if (MIN < x && x < 'a' ) {}" ,
234
+ options : [ "never" , { exceptRange : true } ] ,
235
+ parserOptions : { ecmaVersion : 2015 }
236
+ } ,
237
+ {
238
+ code : "if (MIN < x && x < 'a' ) {}" ,
239
+ options : [ "never" ] ,
240
+ parserOptions : { ecmaVersion : 2015 }
241
+ } ,
242
+ {
141
243
code : "if (`blue` < x.y && x.y < `green`) {}" ,
142
244
options : [ "never" , { exceptRange : true } ] ,
143
245
parserOptions : { ecmaVersion : 2015 }
144
- } , {
246
+ } ,
247
+ {
145
248
code : "if (0 <= x[`y`] && x[`y`] <= 100) {}" ,
146
249
options : [ "never" , { exceptRange : true } ] ,
147
250
parserOptions : { ecmaVersion : 2015 }
148
- } , {
149
- code : "if (0 <= x[`y`] && x[\"y\"] <= 100) {}" ,
251
+ } ,
252
+ {
253
+ code : 'if (0 <= x[`y`] && x["y"] <= 100) {}' ,
150
254
options : [ "never" , { exceptRange : true } ] ,
151
255
parserOptions : { ecmaVersion : 2015 }
152
256
} ,
257
+ {
258
+ code : "if ('a' <= x && x < 'b') {}" ,
259
+ options : [ "never" , { exceptRange : true } ]
260
+ } ,
261
+ {
262
+ code : "if (x < -1n || 1n <= x) {}" ,
263
+ options : [ "never" , { exceptRange : true } ] ,
264
+ parserOptions : { ecmaVersion : 2020 }
265
+ } ,
266
+ {
267
+ code : "if (x < -1n || 1n <= x) {}" ,
268
+ options : [ "always" , { exceptRange : true } ] ,
269
+ parserOptions : { ecmaVersion : 2020 }
270
+ } ,
271
+ {
272
+ code : "if (1 < a && a <= 2) {}" ,
273
+ options : [ "never" , { exceptRange : true } ]
274
+ } ,
275
+ {
276
+ code : "if (x < -1 || 1 < x) {}" ,
277
+ options : [ "never" , { exceptRange : true } ]
278
+ } ,
279
+ {
280
+ code : "if (x <= 'bar' || 'foo' < x) {}" ,
281
+ options : [ "always" , { exceptRange : true } ]
282
+ } ,
283
+ {
284
+ code : "if (x < 0 || 1 <= x) {}" ,
285
+ options : [ "never" , { exceptRange : true } ]
286
+ } ,
287
+ {
288
+ code : "if('a' <= x && x < MAX) {}" ,
289
+ options : [ "never" , { exceptRange : true } ]
290
+ } ,
153
291
154
292
// onlyEquality
155
- { code : "if (0 < x && x <= 1) {}" , options : [ "never" , { onlyEquality : true } ] } ,
156
- { code : "if (x !== 'foo' && 'foo' !== x) {}" , options : [ "never" , { onlyEquality : true } ] } ,
157
- { code : "if (x < 2 && x !== -3) {}" , options : [ "always" , { onlyEquality : true } ] } ,
158
- { code : "if (x !== `foo` && `foo` !== x) {}" , options : [ "never" , { onlyEquality : true } ] , parserOptions : { ecmaVersion : 2015 } } ,
159
- { code : "if (x < `2` && x !== `-3`) {}" , options : [ "always" , { onlyEquality : true } ] , parserOptions : { ecmaVersion : 2015 } }
293
+ {
294
+ code : "if (0 < x && x <= 1) {}" ,
295
+ options : [ "never" , { onlyEquality : true } ]
296
+ } ,
297
+ {
298
+ code : "if (x !== 'foo' && 'foo' !== x) {}" ,
299
+ options : [ "never" , { onlyEquality : true } ]
300
+ } ,
301
+ {
302
+ code : "if (x < 2 && x !== -3) {}" ,
303
+ options : [ "always" , { onlyEquality : true } ]
304
+ } ,
305
+ {
306
+ code : "if (x !== `foo` && `foo` !== x) {}" ,
307
+ options : [ "never" , { onlyEquality : true } ] ,
308
+ parserOptions : { ecmaVersion : 2015 }
309
+ } ,
310
+ {
311
+ code : "if (x < `2` && x !== `-3`) {}" ,
312
+ options : [ "always" , { onlyEquality : true } ] ,
313
+ parserOptions : { ecmaVersion : 2015 }
314
+ }
160
315
] ,
161
316
invalid : [
162
-
163
317
{
164
- code : "if (\"red\" == value) {}" ,
165
- output : "if (value == \"red\") {}" ,
318
+ code : "if (x <= 'foo' || 'bar' < x) {}" ,
319
+ output : "if ('foo' >= x || 'bar' < x) {}" ,
320
+ options : [ "always" , { exceptRange : true } ] ,
321
+ errors : [
322
+ {
323
+ messageId : "expected" ,
324
+ data : { expectedSide : "left" , operator : "<=" } ,
325
+ type : "BinaryExpression"
326
+ }
327
+ ]
328
+ } ,
329
+ {
330
+ code : 'if ("red" == value) {}' ,
331
+ output : 'if (value == "red") {}' ,
166
332
options : [ "never" ] ,
167
333
errors : [
168
334
{
@@ -222,8 +388,8 @@ ruleTester.run("yoda", rule, {
222
388
]
223
389
} ,
224
390
{
225
- code : " if (\ "red\ " <= value) {}" ,
226
- output : " if (value >= \ "red\ ") {}" ,
391
+ code : ' if ("red" <= value) {}' ,
392
+ output : ' if (value >= "red") {}' ,
227
393
options : [ "never" ] ,
228
394
errors : [
229
395
{
@@ -260,8 +426,8 @@ ruleTester.run("yoda", rule, {
260
426
]
261
427
} ,
262
428
{
263
- code : " if (`red` <= `${\ "red\ "}`) {}" ,
264
- output : " if (`${\ "red\ "}` >= `red`) {}" ,
429
+ code : ' if (`red` <= `${"red"}`) {}' ,
430
+ output : ' if (`${"red"}` >= `red`) {}' ,
265
431
options : [ "never" ] ,
266
432
parserOptions : { ecmaVersion : 2015 } ,
267
433
errors : [
@@ -321,8 +487,8 @@ ruleTester.run("yoda", rule, {
321
487
]
322
488
} ,
323
489
{
324
- code : " if (value == \ "red\ ") {}" ,
325
- output : " if (\ "red\ " == value) {}" ,
490
+ code : ' if (value == "red") {}' ,
491
+ output : ' if ("red" == value) {}' ,
326
492
options : [ "always" ] ,
327
493
errors : [
328
494
{
@@ -371,8 +537,8 @@ ruleTester.run("yoda", rule, {
371
537
]
372
538
} ,
373
539
{
374
- code : " if (`${\ "red\ "}` <= `red`) {}" ,
375
- output : " if (`red` >= `${\ "red\ "}`) {}" ,
540
+ code : ' if (`${"red"}` <= `red`) {}' ,
541
+ output : ' if (`red` >= `${"red"}`) {}' ,
376
542
options : [ "always" ] ,
377
543
parserOptions : { ecmaVersion : 2015 } ,
378
544
errors : [
@@ -480,6 +646,19 @@ ruleTester.run("yoda", rule, {
480
646
}
481
647
]
482
648
} ,
649
+ {
650
+ code : "if (`green` < x.y && x.y < `blue`) {}" ,
651
+ output : "if (x.y > `green` && x.y < `blue`) {}" ,
652
+ options : [ "never" , { exceptRange : true } ] ,
653
+ parserOptions : { ecmaVersion : 2015 } ,
654
+ errors : [
655
+ {
656
+ messageId : "expected" ,
657
+ data : { expectedSide : "right" , operator : "<" } ,
658
+ type : "BinaryExpression"
659
+ }
660
+ ]
661
+ } ,
483
662
{
484
663
code : "if (0 <= a[b] && a['b'] < 1) {}" ,
485
664
output : "if (a[b] >= 0 && a['b'] < 1) {}" ,
@@ -641,19 +820,6 @@ ruleTester.run("yoda", rule, {
641
820
}
642
821
]
643
822
} ,
644
- {
645
- code : "if (`green` < x.y && x.y < `blue`) {}" ,
646
- output : "if (x.y > `green` && x.y < `blue`) {}" ,
647
- options : [ "never" , { exceptRange : true } ] ,
648
- parserOptions : { ecmaVersion : 2015 } ,
649
- errors : [
650
- {
651
- messageId : "expected" ,
652
- data : { expectedSide : "right" , operator : "<" } ,
653
- type : "BinaryExpression"
654
- }
655
- ]
656
- } ,
657
823
{
658
824
code : "if (3 == a) {}" ,
659
825
output : "if (a == 3) {}" ,
@@ -992,6 +1158,67 @@ ruleTester.run("yoda", rule, {
992
1158
type : "BinaryExpression"
993
1159
}
994
1160
]
1161
+ } ,
1162
+ {
1163
+ code : "if (`green` < x.y && x.y < `blue`) {}" ,
1164
+ output : "if (`green` < x.y && `blue` > x.y) {}" ,
1165
+ options : [ "always" , { exceptRange : true } ] ,
1166
+ parserOptions : { ecmaVersion : 2015 } ,
1167
+ errors : [
1168
+ {
1169
+ messageId : "expected" ,
1170
+ data : { expectedSide : "left" , operator : "<" } ,
1171
+ type : "BinaryExpression"
1172
+ }
1173
+ ]
1174
+ } ,
1175
+ {
1176
+ code : "if('a' <= x && x < 'b') {}" ,
1177
+ output : "if('a' <= x && 'b' > x) {}" ,
1178
+ options : [ "always" ] ,
1179
+ errors : [
1180
+ {
1181
+ messageId : "expected" ,
1182
+ data : { expectedSide : "left" , operator : "<" } ,
1183
+ type : "BinaryExpression"
1184
+ }
1185
+ ]
1186
+ } ,
1187
+ {
1188
+ code : "if ('b' <= x && x < 'a') {}" ,
1189
+ output : "if (x >= 'b' && x < 'a') {}" ,
1190
+ options : [ "never" , { exceptRange : true } ] ,
1191
+ errors : [
1192
+ {
1193
+ messageId : "expected" ,
1194
+ data : { expectedSide : "right" , operator : "<=" } ,
1195
+ type : "BinaryExpression"
1196
+ }
1197
+ ]
1198
+ } ,
1199
+ {
1200
+ code : "if('a' <= x && x < 1) {}" ,
1201
+ output : "if(x >= 'a' && x < 1) {}" ,
1202
+ options : [ "never" , { exceptRange : true } ] ,
1203
+ errors : [
1204
+ {
1205
+ messageId : "expected" ,
1206
+ data : { expectedSide : "right" , operator : "<=" } ,
1207
+ type : "BinaryExpression"
1208
+ }
1209
+ ]
1210
+ } ,
1211
+ {
1212
+ code : "if (0 < a && b < max) {}" ,
1213
+ output : "if (a > 0 && b < max) {}" ,
1214
+ options : [ "never" , { exceptRange : true } ] ,
1215
+ errors : [
1216
+ {
1217
+ messageId : "expected" ,
1218
+ data : { expectedSide : "right" , operator : "<" } ,
1219
+ type : "BinaryExpression"
1220
+ }
1221
+ ]
995
1222
}
996
1223
]
997
1224
} ) ;
0 commit comments