Skip to content

Commit

Permalink
feat(eslint-plugin): [no-invalid-void-type] better report message for…
Browse files Browse the repository at this point in the history
… 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 <!--tabs-->

* 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 <me@joshuakgoldberg.com>
Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
3 people committed Oct 29, 2022
1 parent 344322a commit d806bda
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
23 changes: 17 additions & 6 deletions packages/eslint-plugin/src/rules/no-invalid-void-type.ts
Expand Up @@ -13,7 +13,8 @@ type MessageIds =
| 'invalidVoidNotReturnOrGeneric'
| 'invalidVoidNotReturn'
| 'invalidVoidNotReturnOrThisParam'
| 'invalidVoidNotReturnOrThisParamOrGeneric';
| 'invalidVoidNotReturnOrThisParamOrGeneric'
| 'invalidVoidUnionConstituent';

export default util.createRule<[Options], MessageIds>({
name: 'no-invalid-void-type',
Expand All @@ -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: [
{
Expand Down Expand Up @@ -136,7 +139,7 @@ export default util.createRule<[Options], MessageIds>({
): void {
if (parentNode.default !== node) {
context.report({
messageId: 'invalidVoidNotReturnOrGeneric',
messageId: getNotReturnOrGenericMessageId(node),
node,
});
}
Expand Down Expand Up @@ -218,7 +221,7 @@ export default util.createRule<[Options], MessageIds>({
allowInGenericTypeArguments && allowAsThisParameter
? 'invalidVoidNotReturnOrThisParamOrGeneric'
: allowInGenericTypeArguments
? 'invalidVoidNotReturnOrGeneric'
? getNotReturnOrGenericMessageId(node)
: allowAsThisParameter
? 'invalidVoidNotReturnOrThisParam'
: 'invalidVoidNotReturn',
Expand All @@ -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';
}
26 changes: 23 additions & 3 deletions packages/eslint-plugin/tests/rules/no-invalid-void-type.test.ts
Expand Up @@ -316,7 +316,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
code: 'type UnionType2 = string | number | void;',
errors: [
{
messageId: 'invalidVoidNotReturnOrGeneric',
messageId: 'invalidVoidUnionConstituent',
line: 1,
column: 37,
},
Expand All @@ -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 extends number | void>(): T;',
errors: [
{
messageId: 'invalidVoidUnionConstituent',
line: 1,
column: 42,
},
],
},
{
code: 'type IntersectionType = string & number & void;',
errors: [
Expand Down Expand Up @@ -394,7 +414,7 @@ ruleTester.run('allowInGenericTypeArguments: true', rule, {
code: 'type invalidVoidUnion = void | Map<string, number>;',
errors: [
{
messageId: 'invalidVoidNotReturnOrGeneric',
messageId: 'invalidVoidUnionConstituent',
line: 1,
column: 25,
},
Expand Down

0 comments on commit d806bda

Please sign in to comment.