Skip to content

Commit

Permalink
fix: fix sorting of complex switch case expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Aug 2, 2024
1 parent de946e3 commit d07f5f7
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 4 deletions.
44 changes: 40 additions & 4 deletions rules/sort-switch-case.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { TSESTree } from '@typescript-eslint/types'
import type { TSESLint } from '@typescript-eslint/utils'

import type { SortingNode } from '../typings'

Expand Down Expand Up @@ -158,7 +159,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
node: right.node,
fix: fixer => {
let nodeGroups = nodes.reduce<
let additionalFixes: TSESLint.RuleFix[] = []
let nodeGroups = structuredClone(nodes).reduce<
SortingNode<TSESTree.SwitchCase>[][]
>(
(
Expand All @@ -182,10 +184,40 @@ export default createEslintRule<Options, MESSAGE_ID>({

let sortedNodeGroups = nodeGroups
.map(group => {
let { consequent } = group.at(-1)!.node
group.at(-1)!.node.consequent = []
let sortedGroup = sortNodes(group, options)
sortedGroup.at(-1)!.node.consequent = consequent

if (group.at(-1)!.name !== sortedGroup.at(-1)!.name) {
let firstSortedNodeConsequent =
sortedGroup.at(0)!.node.consequent
let consequentStart = firstSortedNodeConsequent
.at(0)
?.range.at(0)
let consequentEnd = firstSortedNodeConsequent
.at(-1)
?.range.at(1)
let lastNode = group.at(-1)!.node
if (consequentStart && consequentEnd && lastNode.test) {
lastNode.range = [
lastNode.range.at(0)!,
lastNode.test.range.at(1)! + 1,
]
additionalFixes.push(
...makeFixes(fixer, group, sortedGroup, sourceCode),
fixer.removeRange([
lastNode.range.at(1)!,
consequentEnd,
]),
fixer.insertTextAfter(
lastNode,
sourceCode.text.slice(
lastNode.range.at(1),
consequentEnd,
),
),
)
}
}

return sortedGroup
})
.sort((a, b) => {
Expand All @@ -205,6 +237,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
}

if (additionalFixes.length) {
return additionalFixes
}

return makeFixes(fixer, nodes, sortedNodes, sourceCode)
},
})
Expand Down
147 changes: 147 additions & 0 deletions test/sort-switch-case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,153 @@ describe(ruleName, () => {
],
},
)

ruleTester.run(
`${ruleName}(${type}): works with single grouped case`,
rule,
{
valid: [
{
code: dedent`
switch (x) {
case AA:
case B:
const a = 1;
break;
}
`,
options: [options],
},
],
invalid: [
{
code: dedent`
switch (x) {
case B:
case AA:
const a = 1;
break;
}
`,
output: dedent`
switch (x) {
case AA:
case B:
const a = 1;
break;
}
`,
options: [options],
errors: [
{
messageId: 'unexpectedSwitchCaseOrder',
data: {
left: 'B',
right: 'AA',
},
},
],
},
],
},
)

ruleTester.run(`${ruleName}(${type}): works with complex cases`, rule, {
valid: [
{
code: dedent`
switch (x) {
case AAAAA:
case BBBB:
const a = 1
break
case CCC:
break
case DD:
case E:
const b = () => {
return 2
}
break
default:
const c = 3
}
`,
options: [options],
},
],
invalid: [
{
code: dedent`
switch (x) {
case E:
case DD:
const b = () => {
return 2
}
break
case CCC:
break
case BBBB:
case AAAAA:
const a = 1
break
default:
const c = 3
}
`,
output: dedent`
switch (x) {
case DD:
case E:
const b = () => {
return 2
}
break
case CCC:
break
case AAAAA:
case BBBB:
const a = 1
break
default:
const c = 3
}
`,
options: [options],
errors: [
{
messageId: 'unexpectedSwitchCaseOrder',
data: {
left: 'E',
right: 'DD',
},
},
{
messageId: 'unexpectedSwitchCaseOrder',
data: {
left: 'DD',
right: 'CCC',
},
},
{
messageId: 'unexpectedSwitchCaseOrder',
data: {
left: 'CCC',
right: 'BBBB',
},
},
{
messageId: 'unexpectedSwitchCaseOrder',
data: {
left: 'BBBB',
right: 'AAAAA',
},
},
],
},
],
})
})

describe(`${ruleName}: sorting by natural order`, () => {
Expand Down

0 comments on commit d07f5f7

Please sign in to comment.