File tree Expand file tree Collapse file tree 2 files changed +161
-2
lines changed Expand file tree Collapse file tree 2 files changed +161
-2
lines changed Original file line number Diff line number Diff line change @@ -184,18 +184,27 @@ export default createEslintRule<Options, MESSAGE_ID>({
184184
185185 let sortedNodeGroups = nodeGroups
186186 . map ( group => {
187- let sortedGroup = sortNodes ( group , options )
187+ let sortedGroup = sortNodes ( group , options ) . sort ( ( a , b ) => {
188+ if ( b . name === 'default' ) {
189+ return - 1
190+ }
191+ return 1
192+ } )
188193
189194 if ( group . at ( - 1 ) ! . name !== sortedGroup . at ( - 1 ) ! . name ) {
195+ let consequentNodeIndex = sortedGroup . findIndex (
196+ currentNode => currentNode . node . consequent . length !== 0 ,
197+ )
190198 let firstSortedNodeConsequent =
191- sortedGroup . at ( 0 ) ! . node . consequent
199+ sortedGroup . at ( consequentNodeIndex ) ! . node . consequent
192200 let consequentStart = firstSortedNodeConsequent
193201 . at ( 0 )
194202 ?. range . at ( 0 )
195203 let consequentEnd = firstSortedNodeConsequent
196204 . at ( - 1 )
197205 ?. range . at ( 1 )
198206 let lastNode = group . at ( - 1 ) ! . node
207+
199208 if ( consequentStart && consequentEnd && lastNode . test ) {
200209 lastNode . range = [
201210 lastNode . range . at ( 0 ) ! ,
Original file line number Diff line number Diff line change @@ -481,6 +481,56 @@ describe(ruleName, () => {
481481 } ,
482482 ] ,
483483 } )
484+
485+ ruleTester . run (
486+ `${ ruleName } (${ type } ): works with groups with default` ,
487+ rule ,
488+ {
489+ valid : [
490+ {
491+ code : dedent `
492+ switch (x) {
493+ case 'AA':
494+ case 'B':
495+ default:
496+ const c = 3
497+ }
498+ ` ,
499+ options : [ options ] ,
500+ } ,
501+ ] ,
502+ invalid : [
503+ {
504+ code : dedent `
505+ switch (x) {
506+ case 'AA':
507+ default:
508+ case 'B':
509+ break;
510+ }
511+ ` ,
512+ output : dedent `
513+ switch (x) {
514+ case 'AA':
515+ case 'B':
516+ default:
517+ break;
518+ }
519+ ` ,
520+ options : [ options ] ,
521+ errors : [
522+ {
523+ messageId : 'unexpectedSwitchCaseOrder' ,
524+ data : {
525+ left : 'default' ,
526+ right : 'B' ,
527+ } ,
528+ } ,
529+ ] ,
530+ } ,
531+ ] ,
532+ } ,
533+ )
484534 } )
485535
486536 describe ( `${ ruleName } : sorting by natural order` , ( ) => {
@@ -948,6 +998,56 @@ describe(ruleName, () => {
948998 } ,
949999 ] ,
9501000 } )
1001+
1002+ ruleTester . run (
1003+ `${ ruleName } (${ type } ): works with groups with default` ,
1004+ rule ,
1005+ {
1006+ valid : [
1007+ {
1008+ code : dedent `
1009+ switch (x) {
1010+ case 'AA':
1011+ case 'B':
1012+ default:
1013+ const c = 3
1014+ }
1015+ ` ,
1016+ options : [ options ] ,
1017+ } ,
1018+ ] ,
1019+ invalid : [
1020+ {
1021+ code : dedent `
1022+ switch (x) {
1023+ case 'AA':
1024+ default:
1025+ case 'B':
1026+ break;
1027+ }
1028+ ` ,
1029+ output : dedent `
1030+ switch (x) {
1031+ case 'AA':
1032+ case 'B':
1033+ default:
1034+ break;
1035+ }
1036+ ` ,
1037+ options : [ options ] ,
1038+ errors : [
1039+ {
1040+ messageId : 'unexpectedSwitchCaseOrder' ,
1041+ data : {
1042+ left : 'default' ,
1043+ right : 'B' ,
1044+ } ,
1045+ } ,
1046+ ] ,
1047+ } ,
1048+ ] ,
1049+ } ,
1050+ )
9511051 } )
9521052
9531053 describe ( `${ ruleName } : sorting by line length` , ( ) => {
@@ -1414,6 +1514,56 @@ describe(ruleName, () => {
14141514 } ,
14151515 ] ,
14161516 } )
1517+
1518+ ruleTester . run (
1519+ `${ ruleName } (${ type } ): works with groups with default` ,
1520+ rule ,
1521+ {
1522+ valid : [
1523+ {
1524+ code : dedent `
1525+ switch (x) {
1526+ case 'AA':
1527+ case 'B':
1528+ default:
1529+ const c = 3
1530+ }
1531+ ` ,
1532+ options : [ options ] ,
1533+ } ,
1534+ ] ,
1535+ invalid : [
1536+ {
1537+ code : dedent `
1538+ switch (x) {
1539+ case 'AA':
1540+ default:
1541+ case 'B':
1542+ break;
1543+ }
1544+ ` ,
1545+ output : dedent `
1546+ switch (x) {
1547+ case 'AA':
1548+ case 'B':
1549+ default:
1550+ break;
1551+ }
1552+ ` ,
1553+ options : [ options ] ,
1554+ errors : [
1555+ {
1556+ messageId : 'unexpectedSwitchCaseOrder' ,
1557+ data : {
1558+ left : 'default' ,
1559+ right : 'B' ,
1560+ } ,
1561+ } ,
1562+ ] ,
1563+ } ,
1564+ ] ,
1565+ } ,
1566+ )
14171567 } )
14181568
14191569 describe ( `${ ruleName } : misc` , ( ) => {
You can’t perform that action at this time.
0 commit comments