All files / transpiler/logic/symbols/cpp/collectors NamespaceCollector.ts

84.61% Statements 11/13
66.66% Branches 8/12
100% Functions 2/2
100% Lines 11/11

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                                                      7x 7x   7x 7x     7x   7x                                   7x 7x   7x 7x   7x          
/**
 * NamespaceCollector - Extracts namespace declarations from C++ parse trees.
 *
 * Produces ICppNamespaceSymbol instances.
 */
 
/* eslint-disable @typescript-eslint/no-explicit-any */
 
import ESourceLanguage from "../../../../../utils/types/ESourceLanguage";
import ICppNamespaceSymbol from "../../../../types/symbols/cpp/ICppNamespaceSymbol";
 
class NamespaceCollector {
  /**
   * Collect a namespace definition and return an ICppNamespaceSymbol.
   *
   * @param nsDef The namespace definition context
   * @param sourceFile Source file path
   * @param line Line number
   * @param currentNamespace Optional parent namespace name
   * @returns The namespace symbol
   */
  static collect(
    nsDef: any,
    sourceFile: string,
    line: number,
    currentNamespace?: string,
  ): ICppNamespaceSymbol | null {
    const identifier = nsDef.Identifier?.();
    const originalNs = nsDef.originalNamespaceName?.();
 
    const name = identifier?.getText() ?? originalNs?.getText();
    Iif (!name) return null;
 
    // Use full qualified name for nested namespaces
    const fullName = currentNamespace ? `${currentNamespace}::${name}` : name;
 
    return {
      kind: "namespace",
      name: fullName,
      sourceFile,
      sourceLine: line,
      sourceLanguage: ESourceLanguage.Cpp,
      isExported: true,
      parent: currentNamespace,
    };
  }
 
  /**
   * Get the full namespace name after processing this namespace definition.
   */
  static getFullNamespaceName(
    nsDef: any,
    currentNamespace?: string,
  ): string | undefined {
    const identifier = nsDef.Identifier?.();
    const originalNs = nsDef.originalNamespaceName?.();
 
    const name = identifier?.getText() ?? originalNs?.getText();
    Iif (!name) return currentNamespace;
 
    return currentNamespace ? `${currentNamespace}::${name}` : name;
  }
}
 
export default NamespaceCollector;