All files / transpiler/output/codegen/helpers StatementExpressionCollector.ts

100% Statements 33/33
100% Branches 26/26
100% Functions 4/4
100% Lines 33/33

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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118                                              962x     962x           962x     962x   962x                     962x 60x   962x 305x   962x 364x   962x 78x                     962x 32x   962x 11x   962x 8x   962x 14x                     962x 944x     18x     18x 14x       18x 18x 1x 17x 14x       18x 14x            
/**
 * StatementExpressionCollector
 *
 * Collects all expressions from a statement context that need to be walked
 * for function calls and parameter modifications. This centralizes expression
 * extraction to prevent missing cases (like issue #565).
 *
 * Issue #566: Extracted from CodeGenerator for improved testability.
 */
 
import * as Parser from "../../../logic/parser/grammar/CNextParser.js";
 
class StatementExpressionCollector {
  /**
   * Collect all expressions from a statement that need to be walked for
   * function calls and modifications.
   *
   * Handles:
   * - Simple statements (expression, assignment, variable declaration, return)
   * - Control flow conditions (if, while, do-while, switch)
   * - For statement parts (init, condition, update)
   */
  static collectAll(stmt: Parser.StatementContext): Parser.ExpressionContext[] {
    const expressions: Parser.ExpressionContext[] = [];
 
    // Simple statements with expressions
    StatementExpressionCollector._collectSimpleStatementExprs(
      stmt,
      expressions,
    );
 
    // Control flow conditions
    StatementExpressionCollector._collectControlFlowExprs(stmt, expressions);
 
    // For statement has multiple expression contexts
    StatementExpressionCollector._collectForStatementExprs(stmt, expressions);
 
    return expressions;
  }
 
  /**
   * Collect expressions from simple statements (expression, assignment,
   * variable declaration, return).
   */
  private static _collectSimpleStatementExprs(
    stmt: Parser.StatementContext,
    expressions: Parser.ExpressionContext[],
  ): void {
    if (stmt.expressionStatement()) {
      expressions.push(stmt.expressionStatement()!.expression());
    }
    if (stmt.assignmentStatement()) {
      expressions.push(stmt.assignmentStatement()!.expression());
    }
    if (stmt.variableDeclaration()?.expression()) {
      expressions.push(stmt.variableDeclaration()!.expression()!);
    }
    if (stmt.returnStatement()?.expression()) {
      expressions.push(stmt.returnStatement()!.expression()!);
    }
  }
 
  /**
   * Collect expressions from control flow conditions (if, while, do-while, switch).
   */
  private static _collectControlFlowExprs(
    stmt: Parser.StatementContext,
    expressions: Parser.ExpressionContext[],
  ): void {
    if (stmt.ifStatement()) {
      expressions.push(stmt.ifStatement()!.expression());
    }
    if (stmt.whileStatement()) {
      expressions.push(stmt.whileStatement()!.expression());
    }
    if (stmt.doWhileStatement()) {
      expressions.push(stmt.doWhileStatement()!.expression());
    }
    if (stmt.switchStatement()) {
      expressions.push(stmt.switchStatement()!.expression());
    }
  }
 
  /**
   * Collect expressions from for statement parts (init, condition, update).
   */
  private static _collectForStatementExprs(
    stmt: Parser.StatementContext,
    expressions: Parser.ExpressionContext[],
  ): void {
    if (!stmt.forStatement()) {
      return;
    }
 
    const forStmt = stmt.forStatement()!;
 
    // Condition (optional)
    if (forStmt.expression()) {
      expressions.push(forStmt.expression()!);
    }
 
    // forInit expressions
    const forInit = forStmt.forInit();
    if (forInit?.forAssignment()) {
      expressions.push(forInit.forAssignment()!.expression());
    } else if (forInit?.forVarDecl()?.expression()) {
      expressions.push(forInit.forVarDecl()!.expression()!);
    }
 
    // forUpdate expression
    if (forStmt.forUpdate()) {
      expressions.push(forStmt.forUpdate()!.expression());
    }
  }
}
 
export default StatementExpressionCollector;