Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): add rulesort-type-union-intersection-members#2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
This file contains hidden or 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
144 changes: 144 additions & 0 deletionspackages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md
This file contains hidden or 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,144 @@ | ||
| # Enforces that members of a type union/intersection are sorted alphabetically (`sort-type-union-intersection-members`) | ||
| Sorting union (`|`) and intersection (`&`) types can help: | ||
| - keep your codebase standardized | ||
| - find repeated types | ||
| - reduce diff churn | ||
| ## Rule Details | ||
| Sorting within each group is done using the following code: | ||
| ```ts | ||
| const collator = new Intl.Collator('en', { | ||
| sensitivity: 'base', | ||
| numeric: true, | ||
| }); | ||
| function compare(a, b) { | ||
| return collator.compare(a, b) || (a < b ? -1 : a > b ? 1 : 0); | ||
| } | ||
| ``` | ||
| In other words, the types are sorted alphabetically, case-insensitively and treating numbers like a human would, falling back to character code sorting in case of ties. | ||
| Examples of **incorrect** code for this rule: | ||
| ```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; | ||
| ``` | ||
| Examples of **correct** code for this rule: | ||
| ```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 | ||
| ```ts | ||
| type Options = { | ||
| // true to check intersection types, false otherwise | ||
| checkIntersections?: boolean; | ||
| // true to check union types, false otherwise | ||
| checkUnions?: boolean; | ||
| // the ordering of the groups | ||
| groupOrder?: ( | ||
| | 'conditional' | ||
| | 'function' | ||
| | 'import' | ||
| | 'intersection' | ||
| | 'keyword' | ||
| | 'literal' | ||
| | 'named' | ||
| | 'object' | ||
| | 'operator' | ||
| | 'tuple' | ||
| | 'union' | ||
| )[]; | ||
| }; | ||
| const defaultOptions: Options = { | ||
| checkIntersections: true, | ||
| checkUnions: true, | ||
| groupOrder: [ | ||
| 'named', | ||
| 'keyword', | ||
| 'operator', | ||
| 'literal', | ||
| 'function', | ||
| 'import', | ||
| 'conditional', | ||
| 'object', | ||
| 'tuple', | ||
| 'intersection', | ||
| 'union', | ||
| ], | ||
| }; | ||
| ``` | ||
| ### `groupOrder` | ||
| Each member 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`) |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.ts
This file contains hidden or 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
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or 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.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.