eslint-plugin-sort-class-members

raw JSON →
1.22.1 verified Sat Apr 25 auth: no javascript

An ESLint plugin that enforces consistent ordering of ES6 class members, with support for TypeScript interfaces, accessor pairs, decorators, and custom grouping via configuration. Current stable version is 1.22.1, released in 2024. Maintained actively with periodic updates. Key differentiators include fine-grained matching (by name, type, kind, static, decorator), built-in support for conventional private members, and compatibility with ESLint flat config. Unlike generic sorting rules, it allows complex order specifications and groups like event-handlers or everything-else.

error TypeError: Cannot read properties of undefined (reading 'getSourceCode')
cause Using an outdated version of the plugin with ESLint >=9 that removes context.getSourceCode().
fix
Update eslint-plugin-sort-class-members to v1.22.0 or later.
error ESLint configuration error: Configuration for rule "sort-class-members/sort-class-members" is invalid. Value [2, {...}] must be an array.
cause Using severity number 2 instead of string 'error' or 'warn' in flat config.
fix
Use severity as string: 'error' or 'warn'; for example: ["error", {...}].
error Parsing error: Unexpected token =
cause Class properties syntax not supported without a appropriate parser.
fix
Add parser: '@typescript-eslint/parser' or 'babel-eslint' to ESLint config.
error Error: Failed to load plugin 'sort-class-members' declared in '.eslintrc.json': Cannot find module 'eslint-plugin-sort-class-members'
cause Plugin not installed or not in node_modules.
fix
Run 'npm install eslint-plugin-sort-class-members --save-dev'.
gotcha Class properties require a custom parser like babel-eslint or @typescript-eslint/parser. Without it, properties may not be recognized and may cause false positives.
fix Use a parser that supports class properties: parser: '@typescript-eslint/parser' or 'babel-eslint'.
deprecated The `groups` configuration property referencing `[everything-else]` is deprecated; use a custom group or omit.
fix Define named groups explicitly in the `groups` object instead of relying on built-in 'everything-else'.
gotcha Using regex in the `name` property requires the string to start and end with `/`. If not, it is treated as an exact match.
fix Use '/pattern/' for regex matching; e.g., '/on.+/' matches names starting with 'on'.
breaking ESLint flat config support requires version >=1.20.0. Older versions do not have the `flat/recommended` config and will break when used with flat config.
fix Update the plugin to >=1.20.0 and use `import sortClassMembers from 'eslint-plugin-sort-class-members'; export default [sortClassMembers.configs['flat/recommended']]`.
breaking In ESLint >=9.0.0, flat config is the default; using `.eslintrc` files may cause issues. The plugin supports both.
fix Migrate to flat config if using ESLint >=9, or continue using legacy config with ESLint <9.
gotcha The `accessorPairPositioning` option can cause false positives if get/set pairs are not syntactically adjacent but logically belong together (e.g., in different sections).
fix Set `accessorPairPositioning` to 'any' to allow any ordering, or ensure pairs are placed according to chosen positioning.
npm install eslint-plugin-sort-class-members
yarn add eslint-plugin-sort-class-members
pnpm add eslint-plugin-sort-class-members

Shows installation, ESLint configuration with default order, and a valid class example using the default rule settings.

// Install: npm install eslint eslint-plugin-sort-class-members --save-dev

// .eslintrc.json
{
  "plugins": ["sort-class-members"],
  "rules": {
    "sort-class-members/sort-class-members": [
      "error",
      {
        "order": [
          "[static-properties]",
          "[static-methods]",
          "[properties]",
          "[conventional-private-properties]",
          "constructor",
          "[methods]",
          "[conventional-private-methods]"
        ],
        "accessorPairPositioning": "getThenSet"
      }
    ]
  }
}

// Valid class (default order)
class Example {
  static myStaticProp = 1;
  static myStaticMethod() {}
  myProp = 2;
  #privateProp = 3;
  constructor() {}
  myMethod() {}
  #privateMethod() {}
}