Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sort-interfaces): prevents constructor declarations from being sorted #415

Merged
merged 2 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading