Skip to content

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

js
// ❌ 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
  }
}
js
// ❌ 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:

ts
type Group =
  | 'private-decorated-accessor-property'
  | 'decorated-accessor-property'
  | 'private-decorated-property'
  | 'static-private-method'
  | 'decorated-set-method'
  | 'decorated-get-method'
  | 'decorated-property'
  | 'decorated-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:

json
{
  "groups": [
    "index-signature",
    "static-property",
    "private-property",
    "property",
    "constructor",
    "static-method",
    "private-method",
    "static-private-method",
    "method",
    ["get-method", "set-method"],
    "unknown"
  ]
}

⚙️ Usage

json
// .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"
        ]
      }
    ]
  }
}
js
// 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.

📚 Resources

Released under the MIT License