Skip to content

feat(eslint-plugin) [sort-type-union-intersection-members] rename to sort-type-constituents #5879

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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