Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-empty-function

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 6
πŸ“¦ Imports 8
πŸ“Š Variables & Constants 2
πŸ“‘ Type Aliases 2

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-empty-function.ts

πŸ“€ Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-empty-function'
meta.type 'suggestion'
meta.docs.description 'Disallow empty functions'
meta.docs.extendsBaseRule true
meta.docs.recommended 'stylistic'
meta.hasSuggestions baseRule.meta.hasSuggestions
meta.messages baseRule.meta.messages
meta.schema [schema]

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
JSONSchema4 @typescript-eslint/utils/json-schema
AST_NODE_TYPES @typescript-eslint/utils
InferMessageIdsTypeFromRule ../util
InferOptionsTypeFromRule ../util
createRule ../util
deepMerge ../util
getESLintCoreRule ../util/getESLintCoreRule

Variables & Constants

Name Type Kind Value Exported
defaultOptions Options const [ { allow: [], }, ] βœ—
schema JSONSchema4 const deepMerge( // eslint-disable-next-line @typescript-eslint/no-unsafe-argument ... βœ—

Functions

create(context: any, [{ allow = [] }]: any): any

Parameters:

  • context any
  • [{ allow = [] }] any

Returns: any

Calls:

  • baseRule.create
  • allow.includes
  • node.params.some
  • isBodyEmpty
  • hasParameterProperties
  • isAllowedEmptyConstructor
  • isAllowedEmptyDecoratedFunctions
  • isAllowedEmptyOverrideMethod
  • rules.FunctionExpression

Internal Comments:

/**
     * Check if the method body is empty
     * @param node the node to be validated
     * @returns true if the body is empty
     * @private
     */
/**
     * Check if method has parameter properties
     * @param node the node to be validated
     * @returns true if the body has parameter properties
     * @private
     */
/**
     * @param node the node to be validated
     * @returns true if the constructor is allowed to be empty
     * @private
     */
// allow protected constructors (x3)
// allow private constructors
// allow constructors which have parameter properties (x2)
/**
     * @param node the node to be validated
     * @returns true if a function has decorators
     * @private
     */

Code
create(context, [{ allow = [] }]) {
    const rules = baseRule.create(context);

    const isAllowedProtectedConstructors = allow.includes(
      'protected-constructors',
    );
    const isAllowedPrivateConstructors = allow.includes('private-constructors');
    const isAllowedDecoratedFunctions = allow.includes('decoratedFunctions');
    const isAllowedOverrideMethods = allow.includes('overrideMethods');

    /**
     * Check if the method body is empty
     * @param node the node to be validated
     * @returns true if the body is empty
     * @private
     */
    function isBodyEmpty(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      return node.body.body.length === 0;
    }

    /**
     * Check if method has parameter properties
     * @param node the node to be validated
     * @returns true if the body has parameter properties
     * @private
     */
    function hasParameterProperties(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      return node.params.some(
        param => param.type === AST_NODE_TYPES.TSParameterProperty,
      );
    }

    /**
     * @param node the node to be validated
     * @returns true if the constructor is allowed to be empty
     * @private
     */
    function isAllowedEmptyConstructor(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      const parent = node.parent;
      if (
        isBodyEmpty(node) &&
        parent.type === AST_NODE_TYPES.MethodDefinition &&
        parent.kind === 'constructor'
      ) {
        const { accessibility } = parent;

        return (
          // allow protected constructors
          (accessibility === 'protected' && isAllowedProtectedConstructors) ||
          // allow private constructors
          (accessibility === 'private' && isAllowedPrivateConstructors) ||
          // allow constructors which have parameter properties
          hasParameterProperties(node)
        );
      }

      return false;
    }

    /**
     * @param node the node to be validated
     * @returns true if a function has decorators
     * @private
     */
    function isAllowedEmptyDecoratedFunctions(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
        const decorators =
          node.parent.type === AST_NODE_TYPES.MethodDefinition
            ? node.parent.decorators
            : undefined;
        return !!decorators && !!decorators.length;
      }

      return false;
    }

    function isAllowedEmptyOverrideMethod(
      node: TSESTree.FunctionExpression,
    ): boolean {
      return (
        isAllowedOverrideMethods &&
        isBodyEmpty(node) &&
        node.parent.type === AST_NODE_TYPES.MethodDefinition &&
        node.parent.override
      );
    }

    return {
      ...rules,
      FunctionExpression(node): void {
        if (
          isAllowedEmptyConstructor(node) ||
          isAllowedEmptyDecoratedFunctions(node) ||
          isAllowedEmptyOverrideMethod(node)
        ) {
          return;
        }

        rules.FunctionExpression(node);
      },
    };
  }

Internal helpers

Declared inside another function in this file.

isBodyEmpty(node: TSESTree.FunctionDeclaration | TSESTree…): boolean

Check if the method body is empty

Parameters:

  • node any: the node to be validated

Returns: undefined true if the body is empty

Tags: @private

Raw JSDoc
/**
     * Check if the method body is empty
     * @param node the node to be validated
     * @returns true if the body is empty
     * @private
     */
Code
function isBodyEmpty(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      return node.body.body.length === 0;
    }

hasParameterProperties(node: TSESTree.FunctionDeclaration | TSESTree…): boolean

Check if method has parameter properties

Parameters:

  • node any: the node to be validated

Returns: undefined true if the body has parameter properties

Tags: @private

Raw JSDoc
/**
     * Check if method has parameter properties
     * @param node the node to be validated
     * @returns true if the body has parameter properties
     * @private
     */

Calls:

  • node.params.some
Code
function hasParameterProperties(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      return node.params.some(
        param => param.type === AST_NODE_TYPES.TSParameterProperty,
      );
    }

isAllowedEmptyConstructor(node: TSESTree.FunctionDeclaration | TSESTree…): boolean

Parameters:

  • node any: the node to be validated

Returns: undefined true if the constructor is allowed to be empty

Tags: @private

Raw JSDoc
/**
     * @param node the node to be validated
     * @returns true if the constructor is allowed to be empty
     * @private
     */

Calls:

  • isBodyEmpty
  • hasParameterProperties

Internal Comments:

// allow protected constructors (x3)
// allow private constructors
// allow constructors which have parameter properties (x2)

Code
function isAllowedEmptyConstructor(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      const parent = node.parent;
      if (
        isBodyEmpty(node) &&
        parent.type === AST_NODE_TYPES.MethodDefinition &&
        parent.kind === 'constructor'
      ) {
        const { accessibility } = parent;

        return (
          // allow protected constructors
          (accessibility === 'protected' && isAllowedProtectedConstructors) ||
          // allow private constructors
          (accessibility === 'private' && isAllowedPrivateConstructors) ||
          // allow constructors which have parameter properties
          hasParameterProperties(node)
        );
      }

      return false;
    }

isAllowedEmptyDecoratedFunctions(node: TSESTree.FunctionDeclaration | TSESTree…): boolean

Parameters:

  • node any: the node to be validated

Returns: undefined true if a function has decorators

Tags: @private

Raw JSDoc
/**
     * @param node the node to be validated
     * @returns true if a function has decorators
     * @private
     */

Calls:

  • isBodyEmpty
Code
function isAllowedEmptyDecoratedFunctions(
      node: TSESTree.FunctionDeclaration | TSESTree.FunctionExpression,
    ): boolean {
      if (isAllowedDecoratedFunctions && isBodyEmpty(node)) {
        const decorators =
          node.parent.type === AST_NODE_TYPES.MethodDefinition
            ? node.parent.decorators
            : undefined;
        return !!decorators && !!decorators.length;
      }

      return false;
    }

isAllowedEmptyOverrideMethod(node: TSESTree.FunctionExpression): boolean

Parameters:

  • node TSESTree.FunctionExpression

Returns: boolean

Calls:

  • isBodyEmpty
Code
function isAllowedEmptyOverrideMethod(
      node: TSESTree.FunctionExpression,
    ): boolean {
      return (
        isAllowedOverrideMethods &&
        isBodyEmpty(node) &&
        node.parent.type === AST_NODE_TYPES.MethodDefinition &&
        node.parent.override
      );
    }

Type Aliases

Options

type Options = InferOptionsTypeFromRule<typeof baseRule>;

MessageIds

type MessageIds = InferMessageIdsTypeFromRule<typeof baseRule>;

Generated by Syntax Scribe