Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: sort-classes should consider initialization order #102

Open
2 tasks done
danvk opened this issue Jan 20, 2024 · 2 comments
Open
2 tasks done

Bug: sort-classes should consider initialization order #102

danvk opened this issue Jan 20, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@danvk
Copy link

danvk commented Jan 20, 2024

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

  • Read the docs.
  • Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
@Tragio
Copy link

Tragio commented Mar 12, 2024

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!! 🙏

@azat-io
Copy link
Owner

azat-io commented Mar 13, 2024

I'll try to get on that early next week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants