From d806bda82343712a24e3c78b9b34d4345dd1de3b Mon Sep 17 00:00:00 2001 From: kmin-jeong <53456037+kmin-jeong@users.noreply.github.com> Date: Sat, 29 Oct 2022 21:45:53 +0900 Subject: [PATCH] feat(eslint-plugin): [no-invalid-void-type] better report message for void used as a constituent inside a function return type (#5274) * feat: update repository * remove duplicate examples * feat:add about re-exporting --isolatedModules in When Not To Use It * feat: add exmaples * fix: remove duplicate examples in correct * fix:correct words * fix: fix space-between * fix: fix code * fix: fix code space * fix: check error * fix:missed examples * feat: read review and fix them fix examples and --Isolatedmodules explain * feat: add two taps one is correct, another is incorrect * fix: add * feat: fix explaination about `isolatedmodules` * fix: fix explain about `isolatedModules` * fis: modify When no to use it * fix: revert change log * fix: add lint * fix: add lint * add: fix code * fix:fix docs splits * feat: add lint * Update packages/eslint-plugin/docs/rules/consistent-type-exports.md * feat:change error message better * fix:separating message and made types about union * fix: separate error message * fix:fix mistake type * fix:fix typo * fix:fix error message in case of union * fix:fix error message in InvalidVoidForUnion * fix: add logic about union * feat: add test case * Switched change to just enhance error message * oops comment Co-authored-by: Josh Goldberg Co-authored-by: Josh Goldberg --- .../src/rules/no-invalid-void-type.ts | 23 +++++++++++----- .../tests/rules/no-invalid-void-type.test.ts | 26 ++++++++++++++++--- 2 files changed, 40 insertions(+), 9 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 7cda184e4fd..9f938ac438f 100644 --- a/packages/eslint-plugin/src/rules/no-invalid-void-type.ts +++ b/packages/eslint-plugin/src/rules/no-invalid-void-type.ts @@ -13,7 +13,8 @@ type MessageIds = | 'invalidVoidNotReturnOrGeneric' | 'invalidVoidNotReturn' | 'invalidVoidNotReturnOrThisParam' - | 'invalidVoidNotReturnOrThisParamOrGeneric'; + | 'invalidVoidNotReturnOrThisParamOrGeneric' + | 'invalidVoidUnionConstituent'; export default util.createRule<[Options], MessageIds>({ name: 'no-invalid-void-type', @@ -25,14 +26,16 @@ export default util.createRule<[Options], MessageIds>({ }, messages: { invalidVoidForGeneric: - '{{ generic }} may not have void as a type variable.', + '{{ generic }} may not have void as a type argument.', invalidVoidNotReturnOrGeneric: - 'void is only valid as a return type or generic type variable.', + 'void is only valid as a return type or generic type argument.', invalidVoidNotReturn: 'void is only valid as a return type.', invalidVoidNotReturnOrThisParam: 'void is only valid as return type or type of `this` parameter.', invalidVoidNotReturnOrThisParamOrGeneric: - 'void is only valid as a return type or generic type variable or the type of a `this` parameter.', + 'void is only valid as a return type or generic type argument or the type of a `this` parameter.', + invalidVoidUnionConstituent: + 'void is not valid as a constituent in a union type', }, schema: [ { @@ -136,7 +139,7 @@ export default util.createRule<[Options], MessageIds>({ ): void { if (parentNode.default !== node) { context.report({ - messageId: 'invalidVoidNotReturnOrGeneric', + messageId: getNotReturnOrGenericMessageId(node), node, }); } @@ -218,7 +221,7 @@ export default util.createRule<[Options], MessageIds>({ allowInGenericTypeArguments && allowAsThisParameter ? 'invalidVoidNotReturnOrThisParamOrGeneric' : allowInGenericTypeArguments - ? 'invalidVoidNotReturnOrGeneric' + ? getNotReturnOrGenericMessageId(node) : allowAsThisParameter ? 'invalidVoidNotReturnOrThisParam' : 'invalidVoidNotReturn', @@ -228,3 +231,11 @@ export default util.createRule<[Options], MessageIds>({ }; }, }); + +function getNotReturnOrGenericMessageId( + node: TSESTree.TSVoidKeyword, +): MessageIds { + return node.parent!.type === AST_NODE_TYPES.TSUnionType + ? 'invalidVoidUnionConstituent' + : 'invalidVoidNotReturnOrGeneric'; +} 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 8164e85b2bd..1a972b665be 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 @@ -316,7 +316,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { code: 'type UnionType2 = string | number | void;', errors: [ { - messageId: 'invalidVoidNotReturnOrGeneric', + messageId: 'invalidVoidUnionConstituent', line: 1, column: 37, }, @@ -326,12 +326,32 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { code: 'type UnionType3 = string | ((number & any) | (string | void));', errors: [ { - messageId: 'invalidVoidNotReturnOrGeneric', + messageId: 'invalidVoidUnionConstituent', line: 1, column: 56, }, ], }, + { + code: 'declare function test(): number | void;', + errors: [ + { + messageId: 'invalidVoidUnionConstituent', + line: 1, + column: 35, + }, + ], + }, + { + code: 'declare function test(): T;', + errors: [ + { + messageId: 'invalidVoidUnionConstituent', + line: 1, + column: 42, + }, + ], + }, { code: 'type IntersectionType = string & number & void;', errors: [ @@ -394,7 +414,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, { code: 'type invalidVoidUnion = void | Map;', errors: [ { - messageId: 'invalidVoidNotReturnOrGeneric', + messageId: 'invalidVoidUnionConstituent', line: 1, column: 25, },