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

Function defaultSourceReader

src/graph/dead-code.ts:708–723  ·  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

706 * on disk and this module should not be the thing that trusts it.
707 */
708function defaultSourceReader(cg: CodeGraph): (filePath: string) => string | null {
709 const root = cg.getProjectRoot();
710 return (filePath: string): string | null => {
711 try {
712 // The shared containment check, which also holds for a project at a
713 // drive root: appending a separator to `C:\` refused every file (#415).
714 const absolute = lexicalPathWithinRoot(root, filePath);
715 if (absolute === null) return null;
716 const stat = fs.statSync(absolute);
717 if (!stat.isFile() || stat.size > MAX_CORROBORATION_BYTES) return null;
718 return fs.readFileSync(absolute, 'utf8');
719 } catch {
720 return null;
721 }
722 };
723}
724
725/**
726 * 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