From 5558f410007da58a3f4726bbf9501c924ef166a1 Mon Sep 17 00:00:00 2001 From: Jim Geurts Date: Tue, 5 Jan 2021 13:58:48 -0600 Subject: [PATCH] feat(eslint-plugin): [sort-type-union-intersection-members] add nullish group (#2919) --- .../sort-type-union-intersection-members.md | 3 +++ .../sort-type-union-intersection-members.ts | 8 ++++++-- ...ort-type-union-intersection-members.test.ts | 18 +++++++++++++++++- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md b/packages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md index ecc6fcb13c7..43ef069a716 100644 --- a/packages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md +++ b/packages/eslint-plugin/docs/rules/sort-type-union-intersection-members.md @@ -104,6 +104,7 @@ type Options = { | 'operator' | 'tuple' | 'union' + | 'nullish' )[]; }; @@ -122,6 +123,7 @@ const defaultOptions: Options = { 'tuple', 'intersection', 'union', + 'nullish', ], }; ``` @@ -142,3 +144,4 @@ The ordering of groups is determined by this option. - `operator` - Operator types (`keyof A`, `typeof B`, `readonly C[]`) - `tuple` - Tuple types (`[A, B, C]`) - `union` - Union types (`A | B`) +- `nullish` - `null` and `undefined` diff --git a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts index 5014fd33c24..db7bd7b658e 100644 --- a/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts +++ b/packages/eslint-plugin/src/rules/sort-type-union-intersection-members.ts @@ -12,6 +12,7 @@ enum Group { import = 'import', intersection = 'intersection', keyword = 'keyword', + nullish = 'nullish', literal = 'literal', named = 'named', object = 'object', @@ -42,17 +43,19 @@ function getGroup(node: TSESTree.TypeNode): Group { case AST_NODE_TYPES.TSBigIntKeyword: case AST_NODE_TYPES.TSBooleanKeyword: case AST_NODE_TYPES.TSNeverKeyword: - case AST_NODE_TYPES.TSNullKeyword: case AST_NODE_TYPES.TSNumberKeyword: case AST_NODE_TYPES.TSObjectKeyword: case AST_NODE_TYPES.TSStringKeyword: case AST_NODE_TYPES.TSSymbolKeyword: case AST_NODE_TYPES.TSThisType: - case AST_NODE_TYPES.TSUndefinedKeyword: case AST_NODE_TYPES.TSUnknownKeyword: case AST_NODE_TYPES.TSVoidKeyword: return Group.keyword; + case AST_NODE_TYPES.TSNullKeyword: + case AST_NODE_TYPES.TSUndefinedKeyword: + return Group.nullish; + case AST_NODE_TYPES.TSLiteralType: case AST_NODE_TYPES.TSTemplateLiteralType: return Group.literal; @@ -150,6 +153,7 @@ export default util.createRule({ Group.tuple, Group.intersection, Group.union, + Group.nullish, ], }, ], diff --git a/packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts b/packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts index 469becaf000..bcf07490e61 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-union-intersection-members.test.ts @@ -73,6 +73,8 @@ type T = ${operator} (B & C) ${operator} (A | B) ${operator} (B | C) + ${operator} null + ${operator} undefined `, }, ]; @@ -184,6 +186,18 @@ const invalid = ( }, ], }, + { + code: `type T = () => undefined ${operator} null;`, + output: `type T = () => null ${operator} undefined;`, + errors: [ + { + messageId: 'notSorted', + data: { + type, + }, + }, + ], + }, { code: noFormat` type T = @@ -203,12 +217,14 @@ type T = ${operator} number[] ${operator} B ${operator} A + ${operator} undefined + ${operator} null ${operator} string ${operator} any; `, output: noFormat` type T = - 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]; + 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; `, errors: [ {