From b4ee1cce0176ab00446bf2d06cb5caa7fd8b28a4 Mon Sep 17 00:00:00 2001 From: Hugo <60015232+hugop95@users.noreply.github.com> Date: Fri, 13 Dec 2024 00:31:08 +0100 Subject: [PATCH] fix(sort-interfaces): prevent constructor declarations from being sorted --- rules/sort-object-types.ts | 11 ++++---- test/sort-interfaces.test.ts | 34 +++++++++++++++++++++-- test/sort-object-types.test.ts | 51 ++++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 8 deletions(-) diff --git a/rules/sort-object-types.ts b/rules/sort-object-types.ts index cbd5d6903..3f6885cfa 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 } @@ -240,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 { 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`, () => {