isStructuralName

tcmts.core.canonicalize.isStructuralName · #632i7ihf · term · runs anywhere

Identifiers that are structural names (property keys, labels), never renamed or resolved.

import { isIntrinsicTag } from "#codebase/tcmts/core/canonicalize";
import {
  isBindingElement as ts_isBindingElement, isBreakStatement as ts_isBreakStatement,
  isContinueStatement as ts_isContinueStatement, isEnumMember as ts_isEnumMember,
  isGetAccessorDeclaration as ts_isGetAccessorDeclaration, isJsxAttribute as ts_isJsxAttribute,
  isJsxClosingElement as ts_isJsxClosingElement, isJsxNamespacedName as ts_isJsxNamespacedName,
  isJsxOpeningElement as ts_isJsxOpeningElement, isJsxSelfClosingElement as ts_isJsxSelfClosingElement,
  isLabeledStatement as ts_isLabeledStatement, isMetaProperty as ts_isMetaProperty,
  isMethodDeclaration as ts_isMethodDeclaration, isMethodSignatureDeclaration as ts_isMethodSignature,
  isNamedTupleMember as ts_isNamedTupleMember, isPropertyAccessExpression as ts_isPropertyAccessExpression,
  isPropertyAssignment as ts_isPropertyAssignment, isPropertyDeclaration as ts_isPropertyDeclaration,
  isPropertySignatureDeclaration as ts_isPropertySignature, isQualifiedName as ts_isQualifiedName,
  isSetAccessorDeclaration as ts_isSetAccessorDeclaration,
} from "typescript/ast";
import type { Identifier as ts_Identifier } from "typescript/ast";

export function isStructuralName(id: ts_Identifier): boolean {
  const p = id.parent;
  if (ts_isPropertyAccessExpression(p)) return p.name === id;
  if (ts_isQualifiedName(p)) return p.right === id;
  if (
    ts_isPropertyAssignment(p) ||
    ts_isPropertyDeclaration(p) ||
    ts_isPropertySignature(p) ||
    ts_isMethodDeclaration(p) ||
    ts_isMethodSignature(p) ||
    ts_isGetAccessorDeclaration(p) ||
    ts_isSetAccessorDeclaration(p) ||
    ts_isEnumMember(p)
  ) {
    return p.name === id;
  }
  if (ts_isBindingElement(p)) return p.propertyName === id;
  if (ts_isLabeledStatement(p) || ts_isBreakStatement(p) || ts_isContinueStatement(p)) return p.label === id;
  if (ts_isMetaProperty(p)) return true;
  if (ts_isNamedTupleMember(p)) return p.name === id;
  // `<div className>`: an intrinsic element and its attributes are names, not references
  if (ts_isJsxAttribute(p)) return p.name === id;
  if (ts_isJsxNamespacedName(p)) return true;
  if (ts_isJsxOpeningElement(p) || ts_isJsxSelfClosingElement(p) || ts_isJsxClosingElement(p)) return p.tagName === id && isIntrinsicTag(id.text);
  return false;
}

uses

used by