Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-wrapper-object-types

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 2
πŸ“¦ Imports 5
πŸ“Š Variables & Constants 1

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-wrapper-object-types.ts

πŸ“€ Default Export

export default createRule({ ... })
Property Value
name 'no-wrapper-object-types'
meta.type 'problem'
meta.docs.description 'Disallow using confusing built-in primitive class wrappers'
meta.docs.recommended 'recommended'
meta.fixable 'code'
meta.messages.bannedClassType 'Prefer using the primitive {{preferred}} as a type name, rather than the upper-cased {{typeName}}.'
meta.schema []
defaultOptions []

Entry point: create β€” documented under Functions.


πŸ“¦ Imports

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

Variables & Constants

Name Type Kind Value Exported
classNames Set<string> const new Set([ 'BigInt', // eslint-disable-next-line @typescript-eslint/internal/p... βœ—

Functions

create(context: any): { TSClassImplements(node: any): void; TSInterfaceHeritage(n…

Parameters:

  • context any

Returns: { TSClassImplements(node: any): void; TSInterfaceHeritage(node: any): void; TSTypeReference(node: any): void; }

Calls:

  • classNames.has
  • isReferenceToGlobalFunction (from ../util)
  • typeName.toLowerCase
  • context.report
  • fixer.replaceText
  • checkBannedTypes
Code
create(context) {
    function checkBannedTypes(
      node: TSESTree.EntityName | TSESTree.Expression,
      includeFix: boolean,
    ): void {
      const typeName = node.type === AST_NODE_TYPES.Identifier && node.name;
      if (
        !typeName ||
        !classNames.has(typeName) ||
        !isReferenceToGlobalFunction(typeName, node, context.sourceCode)
      ) {
        return;
      }

      const preferred = typeName.toLowerCase();

      context.report({
        node,
        messageId: 'bannedClassType',
        data: { preferred, typeName },
        fix: includeFix
          ? (fixer): TSESLint.RuleFix => fixer.replaceText(node, preferred)
          : undefined,
      });
    }

    return {
      TSClassImplements(node): void {
        checkBannedTypes(node.expression, false);
      },
      TSInterfaceHeritage(node): void {
        checkBannedTypes(node.expression, false);
      },
      TSTypeReference(node): void {
        checkBannedTypes(node.typeName, true);
      },
    };
  }

Internal helpers

Declared inside another function in this file.

checkBannedTypes(node: TSESTree.EntityName | TSESTree.Expressi…, includeFix: boolean): void

Parameters:

  • node TSESTree.EntityName | TSESTree.Expression
  • includeFix boolean

Returns: void

Calls:

  • classNames.has
  • isReferenceToGlobalFunction (from ../util)
  • typeName.toLowerCase
  • context.report
  • fixer.replaceText
Code
function checkBannedTypes(
      node: TSESTree.EntityName | TSESTree.Expression,
      includeFix: boolean,
    ): void {
      const typeName = node.type === AST_NODE_TYPES.Identifier && node.name;
      if (
        !typeName ||
        !classNames.has(typeName) ||
        !isReferenceToGlobalFunction(typeName, node, context.sourceCode)
      ) {
        return;
      }

      const preferred = typeName.toLowerCase();

      context.report({
        node,
        messageId: 'bannedClassType',
        data: { preferred, typeName },
        fix: includeFix
          ? (fixer): TSESLint.RuleFix => fixer.replaceText(node, preferred)
          : undefined,
      });
    }

Generated by Syntax Scribe