Skip to content

⬅️ Back to Table of Contents

📄 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:

    /**
         * @brief check if the given void keyword is used as a valid generic type
         *
         * reports if the type parametrized by void is not in the allowlist, or
         * allowInGenericTypeArguments is false.
         * no-op if the given void keyword is not used as generic type
         */
    

  • 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:
    // only matches T<..., void, ...>
    // extra check for precaution
    /* istanbul ignore next */
    // check allowlist
    

checkDefaultVoid(node: TSESTree.TSVoidKeyword, parentNode: TSESTree.TSTypeParameter): void

Code
function checkDefaultVoid(
      node: TSESTree.TSVoidKeyword,
      parentNode: TSESTree.TSTypeParameter,
    ): void {
      if (parentNode.default !== node) {
        context.report({
          node,
          messageId: getNotReturnOrGenericMessageId(node),
        });
      }
    }
  • JSDoc:

    /**
         * @brief checks if the generic type parameter defaults to void
         */
    

  • 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:

    /**
         * @brief checks that a union containing void is valid
         * @return true if every member of the union is specified as a valid type in
         * validUnionMembers, or is a valid generic type parametrized by void
         */
    

  • Parameters:

  • node: TSESTree.TSUnionType
  • Return Type: boolean
  • Calls:
  • node.types.every
  • validUnionMembers.includes
  • member.typeArguments.params .map(param => param.type) .includes
  • Internal Comments:
    // allows any T<..., void, ...> here, checked by checkGenericTypeArgument
    

getNotReturnOrGenericMessageId(node: TSESTree.TSVoidKeyword): MessageIds

Code
function getNotReturnOrGenericMessageId(
  node: TSESTree.TSVoidKeyword,
): MessageIds {
  return node.parent.type === AST_NODE_TYPES.TSUnionType
    ? 'invalidVoidUnionConstituent'
    : 'invalidVoidNotReturnOrGeneric';
}
  • 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
export interface Options {
  allowAsThisParameter?: boolean;
  allowInGenericTypeArguments?: boolean | [string, ...string[]];
}

Properties

Name Type Optional Description
allowAsThisParameter boolean
allowInGenericTypeArguments boolean | [string, ...string[]]

Type Aliases

MessageIds

type MessageIds = | 'invalidVoidForGeneric'
  | 'invalidVoidNotReturn'
  | 'invalidVoidNotReturnOrGeneric'
  | 'invalidVoidNotReturnOrThisParam'
  | 'invalidVoidNotReturnOrThisParamOrGeneric'
  | 'invalidVoidUnionConstituent';