From 8aff66dd9c14891c75f951de31dbab5daf3b8b55 Mon Sep 17 00:00:00 2001 From: "hugo.prunaux" Date: Thu, 12 Dec 2024 21:38:59 +0100 Subject: [PATCH 1/2] fix: prevents constructor declarations from being sorted --- rules/sort-object-types.ts | 5 +++- test/sort-interfaces.test.ts | 34 +++++++++++++++++++++-- test/sort-object-types.test.ts | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 4 deletions(-) diff --git a/rules/sort-object-types.ts b/rules/sort-object-types.ts index cbd5d6903..832b8c216 100644 --- a/rules/sort-object-types.ts +++ b/rules/sort-object-types.ts @@ -210,7 +210,10 @@ export let sortObjectTypeElements = ({ let formattedMembers: SortObjectTypesSortingNode[][] = elements.reduce( (accumulator: SortObjectTypesSortingNode[][], typeElement) => { - if (typeElement.type === 'TSCallSignatureDeclaration') { + if ( + typeElement.type === 'TSCallSignatureDeclaration' || + typeElement.type === 'TSConstructSignatureDeclaration' + ) { accumulator.push([]) return accumulator } diff --git a/test/sort-interfaces.test.ts b/test/sort-interfaces.test.ts index 3c7952078..6acd0ef0d 100644 --- a/test/sort-interfaces.test.ts +++ b/test/sort-interfaces.test.ts @@ -477,7 +477,7 @@ describe(ruleName, () => { ) ruleTester.run( - `${ruleName}(${type}): not sorts call signature declarations`, + `${ruleName}(${type}): does not sort call signature declarations`, rule, { valid: [ @@ -500,6 +500,34 @@ describe(ruleName, () => { }, ) + ruleTester.run( + `${ruleName}(${type}): does not sort constructor declarations`, + rule, + { + valid: [ + { + code: dedent` + interface Interface { + new (value: number | string): number; + new (value: number): unknown; + } + `, + options: [options], + }, + { + code: dedent` + interface Interface { + new (value: number): unknown; + new (value: number | string): number; + } + `, + options: [options], + }, + ], + invalid: [], + }, + ) + ruleTester.run( `${ruleName}(${type}): sorts complex predefined groups`, rule, @@ -2462,7 +2490,7 @@ describe(ruleName, () => { ) ruleTester.run( - `${ruleName}(${type}): not sorts call signature declarations`, + `${ruleName}(${type}): does not sort call signature declarations`, rule, { valid: [ @@ -3239,7 +3267,7 @@ describe(ruleName, () => { ) ruleTester.run( - `${ruleName}(${type}): not sorts call signature declarations`, + `${ruleName}(${type}): does not sort call signature declarations`, rule, { valid: [ diff --git a/test/sort-object-types.test.ts b/test/sort-object-types.test.ts index 8c8709f0f..52f347e84 100644 --- a/test/sort-object-types.test.ts +++ b/test/sort-object-types.test.ts @@ -1834,6 +1834,57 @@ describe(ruleName, () => { valid: [], }, ) + ruleTester.run( + `${ruleName}(${type}): does not sort call signature declarations`, + rule, + { + valid: [ + { + code: dedent` + type Type = { + >( + input: AFunction<[Parameters], string> + ): Alternatives + (input: Input): AFunction< + [number], + A[keyof A] + > + } + `, + options: [options], + }, + ], + invalid: [], + }, + ) + + ruleTester.run( + `${ruleName}(${type}): does not sort constructor declarations`, + rule, + { + valid: [ + { + code: dedent` + type Type = { + new (value: number | string): number; + new (value: number): unknown; + } + `, + options: [options], + }, + { + code: dedent` + type Type = { + new (value: number): unknown; + new (value: number | string): number; + } + `, + options: [options], + }, + ], + invalid: [], + }, + ) }) describe(`${ruleName}: sorting by natural order`, () => { From e1b5e4d726f55852dbaaa542da07dec33ab51966 Mon Sep 17 00:00:00 2001 From: "hugo.prunaux" Date: Thu, 12 Dec 2024 21:41:03 +0100 Subject: [PATCH 2/2] refactor: removes useless condition * Reported by ESLint. * Condition now useless as we now filter on the last possible type: `TSConstructSignatureDeclaration` --- rules/sort-object-types.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rules/sort-object-types.ts b/rules/sort-object-types.ts index 832b8c216..3f6885cfa 100644 --- a/rules/sort-object-types.ts +++ b/rules/sort-object-types.ts @@ -243,10 +243,8 @@ export let sortObjectTypeElements = ({ name = formatName( sourceCode.text.slice(typeElement.range.at(0), endIndex), ) - } else if ( - typeElement.type === 'TSMethodSignature' && - 'name' in typeElement.key - ) { + } else if ('name' in typeElement.key) { + // TSMethodSignature ;({ name } = typeElement.key) /* v8 ignore next 8 - Unsure if we can reach it */ } else {