Skip to content

feat(eslint-plugin): [sort-type-union-intersection-members] add nullish group #2919

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
merged 1 commit into from
Jan 5, 2021
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
Original file line number Diff line number Diff line change
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`
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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