8000 Rule proposal: Sort Enum And Interface Keys · Issue #4008 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
8000

Rule proposal: Sort Enum And Interface Keys #4008

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

Closed
PodaruDragos opened this issue Oct 14, 2021 · 4 comments
Closed

Rule proposal: Sort Enum And Interface Keys #4008

PodaruDragos opened this issue Oct 14, 2021 · 4 comments
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@PodaruDragos
Copy link

Description

It would be great it a rule existed where interfaces and enum keys can be sorted.
If this is not considered a thing that might be added, feel free to close this issue

Fail

  interface A {
    b: string;
    a: string;
  }
  
  // eg: Expected a to come before b
  

Pass

interface A {
  a: string;
  b: string;
}
@PodaruDragos PodaruDragos added enhancement: new base rule extension New base rule extension required to handle a TS specific case package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Oct 14, 2021
@bradzacher
Copy link
Member

this should really be two separate rules: sort type properties and sort enum members.

Why?

  • interface/object type properties are always safe to auto-sort.
  • interface/object type properties will only ever be sorted by key name.

otoh

  • enums are not always safe to auto-sort due to implicit enum values
  • enums might want to be sorted by key or by value.

eg this is unsafe to auto-sort:

enum Foo {
  B,
  A,
}

Foo.B === 1 /// === true

///// after sort /////

enum Foo {
  A,
  B,
}

Foo.A === 1 /// === false

Other example of how you might want to sort an enum:

// sorted by value
enum Foo {
  CLIArgFoo = 1,
  CLIArgBar = 2,
  CLIArgBaz = 3,
}
// sorted by key
enum Foo {
  CLIArgBar = 2,
  CLIArgBaz = 3,
  CLIArgFoo = 1,
}

@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look enhancement: new base rule extension New base rule extension required to handle a TS specific case labels Oct 18, 2021
@PodaruDragos
Copy link
Author

That's definitely better, yes,
Thank you.

@JoshuaKGoldberg JoshuaKGoldberg added the accepting prs Go ahead, send a pull request that resolves this issue label Oct 25, 2021
@rubiesonthesky
Copy link
Contributor

I stumbled upon this eslint plugin which seems to take care of these cases https://github.com/azat-io/eslint-plugin-perfectionist Note that I have not tested this plugin myself.

Since ts-eslint tries to shy away from stylistic rules, is these rules still something that project wants?

@JoshuaKGoldberg
Copy link
Member

Aha, I've been using that plugin in http://github.com/JoshuaKGoldberg/template-typescript-node-package. It's great. cc @azat-io 😄

Let's close this out, yes. Per https://typescript-eslint.io/linting/troubleshooting/formatting we're trying not to take on additional responsibility around stylistic concerns. And the plugin does a great job on its own. Thanks for the spot!

@JoshuaKGoldberg JoshuaKGoldberg added wontfix This will not be worked on and removed accepting prs Go ahead, send a pull request that resolves this issue labels Jul 27, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

4 participants
0