Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

eslint-plugin-perfectionist-unofficial

azat-io20MIT2.0.0

Custom version of eslint-plugin-perfectionist (2.0, pre-release)

eslint, eslint-plugin, perfectionist

readme

ESLint Plugin Perfectionist

ESLint Plugin Perfectionist logo

Version GitHub license

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!

🦄 Why

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, then 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 the beauty salon!

📖 Documentation

See docs.

ESLint Plugin Perfectionist usage example

💿 Installation

You'll first need to install ESLint:

npm install --save-dev eslint

Next, install eslint-plugin-perfectionist:

npm install --save-dev eslint-plugin-perfectionist

🚀️️️️ Usage

Add eslint-plugin-perfectionist to the plugins section of the ESLint configuration file and define the list of rules you will use.

Legacy Config (.eslintrc)

{
  "plugins": [
    "perfectionist"
  ],
  "rules": {
    "perfectionist/sort-objects": [
      "error",
      {
        "type": "natural",
        "order": "asc"
      }
    ]
  }
}

Flat Config (eslint.config.js) (requires eslint >= v8.23.0)

import perfectionist from 'eslint-plugin-perfectionist'

export default [
  {
    plugins: {
      perfectionist,
    },
    rules: {
      'perfectionist/sort-objects': [
        'error',
        {
          type: 'natural',
          order: 'asc',
        },
      ],
    },
  },
]

⚙️ Configs

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.

Legacy Config (.eslintrc)

{
  "extends": [
    "plugin:perfectionist/recommended-natural"
  ]
}

Flat Config (eslint.config.js)

import perfectionistNatural from 'eslint-plugin-perfectionist/configs/recommended-natural'

export default [
  perfectionistNatural,
]

List of Configs

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

✅ Rules

🔧 Automatically fixable by the --fix CLI option.

Name Description 🔧
sort-array-includes enforce sorted arrays before include method 🔧
sort-astro-attributes enforce sorted Astro attributes 🔧
sort-classes enforce sorted classes 🔧
sort-enums enforce sorted TypeScript enums 🔧
sort-exports enforce sorted exports 🔧
sort-imports enforce sorted imports 🔧
sort-interfaces enforce sorted interface properties 🔧
sort-jsx-props enforce sorted JSX props 🔧
sort-maps enforce sorted Map elements 🔧
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-svelte-attributes enforce sorted Svelte attributes 🔧
sort-union-types enforce sorted union types 🔧

⁉️ FAQ

Can I automatically fix problems in the editor?

Yes. To do this, you need to enable autofix in ESLint when you save the file in your editor. Instructions for your editor can be found here.

Is it safety?

On the whole, yes. We are very careful to make sure that the work of the plugin does not negatively affect the work of the code. For example, the plugin takes into account spread operators in JSX and objects, comments to the code, exports with *. Safety is our priority. If you encounter any problem, you can create an issue.

Why not Prettier?

I love Prettier. However, this is not his area of responsibility. Prettier is used for formatting, and ESLint is also used 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.

⚠️ Troubleshooting

There are rules of ESLint and other ESLint plugins that may conflict with the rules of ESLint Plugin Perfectionist. We strongly recommend that you disable rules with similar functionality.

I recommend that you read the documentation before using any rules.

<summary>Possible conflicts</summary>

perfectionist/sort-imports:

{
  "rules": {
    "import/order": "off",
    "sort-imports": "off"
  }
}

perfectionist/sort-interfaces:

{
  "rules": {
    "@typescript-eslint/adjacent-overload-signatures": "off"
  }
}

perfectionist/sort-jsx-props:

{
  "rules": {
    "react/jsx-sort-props": "off"
  }
}

perfectionist/sort-named-imports:

{
  "rules": {
    "sort-imports": "off"
  }
}

perfectionist/sort-object-types:

{
  "rules": {
    "@typescript-eslint/adjacent-overload-signatures": "off"
  }
}

perfectionist/sort-objects:

{
  "rules": {
    "sort-keys": "off"
  }
}

perfectionist/sort-union-types:

{
  "rules": {
    "@typescript-eslint/sort-type-constituents": "off"
  }
}

🚥 Versioning Policy

This plugin is following Semantic Versioning and ESLint's Semantic Versioning Policy.

❤️ Contributing

See Contributing Guide.

👁 See Also

🔒 License

MIT © Azat S.

changelog

Changelog

v1.5.1

compare changes

🐞 Bug Fixes

  • Use alphabetical as the default sort type in schemas (3b9366e)
  • Disable sorting side-effect imports (01da88a)

❤️ Contributors

v1.5.0

compare changes

🚀 Features

  • Add external-type import group (47b07cc)

🏎 Performance Improvements

  • Move from foreach to for loops (b648f74)
  • Move from reduce to for loops in sort-imports (16f6361)

🐞 Bug Fixes

  • Don't sort ts call signature declarations in interfactes (5829a65)

❤️ Contributors

v1.4.0

compare changes

🚀 Features

  • Allow separating object properties into logical parts (933b621)

❤️ Contributors

v1.3.0

compare changes

🚀 Features

  • Support custom import groups (0b837d4)

❤️ Contributors

v1.2.1

compare changes

🐞 Bug Fixes

  • Fix removing extra spaces between imports (21bc7a8)

❤️ Contributors

v1.2.0

compare changes

🚀 Features

  • Support stylus file imports (dbef415)
  • Add sort-exports rule (a71eeb3)

❤️ Contributors

v1.1.2

compare changes

🐞 Bug Fixes

  • Fix sorting interfaces and types with comment on same line (03e5508)

❤️ Contributors

v1.1.1

compare changes

🐞 Bug Fixes

  • Fix adding extra lines if import ends with semi (e435f91)
  • Don't sort keys if right value depends on left (3e987ae)
  • Ignore semi at the end of object type member value (623ac67)

❤️ Contributors

v1.1.0

compare changes

🚀 Features

  • Add style group to sort the imports (05bf0f7)
  • Add side-effect group to sort the imports (02f51fb)
  • Add builtin-type group to sort the imports (ca34b5e)

🐞 Bug Fixes

  • Allow to sort destructured objects (65fe6c7)

❤️ Contributors

v1.0.1

compare changes

🐞 Bug Fixes

  • Do not sort enums with implicit values (166edac)

❤️ Contributors

v1.0.0

compare changes

🎉 Stable release

❤️ Contributors

v0.11.6

compare changes

🐞 Bug Fixes

  • Improve sort-imports fix function (e7a39f2)

❤️ Contributors

v0.11.5

compare changes

🐞 Bug Fixes

  • Fix sorting objects with inline comments (37a537d)
  • Split imports if there are other nodes between (b1a8837)

❤️ Contributors

v0.11.4

compare changes

🐞 Bug Fixes

  • Use service comments when sorting imports (b577ac7)
  • Fix sorting nodes with comments on the same line (16887ea)
  • Do not fix objects if last member contains a comment and doesn't contain comma (a9915f1)

❤️ Contributors

v0.11.3

compare changes

🐞 Bug Fixes

  • Fix working sort-map-elements with empty map (de061ff)
  • Disallow to sort default import specifiers (60044c6)
  • Do not sort imports if there are tokens between them (a4fabe9)

❤️ Contributors

v0.11.2

compare changes

🏎 Performance Improvements

  • Do not compute options if rule is not used (4574caa)

🐞 Bug Fixes

  • Fix single line type objects sorting (aaa446a)

❤️ Contributors

v0.11.1

compare changes

🐞 Bug Fixes

  • Fix option names in sort-classes in configs (bf578ed)

❤️ Contributors

v0.11.0

compare changes

🚀 Features

  • Add sort-object-types rule (e3a06cf)
  • Add sort-classes rule (b3a0cb8)

🐞 Bug Fixes

  • Fix multiline option value in sort-jsx-props rule in configs (556690d)
  • Improve error output (c1ad261)
  • Fix internal patter in configs (4be8a74)

❤️ Contributors

v0.10.0

compare changes

🚀 Features

  • Add read-tsconfig option to sort-imports rule (84cfc3d)
  • Allow to ignore interface by pattern (9aaf08a)
  • Add ignore-case option to each rule (e331b9a)
  • Rename spread-last option in sort-array-includes rule to kebab case (fc342d2)
  • Add shorthand position option to sort-jsx-props rule (416ffee)
  • Add callback position option to sort-jsx-props rule (8c6189f)
  • Add multiline position option to sort-jsx-props rule (58e094a)
  • Add always-on-top option to sort-jsx-props rule (57af3a2)
  • Rename sort-object-keys rule to sort-objects (3340a9f)
  • Add always-on-top option to sort-objects rule (464108f)

🏎 Performance Improvements

  • Make reading tsconfig singleton (c748445)
  • Improve sort-imports rule performance (2989539)

🐞 Bug Fixes

  • Fix groups in sort-imports rule in configs (f83c499)
  • Move parentheses when sorting (d09395f)
  • Update peer deps (800c2a3)

❤️ Contributors

v0.9.0

compare changes

🚀 Features

🐞 Bug Fixes

  • Keep code comments when sorting (547f825)
  • Update url to documentation of rules (423b145)

❤️ Contributors

v0.8.0

compare changes

🚀 Features

🐞 Bug Fixes

  • Fix defenition for rule not found error (050d20d)

❤️ Contributors

v0.7.0

compare changes

🐞 Bug Fixes

  • Fix plugin configs creation (559a2ce)

❤️ Contributors

v0.6.0

compare changes

🚀 Features

  • Support flat eslint config (969ae4e)
  • Add sort-object-keys rule (6dcb425)
  • Add recommended-alphabetical config (66c99f0)

❤️ Contributors

v0.5.0

compare changes

🚀 Features

  • Add sort-map-elements rule (049c004)
  • Add sort-array-includes rule (bb7605b)

🐞 Bug Fixes

  • Fix rules descriptions (1d18a26)
  • Add default rules properties (48d2835)
  • Add array constructor support to sort-array-includes rule (d255c22)
  • Fix interface sorting (86e3b56)

❤️ Contributors

v0.4.0

compare changes

🚀 Features

  • Add sort-named-exports rule (b3f4b57)

🐞 Bug Fixes

  • Fix rule configs creation (8a43758)
  • Fix missed sort-union-types rule export (3b02609)

❤️ Contributors

v0.3.0

compare changes

🚀 Features

🐞 Bug Fixes

❤️ Contributors

v0.2.0

compare changes

🚀 Features

  • Add sort-named-imports rule (827ee5a)
  • Add sort-jsx-props rule (656c86b)

🐞 Bug Fixes

❤️ Contributors

v0.1.0

🔥️️ Initial Release