Skip to content

Commit

Permalink
feat(eslint-plugin) [sort-type-union-intersection-members] rename to …
Browse files Browse the repository at this point in the history
…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
JoshuaKGoldberg committed Oct 27, 2022
1 parent 2ee81df commit a0c8285
Show file tree
Hide file tree
Showing 8 changed files with 715 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ module.exports = {
rules: {
// disallow ALL unused vars
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/sort-type-union-intersection-members': 'error',
'@typescript-eslint/sort-type-constituents': 'error',
},
},
{
Expand Down
101 changes: 101 additions & 0 deletions packages/eslint-plugin/docs/rules/sort-type-constituents.md
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`
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ description: 'Enforce members of a type union/intersection to be sorted alphabet
>
> See **https://typescript-eslint.io/rules/sort-type-union-intersection-members** for documentation.
:::danger Deprecated

This rule has been renamed to [`sort-type-union-intersection-members`](./sort-type-union-intersection-members.md).
:::

Sorting union (`|`) and intersection (`&`) types can help:

- keep your codebase standardized
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export = {
'@typescript-eslint/return-await': 'error',
semi: 'off',
'@typescript-eslint/semi': 'error',
'@typescript-eslint/sort-type-union-intersection-members': 'error',
'@typescript-eslint/sort-type-constituents': 'error',
'space-before-blocks': 'off',
'@typescript-eslint/space-before-blocks': 'error',
'space-before-function-paren': 'off',
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import restrictPlusOperands from './restrict-plus-operands';
import restrictTemplateExpressions from './restrict-template-expressions';
import returnAwait from './return-await';
import semi from './semi';
import sortTypeConstituents from './sort-type-constituents';
import sortTypeUnionIntersectionMembers from './sort-type-union-intersection-members';
import spaceBeforeBlocks from './space-before-blocks';
import spaceBeforeFunctionParen from './space-before-function-paren';
Expand Down Expand Up @@ -245,6 +246,7 @@ export default {
'restrict-template-expressions': restrictTemplateExpressions,
'return-await': returnAwait,
semi: semi,
'sort-type-constituents': sortTypeConstituents,
'sort-type-union-intersection-members': sortTypeUnionIntersectionMembers,
'space-before-blocks': spaceBeforeBlocks,
'space-before-function-paren': spaceBeforeFunctionParen,
Expand Down
Loading

0 comments on commit a0c8285

Please sign in to comment.