Skip to content

Commit

Permalink
fix: fix sorting class overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Sep 5, 2023
1 parent f857b80 commit c4939b4
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
let rightNum = getGroupNumber(options.groups, right)

if (
leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options))
left.name !== right.name &&
(leftNum > rightNum ||
(leftNum === rightNum && compare(left, right, options)))
) {
context.report({
messageId: 'unexpectedClassesOrder',
Expand Down
93 changes: 93 additions & 0 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,37 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): sorts class with ts index signatures`,
rule,
{
valid: [
{
code: dedent`
class Decorations {
setBackground(color: number, hexFlag: boolean): this
setBackground(color: Color | string | CSSColor): this
setBackground(r: number, g: number, b: number, a?: number): this
setBackground(color: ColorArgument, arg1?: boolean | number, arg2?: number, arg3?: number): this {
/* ... */
}
}
`,
options: [
{
...options,
groups: [
['static-property', 'private-property', 'property'],
'constructor',
],
},
],
},
],
invalid: [],
},
)
})

describe(`${RULE_NAME}: sorting by natural order`, () => {
Expand Down Expand Up @@ -643,6 +674,37 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): sorts class with ts index signatures`,
rule,
{
valid: [
{
code: dedent`
class Decorations {
setBackground(color: number, hexFlag: boolean): this
setBackground(color: Color | string | CSSColor): this
setBackground(r: number, g: number, b: number, a?: number): this
setBackground(color: ColorArgument, arg1?: boolean | number, arg2?: number, arg3?: number): this {
/* ... */
}
}
`,
options: [
{
...options,
groups: [
['static-property', 'private-property', 'property'],
'constructor',
],
},
],
},
],
invalid: [],
},
)
})

describe(`${RULE_NAME}: sorting by line length`, () => {
Expand Down Expand Up @@ -956,6 +1018,37 @@ describe(RULE_NAME, () => {
],
},
)

ruleTester.run(
`${RULE_NAME}(${type}): sorts class with ts index signatures`,
rule,
{
valid: [
{
code: dedent`
class Decorations {
setBackground(color: number, hexFlag: boolean): this
setBackground(color: Color | string | CSSColor): this
setBackground(r: number, g: number, b: number, a?: number): this
setBackground(color: ColorArgument, arg1?: boolean | number, arg2?: number, arg3?: number): this {
/* ... */
}
}
`,
options: [
{
...options,
groups: [
['static-property', 'private-property', 'property'],
'constructor',
],
},
],
},
],
invalid: [],
},
)
})

describe(`${RULE_NAME}: misc`, () => {
Expand Down

0 comments on commit c4939b4

Please sign in to comment.