sort-classes
💼 This rule is enabled in the following configs: recommended-alphabetical
, recommended-line-length
, recommended-natural
.
🔧 This rule is automatically fixable by the --fix
CLI option.
📖 Rule Details
Enforce sorted class members. By enforcing a consistent order, this rule improves code readability and maintainability. It helps developers quickly locate class members and understand the structure of the class.
Class members that are not sorted in a certain order can cause confusion and reduce code readability.
💡 Examples
// ❌ Incorrect
class Rectangle {
get area() {
return this.calcArea()
}
calcPerimeter() {
return this.height * 2 + this.width * 2
}
calcArea() {
return this.height * this.width
}
constructor(height, width) {
this.height = height
this.width = width
}
}
// ✅ Correct
class Rectangle {
constructor(height, width) {
this.height = height
this.width = width
}
get area() {
return this.calcArea()
}
calcArea() {
return this.height * this.width
}
calcPerimeter() {
return this.height * 2 + this.width * 2
}
}
// ❌ Incorrect
class Rectangle {
get area() {
return this.calcArea()
}
calcPerimeter() {
return this.height * 2 + this.width * 2
}
calcArea() {
return this.height * this.width
}
constructor(height, width) {
this.height = height
this.width = width
}
}
// ✅ Correct
class Rectangle {
constructor(height, width) {
this.height = height
this.width = width
}
calcPerimeter() {
return this.height * 2 + this.width * 2
}
calcArea() {
return this.height * this.width
}
get area() {
return this.calcArea()
}
}
🔧 Options
This rule accepts an options object with the following properties:
type Group =
| 'static-private-method'
| 'private-property'
| 'static-property'
| 'index-signature'
| 'private-method'
| 'static-method'
| 'constructor'
| 'get-method'
| 'set-method'
| 'property'
| 'unknown'
| 'method'
interface Options {
type?: 'alphabetical' | 'natural' | 'line-length'
order?: 'asc' | 'desc'
'ignore-case'?: boolean
groups?: (Group | Group[])[]
}
type
(default: 'alphabetical'
)
alphabetical
- sort alphabetically.natural
- sort in natural order.line-length
- sort by code line length.
order
(default: 'asc'
)
asc
- enforce properties to be in ascending order.desc
- enforce properties to be in descending order.
ignore-case
(default: false
)
Only affects alphabetical and natural sorting. When true
the rule ignores the case-sensitivity of the order.
groups
(default: ['property', 'constructor', 'method', 'unknown']
)
You can set up a list of class members groups for sorting. Groups can be combined.
If you use one of the configs exported by this plugin, you get the following import grouping settings:
{
"groups": [
"index-signature",
"static-property",
"private-property",
"property",
"constructor",
"static-method",
"private-method",
"static-private-method",
"method",
["get-method", "set-method"],
"unknown"
]
}
⚙️ Usage
// .eslintrc
{
"plugins": ["perfectionist"],
"rules": {
"perfectionist/sort-classes": [
"error",
{
"type": "natural",
"order": "asc",
"groups": [
"index-signature",
"static-property",
"private-property",
"property",
"constructor",
"static-method",
"private-method",
"method"
]
}
]
}
}
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-classes': [
'error',
{
type: 'natural',
order: 'asc',
groups: [
'index-signature',
'static-property',
'private-property',
'property',
'constructor',
'static-method',
'private-method',
'method',
],
},
],
},
},
]
🚀 Version
This rule was introduced in v0.11.0.