MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / defaultSourceReader

Function defaultSourceReader

src/graph/dead-code.ts:712–727  ·  view source on GitHub ↗

* Reads a project-relative file off disk, for callers with no chokepoint of * their own (the CLI, a library user). Refuses anything that escapes the * project root — a `filePath` comes out of the index, but the index is a file * on disk and this module should not be the thing that trusts it.

(cg: CodeGraph)

Source from the content-addressed store, hash-verified

710 * on disk and this module should not be the thing that trusts it.
711 */
712function defaultSourceReader(cg: CodeGraph): (filePath: string) => string | null {
713 const root = cg.getProjectRoot();
714 return (filePath: string): string | null => {
715 try {
716 // The shared containment check, which also holds for a project at a
717 // drive root: appending a separator to `C:\` refused every file (#415).
718 const absolute = lexicalPathWithinRoot(root, filePath);
719 if (absolute === null) return null;
720 const stat = fs.statSync(absolute);
721 if (!stat.isFile() || stat.size > MAX_CORROBORATION_BYTES) return null;
722 return fs.readFileSync(absolute, 'utf8');
723 } catch {
724 return null;
725 }
726 };
727}
728
729/**
730 * How many times `name` is written in `source` as a whole identifier, counting

Callers 1

buildDeadCodeReportFunction · 0.85

Calls 2

lexicalPathWithinRootFunction · 0.90
getProjectRootMethod · 0.65

Tested by

no test coverage detected