Sorting class members by type
This feature allows you to define a specific order for class members, such as static properties, static methods, instance properties, the constructor, and instance methods. The provided code sample configures ESLint to enforce this order.
module.exports = {
"plugins": ["sort-class-members"],
"rules": {
"sort-class-members/sort-class-members": [
2,
{
"order": [
"[static-properties]",
"[static-methods]",
"[properties]",
"constructor",
"[methods]"
],
"accessorPairPositioning": "getThenSet"
}
]
}
};
Grouping class members by visibility
This feature allows you to group class members by their visibility (public, protected, private) and whether they are static or instance members. The provided code sample configures ESLint to enforce this grouping.
module.exports = {
"plugins": ["sort-class-members"],
"rules": {
"sort-class-members/sort-class-members": [
2,
{
"order": [
"[public-static-properties]",
"[protected-static-properties]",
"[private-static-properties]",
"[public-static-methods]",
"[protected-static-methods]",
"[private-static-methods]",
"[public-properties]",
"[protected-properties]",
"[private-properties]",
"constructor",
"[public-methods]",
"[protected-methods]",
"[private-methods]"
],
"groups": {
"public-static-properties": [{"name": "^publicStatic[A-Z]", "type": "property", "static": true, "visibility": "public"}],
"protected-static-properties": [{"name": "^protectedStatic[A-Z]", "type": "property", "static": true, "visibility": "protected"}],
"private-static-properties": [{"name": "^privateStatic[A-Z]", "type": "property", "static": true, "visibility": "private"}],
"public-static-methods": [{"name": "^publicStatic[A-Z]", "type": "method", "static": true, "visibility": "public"}],
"protected-static-methods": [{"name": "^protectedStatic[A-Z]", "type": "method", "static": true, "visibility": "protected"}],
"private-static-methods": [{"name": "^privateStatic[A-Z]", "type": "method", "static": true, "visibility": "private"}],
"public-properties": [{"name": "^public[A-Z]", "type": "property", "visibility": "public"}],
"protected-properties": [{"name": "^protected[A-Z]", "type": "property", "visibility": "protected"}],
"private-properties": [{"name": "^private[A-Z]", "type": "property", "visibility": "private"}],
"public-methods": [{"name": "^public[A-Z]", "type": "method", "visibility": "public"}],
"protected-methods": [{"name": "^protected[A-Z]", "type": "method", "visibility": "protected"}],
"private-methods": [{"name": "^private[A-Z]", "type": "method", "visibility": "private"}]
}
}
]
}
};