All files / client/src/lib source-project.ts

100% Statements 25/25
100% Branches 16/16
100% Functions 6/6
100% Lines 20/20

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                      84x       20x       2x 2x             22x 22x       4x 4x 3x                         22x 22x   19x 19x 19x 43x 43x 43x 42x 42x   18x    
import type { SourceProject } from "@shared/source-project";
 
export interface ClientSourceTab {
  id: string;
  name: string;
  content: string;
  /** Project-relative path when supplied by an external example or file loader. */
  path?: string;
}
 
function tabPath(tab: ClientSourceTab): string {
  return tab.path ?? tab.name;
}
 
function isInoPath(path: string): boolean {
  return path.toLowerCase().endsWith(".ino");
}
 
function isHeaderPath(path: string): boolean {
  const lower = path.toLowerCase();
  return lower.endsWith(".h") || lower.endsWith(".hpp");
}
 
/** Choose the stable project entry without depending on the selected tab. */
export function findSourceProjectEntry(
  tabs: readonly ClientSourceTab[],
): string | null {
  const inoTab = tabs.find((tab) => isInoPath(tabPath(tab)));
  if (inoTab) return tabPath(inoTab);
 
  // Legacy single-file sketches may have a non-extension name. Never promote
  // a header-only project to an entry file.
  const mainTab = tabs[0];
  if (mainTab && !isHeaderPath(tabPath(mainTab))) return tabPath(mainTab);
  return null;
}
 
/**
 * Build an immutable analysis snapshot from the current client tab state.
 * The editor's `code` wins for the selected tab because tabs are persisted
 * separately and may lag behind Monaco's latest change.
 */
export function buildSourceProject(
  tabs: readonly ClientSourceTab[],
  activeTabId: string | null,
  code: string,
): SourceProject | null {
  const entryFile = findSourceProjectEntry(tabs);
  if (!entryFile) return null;
 
  const files: Record<string, string> = {};
  const seenPaths = new Set<string>();
  for (const tab of tabs) {
    const path = tabPath(tab);
    const foldedPath = path.toLocaleLowerCase("en-US");
    if (seenPaths.has(foldedPath)) return null;
    seenPaths.add(foldedPath);
    files[path] = tab.id === activeTabId ? code : tab.content;
  }
  return { entryFile, files };
}