azat-io / eslint-plugin-perfectionist

🦄 ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
https://eslint-plugin-perfectionist.azat.io
MIT License
1.64k stars 29 forks source link

Feature: (sort-classes) Add custom-groups to sorting capabilities #145

Open Bengejd opened 3 weeks ago

Bengejd commented 3 weeks ago

What rule do you want to change?

perfectionist/sort-classes

Describe the problem

It would be nice if we could include custom groups similar to perfectionist/sort-objects. Mainly the issue that I am trying to solve is ensuring that Angular Dependency Injections are sorted to the top of the class they are being injected in.

Code example

Which used to look like this before the inject syntax came out:

export class ExampleComponent {
  public user = this.authService.user;

  constructor(private readonly authService: AuthService) {
  }
}

Becomes something like this:

export class ExampleComponent {
  public user = this.authService.user;
  private readonly authService = inject(AuthService);
}

However, this leads to an error similar to #118. If you are accessing the injected dependency before initialization (due to it's sorting order in the component).

Ideally, it would become this instead:

export class ExampleComponent {
  private readonly authService = inject(AuthService);
  public user = this.authService.user;

}

Additional comments

While this issue is Angular specific, I think the implications of being able to create a custom group would solve this issue, as well as other issues that users could potentially face down the line. This would also help prevent a growing complexity when it comes to adding additional groups to the sorter.

Validations