From bb46ef0817fe03ef71f8e0f3df0cf96bc355e068 Mon Sep 17 00:00:00 2001 From: kazizi <50566849+kazizi55@users.noreply.github.com> Date: Sun, 2 Oct 2022 13:28:37 +0900 Subject: [PATCH] feat(eslint-plugin): allow using void as a default type for a generic 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 * Update packages/eslint-plugin/src/rules/no-invalid-void-type.ts Co-authored-by: Josh Goldberg * fix: rename function name * refactor: delete isTSTypeParameter and inline * refactor: checkDefaultVoid Co-authored-by: Josh Goldberg --- .../src/rules/no-invalid-void-type.ts | 25 ++++++++++ .../tests/rules/no-invalid-void-type.test.ts | 47 +------------------ 2 files changed, 27 insertions(+), 45 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-invalid-void-type.ts b/packages/eslint-plugin/src/rules/no-invalid-void-type.ts index 6b9960e9d16..7cda184e4fd 100644 --- a/packages/eslint-plugin/src/rules/no-invalid-void-type.ts +++ b/packages/eslint-plugin/src/rules/no-invalid-void-type.ts @@ -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 @@ -162,6 +177,16 @@ export default util.createRule<[Options], MessageIds>({ return; } + // allow 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 && diff --git a/packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts b/packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts index 0141da093b5..8164e85b2bd 100644 --- a/packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts +++ b/packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts @@ -119,6 +119,8 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { 'type Generic = [T];', 'type voidPromiseUnion = void | Promise;', 'type promiseNeverUnion = Promise | never;', + 'const arrowGeneric1 = (arg: T) => {};', + 'declare function functionDeclaration1(arg: T): void;', ], invalid: [ { @@ -141,16 +143,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { }, ], }, - { - code: 'const arrowGeneric1 = (arg: T) => {};', - errors: [ - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 28, - }, - ], - }, { code: 'const arrowGeneric2 = (arg: T) => {};', errors: [ @@ -159,11 +151,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { line: 1, column: 34, }, - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 41, - }, ], }, { @@ -176,16 +163,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { }, ], }, - { - code: 'function functionGeneric1(arg: T) {}', - errors: [ - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 31, - }, - ], - }, { code: 'function functionGeneric2(arg: T) {}', errors: [ @@ -194,11 +171,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { line: 1, column: 37, }, - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 44, - }, ], }, { @@ -211,16 +183,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { }, ], }, - { - code: 'declare function functionDeclaration1(arg: T): void;', - errors: [ - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 43, - }, - ], - }, { code: 'declare function functionDeclaration2(arg: T): void;', errors: [ @@ -229,11 +191,6 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { line: 1, column: 49, }, - { - messageId: 'invalidVoidNotReturnOrGeneric', - line: 1, - column: 56, - }, ], }, {