Skip to content

⬅️ Back to Table of Contents

πŸ“„ extractComputedName

πŸ“Š Analysis Summary

Metric Count
πŸ”§ Functions 4
πŸ“¦ Imports 6
πŸ“ Interfaces 1

πŸ“š Table of Contents

πŸ› οΈ File Location:

πŸ“‚ packages/eslint-plugin/src/util/class-scope-analyzer/extractComputedName.ts

πŸ“¦ Imports

Name Source
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
Key ./types
MemberNode ./types
privateKey ./types
publicKey ./types

Functions

extractNameForMember(node: MemberNode): ExtractedName | null

Extracts the string name for a member.

Returns: undefined null if the name cannot be extracted due to it being computed.

Raw JSDoc
/**
 * Extracts the string name for a member.
 * @returns `null` if the name cannot be extracted due to it being computed.
 */

Calls:

  • extractNonComputedName
  • extractComputedName
Code
export function extractNameForMember(node: MemberNode): ExtractedName | null {
  if (node.type === AST_NODE_TYPES.TSParameterProperty) {
    const identifier =
      node.parameter.type === AST_NODE_TYPES.Identifier
        ? node.parameter
        : node.parameter.left;
    return extractNonComputedName(identifier);
  }

  if (node.computed) {
    return extractComputedName(node.key);
  }

  if (node.key.type === AST_NODE_TYPES.Literal) {
    return extractComputedName(node.key);
  }

  return extractNonComputedName(node.key);
}

extractNameForMemberExpression(node: TSESTree.MemberExpression): ExtractedName | null

Extracts the string property name for a member.

Returns: undefined null if the name cannot be extracted due to it being a computed.

Raw JSDoc
/**
 * Extracts the string property name for a member.
 * @returns `null` if the name cannot be extracted due to it being a computed.
 */

Calls:

  • extractComputedName
  • extractNonComputedName
Code
export function extractNameForMemberExpression(
  node: TSESTree.MemberExpression,
): ExtractedName | null {
  if (node.computed) {
    return extractComputedName(node.property);
  }

  return extractNonComputedName(node.property);
}

extractComputedName(computedName: TSESTree.Expression): ExtractedName | null

Parameters:

  • computedName TSESTree.Expression

Returns: ExtractedName | null

Calls:

  • computedName.value?.toString
  • publicKey (from ./types)
Code
function extractComputedName(
  computedName: TSESTree.Expression,
): ExtractedName | null {
  if (computedName.type === AST_NODE_TYPES.Literal) {
    const name = computedName.value?.toString() ?? 'null';
    return {
      codeName: name,
      key: publicKey(name),
      nameNode: computedName,
    };
  }
  if (
    computedName.type === AST_NODE_TYPES.TemplateLiteral &&
    computedName.expressions.length === 0
  ) {
    const name = computedName.quasis[0].value.raw;
    return {
      codeName: name,
      key: publicKey(name),
      nameNode: computedName,
    };
  }
  return null;
}

extractNonComputedName(nonComputedName: TSESTree.Identifier | TSESTree.PrivateI…): ExtractedName | null

Parameters:

  • nonComputedName TSESTree.Identifier | TSESTree.PrivateIdentifier

Returns: ExtractedName | null

Calls:

  • privateKey (from ./types)
  • publicKey (from ./types)
Code
function extractNonComputedName(
  nonComputedName: TSESTree.Identifier | TSESTree.PrivateIdentifier,
): ExtractedName | null {
  const name = nonComputedName.name;
  if (nonComputedName.type === AST_NODE_TYPES.PrivateIdentifier) {
    return {
      codeName: `#${name}`,
      key: privateKey(nonComputedName),
      nameNode: nonComputedName,
    };
  }
  return {
    codeName: name,
    key: publicKey(name),
    nameNode: nonComputedName,
  };
}

Interfaces

ExtractedName

Interface Code
export interface ExtractedName {
  key: Key;
  codeName: string;
  nameNode: TSESTree.Node;
}

Properties

Name Type Optional Description
key Key βœ— not shown
codeName string βœ— not shown
nameNode TSESTree.Node βœ— not shown

Generated by Syntax Scribe