Skip to content

Commit

Permalink
feat(eslint-plugin): allow using void as a default type for a generic…
Browse files Browse the repository at this point in the history
… argument if allowInGenericTypeArguments is specified (#5671)

* feat: allow using void as a default type for a generic argument

* test: allow using void as a default type for a generic argument

* fix: build error

* fix: replace any

* test: remove duplicate error

* fix: report invalidVoidNotReturnOrGeneric only one time

* Update packages/eslint-plugin/src/rules/no-invalid-void-type.ts

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

* Update packages/eslint-plugin/src/rules/no-invalid-void-type.ts

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>

* fix: rename function name

* refactor: delete isTSTypeParameter and inline

* refactor: checkDefaultVoid

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
kazizi55 and JoshuaKGoldberg committed Oct 2, 2022
1 parent 5adf7bd commit bb46ef0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 45 deletions.
25 changes: 25 additions & 0 deletions packages/eslint-plugin/src/rules/no-invalid-void-type.ts
Expand Up @@ -127,6 +127,21 @@ export default util.createRule<[Options], MessageIds>({
}
}

/**
* @brief checks if the generic type parameter defaults to void
*/
function checkDefaultVoid(
node: TSESTree.TSVoidKeyword,
parentNode: TSESTree.TSTypeParameter,
): void {
if (parentNode.default !== node) {
context.report({
messageId: 'invalidVoidNotReturnOrGeneric',
node,
});
}
}

/**
* @brief checks that a union containing void is valid
* @return true if every member of the union is specified as a valid type in
Expand Down Expand Up @@ -162,6 +177,16 @@ export default util.createRule<[Options], MessageIds>({
return;
}

// allow <T = void> if allowInGenericTypeArguments is specified, and report if the generic type parameter extends void
if (
allowInGenericTypeArguments &&
node.parent.type === AST_NODE_TYPES.TSTypeParameter &&
node.parent.default?.type === AST_NODE_TYPES.TSVoidKeyword
) {
checkDefaultVoid(node, node.parent);
return;
}

// union w/ void must contain types from validUnionMembers, or a valid generic void type
if (
node.parent.type === AST_NODE_TYPES.TSUnionType &&
Expand Down
47 changes: 2 additions & 45 deletions packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts
Expand Up @@ -119,6 +119,8 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
'type Generic<T> = [T];',
'type voidPromiseUnion = void | Promise<void>;',
'type promiseNeverUnion = Promise<void> | never;',
'const arrowGeneric1 = <T = void>(arg: T) => {};',
'declare function functionDeclaration1<T = void>(arg: T): void;',
],
invalid: [
{
Expand All @@ -141,16 +143,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
},
],
},
{
code: 'const arrowGeneric1 = <T = void>(arg: T) => {};',
errors: [
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 28,
},
],
},
{
code: 'const arrowGeneric2 = <T extends void = void>(arg: T) => {};',
errors: [
Expand All @@ -159,11 +151,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
line: 1,
column: 34,
},
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 41,
},
],
},
{
Expand All @@ -176,16 +163,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
},
],
},
{
code: 'function functionGeneric1<T = void>(arg: T) {}',
errors: [
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 31,
},
],
},
{
code: 'function functionGeneric2<T extends void = void>(arg: T) {}',
errors: [
Expand All @@ -194,11 +171,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
line: 1,
column: 37,
},
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 44,
},
],
},
{
Expand All @@ -211,16 +183,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
},
],
},
{
code: 'declare function functionDeclaration1<T = void>(arg: T): void;',
errors: [
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 43,
},
],
},
{
code: 'declare function functionDeclaration2<T extends void = void>(arg: T): void;',
errors: [
Expand All @@ -229,11 +191,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
line: 1,
column: 49,
},
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 56,
},
],
},
{
Expand Down

0 comments on commit bb46ef0

Please sign in to comment.