eslint-plugin-perfectionist icon indicating copy to clipboard operation
eslint-plugin-perfectionist copied to clipboard

feat: adds @Decorator related sorting rules

Open hugop95 opened this issue 4 months ago • 0 comments

Description

A rule to sort decorators exists here: https://github.com/ElsiKora/ESLint-Plugin-Sort-Decorators.

However, it doesn't benefit from the eslint-plugin-perfectionist options ecosystem (possibility to group elements, add custom groups, partition by comment, etc.).

Decorators order may sometimes matter (see section below), so giving users the option to keep that order in those cases is necessary.

Additionally, this implementation has some differences related to what we consider an accessor decorator to be (explained below as well).

Changes

This PR adds 5 new decorator-related rules:

  • sort-class-decorators
  • sort-property-decorators
  • sort-method-decorators
  • sort-parameter-decorators
  • sort-accessor-decorators

Options

All these rules have the same options: their logic is exactly the same, but applied on different elements.

  • type
  • order
  • ignoreCase
  • groups
  • customGroups
  • partitionByComment
  • matcher

partitionByNewLine was not added as I don't think there is a use case in separating decorators with a new line. This can be easily added if necessary.

There is at the moment only one predefined group: unknown.

Errors

All rules have 2 errors each:

  • When two adjacent decorators do not belong to the same group and are unordered, with a more detailed error.
  • When two adjacent decorators belong to the same group but are unordered.

sort-accessor-decorators

Works on auto-accessors, not getters. Getters/setters are affected by sort-method-decorators.

⚠️ Should this be included in recommended configs?

Decorators are evaluated from top to bottom. While unusual, decorator order may matter in some projects. Activating these rules without proper configuration CAN cause runtime errors.

At the moment, there are 2 other rules that may change behavior unexpectedly through sorting and that are present in recommended configs:

  • sort-maps
  • sort-sets

(If users depend on insertion order, although it is unusual).

I think that if we choose not to include this rule into recommended, then both of the above rules should not be included as well.

Remaining to do

sort-class-decorators

  • [ ] Add tests
  • [ ] Add documentation

sort-method-decorators

  • [ ] Add tests
  • [ ] Add documentation

sort-property-decorators

  • [ ] Add tests
  • [ ] Add documentation

sort-parameter-decorators

  • [ ] Add tests
  • [ ] Add documentation

sort-accessor-decorators

  • [ ] Add tests
  • [ ] Add documentation

What is the purpose of this pull request?

  • [x] New Feature
  • [x] Documentation update

hugop95 avatar Oct 05 '24 16:10 hugop95