All files / TRANSPILE/3-Render/codegen/helpers BaseIdentifierBuilder.ts

100% Statements 6/6
100% Branches 4/4
100% Functions 1/1
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57                                                                  283x   283x   28x     255x       34x             221x          
/**
 * Base Identifier Builder
 *
 * Builds the base identifier string for assignment targets,
 * handling global., this., and bare identifier prefixes.
 *
 * Extracted from CodeGenerator.doGenerateAssignmentTarget to reduce
 * cognitive complexity.
 */
 
import IBaseIdentifierResult from "../types/IBaseIdentifierResult";
import QualifiedNameGenerator from "../../../../utils/QualifiedNameGenerator";
 
/**
 * Static utility for building base identifiers
 */
class BaseIdentifierBuilder {
  /**
   * Build the base identifier with appropriate prefix
   *
   * @param identifier The raw identifier
   * @param hasGlobal Whether global. prefix is present
   * @param hasThis Whether this. prefix is present
   * @param currentScopePath The current scope name (required if hasThis)
   * @returns The built identifier and first ID
   * @throws Error if this. is used outside a scope
   */
  static build(
    identifier: string,
    hasGlobal: boolean,
    hasThis: boolean,
    currentScopePath: string,
  ): IBaseIdentifierResult {
    const firstId = identifier;
 
    if (hasGlobal) {
      // global.x - no prefix needed for code generation
      return { result: firstId, firstId };
    }
 
    if (hasThis) {
      // #1322: `this` outside a scope is E0431 in 2.1, so codegen is never
      // reached with an empty `currentScopePath` here.
      // this.x - prefix with current scope
      return {
        result: QualifiedNameGenerator.forMember(currentScopePath, firstId),
        firstId,
      };
    }
 
    // Bare identifier with postfix ops
    return { result: firstId, firstId };
  }
}
 
export default BaseIdentifierBuilder;