📄 no-invalid-void-type.ts
¶
📊 Analysis Summary¶
Metric | Count |
---|---|
🔧 Functions | 5 |
📦 Imports | 4 |
📊 Variables & Constants | 4 |
📐 Interfaces | 1 |
📑 Type Aliases | 1 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/rules/no-invalid-void-type.ts
📦 Imports¶
Name | Source |
---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
createRule |
../util |
hasOverloadSignatures |
../util |
Variables & Constants¶
Name | Type | Kind | Value | Exported |
---|---|---|---|---|
validParents |
AST_NODE_TYPES[] |
const | `[ | |
AST_NODE_TYPES.TSTypeAnnotation, // | ||||
]` | ✗ | |||
invalidGrandParents |
AST_NODE_TYPES[] |
const | `[ | |
AST_NODE_TYPES.TSPropertySignature, | ||||
AST_NODE_TYPES.CallExpression, | ||||
AST_NODE_TYPES.PropertyDefinition, | ||||
AST_NODE_TYPES.AccessorProperty, | ||||
AST_NODE_TYPES.Identifier, | ||||
]` | ✗ | |||
validUnionMembers |
AST_NODE_TYPES[] |
const | `[ | |
AST_NODE_TYPES.TSVoidKeyword, | ||||
AST_NODE_TYPES.TSNeverKeyword, | ||||
]` | ✗ | |||
current |
any |
let/var | node.parent |
✗ |
Functions¶
checkGenericTypeArgument(node: TSESTree.TSVoidKeyword): void
¶
Code
function checkGenericTypeArgument(node: TSESTree.TSVoidKeyword): void {
// only matches T<..., void, ...>
// extra check for precaution
/* istanbul ignore next */
if (
node.parent.type !== AST_NODE_TYPES.TSTypeParameterInstantiation ||
node.parent.parent.type !== AST_NODE_TYPES.TSTypeReference
) {
return;
}
// check allowlist
if (Array.isArray(allowInGenericTypeArguments)) {
const fullyQualifiedName = context.sourceCode
.getText(node.parent.parent.typeName)
.replaceAll(' ', '');
if (
!allowInGenericTypeArguments
.map(s => s.replaceAll(' ', ''))
.includes(fullyQualifiedName)
) {
context.report({
node,
messageId: 'invalidVoidForGeneric',
data: { generic: fullyQualifiedName },
});
}
return;
}
if (!allowInGenericTypeArguments) {
context.report({
node,
messageId: allowAsThisParameter
? 'invalidVoidNotReturnOrThisParam'
: 'invalidVoidNotReturn',
});
}
}
-
JSDoc:
-
Parameters:
node: TSESTree.TSVoidKeyword
- Return Type:
void
- Calls:
Array.isArray
context.sourceCode .getText(node.parent.parent.typeName) .replaceAll
allowInGenericTypeArguments .map(s => s.replaceAll(' ', '')) .includes
context.report
- Internal Comments:
checkDefaultVoid(node: TSESTree.TSVoidKeyword, parentNode: TSESTree.TSTypeParameter): void
¶
Code
-
JSDoc:
-
Parameters:
node: TSESTree.TSVoidKeyword
parentNode: TSESTree.TSTypeParameter
- Return Type:
void
- Calls:
context.report
getNotReturnOrGenericMessageId
isValidUnionType(node: TSESTree.TSUnionType): boolean
¶
Code
function isValidUnionType(node: TSESTree.TSUnionType): boolean {
return node.types.every(
member =>
validUnionMembers.includes(member.type) ||
// allows any T<..., void, ...> here, checked by checkGenericTypeArgument
(member.type === AST_NODE_TYPES.TSTypeReference &&
member.typeArguments?.type ===
AST_NODE_TYPES.TSTypeParameterInstantiation &&
member.typeArguments.params
.map(param => param.type)
.includes(AST_NODE_TYPES.TSVoidKeyword)),
);
}
-
JSDoc:
-
Parameters:
node: TSESTree.TSUnionType
- Return Type:
boolean
- Calls:
node.types.every
validUnionMembers.includes
member.typeArguments.params .map(param => param.type) .includes
- Internal Comments:
getNotReturnOrGenericMessageId(node: TSESTree.TSVoidKeyword): MessageIds
¶
Code
- Parameters:
node: TSESTree.TSVoidKeyword
- Return Type:
MessageIds
getParentFunctionDeclarationNode(node: TSESTree.Node): TSESTree.FunctionDeclaration | TSESTree.MethodDefinition | null
¶
Code
function getParentFunctionDeclarationNode(
node: TSESTree.Node,
): TSESTree.FunctionDeclaration | TSESTree.MethodDefinition | null {
let current = node.parent;
while (current) {
if (current.type === AST_NODE_TYPES.FunctionDeclaration) {
return current;
}
if (
current.type === AST_NODE_TYPES.MethodDefinition &&
current.value.body != null
) {
return current;
}
current = current.parent;
}
return null;
}
- Parameters:
node: TSESTree.Node
- Return Type:
TSESTree.FunctionDeclaration | TSESTree.MethodDefinition | null
Interfaces¶
Options
¶
Interface Code
Properties¶
Name | Type | Optional | Description |
---|---|---|---|
allowAsThisParameter |
boolean |
✓ | |
allowInGenericTypeArguments |
boolean | [string, ...string[]] |
✓ |
Type Aliases¶
MessageIds
¶
type MessageIds = | 'invalidVoidForGeneric'
| 'invalidVoidNotReturn'
| 'invalidVoidNotReturnOrGeneric'
| 'invalidVoidNotReturnOrThisParam'
| 'invalidVoidNotReturnOrThisParamOrGeneric'
| 'invalidVoidUnionConstituent';