Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-misused-new

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 3
πŸ“¦ Imports 3

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-misused-new.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'no-misused-new'
meta.type 'problem'
meta.docs.description 'Enforce valid definition of new and constructor'
meta.docs.recommended 'recommended'
meta.messages.errorMessageClass 'Class cannot have method named new.'
meta.messages.errorMessageInterface 'Interfaces cannot be constructed, only classes.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
createRule ../util

Functions

create(context: any): { "ClassBody > MethodDefinition[key.name='new']"(node: TSES…

Parameters:

  • context any

Returns: { "ClassBody > MethodDefinition[key.name='new']"(node: TSESTree.MethodDefinition): void; 'TSInterfaceBody > TSConstructSignatureDeclaration'(node: TSESTree.TSConstructSignatureDeclaration): void; "TSMethodSignature[key.name='constructor']"(node: TSESTree.TSMethodSignature): void; }

Calls:

  • getTypeReferenceName
  • isMatchingParentType
  • context.report

Internal Comments:

/**
     * @param node type to be inspected.
     * @returns name of simple type or null
     */
/**
     * @param parent parent node.
     * @param returnType type to be compared
     */
// constructor (x4)

Code
create(context) {
    /**
     * @param node type to be inspected.
     * @returns name of simple type or null
     */
    function getTypeReferenceName(
      node:
        | TSESTree.EntityName
        | TSESTree.TSTypeAnnotation
        | TSESTree.TypeNode
        | undefined,
    ): string | null {
      if (node) {
        switch (node.type) {
          case AST_NODE_TYPES.TSTypeAnnotation:
            return getTypeReferenceName(node.typeAnnotation);
          case AST_NODE_TYPES.TSTypeReference:
            return getTypeReferenceName(node.typeName);
          case AST_NODE_TYPES.Identifier:
            return node.name;
          default:
            break;
        }
      }
      return null;
    }

    /**
     * @param parent parent node.
     * @param returnType type to be compared
     */
    function isMatchingParentType(
      parent:
        | TSESTree.ClassDeclaration
        | TSESTree.ClassExpression
        | TSESTree.Identifier
        | TSESTree.TSInterfaceDeclaration
        | undefined,
      returnType: TSESTree.TSTypeAnnotation | undefined,
    ): boolean {
      if (
        parent &&
        (parent.type === AST_NODE_TYPES.ClassDeclaration ||
          parent.type === AST_NODE_TYPES.ClassExpression ||
          parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) &&
        parent.id
      ) {
        return getTypeReferenceName(returnType) === parent.id.name;
      }
      return false;
    }

    return {
      "ClassBody > MethodDefinition[key.name='new']"(
        node: TSESTree.MethodDefinition,
      ): void {
        if (
          node.value.type === AST_NODE_TYPES.TSEmptyBodyFunctionExpression &&
          isMatchingParentType(node.parent.parent, node.value.returnType)
        ) {
          context.report({
            node,
            messageId: 'errorMessageClass',
          });
        }
      },
      'TSInterfaceBody > TSConstructSignatureDeclaration'(
        node: TSESTree.TSConstructSignatureDeclaration,
      ): void {
        if (
          isMatchingParentType(
            node.parent.parent as TSESTree.TSInterfaceDeclaration,
            node.returnType,
          )
        ) {
          // constructor
          context.report({
            node,
            messageId: 'errorMessageInterface',
          });
        }
      },
      "TSMethodSignature[key.name='constructor']"(
        node: TSESTree.TSMethodSignature,
      ): void {
        context.report({
          node,
          messageId: 'errorMessageInterface',
        });
      },
    };
  }

Internal helpers

Declared inside another function in this file.

getTypeReferenceName(node: | TSESTree.EntityName | TSESTree.TSType…): string | null

Parameters:

  • node any: type to be inspected.

Returns: undefined name of simple type or null

Raw JSDoc
/**
     * @param node type to be inspected.
     * @returns name of simple type or null
     */

Calls:

  • getTypeReferenceName
Code
function getTypeReferenceName(
      node:
        | TSESTree.EntityName
        | TSESTree.TSTypeAnnotation
        | TSESTree.TypeNode
        | undefined,
    ): string | null {
      if (node) {
        switch (node.type) {
          case AST_NODE_TYPES.TSTypeAnnotation:
            return getTypeReferenceName(node.typeAnnotation);
          case AST_NODE_TYPES.TSTypeReference:
            return getTypeReferenceName(node.typeName);
          case AST_NODE_TYPES.Identifier:
            return node.name;
          default:
            break;
        }
      }
      return null;
    }

isMatchingParentType(parent: | TSESTree.ClassDeclaration | TSESTree.…, returnType: TSESTree.TSTypeAnnotation | undefined): boolean

Parameters:

  • parent any: parent node.
  • returnType any: type to be compared
Raw JSDoc
/**
     * @param parent parent node.
     * @param returnType type to be compared
     */

Calls:

  • getTypeReferenceName
Code
function isMatchingParentType(
      parent:
        | TSESTree.ClassDeclaration
        | TSESTree.ClassExpression
        | TSESTree.Identifier
        | TSESTree.TSInterfaceDeclaration
        | undefined,
      returnType: TSESTree.TSTypeAnnotation | undefined,
    ): boolean {
      if (
        parent &&
        (parent.type === AST_NODE_TYPES.ClassDeclaration ||
          parent.type === AST_NODE_TYPES.ClassExpression ||
          parent.type === AST_NODE_TYPES.TSInterfaceDeclaration) &&
        parent.id
      ) {
        return getTypeReferenceName(returnType) === parent.id.name;
      }
      return false;
    }

Generated by Syntax Scribe