diff --git a/test/sort-intersection-types.test.ts b/test/sort-intersection-types.test.ts index 84d419ec1..85d26632c 100644 --- a/test/sort-intersection-types.test.ts +++ b/test/sort-intersection-types.test.ts @@ -1439,5 +1439,19 @@ describe(ruleName, () => { ], }, ) + + ruleTester.run(`${ruleName}: should ignore whitespaces`, rule, { + valid: [ + { + code: dedent` + type T = + { a: string } & + { b: string } + `, + options: [{}], + }, + ], + invalid: [], + }) }) }) diff --git a/test/sort-union-types.test.ts b/test/sort-union-types.test.ts index 73c808e2c..7bb4541ab 100644 --- a/test/sort-union-types.test.ts +++ b/test/sort-union-types.test.ts @@ -1471,5 +1471,19 @@ describe(ruleName, () => { ], }, ) + + ruleTester.run(`${ruleName}: should ignore whitespaces`, rule, { + valid: [ + { + code: dedent` + type T = + { a: string } | + { b: string } + `, + options: [{}], + }, + ], + invalid: [], + }) }) }) diff --git a/utils/compare.ts b/utils/compare.ts index 664815a05..fd00f1642 100644 --- a/utils/compare.ts +++ b/utils/compare.ts @@ -37,16 +37,11 @@ export let compare = ( ): number => { let orderCoefficient = options.order === 'asc' ? 1 : -1 let sortingFunction: (a: SortingNode, b: SortingNode) => number - - let formatString = - options.type === 'line-length' || !options.ignoreCase - ? (string: string) => string - : (string: string) => string.toLowerCase() - let nodeValueGetter = options.nodeValueGetter ?? ((node: SortingNode) => node.name) if (options.type === 'alphabetical') { + let formatString = getFormatStringFunc(!!options.ignoreCase) sortingFunction = (aNode, bNode) => formatString(nodeValueGetter(aNode)).localeCompare( formatString(nodeValueGetter(bNode)), @@ -59,11 +54,13 @@ export let compare = ( } return string } - sortingFunction = (aNode, bNode) => - naturalCompare( + sortingFunction = (aNode, bNode) => { + let formatString = getFormatStringFunc(!!options.ignoreCase) + return naturalCompare( prepareNumeric(formatString(nodeValueGetter(aNode))), prepareNumeric(formatString(nodeValueGetter(bNode))), ) + } } else { sortingFunction = (aNode, bNode) => { let aSize = aNode.size @@ -90,3 +87,11 @@ export let compare = ( return orderCoefficient * sortingFunction(a, b) } + +let getFormatStringFunc = (ignoreCase: boolean) => (value: string) => { + let valueToCompare = value + if (ignoreCase) { + valueToCompare = valueToCompare.toLowerCase() + } + return valueToCompare.replaceAll(/\s/g, '') +}