Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): createno-invalid-void-type
rule#1847
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
bradzacher merged 8 commits intotypescript-eslint:masterfromG-Rath:create-invalid-void-ruleApr 26, 2020
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
409ffd5
feat(eslint-plugin): create `invalid-void` rule
G-Rathfbe207e
chore(eslint-plugin): improve docs for `invalid-void`
G-Rathd832678
chore(eslint-plugin): rename rule to`no-invalid-void-type`
G-Ratha7be7ba
chore(eslint-plugin): update documentation
G-Rath9f7f074
chore(eslint-plugin): use `sourceCode.getText`
G-Rathde88e19
chore(eslint-plugin): rename `allowGenerics` option
G-Rathbc73556
chore(eslint-plugin): support spaces in types
G-Rathbed8d5a
Merge branch 'master' into create-invalid-void-rule
bradzacherFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionspackages/eslint-plugin/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/ROADMAP.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-invalid-void-type.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# Disallows usage of `void` type outside of generic or return types (`no-invalid-void-type`) | ||
Disallows usage of `void` type outside of return types or generic type arguments. | ||
If `void` is used as return type, it shouldn’t be a part of intersection/union type. | ||
## Rationale | ||
The `void` type means “nothing” or that a function does not return any value, | ||
in contrast with implicit `undefined` type which means that a function returns a value `undefined`. | ||
So “nothing” cannot be mixed with any other types. If you need this - use the `undefined` type instead. | ||
## Rule Details | ||
This rule aims to ensure that the `void` type is only used in valid places. | ||
The following patterns are considered warnings: | ||
```ts | ||
type PossibleValues = string | number | void; | ||
type MorePossibleValues = string | ((number & any) | (string | void)); | ||
function logSomething(thing: void) {} | ||
function printArg<T = void>(arg: T) {} | ||
logAndReturn<void>(undefined); | ||
interface Interface { | ||
lambda: () => void; | ||
prop: void; | ||
} | ||
class MyClass { | ||
private readonly propName: void; | ||
} | ||
``` | ||
The following patterns are not considered warnings: | ||
```ts | ||
type NoOp = () => void; | ||
function noop(): void {} | ||
let trulyUndefined = void 0; | ||
async function promiseMeSomething(): Promise<void> {} | ||
``` | ||
### Options | ||
G-Rath marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
```ts | ||
interface Options { | ||
allowInGenericTypeArguments?: boolean | string[]; | ||
} | ||
const defaultOptions: Options = { | ||
allowInGenericTypeArguments: true, | ||
}; | ||
``` | ||
#### `allowInGenericTypeArguments` | ||
This option lets you control if `void` can be used as a valid value for generic type parameters. | ||
Alternatively, you can provide an array of strings which whitelist which types may accept `void` as a generic type parameter. | ||
This option is `true` by default. | ||
The following patterns are considered warnings with `{ allowInGenericTypeArguments: false }`: | ||
```ts | ||
logAndReturn<void>(undefined); | ||
let voidPromise: Promise<void> = new Promise<void>(() => {}); | ||
let voidMap: Map<string, void> = new Map<string, void>(); | ||
``` | ||
The following patterns are considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`: | ||
```ts | ||
logAndReturn<void>(undefined); | ||
type NotAllowedVoid1 = Mx.Tx<void>; | ||
type NotAllowedVoid2 = Tx<void>; | ||
type NotAllowedVoid3 = Promise<void>; | ||
``` | ||
The following patterns are not considered warnings with `{ allowInGenericTypeArguments: ['Ex.Mx.Tx'] }`: | ||
```ts | ||
type AllowedVoid = Ex.MX.Tx<void>; | ||
``` | ||
## When Not To Use It | ||
If you don't care about if `void` is used with other types, | ||
or in invalid places, then you don't need this rule. | ||
## Compatibility | ||
- TSLint: [invalid-void](https://palantir.github.io/tslint/rules/invalid-void/) |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletionspackages/eslint-plugin/src/rules/no-invalid-void-type.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import * as util from '../util'; | ||
interface Options { | ||
allowInGenericTypeArguments: boolean | string[]; | ||
} | ||
type MessageIds = | ||
| 'invalidVoidForGeneric' | ||
| 'invalidVoidNotReturnOrGeneric' | ||
| 'invalidVoidNotReturn'; | ||
export default util.createRule<[Options], MessageIds>({ | ||
name: 'no-invalid-void-type', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Disallows usage of `void` type outside of generic or return types', | ||
category: 'Best Practices', | ||
recommended: false, | ||
}, | ||
messages: { | ||
invalidVoidForGeneric: | ||
'{{ generic }} may not have void as a type variable', | ||
invalidVoidNotReturnOrGeneric: | ||
'void is only valid as a return type or generic type variable', | ||
invalidVoidNotReturn: 'void is only valid as a return type', | ||
}, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
allowInGenericTypeArguments: { | ||
oneOf: [ | ||
{ type: 'boolean' }, | ||
{ | ||
type: 'array', | ||
items: { type: 'string' }, | ||
minLength: 1, | ||
}, | ||
], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}, | ||
defaultOptions: [{ allowInGenericTypeArguments: true }], | ||
create(context, [{ allowInGenericTypeArguments }]) { | ||
const validParents: AST_NODE_TYPES[] = [ | ||
AST_NODE_TYPES.TSTypeAnnotation, // | ||
]; | ||
const invalidGrandParents: AST_NODE_TYPES[] = [ | ||
AST_NODE_TYPES.TSPropertySignature, | ||
AST_NODE_TYPES.CallExpression, | ||
AST_NODE_TYPES.ClassProperty, | ||
AST_NODE_TYPES.Identifier, | ||
]; | ||
if (allowInGenericTypeArguments === true) { | ||
validParents.push(AST_NODE_TYPES.TSTypeParameterInstantiation); | ||
} | ||
return { | ||
TSVoidKeyword(node: TSESTree.TSVoidKeyword): void { | ||
/* istanbul ignore next */ | ||
if (!node.parent?.parent) { | ||
return; | ||
} | ||
if ( | ||
validParents.includes(node.parent.type) && | ||
!invalidGrandParents.includes(node.parent.parent.type) | ||
) { | ||
return; | ||
} | ||
if ( | ||
node.parent.type === AST_NODE_TYPES.TSTypeParameterInstantiation && | ||
node.parent.parent.type === AST_NODE_TYPES.TSTypeReference && | ||
Array.isArray(allowInGenericTypeArguments) | ||
) { | ||
const sourceCode = context.getSourceCode(); | ||
const fullyQualifiedName = sourceCode | ||
.getText(node.parent.parent.typeName) | ||
.replace(/ /gu, ''); | ||
if ( | ||
!allowInGenericTypeArguments | ||
.map(s => s.replace(/ /gu, '')) | ||
.includes(fullyQualifiedName) | ||
) { | ||
context.report({ | ||
messageId: 'invalidVoidForGeneric', | ||
data: { generic: fullyQualifiedName }, | ||
node, | ||
}); | ||
} | ||
return; | ||
} | ||
context.report({ | ||
messageId: allowInGenericTypeArguments | ||
? 'invalidVoidNotReturnOrGeneric' | ||
: 'invalidVoidNotReturn', | ||
node, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.