4.10.1 • Published 3 months ago

eslint-plugin-perfectionist v4.10.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

ESLint Plugin Perfectionist

Version Monthly Download Code Coverage GitHub License

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!

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, 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!

Documentation

See docs.

Alphabetical Sorting

Sorting by Line Length

Installation

You'll first need to install ESLint v8.45.0 or greater:

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.

Flat Config (eslint.config.js)

import perfectionist from 'eslint-plugin-perfectionist'

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

Legacy Config (.eslintrc.js)

module.exports = {
  plugins: [
    'perfectionist',
  ],
  rules: {
    'perfectionist/sort-imports': [
      '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.

Flat Config (eslint.config.js)

import perfectionist from 'eslint-plugin-perfectionist'

export default [
  perfectionist.configs['recommended-natural'],
]

Legacy Config (.eslintrc)

module.exports = {
  extends: [
    'plugin:perfectionist/recommended-natural-legacy',
  ],
}

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
recommended-customAll plugin rules with sorting by your own custom order

Rules

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

NameDescriptionšŸ”§
sort-array-includesEnforce sorted arrays before include methodšŸ”§
sort-classesEnforce sorted classesšŸ”§
sort-decoratorsEnforce sorted decoratorsšŸ”§
sort-enumsEnforce sorted TypeScript enumsšŸ”§
sort-exportsEnforce sorted exportsšŸ”§
sort-heritage-clausesEnforce sorted implements/extends clausesšŸ”§
sort-importsEnforce sorted importsšŸ”§
sort-interfacesEnforce sorted interface propertiesšŸ”§
sort-intersection-typesEnforce sorted intersection typesšŸ”§
sort-jsx-propsEnforce sorted JSX propsšŸ”§
sort-mapsEnforce sorted Map elementsšŸ”§
sort-modulesEnforce sorted modulesšŸ”§
sort-named-exportsEnforce sorted named exportsšŸ”§
sort-named-importsEnforce sorted named importsšŸ”§
sort-object-typesEnforce sorted object typesšŸ”§
sort-objectsEnforce sorted objectsšŸ”§
sort-setsEnforce sorted Set elementsšŸ”§
sort-switch-caseEnforce sorted switch case statementsšŸ”§
sort-union-typesEnforce sorted union typesšŸ”§
sort-variable-declarationsEnforce sorted variable declarationsšŸ”§

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. You may find instructions for your editor here.

Is it safe?

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.

Why not use Prettier?

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.

Versioning Policy

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

Contributing

See Contributing Guide.

License

MIT Ā© Azat S.

@everything-registry/sub-chunk-1613@ifshizuku/eslint-config-basic@daopk/eslint-config-perfectionist@rednightgames/eslint-config-rednight@joaomelo/lintweb-ui-deps@dkshs/eslint-config@codeartz/eslint-config@cuiqg/eslint-config@darksheep/eslint@dethdkn/eslint-config@douglasneuroinformatics/eslint-config@boehringer-ingelheim/eslint-config@belgattitude/eslint-config-bases@payloadcms/eslint-config@payloadcms/eslint-plugin@pearlroc/eslint-config@postanu/eslint-config@rotki/eslint-config@rubiin/eslint-configeslint-config-cct@yugutou/eslint-configeslint-config-rel1cxjs-style-kitkaton-labs-eslint-config@iz7n/eslint-config@merlinalexhjp/eslint-config@demonicattack/eslint@ainsleydev/eslint-config@anytinz/eslint-config@asd55667/eslint-config@arquivei/eslint-config@arvinn/eslint-config@anyions/shared-config-eslint@anyions/shared-eslint-config@ansearch/config@anolilab/eslint-config@antfu/eslint-config@arianrhodsandlot/eslint-config@auvred/eslint-config@ajiu9/eslint-config@alexlit/config-eslint@aliheym/eslint-config@ayingott/eslint-config@babbel/eslint-config@ayato-san/tooling-configs@bksp/style-guide@bfra.me/eslint-config@blade47/editorjs-latex-tool@blade47/editorjs-sanitize-tool@blade47/editorjs-annotation-tool@blade47/editorjs-inline-math-tool@blueking/bkmonitor-lint@blueking/bkui-lint@carrismetropolitana/eslint@cinnabar-forge/eslint-plugin@3yourmind/eslint-config@2030/eslint-config@13onthecode/eslint-config@abrahamsaanchez/eslint-config@coderwyd/eslint-config@cloudflight/eslint-plugin-typescript@bernankez/eslint-config@debbl/eslint-config@denis-sokolov/eslint-plugin@dhzh/eslint-config@didor/style-guide@gravis-os/eslint-config@h1fra/eslint-config@grnx-utils/eslint@fluencelabs/oclif@flandredaisuki/eslint-config@frandnb/eslint-awesome-config@hexadrop/eslint-config@hexatool/eslint-config@flowr/eslint-config@elora-cloud/eslint-config@eleven-labs/eslint-config@eleven-labs/eslint-config-typescript@garvae/eslint-config-garvae@geekjuice/eslint-config@gaze-ui/eslint-config@ethang/eslint-config@fullstacksjs/eslint-config@geops/eslint-config-react@ggbondz/eslint-flat-config@innocenzi/eslint-config@ktam/lint-node@kusotenpa/eslint-plugin@kriszu/eslint-config@liveryvideo/biome-lit@leancodepl/eslint-config@ltteng/eslint-config@logux/eslint-config@lqbach/eslint-config@lshay/eslint-config@lshay/eslint-config-flat@lemonbot.fun/eslint-config@lenne.tech/eslint-config-angular@lenne.tech/eslint-config-ts
4.10.1

3 months ago

4.10.0

4 months ago

4.9.0

4 months ago

4.8.0

5 months ago

4.7.0

5 months ago

4.6.0

6 months ago

4.5.0

6 months ago

4.4.0

6 months ago

4.2.0

7 months ago

4.3.0

6 months ago

4.1.0

7 months ago

4.1.2

7 months ago

4.1.1

7 months ago

4.0.1

7 months ago

4.0.0

7 months ago

4.0.3

7 months ago

4.0.2

7 months ago

3.9.1

8 months ago

3.9.0

8 months ago

3.4.0

10 months ago

3.8.0

9 months ago

3.6.0

9 months ago

3.3.0

10 months ago

3.7.0

9 months ago

3.5.0

10 months ago

3.2.0

10 months ago

3.1.3

11 months ago

3.1.2

11 months ago

3.1.1

11 months ago

3.1.0

11 months ago

3.0.0

11 months ago

2.11.0

1 year ago

2.10.0

1 year ago

2.9.0

1 year ago

2.7.0

1 year ago

2.8.0

1 year ago

2.6.0

1 year ago

2.3.0

2 years ago

2.5.0

2 years ago

2.4.1

2 years ago

2.4.0

2 years ago

2.4.2

2 years ago

2.2.0

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.4.0

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.1.2

2 years ago

0.11.0

2 years ago

0.11.1

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.11.5

2 years ago

0.11.6

2 years ago

0.10.0

2 years ago

0.3.0

2 years ago

0.9.0

2 years ago

0.8.0

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago