Skip to content
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

feat(eslint-plugin): allow using void as a default type for a generic argument if allowInGenericTypeArguments is specified #5671

Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
33 changes: 33 additions & 0 deletions packages/eslint-plugin/src/rules/no-invalid-void-type.ts
Expand Up @@ -127,6 +127,29 @@ export default util.createRule<[Options], MessageIds>({
}
}

/**
* @brief checks if the generic type parameter extends void
*/
function checkExtendedVoid(parentNode: TSESTree.TSTypeParameter): void {
if (parentNode.constraint?.type !== AST_NODE_TYPES.TSVoidKeyword) {
return;
}

context.report({
messageId: 'invalidVoidNotReturnOrGeneric',
node: parentNode.constraint,
});
}

/**
* @brief checks if the type of given argument is TSESTree.TSTypeParameter
*/
function isTSTypeParameter(
node: TSESTree.Node,
): node is TSESTree.TSTypeParameter {
return node.type === AST_NODE_TYPES.TSTypeParameter;
}
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved

/**
* @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 +185,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 &&
isTSTypeParameter(node.parent) &&
node.parent.default?.type === AST_NODE_TYPES.TSVoidKeyword
) {
checkExtendedVoid(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
38 changes: 5 additions & 33 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 @@ -162,7 +154,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 41,
column: 34,
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
},
],
},
Expand All @@ -176,16 +168,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 @@ -197,7 +179,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 44,
column: 37,
},
],
},
Expand All @@ -211,16 +193,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 @@ -232,7 +204,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
{
messageId: 'invalidVoidNotReturnOrGeneric',
line: 1,
column: 56,
column: 49,
},
],
},
Expand Down