-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom sort type through custom alphabet
* feat: adds alphabet generator * We will be using that class in tests as well. * feat: adds `custom` sort type * feat: adds custom sort validation * refactor: create function for each sort type * test: adds tests for all rules * The custom sort generated mimics `localeCompare()` for simplicity. * I have added a single test with the `custom` sort type for each rule. I don't think that adding as many tests as other types is necessary: the objective is not to test that the behavior of a custom sort works for all rules, but rather to check that the rule handles that sorting type. * Custom sorting behavior should be verified in `compare.ts`. * refactor: moves some util files to `utils` folder * docs: updates main pages * docs: updates rules * docs: improves style * docs: fixes invalid documentation * fix: fix invalid config creation * fix: fixes typings * refactor: [FEEDBACK] avoids using undefined Co-authored-by: Azat S. <[email protected]> * refactor: [FEEDBACK] avoids using undefined Co-authored-by: Azat S. <[email protected]> * refactor: [FEEDBACK] reduces comments length * docs: [FEEDBACK] fixes missing `Alphabet` * docs: [FEEDBACK] adds Astro section * feat: [FEEDBACK] adds `convertBooleanToSign` function * docs: added `Alphabet` documentation - Removed `removeUnicodePlane` function, which is likely not that useful for users. * build: [FEEDBACK] adds `eslint-plugin-perfectionist/alphabet` export * docs: [FEEDBACK] adds `eslint-plugin-perfectionist/alphabet` export * style: ESLint fix --------- Co-authored-by: Azat S. <[email protected]>
- Loading branch information
Showing
83 changed files
with
3,068 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
--- | ||
title: recommended-custom | ||
description: Learn more about the recommended custom ESLint Plugin Perfectionist configuration for sorting and organizing your code. Take customization to new levels while maintaining a consistent coding style with this setup | ||
shortDescription: All plugin rules with your own custom sorting | ||
keywords: | ||
- eslint | ||
- recommended custom config | ||
- eslint configuration | ||
- coding standards | ||
- code quality | ||
- javascript linting | ||
- custom sorting | ||
- eslint-plugin-perfectionist | ||
--- | ||
|
||
import CodeTabs from '../../components/CodeTabs.svelte' | ||
import { dedent } from 'ts-dedent' | ||
|
||
Configuration for the `eslint-plugin-perfectionist` plugin, which provides all plugin rules with your predefined custom ordered alphabet. | ||
|
||
This configuration allows you to define your own custom order for sorting elements in your codebase as you truly desire. | ||
|
||
## When to Use | ||
|
||
Each rule in `eslint-plugin-perfectionist` offers a lot of options that should suit most use cases. | ||
|
||
If this is not enough, you may define your own alphabet and use the `recommended-custom` configuration to enforce a consistent custom order across various data structures in your codebase. | ||
|
||
Use this configuration to precisely tune how elements should be sorted while keeping readability and maintainability to their highest levels. | ||
|
||
## Usage | ||
|
||
You must provide an `alphabet` option in the `perfectionist` settings object or for each rule individually. | ||
This option should be a string that represents an ordered alphabet. | ||
|
||
Example: `01234564789abcdef...` | ||
|
||
Use the `Alphabet` utility class from `eslint-plugin-perfectionist/alphabet` to quickly generate a custom alphabet. | ||
|
||
<CodeTabs | ||
code={[ | ||
{ | ||
source: dedent` | ||
// eslint.config.js | ||
import { Alphabet } from 'eslint-plugin-perfectionist/alphabet' | ||
import perfectionist from 'eslint-plugin-perfectionist' | ||
import naturalCompare from 'natural-compare-lite'; | ||
const myCustomAlphabet = Alphabet | ||
.generateRecommendedAlphabet() | ||
.sortingBy((a, b) => naturalCompare(a, b)) | ||
.getCharacters(); | ||
export default [ | ||
{ | ||
...perfectionist.configs['recommended-custom'], | ||
settings: { | ||
perfectionist: { | ||
alphabet: myCustomAlphabet | ||
} | ||
} | ||
} | ||
] | ||
`, | ||
name: 'Flat Config', | ||
value: 'flat', | ||
}, | ||
{ | ||
source: dedent` | ||
// .eslintrc.js | ||
import { Alphabet } from 'eslint-plugin-perfectionist/alphabet' | ||
import perfectionist from 'eslint-plugin-perfectionist' | ||
import naturalCompare from 'natural-compare-lite'; | ||
const myCustomAlphabet = Alphabet | ||
.generateRecommendedAlphabet() | ||
.sortingBy((a, b) => naturalCompare(a, b)) | ||
.getCharacters(); | ||
module.exports = { | ||
extends: [ | ||
'plugin:perfectionist/recommended-custom-legacy', | ||
], | ||
settings: { | ||
perfectionist: { | ||
alphabet: myCustomAlphabet | ||
} | ||
} | ||
} | ||
`, | ||
name: 'Legacy Config', | ||
value: 'legacy', | ||
}, | ||
]} | ||
type="config-type" | ||
client:load | ||
lang="tsx" | ||
/> | ||
|
||
## Alphabet class | ||
|
||
The `Alphabet` class from `eslint-plugin-perfectionist/alphabet` provides a set of methods to generate and manipulate alphabets. | ||
|
||
### Static generators | ||
|
||
#### - `static generateCompleteAlphabet(): Alphabet` | ||
|
||
Generates an alphabet containing all characters from the Unicode standard except for irrelevant [Unicode planes](https://en.wikipedia.org/wiki/Plane_(Unicode)). | ||
Contains the Unicode planes 0, 1, 2 and 3. | ||
|
||
#### - `static generateRecommendedAlphabet(): Alphabet` | ||
|
||
Generates an alphabet containing relevant characters from the Unicode standard. Contains the [Unicode planes](https://en.wikipedia.org/wiki/Plane_(Unicode)) 0 and 1. | ||
|
||
#### - `static generateFrom(values: string[] | string): Alphabet` | ||
|
||
Generates an alphabet from the given characters. | ||
|
||
### Adding/Removing characters | ||
|
||
#### - `pushCharacters(values: string[] | string): this` | ||
|
||
Adds specific characters to the end of the alphabet. | ||
|
||
#### - `removeCharacters(values: string[] | string): this` | ||
|
||
Removes specific characters from the alphabet. | ||
|
||
#### - `removeUnicodeRange({ start: number; end: number }): this` | ||
|
||
Removes specific characters from the alphabet by their range | ||
|
||
### Sorters | ||
|
||
#### - `sortByLocaleCompare(locales?: Intl.LocalesArgument): this` | ||
|
||
Sorts the alphabet by the locale order of the characters. | ||
|
||
#### - `sortByNaturalSort(locale?: string): this` | ||
|
||
Sorts the alphabet by the natural order of the characters using [natural-orderby](https://github.com/yobacca/natural-orderby). | ||
|
||
#### - `sortByCharCodeAt(): this` | ||
|
||
Sorts the alphabet by the character code point. | ||
|
||
#### - `sortBy(sortingFunction: (characterA: string, characterB: string) => number): this` | ||
|
||
Sorts the alphabet by the sorting function provided | ||
|
||
#### - `reverse(): this` | ||
|
||
Reverses the alphabet. | ||
|
||
### Other methods | ||
|
||
#### - `prioritizeCase(casePriority: 'lowercase' | 'uppercase'): this` | ||
|
||
For each character with a lower and upper case, permutes the two cases so that the alphabet is ordered by the case priority entered. | ||
|
||
```ts | ||
Alphabet.generateFrom('aAbBcdCD') | ||
.prioritizeCase('uppercase') | ||
// Returns 'AaBbCDcd' | ||
```` | ||
|
||
#### - `placeAllWithCaseBeforeAllWithOtherCase(caseToComeFirst: 'uppercase' | 'lowercase'): this` | ||
|
||
Permutes characters with cases so that all characters with the entered case are put before the other characters. | ||
|
||
```ts | ||
Alphabet.generateFrom('aAbBcCdD') | ||
.placeAllWithCaseBeforeAllWithOtherCase('lowercase') | ||
// Returns 'abcdABCD' | ||
```` | ||
#### - `placeCharacterBefore({ characterBefore: string; characterAfter: string }): this` | ||
Places a specific character right before another character in the alphabet. | ||
```ts | ||
Alphabet.generateFrom('ab-cd/') | ||
.placeCharacterBefore({ characterBefore: '/', characterAfter: '-' }) | ||
// Returns 'ab/-cd' | ||
``` | ||
|
||
#### - `placeCharacterAfter({ characterBefore: string; characterAfter: string }): this` | ||
|
||
Places a specific character right after another character in the alphabet. | ||
|
||
```ts | ||
Alphabet.generateFrom('ab-cd/') | ||
.placeCharacterAfter({ characterBefore: '/', characterAfter: '-' }) | ||
// Returns 'abcd/-' | ||
``` | ||
|
||
#### - `getCharacters(): string` | ||
|
||
Retrieves the characters from the alphabet. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.