(
file: ts.SourceFile | AbsoluteFsPath,
{sortedRootDirs, projectRoot}: Pick<ProgramInfo, 'sortedRootDirs' | 'projectRoot'>,
)
| 93 | * See {@link ProjectFile}. |
| 94 | */ |
| 95 | export function projectFile( |
| 96 | file: ts.SourceFile | AbsoluteFsPath, |
| 97 | {sortedRootDirs, projectRoot}: Pick<ProgramInfo, 'sortedRootDirs' | 'projectRoot'>, |
| 98 | ): ProjectFile { |
| 99 | const fs = getFileSystem(); |
| 100 | const filePath = fs.resolve(typeof file === 'string' ? file : file.fileName); |
| 101 | |
| 102 | // Sorted root directories are sorted longest to shortest. First match |
| 103 | // is the appropriate root directory for ID computation. |
| 104 | for (const rootDir of sortedRootDirs) { |
| 105 | if (!isWithinBasePath(fs, rootDir, filePath)) { |
| 106 | continue; |
| 107 | } |
| 108 | return { |
| 109 | id: fs.relative(rootDir, filePath) as string as ProjectFileID, |
| 110 | rootRelativePath: fs.relative(projectRoot, filePath) as string as ProjectRootRelativePath, |
| 111 | }; |
| 112 | } |
| 113 | |
| 114 | // E.g. project directory may be `src/`, but files may be looked up |
| 115 | // from `node_modules/`. This is fine, but in those cases, no root |
| 116 | // directory matches. |
| 117 | const rootRelativePath = fs.relative(projectRoot, filePath); |
| 118 | return { |
| 119 | id: rootRelativePath as string as ProjectFileID, |
| 120 | rootRelativePath: rootRelativePath as string as ProjectRootRelativePath, |
| 121 | }; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Whether `path` is a descendant of the `base`? |
searching dependent graphs…