Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat(eslint-plugin): [sort-type-union-intersection-members] add nulli…
…sh group (#2919)
  • Loading branch information
jgeurts committed Jan 5, 2021
1 parent f606846 commit 5558f41
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Expand Up @@ -104,6 +104,7 @@ type Options = {
| 'operator'
| 'tuple'
| 'union'
| 'nullish'
)[];
};

Expand All @@ -122,6 +123,7 @@ const defaultOptions: Options = {
'tuple',
'intersection',
'union',
'nullish',
],
};
```
Expand All @@ -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`
Expand Up @@ -12,6 +12,7 @@ enum Group {
import = 'import',
intersection = 'intersection',
keyword = 'keyword',
nullish = 'nullish',
literal = 'literal',
named = 'named',
object = 'object',
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -150,6 +153,7 @@ export default util.createRule<Options, MessageIds>({
Group.tuple,
Group.intersection,
Group.union,
Group.nullish,
],
},
],
Expand Down
Expand Up @@ -73,6 +73,8 @@ type T =
${operator} (B & C)
${operator} (A | B)
${operator} (B | C)
${operator} null
${operator} undefined
`,
},
];
Expand Down Expand Up @@ -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 =
Expand All @@ -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: [
{
Expand Down

0 comments on commit 5558f41

Please sign in to comment.