azat-io / eslint-plugin-perfectionist

☂️ ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://perfectionist.dev
MIT License
1.83k stars 37 forks source link

Bug(sort-classes): Unattended sorting for method as arrow function #236

Open jerome-benoit opened 2 weeks ago

jerome-benoit commented 2 weeks ago

Describe the bug

Class with methods as arrow function do not seem to be sorted as part of the class methods.

Code example

I have not yet identified the minimum code to extract, but if you look at that class: https://github.com/SAP/e-mobility-charging-stations-simulator/blob/main/src/charging-station/Bootstrap.ts:

ESLint version

9.9.1

ESLint Plugin Perfectionist version

3.3.0

Additional comments

No response

Validations

hugop95 commented 2 weeks ago

This is the expected behavior in today's version: the function-property selector is not part of any of the default groups, so those properties get matched with the property selector instead.

Changing your configuration to regroup method and function-property together get the result you expect.

Example:

 'groups': [
              "constructor",
              [
                "public-method",
                "public-function-property"
              ],
              [
                "protected-method",
                "protected-function-property"
              ],
              [
                "private-method",
                "private-function-property"
              ],
              [
                "public-static-method",
                "public-static-function-property"
              ],
              [
                "protected-static-method",
                "protected-static-function-property"
              ],
              [
                "private-static-method",
                "private-static-function-property"
              ],
              ...

In my opinion, it makes sense to regroup methods and function properties together, so maybe we can change the default options regarding this?

jerome-benoit commented 2 weeks ago

I fully agree with your proposal to group and sort methods and functions in classes in default configurations.

The rationale being that using arrow function in class method is an implementation detail for the same primitive and it should not impact the class sorting of identical primitives. Since it's a breaking change, is there an easy to just extends the existing default configuration of sort-classes with just different grouping?

hugop95 commented 2 weeks ago

The default groups configuration itself is not extendable so I don't think that what you request is possible today: you need to copy the default groups configuration and make the changes afterward.

Changing the default configuration will indeed affect all users using the default grouping. I don't know if in practice it is that big of an issue. Until recently, static blocks and accessor properties were put at the bottom of the file by default, so I assume a lot of users either have their own custom groups configuration or would understand the idea behind grouping methods and function properties together.