ESLint Plugin Perfectionist
Sorting in ESLint used to be split across several plugins: one for imports, another for object keys, a third for JSX props. Each had its own settings. I wanted one plugin for all sorting, so I wrote Perfectionist.
Today it sorts imports, objects, types, JSX props, classes, and much more. Every rule fixes the code automatically.
Before:
let config = {
port: 8080,
// Use HTTPS in production
https: true,
host: 'localhost',
}After:
let config = {
host: 'localhost',
// Use HTTPS in production
https: true,
port: 8080,
}It’s important that sorting doesn’t break anything. A comment moves together with its line, and keys never jump over a ...spread, because their order decides which value ends up in the object.
You choose the sort order: alphabetical, natural (item2 before item10), by line length, or your own. I sort by line length myself, so my imports form a staircase.
How to start using it?
Install ESLint and the plugin:
npm install --save-dev eslint eslint-plugin-perfectionistAdd one of the ready-made configs to eslint.config.js, for example the one with natural sorting:
import perfectionist from 'eslint-plugin-perfectionist'
export default [perfectionist.configs['recommended-natural']]At first, the linter will show a lot of errors. That’s fine: run ESLint with the --fix flag, and the plugin will sort everything for you.
All rules and their options are documented at perfectionist.dev.