Skip to content

⬅️ Back to Table of Contents

πŸ“„ no-this-alias

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 1
πŸ“¦ Imports 3
πŸ“‘ Type Aliases 2

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/rules/no-this-alias.ts

πŸ“€ Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-this-alias'
meta.type 'suggestion'
meta.docs.description 'Disallow aliasing this'
meta.docs.recommended 'recommended'
meta.messages.thisAssignment "Unexpected aliasing of 'this' to local variable."
meta.messages.thisDestructure "Unexpected aliasing of members of 'this' to local variables."
meta.schema [ { type: 'object', additionalProperties: false, properties: { allowDestructuring: { type: 'boolean', description: 'W...
defaultOptions [ { allowDestructuring: true, allowedNames: [], }, ]

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, [{ allowDestructuring, allowedN…: any): { "VariableDeclarator[init.type='ThisExpression'], Assignme…

Parameters:

  • context any
  • [{ allowDestructuring, allowedNames }] any

Returns: { "VariableDeclarator[init.type='ThisExpression'], AssignmentExpression[right.type='ThisExpression']"(node: TSESTree.AssignmentExpression | TSESTree.VariableDeclarator): void; }

Calls:

  • allowedNames!.includes
  • context.report

Internal Comments:

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion (x4)

Code
create(context, [{ allowDestructuring, allowedNames }]) {
    return {
      "VariableDeclarator[init.type='ThisExpression'], AssignmentExpression[right.type='ThisExpression']"(
        node: TSESTree.AssignmentExpression | TSESTree.VariableDeclarator,
      ): void {
        const id =
          node.type === AST_NODE_TYPES.VariableDeclarator ? node.id : node.left;
        if (allowDestructuring && id.type !== AST_NODE_TYPES.Identifier) {
          return;
        }

        const hasAllowedName =
          id.type === AST_NODE_TYPES.Identifier
            ? // https://github.com/typescript-eslint/typescript-eslint/issues/5439
              // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
              allowedNames!.includes(id.name)
            : false;
        if (!hasAllowedName) {
          context.report({
            node: id,
            messageId:
              id.type === AST_NODE_TYPES.Identifier
                ? 'thisAssignment'
                : 'thisDestructure',
          });
        }
      },
    };
  }

Type Aliases

Options

type Options = [
  {
    allowDestructuring?: boolean;
    allowedNames?: string[];
  },
];

MessageIds

type MessageIds = 'thisAssignment' | 'thisDestructure';

Generated by Syntax Scribe