brandongregoryscott / eslint-plugin-collation

ESLint plugin for making your code easier to read, with autofix and TypeScript support
https://eslint-plugin-collation.brandonscott.me
Apache License 2.0
4 stars 0 forks source link

Alphabetize enum members by key #1

Closed brandongregoryscott closed 2 years ago

brandongregoryscott commented 2 years ago

Write a rule to alphabetize enum members by their key. This should handle both string-based enums and explicit numeric enums. Implicit numeric enums get a little tricky, because there might be code that is depending on their values which are generated by the current order (i.e. enums that are used in a database table)

String-based

enum Animals {
    Dog = "dog",
    Wolf = "wolf",
    Cat = "cat",
}

will be transformed to:

enum Animals {
    Cat = "cat",
    Dog = "dog",
    Wolf = "wolf",
}

Explicitly numeric

enum SortOrder {
    DESC = 0,
    ASC = 1,
}

will be transformed to:

enum SortOrder {
    ASC = 1,
    DESC = 0,
}

Implicitly numeric

For now, we can skip out on transforming implicit enums, but in the future, it might be nice to have an option that converts the enum to their explicit numeric values and then alphabetizes them, for example:

enum Animals {
    Dog,
    Wolf,
    Cat,
}

will first be transformed to:

enum Animals {
    Dog = 0,
    Wolf = 1,
    Cat = 2,
}

and then transformed to:

enum Animals {
    Cat = 2,
    Dog = 0,
    Wolf = 1,
}