Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-restricted-types

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 5
πŸ“¦ Imports 5
πŸ“Š Variables & Constants 1
πŸ“‘ Type Aliases 3

πŸ“š Table of Contents

πŸ› οΈ File Location:

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

πŸ“€ Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-restricted-types'
meta.type 'suggestion'
meta.docs.description 'Disallow certain types'
meta.fixable 'code'
meta.hasSuggestions true
meta.messages.bannedTypeMessage "Don't use {{name}} as a type.{{customMessage}}"
meta.messages.bannedTypeReplacement 'Replace {{name}} with {{replacement}}.'
meta.schema [ { type: 'object', $defs: { banConfig: { oneOf: [ { type: 'boolean', description: 'Bans the type with the default me...
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
objectReduceKey ../util

Variables & Constants

Name Type Kind Value Exported
TYPE_KEYWORDS { bigint: any; boolean: any; never: a... const { bigint: AST_NODE_TYPES.TSBigIntKeyword, boolean: AST_NODE_TYPES.TSBooleanKe... βœ—

Functions

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

Parameters:

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

Returns: any

Calls:

  • Object.entries(types).map
  • removeSpaces
  • stringifyNode
  • bannedTypes.get
  • getCustomMessage
  • context.report
  • fixer.replaceText
  • suggest?.map
  • objectReduceKey (from ../util)
  • bannedTypes.has
  • checkBannedTypes
Code
create(context, [{ types = {} }]) {
    const bannedTypes = new Map(
      Object.entries(types).map(([type, data]) => [removeSpaces(type), data]),
    );

    function checkBannedTypes(
      typeNode: TSESTree.Node,
      name = stringifyNode(typeNode, context.sourceCode),
    ): void {
      const bannedType = bannedTypes.get(name);

      if (bannedType == null || bannedType === false) {
        return;
      }

      const customMessage = getCustomMessage(bannedType);
      const fixWith =
        bannedType && typeof bannedType === 'object' && bannedType.fixWith;
      const suggest =
        bannedType && typeof bannedType === 'object'
          ? bannedType.suggest
          : undefined;

      context.report({
        node: typeNode,
        messageId: 'bannedTypeMessage',
        data: {
          name,
          customMessage,
        },
        fix: fixWith
          ? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
          : null,
        suggest: suggest?.map(replacement => ({
          messageId: 'bannedTypeReplacement',
          data: {
            name,
            replacement,
          },
          fix: (fixer): TSESLint.RuleFix =>
            fixer.replaceText(typeNode, replacement),
        })),
      });
    }

    const keywordSelectors = objectReduceKey(
      TYPE_KEYWORDS,
      (acc: TSESLint.RuleListener, keyword) => {
        if (bannedTypes.has(keyword)) {
          acc[TYPE_KEYWORDS[keyword]] = (node: TSESTree.Node): void =>
            checkBannedTypes(node, keyword);
        }

        return acc;
      },
      {},
    );

    return {
      ...keywordSelectors,

      TSClassImplements(node): void {
        checkBannedTypes(node.expression);

        if (node.typeArguments) {
          checkBannedTypes(node);
        }
      },
      TSInterfaceHeritage(node): void {
        checkBannedTypes(node.expression);

        if (node.typeArguments) {
          checkBannedTypes(node);
        }
      },
      TSTupleType(node): void {
        if (!node.elementTypes.length) {
          checkBannedTypes(node);
        }
      },
      TSTypeLiteral(node): void {
        if (!node.members.length) {
          checkBannedTypes(node);
        }
      },
      TSTypeReference(node): void {
        checkBannedTypes(node.typeName);

        if (node.typeArguments) {
          checkBannedTypes(node);
        }
      },
    };
  }

removeSpaces(str: string): string

Parameters:

  • str string

Returns: string

Calls:

  • str.replaceAll
Code
function removeSpaces(str: string): string {
  return str.replaceAll(/\s/g, '');
}

stringifyNode(node: TSESTree.Node, sourceCode: TSESLint.SourceCode): string

Parameters:

  • node TSESTree.Node
  • sourceCode TSESLint.SourceCode

Returns: string

Calls:

  • removeSpaces
  • sourceCode.getText
Code
function stringifyNode(
  node: TSESTree.Node,
  sourceCode: TSESLint.SourceCode,
): string {
  return removeSpaces(sourceCode.getText(node));
}

getCustomMessage(bannedType: string | true | { fixWith?: string; mes…): string

Parameters:

  • bannedType string | true | { fixWith?: string; message?: string } | null

Returns: string

Code
function getCustomMessage(
  bannedType: string | true | { fixWith?: string; message?: string } | null,
): string {
  if (!bannedType || bannedType === true) {
    return '';
  }

  if (typeof bannedType === 'string') {
    return ` ${bannedType}`;
  }

  if (bannedType.message) {
    return ` ${bannedType.message}`;
  }

  return '';
}

Internal helpers

Declared inside another function in this file.

checkBannedTypes(typeNode: TSESTree.Node, name: string): void

Parameters:

  • typeNode TSESTree.Node
  • name string

Returns: void

Calls:

  • bannedTypes.get
  • getCustomMessage
  • context.report
  • fixer.replaceText
  • suggest?.map
Code
function checkBannedTypes(
      typeNode: TSESTree.Node,
      name = stringifyNode(typeNode, context.sourceCode),
    ): void {
      const bannedType = bannedTypes.get(name);

      if (bannedType == null || bannedType === false) {
        return;
      }

      const customMessage = getCustomMessage(bannedType);
      const fixWith =
        bannedType && typeof bannedType === 'object' && bannedType.fixWith;
      const suggest =
        bannedType && typeof bannedType === 'object'
          ? bannedType.suggest
          : undefined;

      context.report({
        node: typeNode,
        messageId: 'bannedTypeMessage',
        data: {
          name,
          customMessage,
        },
        fix: fixWith
          ? (fixer): TSESLint.RuleFix => fixer.replaceText(typeNode, fixWith)
          : null,
        suggest: suggest?.map(replacement => ({
          messageId: 'bannedTypeReplacement',
          data: {
            name,
            replacement,
          },
          fix: (fixer): TSESLint.RuleFix =>
            fixer.replaceText(typeNode, replacement),
        })),
      });
    }

Type Aliases

Types

type Types = Record<
  string,
  | boolean
  | string
  | {
      fixWith?: string;
      message: string;
      suggest?: readonly string[];
    }
  | null
>;

Options

type Options = [
  {
    types?: Types;
  },
];

MessageIds

type MessageIds = 'bannedTypeMessage' | 'bannedTypeReplacement';

Generated by Syntax Scribe