-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin) [sort-type-union-intersection-members] rename to …
…sort-type-constituents (#5879) * feat(eslint-plugin) [sort-type-union-intersection-members] rename to sort-type-constituents * Sigh, test rename * Added back rule, with replacedBy * Fixed up rules exports
- Loading branch information
1 parent
2ee81df
commit a0c8285
Showing
8 changed files
with
715 additions
and
2 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
101 changes: 101 additions & 0 deletions
101
packages/eslint-plugin/docs/rules/sort-type-constituents.md
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,101 @@ | ||
--- | ||
description: 'Enforce constituents of a type union/intersection to be sorted alphabetically.' | ||
--- | ||
|
||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/sort-type-constituents** for documentation. | ||
Sorting union (`|`) and intersection (`&`) types can help: | ||
|
||
- keep your codebase standardized | ||
- find repeated types | ||
- reduce diff churn | ||
|
||
This rule reports on any types that aren't sorted alphabetically. | ||
|
||
> Types are sorted case-insensitively and treating numbers like a human would, falling back to character code sorting in case of ties. | ||
## Examples | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
type T1 = B | A; | ||
|
||
type T2 = { b: string } & { a: string }; | ||
|
||
type T3 = [1, 2, 4] & [1, 2, 3]; | ||
|
||
type T4 = | ||
| [1, 2, 4] | ||
| [1, 2, 3] | ||
| { b: string } | ||
| { a: string } | ||
| (() => void) | ||
| (() => string) | ||
| 'b' | ||
| 'a' | ||
| 'b' | ||
| 'a' | ||
| readonly string[] | ||
| readonly number[] | ||
| string[] | ||
| number[] | ||
| B | ||
| A | ||
| string | ||
| any; | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
type T1 = A | B; | ||
|
||
type T2 = { a: string } & { b: string }; | ||
|
||
type T3 = [1, 2, 3] & [1, 2, 4]; | ||
|
||
type T4 = | ||
| any | ||
| string | ||
| A | ||
| B | ||
| number[] | ||
| string[] | ||
| readonly number[] | ||
| readonly string[] | ||
| 'a' | ||
| 'b' | ||
| 'a' | ||
| 'b' | ||
| (() => string) | ||
| (() => void) | ||
| { a: string } | ||
| { b: string } | ||
| [1, 2, 3] | ||
| [1, 2, 4]; | ||
``` | ||
|
||
## Options | ||
|
||
### `groupOrder` | ||
|
||
Each constituent of the type is placed into a group, and then the rule sorts alphabetically within each group. | ||
The ordering of groups is determined by this option. | ||
|
||
- `conditional` - Conditional types (`A extends B ? C : D`) | ||
- `function` - Function and constructor types (`() => void`, `new () => type`) | ||
- `import` - Import types (`import('path')`) | ||
- `intersection` - Intersection types (`A & B`) | ||
- `keyword` - Keyword types (`any`, `string`, etc) | ||
- `literal` - Literal types (`1`, `'b'`, `true`, etc) | ||
- `named` - Named types (`A`, `A['prop']`, `B[]`, `Array<C>`) | ||
- `object` - Object types (`{ a: string }`, `{ [key: string]: number }`) | ||
- `operator` - Operator types (`keyof A`, `typeof B`, `readonly C[]`) | ||
- `tuple` - Tuple types (`[A, B, C]`) | ||
- `union` - Union types (`A | B`) | ||
- `nullish` - `null` and `undefined` |
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.