2.10.0 ā€¢ Published 6 days ago

eslint-plugin-perfectionist v2.10.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 days ago

ESLint Plugin Perfectionist

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

NameDescription
recommended-alphabeticalall plugin rules with alphabetical sorting in ascending order
recommended-naturalall plugin rules with natural sorting in ascending order
recommended-line-lengthall plugin rules with sorting by line length in descending order

āœ… Rules

šŸ”§ Automatically fixable by the --fix CLI option.

NameDescriptionšŸ”§
sort-array-includesenforce sorted arrays before include methodšŸ”§
sort-astro-attributesenforce sorted Astro attributesšŸ”§
sort-classesenforce sorted classesšŸ”§
sort-enumsenforce sorted TypeScript enumsšŸ”§
sort-exportsenforce sorted exportsšŸ”§
sort-importsenforce sorted importsšŸ”§
sort-interfacesenforce sorted interface propertiesšŸ”§
sort-jsx-propsenforce sorted JSX propsšŸ”§
sort-mapsenforce sorted Map elementsšŸ”§
sort-named-exportsenforce sorted named exportsšŸ”§
sort-named-importsenforce sorted named importsšŸ”§
sort-object-typesenforce sorted object typesšŸ”§
sort-objectsenforce sorted objectsšŸ”§
sort-svelte-attributesenforce sorted Svelte attributesšŸ”§
sort-intersection-typesenforce sorted intersection typesšŸ”§
sort-union-typesenforce sorted union typesšŸ”§
sort-vue-attributesenforce 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.

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.

@everything-registry/sub-chunk-1613@ifshizuku/eslint-config-basic@daopk/eslint-config-perfectionist@zeus-lint/eslint-config@zhangwj0520/eslint-config@abrahamsaanchez/eslint-config@cinnabar-forge/eslint-plugin@anolilab/eslint-config@auvred/eslint-config@arianrhodsandlot/eslint-config@arquivei/eslint-config@13onthecode/eslint-config@virtual-live-lab/eslint-config@coderwyd/eslint-config@belgattitude/eslint-config-basesangular-eslint-flat-config@antfu/eslint-configweb-ui-depsbisnow-eslint-config@asd55667/eslint-config@arvinn/eslint-config@douglasneuroinformatics/eslint-config@gravis-os/eslint-config@dkshs/eslint-config@grnx-utils/eslint@wearzdk/eslint-config-prettier@aliheym/eslint-config@boehringer-ingelheim/eslint-config@bernankez/eslint-configlint-fn@geekjuice/eslint-config@hexatool/eslint-config@erictooth/eslint-config@eslegant/js@eleven-labs/eslint-config@eleven-labs/eslint-config-typescript@exer7um/eslint-config@flowr/eslint-config@flandredaisuki/eslint-config@reallyland/tools@sparticuz/eslint-configeslint-config-escapaceeslint-config-eschadeslint-config-elegant-codingeslint-config-pcpeslint-config-janfreslint-config-jaideslint-config-jaid-reacteslint-config-oclif-typescripteslint-config-hardcoreeslint-config-ksieslint-config-donovaneslint-config-besteslint-config-current-thing@stacksjs/eslint-configeslint-config-avrora-vueeslint-config-aesthetic-codereslint-config-symmetryseslint-config-rzpackeslint-config-tidaleslint-config-y2eslint-config-sensible-prettier-typescripteslint-config-tonyfromundefinedeslint-config-reperioeslint-config-shinyeslint-config-vaadineslint-config-react-native-matipl01eslint-config-rel1cxeslint-config-removifyeslint-config-redpanda@syntatis/eslint-config@sxzz/eslint-configeslint-plugin-payload@touchspot/eslint-plugin@tm2js/eslint-config@trpl/eslint-config@pumpn/eslint-config@putdotio/eslint-config@rednightgames/eslint-config-rednight@rotki/eslint-config@rubiin/eslint-config@rockpack/codestyle@sales-journey/eslint-config@removify/eslint-config@ryanep/eslint-config@samialdury/config@snowball-tech/eslint-config@sundarshahi/eslint-config@taskylizard/eslint-config@tb-dev/eslint-config@unshared/eslint-config@wisemen/eslint-config-vue@witheslint/core@u3u/eslint-config@re-taro/eslint-config@thenbe/eslint-config@thinkbuff/eslint-config@tiwariav/eslint-config@nekn3x/eslint-config@nextime-ui/eslint-config
2.10.0

6 days ago

2.9.0

18 days ago

2.7.0

2 months ago

2.8.0

27 days ago

2.6.0

2 months ago

2.3.0

6 months ago

2.5.0

5 months ago

2.4.1

5 months ago

2.4.0

6 months ago

2.4.2

5 months ago

2.2.0

7 months ago

2.1.0

8 months ago

2.0.1

8 months ago

2.0.0

8 months ago

1.5.1

9 months ago

1.5.0

10 months ago

1.2.0

11 months ago

1.1.1

11 months ago

1.1.0

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago

1.4.0

11 months ago

1.3.0

11 months ago

1.2.1

11 months ago

1.1.2

11 months ago

0.11.0

11 months ago

0.11.1

11 months ago

0.11.2

11 months ago

0.11.3

11 months ago

0.11.4

11 months ago

0.11.5

11 months ago

0.11.6

11 months ago

0.10.0

11 months ago

0.3.0

12 months ago

0.9.0

11 months ago

0.8.0

11 months ago

0.5.0

11 months ago

0.4.0

12 months ago

0.7.0

11 months ago

0.6.0

11 months ago

0.2.0

12 months ago

0.1.0

12 months ago