All files / PARSE/3-Declare/cpp/collectors VariableCollector.ts

83.33% Statements 5/6
87.5% Branches 7/8
100% Functions 1/1
100% Lines 5/5

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                                                              7x 7x   7x     7x   7x                                
/**
 * VariableCollector - Extracts variable declarations from C++ parse trees.
 *
 * Produces ICppVariableSymbol instances.
 */
 
/* eslint-disable @typescript-eslint/no-explicit-any */
 
import ESourceLanguage from "../../../../utils/types/ESourceLanguage";
import ICppVariableSymbol from "../../../../types/symbols/cpp/ICppVariableSymbol";
import DeclaratorUtils from "../utils/DeclaratorUtils";
import type ISourceSpan from "../../../../types/ISourceSpan";
 
class VariableCollector {
  /**
   * Collect a variable declaration and return an ICppVariableSymbol.
   *
   * @param declarator The declarator context
   * @param baseType The variable type string
   * @param sourceFile Source file path
   * @param span Source span of the declaration
   * @param currentNamespace Optional current namespace
   * @returns The variable symbol or null if no name
   */
  static collect(
    declarator: any,
    baseType: string,
    sourceFile: string,
    span: ISourceSpan,
    currentNamespace?: string,
  ): ICppVariableSymbol | null {
    const name = DeclaratorUtils.extractDeclaratorName(declarator);
    Iif (!name) return null;
 
    const fullName = currentNamespace ? `${currentNamespace}::${name}` : name;
 
    // Extract array dimensions
    const arrayDimensions = DeclaratorUtils.extractArrayDimensions(declarator);
 
    return {
      kind: "variable",
      name: fullName,
      type: baseType,
      sourceFile,
      span,
      sourceLanguage: ESourceLanguage.Cpp,
      visibility: "public",
      parent: currentNamespace,
      isArray: arrayDimensions.length > 0 ? true : undefined,
      arrayDimensions: arrayDimensions.length > 0 ? arrayDimensions : undefined,
    };
  }
}
 
export default VariableCollector;