All files / transpiler/logic/analysis AnalyzerContextBuilder.ts

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

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                                                                    142x 142x   142x 21x 21x   39x       35x     21x 20x       142x          
/**
 * AnalyzerContextBuilder
 * Issue #591: Extracted from Transpiler.transpileSource() to reduce complexity
 *
 * Builds the context needed for running semantic analyzers, specifically
 * converting struct field information from the symbol table format to
 * the analyzer-compatible format.
 */
 
import SymbolTable from "../symbols/SymbolTable";
 
/**
 * Builds analyzer context from symbol table data.
 *
 * The analyzer requires struct fields as Map<string, Set<string>> (structName -> fieldNames)
 * while the symbol table stores more detailed IStructFieldInfo. This builder handles
 * the conversion, including Issue #355's requirement to exclude array fields.
 */
class AnalyzerContextBuilder {
  /**
   * Build external struct fields map for analyzer from symbol table.
   *
   * Converts the symbol table's detailed struct field info to the simpler
   * format needed by InitializationAnalyzer.
   *
   * Issue #355: Excludes array fields from init checking since the analyzer
   * can't prove loop-based array initialization is complete.
   *
   * @param symbolTable - Symbol table containing struct definitions
   * @returns Map of struct name to set of non-array field names
   */
  static buildExternalStructFields(
    symbolTable: SymbolTable,
  ): Map<string, Set<string>> {
    const externalStructFields = new Map<string, Set<string>>();
    const allStructFields = symbolTable.getAllStructFields();
 
    for (const [structName, fieldMap] of allStructFields) {
      const nonArrayFields = new Set<string>();
      for (const [fieldName, fieldInfo] of fieldMap) {
        // Only include non-array fields in init checking
        if (
          !fieldInfo.arrayDimensions ||
          fieldInfo.arrayDimensions.length === 0
        ) {
          nonArrayFields.add(fieldName);
        }
      }
      if (nonArrayFields.size > 0) {
        externalStructFields.set(structName, nonArrayFields);
      }
    }
 
    return externalStructFields;
  }
}
 
export default AnalyzerContextBuilder;