Skip to content

Commit

Permalink
fix(sort-interfaces): prevent constructor declarations from being sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Dec 12, 2024
1 parent 648cf89 commit b4ee1cc
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 8 deletions.
11 changes: 6 additions & 5 deletions rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ export let sortObjectTypeElements = <MessageIds extends string>({

let formattedMembers: SortObjectTypesSortingNode[][] = elements.reduce(
(accumulator: SortObjectTypesSortingNode[][], typeElement) => {
if (typeElement.type === 'TSCallSignatureDeclaration') {
if (
typeElement.type === 'TSCallSignatureDeclaration' ||
typeElement.type === 'TSConstructSignatureDeclaration'
) {
accumulator.push([])
return accumulator
}
Expand Down Expand Up @@ -240,10 +243,8 @@ export let sortObjectTypeElements = <MessageIds extends string>({
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 {
Expand Down
34 changes: 31 additions & 3 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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,
Expand Down Expand Up @@ -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: [
Expand Down Expand Up @@ -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: [
Expand Down
51 changes: 51 additions & 0 deletions test/sort-object-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,57 @@ describe(ruleName, () => {
valid: [],
},
)
ruleTester.run(
`${ruleName}(${type}): does not sort call signature declarations`,
rule,
{
valid: [
{
code: dedent`
type Type = {
<Parameters extends Record<string, number | string>>(
input: AFunction<[Parameters], string>
): Alternatives<Parameters>
<A extends CountA>(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`, () => {
Expand Down

0 comments on commit b4ee1cc

Please sign in to comment.