solid_lints icon indicating copy to clipboard operation
solid_lints copied to clipboard

Add rule to sort class methods by their modifiers and usage in other methods

Open solid-danylosafonov opened this issue 3 years ago • 4 comments

There are already rules that enforce following order:

  1. attributes
  2. getters/setters
  3. constructor
  4. methods

I propose to make it stricter and enforce order of methods:

  • attributes
  • public getters/setters
  • private getters/setters?
  • constructor
  • overrides
  • public methods*
  • private methods*

* - can go above in order if they go just after method they are used in

BAD:

_privateMethod() {
  ...
}

publicMethod() {
  ...
}

@override
overwritenMethod() {
  ...
}

GOOD:

@override
overwritenMethod() {
  ...
}

publicMethod() {
  ...
}

_privateMethod() {
  ...
}

GOOD:

@override
overwritenMethod() {
  ...
  _privateMethod();
  ...
}

_privateMethod() {
  ...
}

publicMethod() {
  ...
}

solid-danylosafonov avatar Oct 10 '22 13:10 solid-danylosafonov

That would require a custom rule, correct?

illia-romanenko avatar May 11 '23 18:05 illia-romanenko

There is an updated dcm functionality that I believe allows doing this

ruslan-rudenko-solid avatar May 15 '23 14:05 ruslan-rudenko-solid

It probably doesn't cover this case:

    • can go above in order if they go just after method they are used in

illia-romanenko avatar May 15 '23 16:05 illia-romanenko

It probably doesn't cover this case:

can go above in order if they go just after method they are used in

Yes, it doesn't seem to, but it allows to enforce privates/overrides, but I guess it's too strict without allowing the mixed order depending on usage?

That would require a custom rule, correct?

More like a variant of existing one, but yes

danylo-safonov-solid avatar May 16 '23 07:05 danylo-safonov-solid