All files / PARSE/1-Discover FileDiscovery.ts

100% Statements 22/22
100% Branches 10/10
100% Functions 8/8
100% Lines 21/21

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 119 120 121 122 123                          53x                                                 1656x 1656x 1656x                                 532x   532x 5x     527x 2x     525x                         12x   12x 36x 36x 33x   3x       12x                   19x             2x             3x 10x            
/**
 * File Discovery
 * Classifies source files by type
 */
 
import { extname, resolve } from "node:path";
import EFileType from "./types/EFileType";
import IDiscoveredFile from "./types/IDiscoveredFile";
import IFileSystem from "../../types/IFileSystem";
 
/**
 * Default extensions for each file type
 */
const EXTENSION_MAP: Record<string, EFileType> = {
  ".cnx": EFileType.CNext,
  ".cnext": EFileType.CNext,
  ".h": EFileType.CHeader,
  ".hpp": EFileType.CppHeader,
  ".hxx": EFileType.CppHeader,
  ".hh": EFileType.CppHeader,
  ".c": EFileType.CSource,
  ".cpp": EFileType.CppSource,
  ".cxx": EFileType.CppSource,
  ".cc": EFileType.CppSource,
  // #1840, owner ruling 4 on #1444: GCC treats `.c++` as C++ source, and the
  // CLI already accepted it as a C++ entry point. E0503 reads this row too
  ".c++": EFileType.CppSource,
};
 
/**
 * Classifies and discovers source files
 */
class FileDiscovery {
  /**
   * Classify a file path into a discovered file, by its extension alone --
   * nothing is read, so the path need not exist.
   */
  static classifyFile(filePath: string): IDiscoveredFile {
    const ext = extname(filePath).toLowerCase();
    const type = EXTENSION_MAP[ext] ?? EFileType.Unknown;
    return {
      path: filePath,
      type,
      extension: ext,
    };
  }
 
  /**
   * Discover a single file
   *
   * @param filePath - Path to the file
   * @param fs - File system abstraction
   */
  static discoverFile(
    filePath: string,
    fs: IFileSystem,
  ): IDiscoveredFile | null {
    const resolvedPath = resolve(filePath);
 
    if (!fs.exists(resolvedPath)) {
      return null;
    }
 
    if (!fs.isFile(resolvedPath)) {
      return null;
    }
 
    return this.classifyFile(resolvedPath);
  }
 
  /**
   * Discover multiple specific files
   *
   * @param filePaths - Paths to the files
   * @param fs - File system abstraction
   */
  static discoverFiles(
    filePaths: string[],
    fs: IFileSystem,
  ): IDiscoveredFile[] {
    const files: IDiscoveredFile[] = [];
 
    for (const filePath of filePaths) {
      const file = this.discoverFile(filePath, fs);
      if (file) {
        files.push(file);
      } else {
        console.warn(`Warning: File not found: ${filePath}`);
      }
    }
 
    return files;
  }
 
  /**
   * Filter discovered files by type
   */
  static filterByType(
    files: IDiscoveredFile[],
    type: EFileType,
  ): IDiscoveredFile[] {
    return files.filter((f) => f.type === type);
  }
 
  /**
   * Get C-Next files from a list
   */
  static getCNextFiles(files: IDiscoveredFile[]): IDiscoveredFile[] {
    return this.filterByType(files, EFileType.CNext);
  }
 
  /**
   * Get C/C++ header files from a list
   */
  static getHeaderFiles(files: IDiscoveredFile[]): IDiscoveredFile[] {
    return files.filter(
      (f) => f.type === EFileType.CHeader || f.type === EFileType.CppHeader,
    );
  }
}
 
export default FileDiscovery;