Skip to content

Commit 5558f41

Browse files
authored
feat(eslint-plugin): [sort-type-union-intersection-members] add nullish group (#2919)
1 parent f606846 commit 5558f41

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Diff for: packages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md

+3
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type Options = {
104104
| 'operator'
105105
| 'tuple'
106106
| 'union'
107+
| 'nullish'
107108
)[];
108109
};
109110

@@ -122,6 +123,7 @@ const defaultOptions: Options = {
122123
'tuple',
123124
'intersection',
124125
'union',
126+
'nullish',
125127
],
126128
};
127129
```
@@ -142,3 +144,4 @@ The ordering of groups is determined by this option.
142144
- `operator` - Operator types (`keyof A`, `typeof B`, `readonly C[]`)
143145
- `tuple` - Tuple types (`[A, B, C]`)
144146
- `union` - Union types (`A | B`)
147+
- `nullish` - `null` and `undefined`

Diff for: packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ enum Group {
1212
import = 'import',
1313
intersection = 'intersection',
1414
keyword = 'keyword',
15+
nullish = 'nullish',
1516
literal = 'literal',
1617
named = 'named',
1718
object = 'object',
@@ -42,17 +43,19 @@ function getGroup(node: TSESTree.TypeNode): Group {
4243
case AST_NODE_TYPES.TSBigIntKeyword:
4344
case AST_NODE_TYPES.TSBooleanKeyword:
4445
case AST_NODE_TYPES.TSNeverKeyword:
45-
case AST_NODE_TYPES.TSNullKeyword:
4646
case AST_NODE_TYPES.TSNumberKeyword:
4747
case AST_NODE_TYPES.TSObjectKeyword:
4848
case AST_NODE_TYPES.TSStringKeyword:
4949
case AST_NODE_TYPES.TSSymbolKeyword:
5050
case AST_NODE_TYPES.TSThisType:
51-
case AST_NODE_TYPES.TSUndefinedKeyword:
5251
case AST_NODE_TYPES.TSUnknownKeyword:
5352
case AST_NODE_TYPES.TSVoidKeyword:
5453
return Group.keyword;
5554

55+
case AST_NODE_TYPES.TSNullKeyword:
56+
case AST_NODE_TYPES.TSUndefinedKeyword:
57+
return Group.nullish;
58+
5659
case AST_NODE_TYPES.TSLiteralType:
5760
case AST_NODE_TYPES.TSTemplateLiteralType:
5861
return Group.literal;
@@ -150,6 +153,7 @@ export default util.createRule<Options, MessageIds>({
150153
Group.tuple,
151154
Group.intersection,
152155
Group.union,
156+
Group.nullish,
153157
],
154158
},
155159
],

Diff for: packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ type T =
7373
${operator} (B & C)
7474
${operator} (A | B)
7575
${operator} (B | C)
76+
${operator} null
77+
${operator} undefined
7678
`,
7779
},
7880
];
@@ -184,6 +186,18 @@ const invalid = (
184186
},
185187
],
186188
},
189+
{
190+
code: `type T = () => undefined ${operator} null;`,
191+
output: `type T = () => null ${operator} undefined;`,
192+
errors: [
193+
{
194+
messageId: 'notSorted',
195+
data: {
196+
type,
197+
},
198+
},
199+
],
200+
},
187201
{
188202
code: noFormat`
189203
type T =
@@ -203,12 +217,14 @@ type T =
203217
${operator} number[]
204218
${operator} B
205219
${operator} A
220+
${operator} undefined
221+
${operator} null
206222
${operator} string
207223
${operator} any;
208224
`,
209225
output: noFormat`
210226
type T =
211-
A ${operator} B ${operator} number[] ${operator} string[] ${operator} any ${operator} string ${operator} readonly number[] ${operator} readonly string[] ${operator} 'a' ${operator} 'b' ${operator} "a" ${operator} "b" ${operator} (() => string) ${operator} (() => void) ${operator} { a: string } ${operator} { b: string } ${operator} [1, 2, 3] ${operator} [1, 2, 4];
227+
A ${operator} B ${operator} number[] ${operator} string[] ${operator} any ${operator} string ${operator} readonly number[] ${operator} readonly string[] ${operator} 'a' ${operator} 'b' ${operator} "a" ${operator} "b" ${operator} (() => string) ${operator} (() => void) ${operator} { a: string } ${operator} { b: string } ${operator} [1, 2, 3] ${operator} [1, 2, 4] ${operator} null ${operator} undefined;
212228
`,
213229
errors: [
214230
{

0 commit comments

Comments
 (0)