JSPM

  • Created
  • Published
  • Downloads 1162124
  • Score
    100M100P100Q195658F
  • License MIT

ESLint plugin for code formatting in perfectionist style

Package Exports

  • eslint-plugin-perfectionist
  • eslint-plugin-perfectionist/configs/recommended-alphabetical
  • eslint-plugin-perfectionist/configs/recommended-line-length
  • eslint-plugin-perfectionist/configs/recommended-natural
  • eslint-plugin-perfectionist/package.json

Readme

ESLint Plugin Perfectionist

ESLint

Version

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, etc.

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.

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-array-includes": [
      "error",
      {
        "type": "line-length",
        "order": "desc",
        "spreadLast": true
      }
    ]
  }
}

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

import perfectionist from 'eslint-plugin-perfectionist'

export default [
  {
    plugins: {
      perfectionist,
    },
    rules: {
      'perfectionist/sort-array-includes': [
        'error',
        {
          type: 'line-length',
          order: 'desc',
          spreadLast: true,
        },
      ],
    },
  },
]

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) (requires eslint >= v8.23.0)

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

export default [perfectionistPluginRecommendedNatural]

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

💼 - Configurations enabled in.
🔧 - Automatically fixable by the --fix CLI option.

Name Description 💼 🛠
sort-array-includes Enforce sorted arrays before include method
sort-enums Enforce sorted TypeScript enums
sort-imports Enforce sorted imports
sort-interfaces Enforce sorted interface properties
sort-jsx-props Enforce sorted JSX props
sort-map-elements Enforce sorted Map elements
sort-named-exports Enforce sorted named exports
sort-named-imports Enforce sorted named imports
sort-object-keys Enforce sorted object keys
sort-union-types Enforce sorted union types

See also

License

MIT © Azat S.