* 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)
| 706 | * on disk and this module should not be the thing that trusts it. |
| 707 | */ |
| 708 | function 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 |
no test coverage detected