Git Product home page Git Product logo

eslint-plugin-perfectionist's Introduction

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-intersection-types enforce sorted intersection types ๐Ÿ”ง
sort-union-types enforce sorted union types ๐Ÿ”ง
sort-vue-attributes enforce sorted Vue attributes ๐Ÿ”ง

โ‰๏ธ 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. 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.

Possible conflicts

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.

eslint-plugin-perfectionist's People

Contributors

azat-io avatar chirokas avatar joshuakgoldberg avatar wondermarin avatar ai avatar hampus-stravito avatar haocheng6 avatar sitek94 avatar stovmascript avatar renato-bohler avatar kid-joker avatar deathemperor avatar tthornton3-chwy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.