eslint-plugin-perfectionist
eslint-plugin-perfectionist copied to clipboard
Bug: sort-classes should consider initialization order
Describe the bug
If a class's properties include initializers, then perfectionist/sort-classes must consider references between those properties when sorting them.
Sorting property a before property z is incorrect if z references a in its initializer (see code sample below).
Code example
perfectionist/sort-classes reports an error on this class:
class C {
z = new Date();
a = this.z;
}
// Expected "a" to come before "z"
Autofixing the error "corrects" the class to this:
class C {
a = this.z;
// ~ Property 'z' is used before its initialization.ts(2729)
z = new Date();
}
But this has a type error and a will be undefined at runtime.
ESLint version
v8.56.0
ESLint Plugin Perfectionist version
v2.5.0
Additional comments
What's the correct behavior here? Ideal behavior would be to sort as usual while still preserving the initialization graph. Maybe slightly easier would be to preserve the relative order of any properties that reference another one in their initializer, or are referenced in an initializer.
Validations
- [X] Read the docs.
- [X] Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
I'm facing a similar issue in our codebase. @azat-io do you have any fix in the works? 😄
Thank you for your amazing work!! 🙏
I'll try to get on that early next week
Thank you for your issue!
This bug was fixed in 8c35a7dfbe9b841f86f303c4e9fd4170b47c9c0b and released in v3.0.0.
If you'd like, you can support the release with a retweet: https://x.com/azat_io_en/status/1815367279191761054