File tree 2 files changed +251
-1
lines changed
2 files changed +251
-1
lines changed Original file line number Diff line number Diff line change @@ -102,7 +102,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
102
102
caseNode . consequent . some (
103
103
currentConsequent =>
104
104
currentConsequent . type === 'BreakStatement' ||
105
- currentConsequent . type === 'ReturnStatement' ,
105
+ currentConsequent . type === 'ReturnStatement' ||
106
+ currentConsequent . type === 'BlockStatement' ,
106
107
) ,
107
108
)
108
109
Original file line number Diff line number Diff line change @@ -176,6 +176,89 @@ describe(ruleName, () => {
176
176
} ,
177
177
)
178
178
179
+ ruleTester . run (
180
+ `${ ruleName } (${ type } ): sorts switch cases with block statements` ,
181
+ rule ,
182
+ {
183
+ valid : [
184
+ {
185
+ code : dedent `
186
+ function func(name) {
187
+ let size
188
+ switch(name) {
189
+ case 'aaa': {
190
+ height = 1
191
+ }
192
+ case 'bb': {
193
+ height = 2
194
+ }
195
+ case 'c': {
196
+ height = 3
197
+ }
198
+ default:
199
+ height = NaN
200
+ }
201
+ return size
202
+ }
203
+ ` ,
204
+ options : [ options ] ,
205
+ } ,
206
+ ] ,
207
+ invalid : [
208
+ {
209
+ code : dedent `
210
+ function func(name) {
211
+ let size
212
+ switch(name) {
213
+ case 'aaa': {
214
+ height = 1
215
+ }
216
+ case 'c': {
217
+ height = 3
218
+ }
219
+ case 'bb': {
220
+ height = 2
221
+ }
222
+ default:
223
+ height = NaN
224
+ }
225
+ return size
226
+ }
227
+ ` ,
228
+ output : dedent `
229
+ function func(name) {
230
+ let size
231
+ switch(name) {
232
+ case 'aaa': {
233
+ height = 1
234
+ }
235
+ case 'bb': {
236
+ height = 2
237
+ }
238
+ case 'c': {
239
+ height = 3
240
+ }
241
+ default:
242
+ height = NaN
243
+ }
244
+ return size
245
+ }
246
+ ` ,
247
+ options : [ options ] ,
248
+ errors : [
249
+ {
250
+ messageId : 'unexpectedSwitchCaseOrder' ,
251
+ data : {
252
+ left : 'c' ,
253
+ right : 'bb' ,
254
+ } ,
255
+ } ,
256
+ ] ,
257
+ } ,
258
+ ] ,
259
+ } ,
260
+ )
261
+
179
262
ruleTester . run ( `${ ruleName } (${ type } ): works with grouped cases` , rule , {
180
263
valid : [
181
264
{
@@ -744,6 +827,89 @@ describe(ruleName, () => {
744
827
} ,
745
828
)
746
829
830
+ ruleTester . run (
831
+ `${ ruleName } (${ type } ): sorts switch cases with block statements` ,
832
+ rule ,
833
+ {
834
+ valid : [
835
+ {
836
+ code : dedent `
837
+ function func(name) {
838
+ let size
839
+ switch(name) {
840
+ case 'aaa': {
841
+ height = 1
842
+ }
843
+ case 'bb': {
844
+ height = 2
845
+ }
846
+ case 'c': {
847
+ height = 3
848
+ }
849
+ default:
850
+ height = NaN
851
+ }
852
+ return size
853
+ }
854
+ ` ,
855
+ options : [ options ] ,
856
+ } ,
857
+ ] ,
858
+ invalid : [
859
+ {
860
+ code : dedent `
861
+ function func(name) {
862
+ let size
863
+ switch(name) {
864
+ case 'aaa': {
865
+ height = 1
866
+ }
867
+ case 'c': {
868
+ height = 3
869
+ }
870
+ case 'bb': {
871
+ height = 2
872
+ }
873
+ default:
874
+ height = NaN
875
+ }
876
+ return size
877
+ }
878
+ ` ,
879
+ output : dedent `
880
+ function func(name) {
881
+ let size
882
+ switch(name) {
883
+ case 'aaa': {
884
+ height = 1
885
+ }
886
+ case 'bb': {
887
+ height = 2
888
+ }
889
+ case 'c': {
890
+ height = 3
891
+ }
892
+ default:
893
+ height = NaN
894
+ }
895
+ return size
896
+ }
897
+ ` ,
898
+ options : [ options ] ,
899
+ errors : [
900
+ {
901
+ messageId : 'unexpectedSwitchCaseOrder' ,
902
+ data : {
903
+ left : 'c' ,
904
+ right : 'bb' ,
905
+ } ,
906
+ } ,
907
+ ] ,
908
+ } ,
909
+ ] ,
910
+ } ,
911
+ )
912
+
747
913
ruleTester . run ( `${ ruleName } (${ type } ): works with grouped cases` , rule , {
748
914
valid : [
749
915
{
@@ -1260,6 +1426,89 @@ describe(ruleName, () => {
1260
1426
} ,
1261
1427
)
1262
1428
1429
+ ruleTester . run (
1430
+ `${ ruleName } (${ type } ): sorts switch cases with block statements` ,
1431
+ rule ,
1432
+ {
1433
+ valid : [
1434
+ {
1435
+ code : dedent `
1436
+ function func(name) {
1437
+ let size
1438
+ switch(name) {
1439
+ case 'aaa': {
1440
+ height = 1
1441
+ }
1442
+ case 'bb': {
1443
+ height = 2
1444
+ }
1445
+ case 'c': {
1446
+ height = 3
1447
+ }
1448
+ default:
1449
+ height = NaN
1450
+ }
1451
+ return size
1452
+ }
1453
+ ` ,
1454
+ options : [ options ] ,
1455
+ } ,
1456
+ ] ,
1457
+ invalid : [
1458
+ {
1459
+ code : dedent `
1460
+ function func(name) {
1461
+ let size
1462
+ switch(name) {
1463
+ case 'aaa': {
1464
+ height = 1
1465
+ }
1466
+ case 'c': {
1467
+ height = 3
1468
+ }
1469
+ case 'bb': {
1470
+ height = 2
1471
+ }
1472
+ default:
1473
+ height = NaN
1474
+ }
1475
+ return size
1476
+ }
1477
+ ` ,
1478
+ output : dedent `
1479
+ function func(name) {
1480
+ let size
1481
+ switch(name) {
1482
+ case 'aaa': {
1483
+ height = 1
1484
+ }
1485
+ case 'bb': {
1486
+ height = 2
1487
+ }
1488
+ case 'c': {
1489
+ height = 3
1490
+ }
1491
+ default:
1492
+ height = NaN
1493
+ }
1494
+ return size
1495
+ }
1496
+ ` ,
1497
+ options : [ options ] ,
1498
+ errors : [
1499
+ {
1500
+ messageId : 'unexpectedSwitchCaseOrder' ,
1501
+ data : {
1502
+ left : 'c' ,
1503
+ right : 'bb' ,
1504
+ } ,
1505
+ } ,
1506
+ ] ,
1507
+ } ,
1508
+ ] ,
1509
+ } ,
1510
+ )
1511
+
1263
1512
ruleTester . run ( `${ ruleName } (${ type } ): works with grouped cases` , rule , {
1264
1513
valid : [
1265
1514
{
You can’t perform that action at this time.
0 commit comments