ESLint plugin for sorting various data such as objects, imports, types, enums, JSX props, etc.
npm install eslint-plugin-perfectionist src="https://raw.githubusercontent.com/azat-io/eslint-plugin-perfectionist/main/docs/public/logo.svg"
alt="ESLint Plugin Perfectionist logo"
align="right"
height="160"
width="160"
/>




An ESLint plugin that sets rules to format your code and make it consistent.
This plugin defines rules for sorting various data, such as objects, imports,
TypeScript types, enums, JSX props, Svelte attributes, etc. alphabetically,
naturally, or by line length.
All rules are automatically fixable. It's safe!
Sorting imports and properties in software development offers numerous benefits:
- Readability: Finding declarations in a sorted, large list is a little
faster. Remember that you read the code much more often than you write it.
- Maintainability: Sorting imports and properties is considered a good
practice in software development, contributing to code quality and consistency
across the codebase.
- Code Review and Collaboration: If you set rules that say you can only do
things one way, no one will have to spend time thinking about how to do it.
- Code Uniformity: When all code looks exactly the same, it is very hard to
see who wrote it, which makes achieving the lofty goal of _collective code
ownership_ easier.
- Aesthetics: This not only provides functional benefits, but also gives the
code an aesthetic appeal, visually pleasing and harmonious structure. Take
your code to a beauty salon!
See docs.
media="(prefers-color-scheme: light)"
/>
media="(prefers-color-scheme: dark)"
/>
src="https://raw.githubusercontent.com/azat-io/eslint-plugin-perfectionist/main/docs/public/examples/example-alphabetical-light.webp"
alt="ESLint Plugin Perfectionist alphabetical usage example"
/>
media="(prefers-color-scheme: light)"
/>
media="(prefers-color-scheme: dark)"
/>
src="https://raw.githubusercontent.com/azat-io/eslint-plugin-perfectionist/main/docs/public/examples/example-line-length-light.webp"
alt="ESLint Plugin Perfectionist line length usage example"
/>
You'll first need to install ESLint v8.45.0 or greater:
``sh`
npm install --save-dev eslint
Next, install eslint-plugin-perfectionist:
`sh`
npm install --save-dev eslint-plugin-perfectionist
Add eslint-plugin-perfectionist to the plugins section of the ESLint
configuration file and define the list of rules you will use.
`js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-imports': [
'error',
{
type: 'natural',
order: 'asc',
},
],
},
},
]
`
`js`
module.exports = {
plugins: [
'perfectionist',
],
rules: {
'perfectionist/sort-imports': [
'error',
{
type: 'natural',
order: 'asc',
}
]
}
}
The easiest way to use eslint-plugin-perfectionist is to use ready-made
configs. Config files use all the rules of the current plugin, but you can
override them.
`js
import perfectionist from 'eslint-plugin-perfectionist'
export default [
perfectionist.configs['recommended-natural'],
]
`
`js`
module.exports = {
extends: [
'plugin:perfectionist/recommended-natural-legacy',
],
}
| Name | Description |
| :------------------------------------------------------------------------------------- | :--------------------------------------------------------------- |
| recommended-alphabetical | All plugin rules with alphabetical sorting in ascending order |
| recommended-natural | All plugin rules with natural sorting in ascending order |
| recommended-line-length | All plugin rules with sorting by line length in descending order |
| recommended-custom | All plugin rules with sorting by your own custom order |
š§ Automatically fixable by the
--fix CLI option.
| Name | Description | š§ |
| :--------------------------------------------------------------------------------------- | :-------------------------------------------- | :-- |
| sort-array-includes | Enforce sorted arrays before include method | š§ |
| sort-classes | Enforce sorted classes | š§ |
| sort-decorators | Enforce sorted decorators | š§ |
| sort-enums | Enforce sorted TypeScript enums | š§ |
| sort-export-attributes | Enforce sorted export attributes | š§ |
| sort-exports | Enforce sorted exports | š§ |
| sort-heritage-clauses | Enforce sorted implements/extends` clauses | š§ |
| sort-import-attributes | Enforce sorted import attributes | š§ |
| sort-imports | Enforce sorted imports | š§ |
| sort-interfaces | Enforce sorted interface properties | š§ |
| sort-intersection-types | Enforce sorted intersection types | š§ |
| sort-jsx-props | Enforce sorted JSX props | š§ |
| sort-maps | Enforce sorted Map elements | š§ |
| sort-modules | Enforce sorted modules | š§ |
| sort-named-exports | Enforce sorted named exports | š§ |
| sort-named-imports | Enforce sorted named imports | š§ |
| sort-object-types | Enforce sorted object types | š§ |
| sort-objects | Enforce sorted objects | š§ |
| sort-sets | Enforce sorted Set elements | š§ |
| sort-switch-case | Enforce sorted switch case statements | š§ |
| sort-union-types | Enforce sorted union types | š§ |
| sort-variable-declarations | Enforce sorted variable declarations | š§ |
Yes. To do this, you need to enable autofix in ESLint when you save the file in
your editor. You may find instructions for your editor
here.
Overall, yes. We want to make sure that the work of the plugin does not
negatively affect the behavior of the code. For example, the plugin takes into
account spread operators in JSX and objects, comments to the code. Safety is our
priority. If you encounter any problem, you can create an
issue.
I love Prettier. However, this is not its area of responsibility. Prettier is
used for formatting, and ESLint for styling. For example, changing the order of
imports can affect how the code works (console.log calls, fetch, style loading).
Prettier should not change the AST. There is a cool article about this:
"The Blurry Line Between Formatting and Style"
by @joshuakgoldberg.
This plugin is following Semantic Versioning and
ESLint's Semantic Versioning Policy.
MIT © Azat S.