All files / utils PostfixAnalysisUtils.ts

100% Statements 7/7
100% Branches 2/2
100% Functions 1/1
100% Lines 7/7

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                                                                  124x 124x   124x 149x 97x   52x       124x        
/**
 * PostfixAnalysisUtils - Shared utilities for analyzing postfix target operations
 *
 * Extracts common logic for analyzing assignment targets that may have
 * member access (a.b.c) or subscript access (a[i]).
 */
import * as Parser from "../transpiler/logic/parser/grammar/CNextParser";
 
/**
 * Result of analyzing postfix target operations.
 */
interface IPostfixAnalysisResult {
  /** Chain of identifiers from base through member accesses */
  readonly identifiers: string[];
  /** Whether any subscript (array access) operations were found */
  readonly hasSubscript: boolean;
}
 
/**
 * Analyze postfix target operations to extract identifier chain and detect subscripts.
 *
 * Given an assignment target like `a.b.c[i].d`, this extracts:
 * - identifiers: ["a", "b", "c", "d"]
 * - hasSubscript: true
 *
 * @param baseId - The base identifier (leftmost name)
 * @param postfixOps - Array of postfix operations (member access or subscript)
 * @returns Analysis result with identifiers and subscript detection
 */
function analyzePostfixOps(
  baseId: string,
  postfixOps: Parser.PostfixTargetOpContext[],
): IPostfixAnalysisResult {
  const identifiers: string[] = [baseId];
  let hasSubscript = false;
 
  for (const op of postfixOps) {
    if (op.IDENTIFIER()) {
      identifiers.push(op.IDENTIFIER()!.getText());
    } else {
      hasSubscript = true;
    }
  }
 
  return { identifiers, hasSubscript };
}
 
export default analyzePostfixOps;